Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

added dynamic_snippets #13

Open
wants to merge 7 commits into from

2 participants

@akennedy

There is already a pull request for this issue from @rochgs, but I just thought you may be interested in how I did it. Also updated content_of method to allow body_content_left and body_content_right.

use content_of(@page, :part, yield(:part)) when you need it to work with other engines such as blog

@rochgs

@akennedy proposal is very similar to mine right now. My pull request is seriously obsolete. In a first modification I also moved the rendering method to the PagesHelper but, finally, I moved it to the ApplicationHelper so it could be easily accessed from everywhere.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Jan 14, 2012
  1. @akennedy

    added dynamic_snippets

    akennedy authored
  2. @akennedy
Commits on Jan 15, 2012
  1. @akennedy

    rename helper_spec

    akennedy authored
  2. @akennedy

    fix misspelling

    akennedy authored
  3. @akennedy

    fix helper tests

    akennedy authored
  4. @akennedy

    fix raise exception in test

    akennedy authored
  5. @akennedy
This page is out of date. Refresh to see the latest.
View
53 lib/extensions/application_helper_extensions.rb
@@ -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
View
35 lib/extensions/pages_helper_extensions.rb
@@ -1,35 +0,0 @@
-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
View
4 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
View
11 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.
View
22 spec/helpers/pages_helper_spec.rb → spec/helpers/application_helper_spec.rb
@@ -1,6 +1,6 @@
require 'spec_helper'
-describe PagesHelper do
+describe ApplicationHelper do
before(:each) do
@page = Page.create!(:title => 'Page title')
@@ -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
Something went wrong with that request. Please try again.