From 9299699f4b291b3a9e1ea41fa183fa8a45a142ce Mon Sep 17 00:00:00 2001 From: Matthew Werner Date: Tue, 25 Oct 2016 16:37:35 -0700 Subject: [PATCH] better category and field creation --- app/assets/javascripts/content.js.coffee | 4 ++ app/assets/stylesheets/content.css.scss | 2 +- .../attribute_fields_controller.rb | 26 +++++++++++- app/helpers/attributes_helper.rb | 2 +- app/helpers/content_helper.rb | 15 +++++++ app/models/attribute_category.rb | 16 ++++++- app/models/attribute_field.rb | 14 +++++-- app/models/concerns/has_attributes.rb | 4 +- app/views/attribute_fields/_modal.html.erb | 37 ++++++++++++++++ app/views/characters/edit.html.erb | 4 +- app/views/characters/new.html.erb | 4 +- app/views/content/_form.html.erb | 4 +- app/views/content/_show.html.erb | 2 +- .../content/form/_actions_dropdown.html.erb | 21 ++++++---- app/views/content/form/_panel.html.erb | 42 +++++++++---------- app/views/content/form/_text_input.html.erb | 21 ++++++---- app/views/content/form/_title.html.erb | 2 +- app/views/content/index.html.erb | 12 +----- app/views/items/edit.html.erb | 4 +- app/views/items/new.html.erb | 4 +- app/views/layouts/_navbar.html.erb | 13 ------ app/views/locations/edit.html.erb | 4 +- app/views/locations/new.html.erb | 4 +- app/views/universes/edit.html.erb | 1 + app/views/universes/new.html.erb | 4 +- config/attributes/attribute_category.yml | 8 +--- 26 files changed, 185 insertions(+), 89 deletions(-) create mode 100644 app/helpers/content_helper.rb create mode 100644 app/views/attribute_fields/_modal.html.erb diff --git a/app/assets/javascripts/content.js.coffee b/app/assets/javascripts/content.js.coffee index c0356a4b..95504a7b 100644 --- a/app/assets/javascripts/content.js.coffee +++ b/app/assets/javascripts/content.js.coffee @@ -9,5 +9,9 @@ $(document).ready -> window.scrollTo(0, 0); ), 1 + $('.new-attribute-field-link').click (e) -> + e.preventDefault() + $("#attribute-field-modal").openModal() + $('.share').click -> $('#share-modal').openModal() diff --git a/app/assets/stylesheets/content.css.scss b/app/assets/stylesheets/content.css.scss index c63cfa2c..07dbf929 100644 --- a/app/assets/stylesheets/content.css.scss +++ b/app/assets/stylesheets/content.css.scss @@ -29,4 +29,4 @@ p.long-form { .content-field { min-height: 140px; -} \ No newline at end of file +} diff --git a/app/controllers/attribute_fields_controller.rb b/app/controllers/attribute_fields_controller.rb index 30a651da..abebd7a7 100644 --- a/app/controllers/attribute_fields_controller.rb +++ b/app/controllers/attribute_fields_controller.rb @@ -1,7 +1,31 @@ # Controller for the Attribute model class AttributeFieldsController < ContentController + + def create + initialize_object + + if @content.save + successful_response(:back, t(:create_success, model_name: humanized_model_name)) + else + failed_response('new', :unprocessable_entity) + end + end + private + def initialize_object + category = current_user.attribute_categories.where(label: content_params[:attribute_category]).first_or_initialize.tap do |c| + c.entity_type = params[:entity_type] + c.save! + end + + @content = AttributeField.new(label: content_params[:label]).tap do |f| + f.attribute_category_id = category.id + f.user_id = current_user.id + f.field_type = 'textearea' + end + end + def content_params params.require(:attribute_field).permit(content_param_list) end @@ -9,7 +33,7 @@ class AttributeFieldsController < ContentController def content_param_list [ :universe_id, :user_id, - :attribute_category_id, + :attribute_category, :name, :field_type, :label, :description ] diff --git a/app/helpers/attributes_helper.rb b/app/helpers/attributes_helper.rb index 9c0747b6..7eac7d43 100644 --- a/app/helpers/attributes_helper.rb +++ b/app/helpers/attributes_helper.rb @@ -8,7 +8,7 @@ module AttributesHelper end end - link = content_tag(:a, category.label, href: "##{category.name}_panel") + link = content_tag(:a, category.label, href: "##{category.name.gsub("'", '')}_panel") content_tag(:li, link, class: "tab col s3 #{is_disabled}") end end diff --git a/app/helpers/content_helper.rb b/app/helpers/content_helper.rb new file mode 100644 index 00000000..ea5284dd --- /dev/null +++ b/app/helpers/content_helper.rb @@ -0,0 +1,15 @@ +module ContentHelper + def new_content_button(content) + icon = content_tag(:i, content.icon, class: "material-icons #{content.color}-text") + link = link_to("+ #{icon}".html_safe, new_polymorphic_path(content.build), class: "btn white #{content.color}-text") + + content_tag(:small, link, class: 'right') + end + + def content_settings_button(content) + icon = content_tag(:i, 'chrome_reader_mode', class: "material-icons #{content.color}-text") + link = link_to("+ #{icon}".html_safe, new_attribute_category_for_entity_path(content.content_name), class: "btn white #{content.color}-text") + + content_tag(:small, link, class: 'right') + end +end diff --git a/app/models/attribute_category.rb b/app/models/attribute_category.rb index 2cb24b49..a164dc6b 100644 --- a/app/models/attribute_category.rb +++ b/app/models/attribute_category.rb @@ -1,19 +1,33 @@ class AttributeCategory < ActiveRecord::Base + validates :name, presence: true + belongs_to :user has_many :attribute_fields include HasAttributes include Serendipitous::Concern + before_validation :ensure_name + def self.color 'amber' end def self.icon - 'chrome_reader_mode' + 'tab' end def self.content_name 'attribute_category' end + + def icon + self['icon'] || self.class.icon + end + + private + + def ensure_name + self.name ||= "#{label}-#{Time.now.to_i}".underscore.gsub(' ', '_') + end end diff --git a/app/models/attribute_field.rb b/app/models/attribute_field.rb index 4069a847..f8a7cd8a 100644 --- a/app/models/attribute_field.rb +++ b/app/models/attribute_field.rb @@ -1,4 +1,6 @@ class AttributeField < ActiveRecord::Base + validates :name, presence: true + belongs_to :user belongs_to :attribute_category has_many :attribute_values, class_name: 'Attribute' @@ -8,6 +10,8 @@ class AttributeField < ActiveRecord::Base attr_accessor :system + before_validation :ensure_name + scope :is_public, -> { eager_load(:universe).where('universes.privacy = ? OR attribute_fields.privacy = ?', 'public', 'public') } def self.color @@ -22,10 +26,6 @@ class AttributeField < ActiveRecord::Base 'attribute' end - def name - (self['name'] || "custom field #{Time.now.to_i}").downcase.gsub(' ','_') - end - def humanize label end @@ -37,4 +37,10 @@ class AttributeField < ActiveRecord::Base def system? !!self.system end + + private + + def ensure_name + self.name ||= "#{label}-#{Time.now.to_i}".underscore.gsub(' ', '_') + end end diff --git a/app/models/concerns/has_attributes.rb b/app/models/concerns/has_attributes.rb index eac2c9f3..de119941 100644 --- a/app/models/concerns/has_attributes.rb +++ b/app/models/concerns/has_attributes.rb @@ -9,13 +9,13 @@ module HasAttributes def self.attribute_categories(user = nil) categories = YAML.load_file(Rails.root.join('config', 'attributes', "#{content_name}.yml")).map do |category_name, details| - category = AttributeCategory.new(entity_type: self.name, name: category_name.to_s, label: details[:label], icon: details[:icon]) + category = AttributeCategory.new(entity_type: self.content_name, name: category_name.to_s, label: details[:label], icon: details[:icon]) category.attribute_fields << details[:attributes].map { |field| AttributeField.new(field.merge(system: true)) } category end return categories if user.nil? - [categories, user.attribute_categories.joins(:attribute_fields).where(['attribute_categories.entity_type = ?', content_name])].flatten.uniq + [categories, user.attribute_categories.where(['attribute_categories.entity_type = ?', content_name]).includes(:attribute_fields)].flatten.uniq end def update_custom_attributes diff --git a/app/views/attribute_fields/_modal.html.erb b/app/views/attribute_fields/_modal.html.erb new file mode 100644 index 00000000..87ae01fa --- /dev/null +++ b/app/views/attribute_fields/_modal.html.erb @@ -0,0 +1,37 @@ + diff --git a/app/views/characters/edit.html.erb b/app/views/characters/edit.html.erb index 75ebd595..b18f622e 100644 --- a/app/views/characters/edit.html.erb +++ b/app/views/characters/edit.html.erb @@ -1,3 +1,5 @@ <%= form_for @content do |form| %> <%= render partial: 'content/form', locals: { f: form, content: @content } %> -<% end %> \ No newline at end of file +<% end %> + +<%= render 'attribute_fields/modal', content: @content %> diff --git a/app/views/characters/new.html.erb b/app/views/characters/new.html.erb index 75ebd595..b18f622e 100644 --- a/app/views/characters/new.html.erb +++ b/app/views/characters/new.html.erb @@ -1,3 +1,5 @@ <%= form_for @content do |form| %> <%= render partial: 'content/form', locals: { f: form, content: @content } %> -<% end %> \ No newline at end of file +<% end %> + +<%= render 'attribute_fields/modal', content: @content %> diff --git a/app/views/content/_form.html.erb b/app/views/content/_form.html.erb index 058ab7d2..9e77add3 100644 --- a/app/views/content/_form.html.erb +++ b/app/views/content/_form.html.erb @@ -17,7 +17,7 @@