diff --git a/app/controllers/content_controller.rb b/app/controllers/content_controller.rb index e71762c4..3f5ba701 100644 --- a/app/controllers/content_controller.rb +++ b/app/controllers/content_controller.rb @@ -358,21 +358,17 @@ class ContentController < ApplicationController # Ugh not another one of these backfills if content.position.nil? - content_to_order_first = if content.is_a?(AttributeCategory) - content_type_class = content.entity_type.titleize.constantize - content_type_class.attribute_categories(current_user, show_hidden: true) + if content.is_a?(AttributeCategory) + content.backfill_categories_ordering! elsif content.is_a?(AttributeField) - content.attribute_category.attribute_fields - end - - ActiveRecord::Base.transaction do - content_to_order_first.each.with_index do |content_to_order, index| - content_to_order.update_column(:position, 1 + index) - end + content.attribute_category.backfill_fields_ordering! + else + raise "Attempting to backfill ordering for a new class: #{content.class.name}" end end - if content.reload && content.insert_at(1 + sort_params[:intended_position].to_i) + content.reload + if content.insert_at(1 + sort_params[:intended_position].to_i) render json: 200 else render json: 500 @@ -538,6 +534,7 @@ class ContentController < ApplicationController @navbar_actions << { label: 'Customize template', + class: 'right', href: main_app.attribute_customization_path(content_type.name.downcase) } end diff --git a/app/jobs/document_mention_job.rb b/app/jobs/document_mention_job.rb index 94b141a7..0be41122 100644 --- a/app/jobs/document_mention_job.rb +++ b/app/jobs/document_mention_job.rb @@ -6,7 +6,7 @@ class DocumentMentionJob < ApplicationJob def perform(*args) document_id = args.shift - document = Document.find(document_id) + document = Document.find_by(id: document_id) return unless document.present? return if document.body.nil? || document.body.empty? diff --git a/app/models/concerns/has_attributes.rb b/app/models/concerns/has_attributes.rb index 46dac36b..17652691 100644 --- a/app/models/concerns/has_attributes.rb +++ b/app/models/concerns/has_attributes.rb @@ -177,7 +177,7 @@ module HasAttributes ).pluck(:id) # Todo these two queries should be able to be joined into one - name_field = AttributeField.find_by( + AttributeField.find_by( user_id: user_id, attribute_category_id: category_ids, field_type: 'name' @@ -191,7 +191,7 @@ module HasAttributes ).pluck(:id) # Todo these two queries should be able to be joined into one - name_field = AttributeField.find_by( + AttributeField.find_by( user_id: user_id, attribute_category_id: category_ids, field_type: 'universe' @@ -205,7 +205,7 @@ module HasAttributes ).pluck(:id) # Todo these two queries should be able to be joined into one - field = AttributeField.find_by( + AttributeField.find_by( user_id: user_id, attribute_category_id: category_ids, label: label, diff --git a/app/models/page_data/attribute_category.rb b/app/models/page_data/attribute_category.rb index b2154ba4..e2fb6458 100644 --- a/app/models/page_data/attribute_category.rb +++ b/app/models/page_data/attribute_category.rb @@ -40,6 +40,50 @@ class AttributeCategory < ApplicationRecord entity_type.titleize.constantize end + def backfill_categories_ordering! + content_type_class = entity_type.titleize.constantize + category_owner = User.find(user_id) + + categories = content_type_class.attribute_categories(category_owner, show_hidden: true).to_a + + ActiveRecord::Base.transaction do + categories.each.with_index do |content_to_order, index| + content_to_order.update_column(:position, 1 + index) + + # While we're doing this, we might as well backfill the fields also + content_to_order.backfill_fields_ordering! + end + end + end + + def backfill_fields_ordering! + sorted_fields = attribute_fields.sort do |a, b| + a_value = case a.field_type + when 'name' then 0 + when 'universe' then 1 + else 2 # 'text_area', 'link' + end + + b_value = case b.field_type + when 'name' then 0 + when 'universe' then 1 + else 2 + end + + if a.position && b.position + a.position <=> b.position + else + a_value <=> b_value + end + end + + ActiveRecord::Base.transaction do + sorted_fields.each.with_index do |content_to_order, index| + content_to_order.update_column(:position, 1 + index) + end + end + end + private def ensure_name diff --git a/app/models/serializers/content_serializer.rb b/app/models/serializers/content_serializer.rb index 2fc821fd..6ed8ad76 100644 --- a/app/models/serializers/content_serializer.rb +++ b/app/models/serializers/content_serializer.rb @@ -68,18 +68,6 @@ class ContentSerializer value: value_for(field, content) } }.sort do |a, b| - a_value = case a[:type] - when 'name' then 0 - when 'universe' then 1 - else 2 # 'text_area', 'link' - end - - b_value = case b[:type] - when 'name' then 0 - when 'universe' then 1 - else 2 - end - # if a_value != b_value # a_value <=> b_value # else @@ -88,7 +76,20 @@ class ContentSerializer if a[:position] && b[:position] a[:position] <=> b[:position] + else + a_value = case a[:type] + when 'name' then 0 + when 'universe' then 1 + else 2 # 'text_area', 'link' + end + + b_value = case b[:type] + when 'name' then 0 + when 'universe' then 1 + else 2 + end + a_value <=> b_value end end diff --git a/app/views/content/attributes.html.erb b/app/views/content/attributes.html.erb index 875340fc..185be7ac 100644 --- a/app/views/content/attributes.html.erb +++ b/app/views/content/attributes.html.erb @@ -11,7 +11,9 @@