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/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 new file mode 100644 index 0000000..bd20db1 --- /dev/null +++ b/app/helpers/component_helper.rb @@ -0,0 +1,21 @@ +module ComponentHelper + 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 + + 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 acb8ca5..1dd3e8c 100644 --- a/lib/atomic_cms/engine.rb +++ b/lib/atomic_cms/engine.rb @@ -4,10 +4,16 @@ 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