Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

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...
commit 2a6c8b59bae905372ce77a7ab241600f4fd1efbf 1 parent 3e27cdd
Rodrigo Garcia Suarez authored
24 lib/extensions/page_extensions.rb
@@ -13,30 +13,6 @@ def self.included(base)
13 13 }
14 14 }
15 15
16   - alias_method :content_without_snippets_for, :content_for
17   - # Accessor method to get a page part from a page.
18   - # Example:
19   - #
20   - # Page.first.content_for(:body)
21   - #
22   - # Will return the body page part of the first page wrap with its
23   - # attached snippets.
24   - def content_for(part_title)
25   - part = self.parts.detect do |part|
26   - part.title.present? and #protecting against the problem that occurs when have nil title
27   - part.title == part_title.to_s or
28   - part.title.downcase.gsub(" ", "_") == part_title.to_s.downcase.gsub(" ", "_")
29   - end
30   -
31   - if part
32   - content = ""
33   - content += part.snippets.before.map{|snippet| snippet.try(:body)}.join
34   - part_body = part.try(:body)
35   - content += part_body unless part_body.nil?
36   - content += part.snippets.after.map{|snippet| snippet.try(:body)}.join
37   - end
38   - end
39   -
40 16 def snippets
41 17 Snippet.for_page(self)
42 18 end
35 lib/extensions/pages_helper_extensions.rb
... ... @@ -0,0 +1,35 @@
  1 +module Extensions
  2 + module PagesHelper
  3 +
  4 + def self.included(base)
  5 + base.class_eval do
  6 +
  7 + ##
  8 + # Accessor method to get a page part from a page.
  9 + # Example:
  10 + #
  11 + # content_of(Page.first, :body)
  12 + #
  13 + # Will return the body page part of the first page wrap with its
  14 + # attached snippets.
  15 + def content_of(page, part_title)
  16 + part = page.parts.detect do |part|
  17 + part.title.present? and #protecting against the problem that occurs when have nil title
  18 + part.title == part_title.to_s or
  19 + part.title.downcase.gsub(" ", "_") == part_title.to_s.downcase.gsub(" ", "_")
  20 + end
  21 +
  22 + if part
  23 + content = ""
  24 + content += part.snippets.before.map{|snippet| snippet.try(:body)}.join
  25 + part_body = part.try(:body)
  26 + content += part_body unless part_body.nil?
  27 + content += part.snippets.after.map{|snippet| snippet.try(:body)}.join
  28 + end
  29 + end
  30 +
  31 + end
  32 + end
  33 +
  34 + end
  35 +end
2  lib/refinerycms-snippets.rb
@@ -6,6 +6,7 @@ class Engine < Rails::Engine
6 6
7 7 config.before_initialize do
8 8 require 'extensions/page_extensions'
  9 + require 'extensions/pages_helper_extensions'
9 10 end
10 11
11 12 initializer "static assets" do |app|
@@ -18,6 +19,7 @@ class Engine < Rails::Engine
18 19 has_many :snippets, :through => :snippet_page_parts, :order => 'position ASC'
19 20 end
20 21 Page.send :include, Extensions::Page
  22 + PagesHelper.send :include, Extensions::PagesHelper
21 23 end
22 24
23 25 config.after_initialize do
25 spec/helpers/pages_helper_spec.rb
... ... @@ -0,0 +1,25 @@
  1 +require 'spec_helper'
  2 +
  3 +describe PagesHelper do
  4 +
  5 + before(:each) do
  6 + @page = Page.create!(:title => 'Page title')
  7 + @part = PagePart.create!(:title => 'Part', :body => "PART BODY")
  8 + @snippet_before = Snippet.create!(:title => 'Before title', :body => "BEFORE BODY")
  9 + @snippet_after = Snippet.create!(:title => 'After title', :body => "AFTER BODY")
  10 + SnippetPagePart.create!(:page_part_id => @part.id, :snippet_id => @snippet_before.id, :before_body => true)
  11 + SnippetPagePart.create!(:page_part_id => @part.id, :snippet_id => @snippet_after.id)
  12 + @page.parts << @part
  13 + end
  14 +
  15 + it 'should give content for one part wrapped with its snippets' do
  16 + content_of(@page, :part).should == "BEFORE BODY<p>PART BODY</p>AFTER BODY"
  17 + end
  18 +
  19 + it "should work when body of part is nil or don't have snippets" do
  20 + @part.update_attributes(:body => nil)
  21 + @part.snippets.map(&:delete)
  22 + Proc.new {content_of(@page, :part)}.should_not raise_exception
  23 + end
  24 +
  25 +end
8 spec/models/page_spec.rb
@@ -12,14 +12,6 @@
12 12 @page.parts << @part
13 13 end
14 14
15   - it 'should give content for one part wrapped with is snippets' do
16   - @page.content_for(:part).should == "BEFORE BODY<p>PART BODY</p>AFTER BODY"
17   - end
18   -
19   - it 'should give content for one part without is snippets' do
20   - @page.content_without_snippets_for(:part).should == "<p>PART BODY</p>"
21   - end
22   -
23 15 it 'should return all snippets attached to its parts' do
24 16 page = Page.create!(:title => 'Other page')
25 17 part = PagePart.create!(:title => 'Other part', :body => "OTHER PART BODY")

0 comments on commit 2a6c8b5

Please sign in to comment.
Something went wrong with that request. Please try again.