From f9bb59d177023d8214777adfe4f196675c5a18aa Mon Sep 17 00:00:00 2001 From: Jacob Date: Tue, 20 Oct 2015 16:52:22 -0400 Subject: [PATCH 1/2] Include helpers Why: So we don't have to manually do it in the readme. This change addresses the need by: Include helpers in engine. --- README.md | 20 -------------------- app/helpers/component_helper.rb | 18 ++++++++++++++++++ lib/atomic_cms/engine.rb | 9 ++++++++- 3 files changed, 26 insertions(+), 21 deletions(-) create mode 100644 app/helpers/component_helper.rb diff --git a/README.md b/README.md index f50df86..a59135a 100644 --- a/README.md +++ b/README.md @@ -141,26 +141,6 @@ class PagesController < ApplicationController end end ``` -#### Helper -Create `app/helpers/application_helper.rb` and update it to match the following: -```ruby -module ApplicationHelper - def add_option(option, output = nil) - return unless option - return output if output - option - end - - def markdown(text) - return unless text - Redcarpet::Markdown.new(Redcarpet::Render::HTML).render(text).html_safe - end - - def markdown_help_url - "http://nestacms.com/docs/creating-content/markdown-cheat-sheet" - end -end -``` #### Views Create a view at `app/views/pages/page.html.slim` that contains the following: ```ruby diff --git a/app/helpers/component_helper.rb b/app/helpers/component_helper.rb new file mode 100644 index 0000000..f9279d2 --- /dev/null +++ b/app/helpers/component_helper.rb @@ -0,0 +1,18 @@ +module ComponentHelper + def add_option(option, output = nil) + (output) ? output : option if option + end + + def markdown(text) + return unless text + Redcarpet::Markdown.new(Redcarpet::Render::HTML).render(text).html_safe + end + + def markdown_help_url + "http://nestacms.com/docs/creating-content/markdown-cheat-sheet" + end + + def render_children(children) + children.map(&:render).reduce(:+) + end +end diff --git a/lib/atomic_cms/engine.rb b/lib/atomic_cms/engine.rb index acb8ca5..f5d86e9 100644 --- a/lib/atomic_cms/engine.rb +++ b/lib/atomic_cms/engine.rb @@ -1,13 +1,20 @@ module AtomicCms require 'paperclip' + require 'pry' class Engine < ::Rails::Engine isolate_namespace AtomicCms config.generators do |g| - g.test_framework :rspec, fixture: false + g.test_framework :rspec, fixture: false g.fixture_replacement :factory_girl, dir: 'spec/factories' g.assets false g.helper false end + + initializer 'atomic_cms.action_controller' do |_app| + ActiveSupport.on_load :action_controller do + helper AtomicCms::ComponentHelper + end + end end end From 3780da5a260094b9f62f277130cd89f0b6cf9487 Mon Sep 17 00:00:00 2001 From: Jacob Date: Thu, 22 Oct 2015 14:08:33 -0400 Subject: [PATCH 2/2] Add the ability to nest components in the CMS Why: We need to be able to include some component inside of other bigger components and have them render a dynamic number of options. This change addresses the need by: Including some methods from the ArrayComponent class in the larger Editor class so they are available to all inheriting classes. Update the atomic_cms.js to be able to add subcomponents to parent ones. --- app/assets/javascripts/atomic_cms.js | 25 +++++++++++++++---- app/components/array_component.rb | 13 ++-------- .../atomic_cms/components_controller.rb | 8 +++--- app/helpers/component_helper.rb | 5 +++- .../components/_children_field.html.slim | 6 +++++ lib/atomic_cms/editor.rb | 14 +++++++++++ lib/atomic_cms/engine.rb | 3 +-- 7 files changed, 52 insertions(+), 22 deletions(-) create mode 100644 app/views/components/_children_field.html.slim diff --git a/app/assets/javascripts/atomic_cms.js b/app/assets/javascripts/atomic_cms.js index 9e2070e..5b55b7a 100644 --- a/app/assets/javascripts/atomic_cms.js +++ b/app/assets/javascripts/atomic_cms.js @@ -143,11 +143,10 @@ var $next = $($input.siblings('input')); $input.attr('name', null).val(''); - return $input.on('change', function() { + $input.on('change', function() { var formData = new FormData(), - fileData = event.target.files[0], - next = $(event.target).next(); + fileData = event.target.files[0]; formData.append('file', fileData); $.ajax({ @@ -166,15 +165,31 @@ }); }); + $editor.find('.add-children-sublist-item').each(function() { + var $input = $(this); + $input.attr('name', null).val(''); + $input.click(function(e) { + e.preventDefault(); + var component = $(this).siblings('.children-sublist').val(); + if (component !== ''){ + $scope.$broadcast('append', { + href: component + }); + } + }); + }); + $editor.find(':input').each(function() { - if($(this).prop('type') === 'file') return;//guard clause, already processed file fields + //guard clause for handling alternatly handled inputs + if($(this).prop('type') === 'file') { return; } + if($(this).hasClass('children-sublist')) { return; } var $input = $(this); var fieldName = $input.attr('name').replace($scope.prefix, '').replace(/\[|\]/g, ''); $input.attr('name', null).val($scope.preview[fieldName]); - return $input.on('keyup change', function() { + $input.on('keyup change', function() { $scope.$apply(function() { $scope.preview[fieldName] = $input.val(); }); diff --git a/app/components/array_component.rb b/app/components/array_component.rb index e194b1b..b44e9ba 100644 --- a/app/components/array_component.rb +++ b/app/components/array_component.rb @@ -1,20 +1,11 @@ class ArrayComponent < AtomicAssets::Component - def children - options[:children] ||= [] - end - def render children.map(&:render).reduce(:+) end def edit rtn = cms_fields - rtn << cms_array(:children) do - rtn2 = "" - children.each do |child| - rtn2 << cms_array_node { child.edit } - end - rtn2.html_safe - end + rtn << render_child_array + rtn.html_safe end end diff --git a/app/controllers/atomic_cms/components_controller.rb b/app/controllers/atomic_cms/components_controller.rb index 860305a..8c87516 100644 --- a/app/controllers/atomic_cms/components_controller.rb +++ b/app/controllers/atomic_cms/components_controller.rb @@ -1,5 +1,7 @@ -class AtomicCms::ComponentsController < ApplicationController - def edit - render text: component(params[:id]).edit_array(!!params[:inline]) +module AtomicCms + class ComponentsController < ApplicationController + def edit + render text: component(params[:id]).edit_array(!!params[:inline]) + end end end diff --git a/app/helpers/component_helper.rb b/app/helpers/component_helper.rb index f9279d2..bd20db1 100644 --- a/app/helpers/component_helper.rb +++ b/app/helpers/component_helper.rb @@ -1,6 +1,8 @@ module ComponentHelper def add_option(option, output = nil) - (output) ? output : option if option + return unless option + return output if output + option end def markdown(text) @@ -13,6 +15,7 @@ def markdown_help_url end def render_children(children) + return children unless children.present? && children.is_a?(Array) children.map(&:render).reduce(:+) end end diff --git a/app/views/components/_children_field.html.slim b/app/views/components/_children_field.html.slim new file mode 100644 index 0000000..7cc0c50 --- /dev/null +++ b/app/views/components/_children_field.html.slim @@ -0,0 +1,6 @@ +span.li.string.input + label.label #{name.to_s.humanize} + select.cms-field class="children-sublist" + option + = options_for_select(options[:collection], value) + a class="button add-children-sublist-item" Add diff --git a/lib/atomic_cms/editor.rb b/lib/atomic_cms/editor.rb index 05ca61a..4f6adc5 100644 --- a/lib/atomic_cms/editor.rb +++ b/lib/atomic_cms/editor.rb @@ -14,6 +14,20 @@ def edit_array(inline = false) end end + def render_child_array + cms_array(:children) do + rtn = "" + children.each do |child| + rtn << cms_array_node { child.edit } + end + rtn.html_safe + end + end + + def children + options[:children] ||= [] + end + module ClassMethods def from_hash(params) h.component(params.delete(:template_name)).tap do |obj| diff --git a/lib/atomic_cms/engine.rb b/lib/atomic_cms/engine.rb index f5d86e9..1dd3e8c 100644 --- a/lib/atomic_cms/engine.rb +++ b/lib/atomic_cms/engine.rb @@ -1,6 +1,5 @@ module AtomicCms require 'paperclip' - require 'pry' class Engine < ::Rails::Engine isolate_namespace AtomicCms @@ -11,7 +10,7 @@ class Engine < ::Rails::Engine g.helper false end - initializer 'atomic_cms.action_controller' do |_app| + initializer "atomic_cms.action_controller" do |_app| ActiveSupport.on_load :action_controller do helper AtomicCms::ComponentHelper end