Skip to content

Commit

Permalink
Merge pull request #33 from spartansystems/nested_collections
Browse files Browse the repository at this point in the history
Nested collections
  • Loading branch information
Mrjaco12 committed Oct 26, 2015
2 parents dec5780 + 3780da5 commit 4de89b7
Show file tree
Hide file tree
Showing 8 changed files with 75 additions and 40 deletions.
20 changes: 0 additions & 20 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
25 changes: 20 additions & 5 deletions app/assets/javascripts/atomic_cms.js
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand All @@ -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();
});
Expand Down
13 changes: 2 additions & 11 deletions app/components/array_component.rb
Original file line number Diff line number Diff line change
@@ -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
8 changes: 5 additions & 3 deletions app/controllers/atomic_cms/components_controller.rb
Original file line number Diff line number Diff line change
@@ -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
21 changes: 21 additions & 0 deletions app/helpers/component_helper.rb
Original file line number Diff line number Diff line change
@@ -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
6 changes: 6 additions & 0 deletions app/views/components/_children_field.html.slim
Original file line number Diff line number Diff line change
@@ -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
14 changes: 14 additions & 0 deletions lib/atomic_cms/editor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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|
Expand Down
8 changes: 7 additions & 1 deletion lib/atomic_cms/engine.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 4de89b7

Please sign in to comment.