Permalink
Browse files

The method to get the body of a part with its snippets has been moved

from Extensions::Pages#content_for to
Extensions::PageHelper#content_of. I think is better not to override
Pages#content_for and when erb templates sypport been added, it is
better to render those templates in a helper.
  • Loading branch information...
rochgs committed Sep 13, 2011
1 parent 3e27cdd commit 2a6c8b59bae905372ce77a7ab241600f4fd1efbf
@@ -13,30 +13,6 @@ def self.included(base)
}
}
- alias_method :content_without_snippets_for, :content_for
- # Accessor method to get a page part from a page.
- # Example:
- #
- # Page.first.content_for(:body)
- #
- # Will return the body page part of the first page wrap with its
- # attached snippets.
- def content_for(part_title)
- part = self.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
-
def snippets
Snippet.for_page(self)
end
@@ -0,0 +1,35 @@
+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
@@ -6,6 +6,7 @@ class Engine < Rails::Engine
config.before_initialize do
require 'extensions/page_extensions'
+ require 'extensions/pages_helper_extensions'
end
initializer "static assets" do |app|
@@ -18,6 +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
end
config.after_initialize do
@@ -0,0 +1,25 @@
+require 'spec_helper'
+
+describe PagesHelper do
+
+ before(:each) do
+ @page = Page.create!(:title => 'Page title')
+ @part = PagePart.create!(:title => 'Part', :body => "PART BODY")
+ @snippet_before = Snippet.create!(:title => 'Before title', :body => "BEFORE BODY")
+ @snippet_after = Snippet.create!(:title => 'After title', :body => "AFTER BODY")
+ SnippetPagePart.create!(:page_part_id => @part.id, :snippet_id => @snippet_before.id, :before_body => true)
+ SnippetPagePart.create!(:page_part_id => @part.id, :snippet_id => @snippet_after.id)
+ @page.parts << @part
+ end
+
+ it 'should give content for one part wrapped with its snippets' do
+ content_of(@page, :part).should == "BEFORE BODY<p>PART BODY</p>AFTER BODY"
+ end
+
+ it "should work when body of part is nil or don't have snippets" do
+ @part.update_attributes(:body => nil)
+ @part.snippets.map(&:delete)
+ Proc.new {content_of(@page, :part)}.should_not raise_exception
+ end
+
+end
View
@@ -12,14 +12,6 @@
@page.parts << @part
end
- it 'should give content for one part wrapped with is snippets' do
- @page.content_for(:part).should == "BEFORE BODY<p>PART BODY</p>AFTER BODY"
- end
-
- it 'should give content for one part without is snippets' do
- @page.content_without_snippets_for(:part).should == "<p>PART BODY</p>"
- end
-
it 'should return all snippets attached to its parts' do
page = Page.create!(:title => 'Other page')
part = PagePart.create!(:title => 'Other part', :body => "OTHER PART BODY")

0 comments on commit 2a6c8b5

Please sign in to comment.