From cd3cc2f47c019a2cc074bf9cf87d225e8a297654 Mon Sep 17 00:00:00 2001 From: Andrew Brown Date: Fri, 3 Jan 2020 11:04:55 -0600 Subject: [PATCH] field editor fix --- app/controllers/attribute_fields_controller.rb | 14 ++++++++++---- app/models/concerns/has_attributes.rb | 2 +- app/models/page_data/attribute_category.rb | 1 + .../_category_fields_customization.html.erb | 3 ++- 4 files changed, 14 insertions(+), 6 deletions(-) diff --git a/app/controllers/attribute_fields_controller.rb b/app/controllers/attribute_fields_controller.rb index 938c1ce6..a8e67b59 100644 --- a/app/controllers/attribute_fields_controller.rb +++ b/app/controllers/attribute_fields_controller.rb @@ -27,10 +27,15 @@ class AttributeFieldsController < ContentController end if @content.attribute_category_id.nil? - category = current_user.attribute_categories.where(label: content_params[:attribute_category] || content_params[:label]).first_or_initialize.tap do |c| - c.entity_type = params[:entity_type] || content_params[:entity_type] - c.save! + category = current_user.attribute_categories.where(id: content_params[:attribute_category_id]).first + + if category.nil? + category = current_user.attribute_categories.where(label: content_params[:attribute_category] || content_params[:label]).first_or_initialize.tap do |c| + c.entity_type = params[:entity_type] || content_params[:entity_type] + c.save! + end end + @content.attribute_category_id = category.id end @@ -71,7 +76,8 @@ class AttributeFieldsController < ContentController :attribute_category, :name, :field_type, :label, :description, - :entity_type, :attribute_category_id, + :entity_type, + :attribute_category_id, :hidden ] end diff --git a/app/models/concerns/has_attributes.rb b/app/models/concerns/has_attributes.rb index 0504cae6..6271bc6d 100644 --- a/app/models/concerns/has_attributes.rb +++ b/app/models/concerns/has_attributes.rb @@ -23,7 +23,7 @@ module HasAttributes old_column_source: field[:name], user: user, field_type: field[:field_type].presence || "text_area", - label: field[:label].presence || 'Untitled field' + label: field[:label].presence || 'Untitled field' ) af_field end if details.key?(:attributes) diff --git a/app/models/page_data/attribute_category.rb b/app/models/page_data/attribute_category.rb index cb9d4bb3..edae7874 100644 --- a/app/models/page_data/attribute_category.rb +++ b/app/models/page_data/attribute_category.rb @@ -2,6 +2,7 @@ class AttributeCategory < ApplicationRecord acts_as_paranoid validates :name, presence: true + # validates :entity_type, presence: true # todo turn this on, but check prod data for exceptions first belongs_to :user has_many :attribute_fields, dependent: :destroy diff --git a/app/views/content/attributes/_category_fields_customization.html.erb b/app/views/content/attributes/_category_fields_customization.html.erb index 72a3f6dd..65476c46 100644 --- a/app/views/content/attributes/_category_fields_customization.html.erb +++ b/app/views/content/attributes/_category_fields_customization.html.erb @@ -29,7 +29,8 @@
<%= form_for(category.attribute_fields.build, method: :post) do |f| %> <%# todo the following line shouldn't be necessary %> - <%= f.hidden_field :category_id, value: category.id %> + <%= f.hidden_field :attribute_category_id, value: category.id %> +
<%= f.text_area :label, class: 'materialize-textarea js-field-input' %>