Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 53 additions & 0 deletions lib/extensions/application_helper_extensions.rb
Original file line number Diff line number Diff line change
@@ -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
35 changes: 0 additions & 35 deletions lib/extensions/pages_helper_extensions.rb

This file was deleted.

4 changes: 2 additions & 2 deletions lib/refinerycms-snippets.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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|
Expand All @@ -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
Expand Down
11 changes: 6 additions & 5 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
require 'spec_helper'

describe PagesHelper do
describe ApplicationHelper do

before(:each) do
@page = Page.create!(:title => 'Page title')
Expand All @@ -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