diff --git a/app/models/application_record.rb b/app/models/application_record.rb index e5c54e276baf834b15b95bcf6998364edd9f7441..2aa76e8e6c7c565c063c8dbdf83afe52ce42baef 100644 --- a/app/models/application_record.rb +++ b/app/models/application_record.rb @@ -30,6 +30,13 @@ class ApplicationRecord < ActiveRecord::Base ActiveRecord::Base.send(:sanitize_sql_array, ["MATCH (#{cols}) AGAINST (? IN BOOLEAN MODE)", term]) end + + def self.sanitize_sql_in(ary) + return "(NULL)" unless ary.present? && ary.respond_to?(:map) + + ary = ary.map { |el| ActiveRecord::Base.sanitize_sql_array(['?', el]) } + "(#{ary.join(', ')})" + end end module UserSortable diff --git a/app/models/tag.rb b/app/models/tag.rb index 65024e43b4894060fdb8b705445547c91a27a145..cd89a7d0d35a75be50baf01908b569ee4031c212 100644 --- a/app/models/tag.rb +++ b/app/models/tag.rb @@ -1,6 +1,13 @@ class Tag < ApplicationRecord include CommunityRelated + scope :category_order, -> (required_ids, topic_ids) do + helpers = ActionController::Base.helpers + order(Arel.sql("id IN #{sanitize_sql_in(required_ids)} DESC"), + Arel.sql("id IN #{sanitize_sql_in(topic_ids)} DESC"), + name: :asc) + end + has_and_belongs_to_many :posts belongs_to :tag_set diff --git a/app/views/posts/_expanded.html.erb b/app/views/posts/_expanded.html.erb index b00c2ca01bd8c09f02444dda6b99f940ce94bc5e..a2b45b8092f74a88507ba14246cd547d76f00547 100644 --- a/app/views/posts/_expanded.html.erb +++ b/app/views/posts/_expanded.html.erb @@ -98,10 +98,12 @@ <% if is_question %> <div class="post--tags has-padding-2"> <% tag_set = post.tag_set %> - <% post.tags.each do |tag| %> + <% required_ids = post.category&.required_tag_ids %> + <% topic_ids = post.category&.topic_tag_ids %> + <% post.tags.category_order(required_ids, topic_ids).each do |tag| %> <% next if tag.nil? %> - <% required = post.category&.required_tag_ids&.include? tag.id %> - <% topic = post.category&.topic_tag_ids&.include? tag.id %> + <% required = required_ids&.include? tag.id %> + <% topic = topic_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 %> diff --git a/app/views/posts/_list.html.erb b/app/views/posts/_list.html.erb index 5572829477260003e0a7ff11866afb386eddf95a..5703b4e84e486b74448fecae7667d7701ab71633 100644 --- a/app/views/posts/_list.html.erb +++ b/app/views/posts/_list.html.erb @@ -27,9 +27,11 @@ <div class="has-padding-top-2"> <% if is_question %> <% tag_set = post.tag_set %> - <% post.tags.each do |tag| %> - <% required = post.category&.required_tag_ids&.include? tag.id %> - <% topic = post.category&.topic_tag_ids&.include? tag.id %> + <% required_ids = post.category&.required_tag_ids %> + <% topic_ids = post.category&.topic_tag_ids %> + <% post.tags.category_order(required_ids, topic_ids).each do |tag| %> + <% required = required_ids&.include? tag.id %> + <% topic = topic_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 %>