Skip to content
Snippets Groups Projects
Commit 6c91a9a1 authored by ArtOfCode-'s avatar ArtOfCode-
Browse files

Add model validations and display

parent 129443a4
Branches
Tags
No related merge requests found
...@@ -84,3 +84,7 @@ pre.unformatted { ...@@ -84,3 +84,7 @@ pre.unformatted {
.stat-value { .stat-value {
font-size: 2.0em; font-size: 2.0em;
} }
.badge.is-tag.is-outlined {
border: 1px solid #001db1;
}
\ No newline at end of file
...@@ -49,7 +49,7 @@ class QuestionsController < ApplicationController ...@@ -49,7 +49,7 @@ class QuestionsController < ApplicationController
not_found not_found
return return
end end
@questions = @tag.posts.undeleted.order('updated_at DESC').paginate(page: params[:page], per_page: 50) @questions = @tag.posts.list_includes.undeleted.order('updated_at DESC').paginate(page: params[:page], per_page: 50)
end end
def lottery def lottery
... ...
......
...@@ -30,17 +30,18 @@ class Post < ApplicationRecord ...@@ -30,17 +30,18 @@ class Post < ApplicationRecord
validate :stripped_minimum, if: :question? validate :stripped_minimum, if: :question?
validate :category_allows_post_type validate :category_allows_post_type
validate :license_available validate :license_available
validate :has_required_tags
scope :undeleted, -> { where(deleted: false) } scope :undeleted, -> { where(deleted: false) }
scope :deleted, -> { where(deleted: true) } scope :deleted, -> { where(deleted: true) }
scope :qa_only, -> { where(post_type_id: [Question.post_type_id, Answer.post_type_id]) } scope :qa_only, -> { where(post_type_id: [Question.post_type_id, Answer.post_type_id]) }
scope :list_includes, -> { includes(:user, user: :avatar_attachment) } scope :list_includes, -> { includes(:user, :tags, user: :avatar_attachment) }
after_save :check_attribution_notice after_save :check_attribution_notice
after_save :modify_author_reputation after_save :modify_author_reputation
after_save :copy_last_activity_to_parent after_save :copy_last_activity_to_parent
after_save :break_description_cache after_save :break_description_cache
after_save :update_tag_associations, if: :question? before_validation :update_tag_associations, if: :question?
after_create :create_initial_revision after_create :create_initial_revision
after_create :add_license_if_nil after_create :add_license_if_nil
...@@ -230,4 +231,13 @@ class Post < ApplicationRecord ...@@ -230,4 +231,13 @@ class Post < ApplicationRecord
update(license: License.site_default) update(license: License.site_default)
end end
end end
def has_required_tags
required = category&.required_tag_ids
return unless required.present? && !required.empty?
unless tag_ids.any? { |t| required.include? t }
errors.add(:tags, "must contain at least one required tag (#{category.required_tags.pluck(:name).join(', ')})")
end
end
end end
...@@ -11,15 +11,6 @@ class Question < Post ...@@ -11,15 +11,6 @@ class Question < Post
PostType.mapping['Question'] PostType.mapping['Question']
end end
validates :title, :body, :tags_cache, presence: true
validate :tags_in_tag_set
validate :maximum_tags
validate :maximum_tag_length
validate :no_spaces_in_tags
validate :stripped_minimum
after_save :update_tag_associations
def answers def answers
Answer.where(parent: self) Answer.where(parent: self)
end end
... ...
......
...@@ -98,9 +98,12 @@ ...@@ -98,9 +98,12 @@
<% if is_question %> <% if is_question %>
<div class="post--tags has-padding-2"> <div class="post--tags has-padding-2">
<% tag_set = post.tag_set %> <% tag_set = post.tag_set %>
<% post.tags_cache.each do |tag| %> <% post.tags.each do |tag| %>
<% next if tag.nil? || tag.empty? %> <% next if tag.nil? %>
<%= link_to tag, questions_tagged_path(tag_set: tag_set.id, tag: tag), class: 'badge is-tag' %> <% required = post.category&.required_tag_ids&.include? tag.id %>
<% topic = post.category&.topic_tag_ids&.include? tag.id %>
<%= link_to tag.name, questions_tagged_path(tag_set: tag_set.id, tag: tag.name),
class: "badge is-tag #{required ? 'is-filled' : ''} #{topic ? 'is-outlined' : ''}" %>
<% end %> <% end %>
</div> </div>
<% end %> <% end %>
... ...
......
...@@ -27,8 +27,11 @@ ...@@ -27,8 +27,11 @@
<div class="has-padding-top-2"> <div class="has-padding-top-2">
<% if is_question %> <% if is_question %>
<% tag_set = post.tag_set %> <% tag_set = post.tag_set %>
<% post.tags_cache.each do |tag| %> <% post.tags.each do |tag| %>
<%= link_to tag, questions_tagged_path(tag_set: tag_set.id, tag: tag), class: 'badge is-tag' %> <% required = post.category&.required_tag_ids&.include? tag.id %>
<% topic = post.category&.topic_tag_ids&.include? tag.id %>
<%= link_to tag.name, questions_tagged_path(tag_set: tag_set.id, tag: tag.name),
class: "badge is-tag #{required ? 'is-filled' : ''} #{topic ? 'is-outlined' : ''}" %>
<% end %> <% end %>
<% end %> <% end %>
</div> </div>
... ...
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please to comment