From d01d15261ce03d12b36189937e17f20c6ce64758 Mon Sep 17 00:00:00 2001 From: Andrew Kennedy Date: Sat, 14 Jan 2012 01:17:57 -0500 Subject: [PATCH 1/7] added dynamic_snippets --- lib/extensions/pages_helper_extensions.rb | 26 +++++++++++++++++++---- spec/helpers/pages_helper_spec.rb | 20 +++++++++++++++++ 2 files changed, 42 insertions(+), 4 deletions(-) diff --git a/lib/extensions/pages_helper_extensions.rb b/lib/extensions/pages_helper_extensions.rb index 06a74f4..4078c44 100644 --- a/lib/extensions/pages_helper_extensions.rb +++ b/lib/extensions/pages_helper_extensions.rb @@ -15,16 +15,34 @@ def self.included(base) 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(" ", "_") + title(part.title) == part_title end if part content = "" - content += part.snippets.before.map{|snippet| snippet.try(:body)}.join + content += part.snippets.before.map{|snippet| render_snippet(snippet)}.join part_body = part.try(:body) content += part_body unless part_body.nil? - content += part.snippets.after.map{|snippet| snippet.try(:body)}.join + 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 diff --git a/spec/helpers/pages_helper_spec.rb b/spec/helpers/pages_helper_spec.rb index e15e743..69d60fb 100644 --- a/spec/helpers/pages_helper_spec.rb +++ b/spec/helpers/pages_helper_spec.rb @@ -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("shared/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("shared/before_title").and_raise(ActionView::MissingTemplate) + render_snippet(@snippet_before).should == 'BEFORE BODY' + end + + it "should return nil if snippet body and template don't exist" do + @before_snippet.update_attribute(:body, nil) + should_receive(:render).with("shared/before_title").and_raise(ActionView::MissingTemplate) + render_snippet(@snippet_before).should be_nil + end + + it 'should return title' do + title(@part.title).should == :part + end + end From 5891588b7520e39db16822017d3736c7a9a0ed06 Mon Sep 17 00:00:00 2001 From: Andrew Kennedy Date: Sat, 14 Jan 2012 03:20:42 -0500 Subject: [PATCH 2/7] allow it to work with other engines --- ...extensions.rb => application_helper_extensions.rb} | 8 ++++---- lib/refinerycms-snippets.rb | 4 ++-- readme.md | 11 ++++++----- 3 files changed, 12 insertions(+), 11 deletions(-) rename lib/extensions/{pages_helper_extensions.rb => application_helper_extensions.rb} (87%) diff --git a/lib/extensions/pages_helper_extensions.rb b/lib/extensions/application_helper_extensions.rb similarity index 87% rename from lib/extensions/pages_helper_extensions.rb rename to lib/extensions/application_helper_extensions.rb index 4078c44..234ad52 100644 --- a/lib/extensions/pages_helper_extensions.rb +++ b/lib/extensions/application_helper_extensions.rb @@ -1,5 +1,5 @@ module Extensions - module PagesHelper + module ApplicationHelper def self.included(base) base.class_eval do @@ -12,7 +12,7 @@ def self.included(base) # # Will return the body page part of the first page wrap with its # attached snippets. - def content_of(page, part_title) + 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 @@ -21,8 +21,8 @@ def content_of(page, part_title) if part content = "" content += part.snippets.before.map{|snippet| render_snippet(snippet)}.join - part_body = part.try(:body) - content += part_body unless part_body.nil? + content += part.body if part.try(:body).present? + content += yield_body if yield_body.present? content += part.snippets.after.map{|snippet| render_snippet(snippet)}.join 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. From ca542426b0cbc41e4e024ce963440b7956b7a945 Mon Sep 17 00:00:00 2001 From: Andrew Kennedy Date: Sun, 15 Jan 2012 00:00:49 -0500 Subject: [PATCH 3/7] rename helper_spec --- .../{pages_helper_spec.rb => application_helper_spec.rb} | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) rename spec/helpers/{pages_helper_spec.rb => application_helper_spec.rb} (98%) diff --git a/spec/helpers/pages_helper_spec.rb b/spec/helpers/application_helper_spec.rb similarity index 98% rename from spec/helpers/pages_helper_spec.rb rename to spec/helpers/application_helper_spec.rb index 69d60fb..2c71b77 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 ApplcationHelper do before(:each) do @page = Page.create!(:title => 'Page title') From 11040ac0584d85e1f6d4b9ef846c15dbc0aed204 Mon Sep 17 00:00:00 2001 From: Andrew Kennedy Date: Sun, 15 Jan 2012 00:14:30 -0500 Subject: [PATCH 4/7] fix misspelling --- spec/helpers/application_helper_spec.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/spec/helpers/application_helper_spec.rb b/spec/helpers/application_helper_spec.rb index 2c71b77..2daf105 100644 --- a/spec/helpers/application_helper_spec.rb +++ b/spec/helpers/application_helper_spec.rb @@ -1,6 +1,6 @@ require 'spec_helper' -describe ApplcationHelper do +describe ApplicationHelper do before(:each) do @page = Page.create!(:title => 'Page title') From 82651d6356b2b5e4ac94264fdd74192c994d9516 Mon Sep 17 00:00:00 2001 From: Andrew Kennedy Date: Sun, 15 Jan 2012 00:24:56 -0500 Subject: [PATCH 5/7] fix helper tests --- spec/helpers/application_helper_spec.rb | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/spec/helpers/application_helper_spec.rb b/spec/helpers/application_helper_spec.rb index 2daf105..d9bb42e 100644 --- a/spec/helpers/application_helper_spec.rb +++ b/spec/helpers/application_helper_spec.rb @@ -23,17 +23,17 @@ end it "should return template if snippet template exists" do - should_receive(:render).with("shared/before_title").and_return('TEMPLATE') + 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("shared/before_title").and_raise(ActionView::MissingTemplate) + should_receive(:render).with({:partial=>"snippets/before_title"}).and_raise(ActionView::MissingTemplate) render_snippet(@snippet_before).should == 'BEFORE BODY' end it "should return nil if snippet body and template don't exist" do - @before_snippet.update_attribute(:body, nil) + @snippet_before.update_attribute(:body, nil) should_receive(:render).with("shared/before_title").and_raise(ActionView::MissingTemplate) render_snippet(@snippet_before).should be_nil end From c23093a6893f83926a0a69ace57e1b1debdd7c7a Mon Sep 17 00:00:00 2001 From: Andrew Kennedy Date: Sun, 15 Jan 2012 00:34:34 -0500 Subject: [PATCH 6/7] fix raise exception in test --- spec/helpers/application_helper_spec.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/spec/helpers/application_helper_spec.rb b/spec/helpers/application_helper_spec.rb index d9bb42e..b290322 100644 --- a/spec/helpers/application_helper_spec.rb +++ b/spec/helpers/application_helper_spec.rb @@ -28,13 +28,13 @@ end it "should return body if snippet template doesn't exist" do - should_receive(:render).with({:partial=>"snippets/before_title"}).and_raise(ActionView::MissingTemplate) + 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("shared/before_title").and_raise(ActionView::MissingTemplate) + should_receive(:render).with({:partial=>"snippets/before_title"}).and_raise(ActionView::MissingTemplate.new([], nil, nil, nil)) render_snippet(@snippet_before).should be_nil end From b90688fe941252280fd2ef70a77c9f4f2c11eebf Mon Sep 17 00:00:00 2001 From: Andrew Kennedy Date: Sun, 15 Jan 2012 02:16:10 -0500 Subject: [PATCH 7/7] prevent body fromrendering twice on engine pages --- lib/extensions/application_helper_extensions.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/extensions/application_helper_extensions.rb b/lib/extensions/application_helper_extensions.rb index 234ad52..d5bfb3b 100644 --- a/lib/extensions/application_helper_extensions.rb +++ b/lib/extensions/application_helper_extensions.rb @@ -21,7 +21,7 @@ def content_of(page, part_title, yield_body=nil) if part content = "" content += part.snippets.before.map{|snippet| render_snippet(snippet)}.join - content += part.body if part.try(:body).present? + 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