diff --git a/lib/extensions/application_helper_extensions.rb b/lib/extensions/application_helper_extensions.rb new file mode 100644 index 0000000..d5bfb3b --- /dev/null +++ b/lib/extensions/application_helper_extensions.rb @@ -0,0 +1,53 @@ +module Extensions + module ApplicationHelper + + def self.included(base) + base.class_eval do + + ## + # Accessor method to get a page part from a page. + # Example: + # + # content_of(Page.first, :body) + # + # Will return the body page part of the first page wrap with its + # attached snippets. + def content_of(page, part_title, yield_body=nil) + part = page.parts.detect do |part| + part.title.present? and #protecting against the problem that occurs when have nil title + title(part.title) == part_title + end + + if part + content = "" + content += part.snippets.before.map{|snippet| render_snippet(snippet)}.join + content += part.body if part.body.present? and !yield_body.present? + content += yield_body if yield_body.present? + content += part.snippets.after.map{|snippet| render_snippet(snippet)}.join + end + end + + private + + def title(title) + case (title_symbol = title.to_s.gsub(/\ /, '').underscore.to_sym) + when :body then :body_content_left + when :side_body then :body_content_right + else title_symbol + end + end + + def render_snippet(snippet) + title_slug = snippet.title.to_s.gsub(/\s/, "_").gsub(/[^-\w]/, "").downcase + begin + render :partial => "snippets/#{title_slug}" + rescue ActionView::MissingTemplate + snippet.try(:body) + end + end + + end + end + + end +end diff --git a/lib/extensions/pages_helper_extensions.rb b/lib/extensions/pages_helper_extensions.rb deleted file mode 100644 index 06a74f4..0000000 --- a/lib/extensions/pages_helper_extensions.rb +++ /dev/null @@ -1,35 +0,0 @@ -module Extensions - module PagesHelper - - def self.included(base) - base.class_eval do - - ## - # Accessor method to get a page part from a page. - # Example: - # - # content_of(Page.first, :body) - # - # Will return the body page part of the first page wrap with its - # attached snippets. - def content_of(page, part_title) - part = page.parts.detect do |part| - part.title.present? and #protecting against the problem that occurs when have nil title - part.title == part_title.to_s or - part.title.downcase.gsub(" ", "_") == part_title.to_s.downcase.gsub(" ", "_") - end - - if part - content = "" - content += part.snippets.before.map{|snippet| snippet.try(:body)}.join - part_body = part.try(:body) - content += part_body unless part_body.nil? - content += part.snippets.after.map{|snippet| snippet.try(:body)}.join - end - end - - end - end - - end -end diff --git a/lib/refinerycms-snippets.rb b/lib/refinerycms-snippets.rb index 03e9b1c..debd45c 100644 --- a/lib/refinerycms-snippets.rb +++ b/lib/refinerycms-snippets.rb @@ -6,7 +6,7 @@ class Engine < Rails::Engine config.before_initialize do require 'extensions/page_extensions' - require 'extensions/pages_helper_extensions' + require 'extensions/application_helper_extensions' end initializer "static assets" do |app| @@ -19,7 +19,7 @@ class Engine < Rails::Engine has_many :snippets, :through => :snippet_page_parts, :order => 'position ASC' end Page.send :include, Extensions::Page - PagesHelper.send :include, Extensions::PagesHelper + ApplicationHelper.send :include, Extensions::ApplicationHelper end config.after_initialize do diff --git a/readme.md b/readme.md index c72aa14..de6cc41 100644 --- a/readme.md +++ b/readme.md @@ -15,13 +15,13 @@ Snippets allows you to relate one or more html blocks to any page in Refinery. T * Save and Continue button -- done * Attaching snippet to page part directly through administration, without overriding view template -- done * Documentation -* Tests +* Tests ### to 2.0 * custom attributes for snippet (id, classes etc) * support for dynamic snippets (erb templates, forms etc) -* improve UI -* inheritance and clone +* improve UI +* inheritance and clone ## Install @@ -38,8 +38,9 @@ Next run ## Usage * Create Snippet on /refinery/snippets -* Now you can attach snippet to page when you click Edit this page on `/refinery/pages`. In the Snippets tab you can select the part to which you want to attach the block and add it after and/or before the html body of the part. -* Next you can use content_of(@page, :part) to print the body of the part and the snippets attached to it in the pages views. +* Now you can attach snippet to page when you click Edit this page on `/refinery/pages`. In the Snippets tab you can select the part to which you want to attach the block and add it after and/or before the html body of the part. +* Next you can use content_of(@page, :part) or to print the body of the part and the snippets attached to it in the pages views. +* You can also use content_of(@page, :part, yield(:part)) for it to work with other engines such as blog and news * You have some other interesting methods to work with snippets: * page.snippets: returns all the snippets attached to all the parts of page. * part.after: returns all the snippets attached after the html body of part. diff --git a/spec/helpers/pages_helper_spec.rb b/spec/helpers/application_helper_spec.rb similarity index 51% rename from spec/helpers/pages_helper_spec.rb rename to spec/helpers/application_helper_spec.rb index e15e743..b290322 100644 --- a/spec/helpers/pages_helper_spec.rb +++ b/spec/helpers/application_helper_spec.rb @@ -1,6 +1,6 @@ require 'spec_helper' -describe PagesHelper do +describe ApplicationHelper do before(:each) do @page = Page.create!(:title => 'Page title') @@ -22,4 +22,24 @@ Proc.new {content_of(@page, :part)}.should_not raise_exception end + it "should return template if snippet template exists" do + should_receive(:render).with({:partial=>"snippets/before_title"}).and_return('TEMPLATE') + render_snippet(@snippet_before).should == 'TEMPLATE' + end + + it "should return body if snippet template doesn't exist" do + should_receive(:render).with({:partial=>"snippets/before_title"}).and_raise(ActionView::MissingTemplate.new([], nil, nil, nil)) + render_snippet(@snippet_before).should == 'BEFORE BODY' + end + + it "should return nil if snippet body and template don't exist" do + @snippet_before.update_attribute(:body, nil) + should_receive(:render).with({:partial=>"snippets/before_title"}).and_raise(ActionView::MissingTemplate.new([], nil, nil, nil)) + render_snippet(@snippet_before).should be_nil + end + + it 'should return title' do + title(@part.title).should == :part + end + end