Skip to content
Browse files

Added wrapper methods to the scopes Page.for_snippet and

Snippet.for_page so you can call page.snippets and snippet.page. With
tests.
  • Loading branch information...
1 parent 098ebed commit dcc26e9987f0d903ae8373dbd5b20acba62da646 Rodrigo Garcia Suarez committed Aug 25, 2011
Showing with 45 additions and 9 deletions.
  1. +6 −1 app/models/snippet.rb
  2. +7 −3 lib/extensions/page_extensions.rb
  3. +16 −2 spec/models/page_spec.rb
  4. +16 −3 spec/models/snippet_spec.rb
View
7 app/models/snippet.rb
@@ -26,7 +26,7 @@ class Snippet < ActiveRecord::Base
:joins => :page_parts,
:conditions => {:snippets_page_parts => {:before_body => false}}
}
-
+
def self.inactive(page)
@page = page
snippets = scoped
@@ -42,4 +42,9 @@ def self.inactive(page)
includes(:translations).where(
translations[:locale].eq(Globalize.locale)).where(pages[:id].eq(translations[:snippet_id]))
}
+
+ def pages
+ Page.for_snippet(self)
+ end
+
end
View
10 lib/extensions/page_extensions.rb
@@ -2,17 +2,17 @@ module Extensions
module Page
def self.included(base)
-
+
base.class_eval do
-
+
named_scope :for_snippet, lambda{ |snippet|
raise RuntimeError.new("Couldn't find Pages for a nil Snippet") if snippet.blank?
{
:joins => {:parts, :snippets},
:conditions => {:snippets_page_parts => {:snippet_id => snippet.id}}
}
}
-
+
alias_method :content_without_snippets_for, :content_for
# Accessor method to get a page part from a page.
# Example:
@@ -34,6 +34,10 @@ def content_for(part_title)
content += part.snippets.after.map{|snippet| snippet.try(:body)}.join
end
+ def snippets
+ Snippet.for_page(self)
+ end
+
end
end
View
18 spec/models/page_spec.rb
@@ -7,8 +7,8 @@
@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)
+ 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
@@ -20,4 +20,18 @@
@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")
+ snippet = Snippet.create!(:title => 'Other snippet', :body => "SNIPPET BODY")
+ part.snippets << snippet
+ @page.snippets.should have(2).snippets
+ @page.snippets.each do |s|
+ s.title.should_not == snippet.title
+ end
+ part.snippets.clear
+ part.save
+ page.snippets.should be_empty
+ end
+
end
View
19 spec/models/snippet_spec.rb
@@ -17,7 +17,7 @@ def reset_snippet(options = {})
end
context "validations" do
-
+
it "rejects empty title" do
Snippet.new(@valid_attributes.merge(:title => "")).should_not be_valid
end
@@ -26,7 +26,20 @@ def reset_snippet(options = {})
# as one gets created before each spec by reset_snippet
Snippet.new(@valid_attributes).should_not be_valid
end
-
+
+ end
+
+ it 'should return the page it is attached' do
+ @snippet.pages.should be_empty
+ page = Page.create!(:title => 'Page')
+ 2.times do
+ part = PagePart.create!(:title => 'Other part', :body => "OTHER PART BODY", :page_id => Page.create!(:title => 'Other Page').id)
+ part.snippets << @snippet
+ end
+ @snippet.pages.should have(2).pages
+ @snippet.pages.each do |p|
+ p.title.should_not == page.title
+ end
end
-end
+end

0 comments on commit dcc26e9

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