Permalink
Browse files

moved rendering code to Cell::Rendering. minor cleanups.

  • Loading branch information...
1 parent a6bdea7 commit 1d8a5412da4f00c40724f62dc517388fd6206c9c @apotonick apotonick committed Dec 5, 2011
Showing with 120 additions and 125 deletions.
  1. +1 −0 CHANGES.textile
  2. +10 −123 lib/cell/base.rb
  3. +109 −0 lib/cell/rendering.rb
  4. +0 −2 lib/cells.rb
View
@@ -3,6 +3,7 @@ h2. 3.7.0
* @Cell::Base@ got rid of the controller dependency. If you want the @ActionController@ instance around in your cell, use @Cell::Rails@ - this should be the default in a standard Rails setup. However, if you plan on using a Cell in a Rack middleware or don't need the controller, use @Cell::Base@.
* New API for @Rails.create_cell_for@ and @Rails.render_cell_for@: the constructor is now the 3rd argument.
* Moved builder methods to @Cell::Builder@ module.
+* @DEFAULT_VIEW_PATHS@ is now in @Cell::Base@.
h2. 3.7.0
View
@@ -1,20 +1,23 @@
require 'abstract_controller'
require 'cell/builder'
require 'cell/caching'
-
+require 'cell/rendering'
+require 'cell/rails3_0_strategy' if Cells.rails3_0?
+require 'cell/rails3_1_strategy' if Cells.rails3_1?
+
module Cell
class Base < AbstractController::Base
+ abstract!
DEFAULT_VIEW_PATHS = [File.join('app', 'cells')]
extend Builder
include AbstractController
- include Rendering, Layouts, Helpers, Callbacks, Translation, Logger
+ include AbstractController::Rendering, Layouts, Helpers, Callbacks, Translation, Logger
+
+ include VersionStrategy
+ include Rendering
+ include Caching
- if Cells.rails3_0?
- require 'cell/rails3_0_strategy'
- elsif Cells.rails3_1?
- require 'cell/rails3_1_strategy'
- end
class View < ActionView::Base
def render(*args, &block)
@@ -26,39 +29,6 @@ def render(*args, &block)
end
- module Rendering
- extend ActiveSupport::Concern
-
- # Invoke the state method for +state+ which usually renders something nice.
- def render_state(state, *args)
- process(state, *args)
- end
-
- module ClassMethods
- # Main entry point for #render_cell.
- def render_cell_for(name, state, *args)
- cell = create_cell_for(name, *args)
- yield cell if block_given?
-
- render_cell_state(cell, state, *args)
- end
-
- private
- def render_cell_state(cell, state, *args)
- cell.render_state(state, *args)
- end
- end
- end
-
-
- include VersionStrategy
- include Rendering
- include Caching
-
- abstract!
-
-
-
# Called in Railtie at initialization time.
def self.setup_view_paths!
self.view_paths = DEFAULT_VIEW_PATHS
@@ -67,88 +37,5 @@ def self.setup_view_paths!
def self.controller_path
@controller_path ||= name.sub(/Cell$/, '').underscore unless anonymous?
end
-
- # Renders the view for the current state and returns the markup.
- # Don't forget to return the markup itself from the state method.
- #
- # === Options
- # +:view+:: Specifies the name of the view file to render. Defaults to the current state name.
- # +:layout+:: Renders the state wrapped in the layout. Layouts reside in <tt>app/cells/layouts</tt>.
- # +:locals+:: Makes the named parameters available as variables in the view.
- # +:text+:: Just renders plain text.
- # +:inline+:: Renders an inline template as state view. See ActionView::Base#render for details.
- # +:file+:: Specifies the name of the file template to render.
- # +:nothing+:: Doesn't invoke the rendering process.
- # +:state+:: Instantly invokes another rendering cycle for the passed state and returns. You may pass arbitrary state-args to the called state.
- # +:format+:: Sets a different template format, e.g. +:json+. Use this option with caution as it currently modifies the global format variable. This might lead to unexpected subsequent render behaviour due to a design flaw in Rails.
- #
- # Example:
- # class MusicianCell < ::Cell::Base
- # def sing
- # # ... laalaa
- # render
- # end
- #
- # renders the view <tt>musician/sing.html</tt>.
- #
- # def sing
- # # ... laalaa
- # render :view => :shout, :layout => 'metal'
- # end
- #
- # renders <tt>musician/shout.html</tt> and wrap it in <tt>app/cells/layouts/metal.html.erb</tt>.
- #
- # === #render is explicit!
- # You can also alter the markup from #render. Just remember to return it.
- #
- # def sing
- # render + render + render
- # end
- #
- # will render three concated views.
- #
- # === Partials?
- #
- # In Cells we abandoned the term 'partial' in favor of plain 'views' - we don't need to distinguish
- # between both terms. A cell view is both, a view and a kind of partial as it represents only a fragment
- # of the page.
- #
- # Just use <tt>:view</tt> and enjoy.
- #
- # === Using states instead of helpers
- #
- # Sometimes it's useful to not only render a view but also invoke the associated state. This is
- # especially helpful when replacing helpers. Do that with <tt>render :state</tt>.
- #
- # def show_cheap_item(item)
- # render if item.price <= 1
- # end
- #
- # A view could use this state in place of an odd helper.
- #
- # - @items.each do |item|
- # = render({:state => :show_cheap_item}, item)
- #
- # This calls the state method which in turn will render its view - if the item isn't too expensive.
- def render(*args)
- render_view_for(self.action_name, *args)
- end
-
- private
- # Renders the view belonging to the given state. Will raise ActionView::MissingTemplate
- # if it can't find a view.
- def render_view_for(state, *args)
- opts = args.first.is_a?(::Hash) ? args.shift : {}
-
- return "" if opts[:nothing]
-
- if opts[:state]
- opts[:text] = render_state(opts.delete(:state), *args)
- elsif (opts.keys & [:text, :inline, :file]).blank?
- process_opts_for(opts, state)
- end
-
- render_to_string(opts).html_safe # ActionView::Template::Text doesn't do that for us.
- end
end
end
View
@@ -0,0 +1,109 @@
+module Cell
+ module Rendering
+ extend ActiveSupport::Concern
+
+ # Invoke the state method for +state+ which usually renders something nice.
+ def render_state(state, *args)
+ process(state, *args)
+ end
+
+ # Renders the view for the current state and returns the markup.
+ # Don't forget to return the markup itself from the state method.
+ #
+ # === Options
+ # +:view+:: Specifies the name of the view file to render. Defaults to the current state name.
+ # +:layout+:: Renders the state wrapped in the layout. Layouts reside in <tt>app/cells/layouts</tt>.
+ # +:locals+:: Makes the named parameters available as variables in the view.
+ # +:text+:: Just renders plain text.
+ # +:inline+:: Renders an inline template as state view. See ActionView::Base#render for details.
+ # +:file+:: Specifies the name of the file template to render.
+ # +:nothing+:: Doesn't invoke the rendering process.
+ # +:state+:: Instantly invokes another rendering cycle for the passed state and returns. You may pass arbitrary state-args to the called state.
+ # +:format+:: Sets a different template format, e.g. +:json+. Use this option with caution as it currently modifies the global format variable. This might lead to unexpected subsequent render behaviour due to a design flaw in Rails.
+ #
+ # Example:
+ # class MusicianCell < ::Cell::Base
+ # def sing
+ # # ... laalaa
+ # render
+ # end
+ #
+ # renders the view <tt>musician/sing.html</tt>.
+ #
+ # def sing
+ # # ... laalaa
+ # render :view => :shout, :layout => 'metal'
+ # end
+ #
+ # renders <tt>musician/shout.html</tt> and wrap it in <tt>app/cells/layouts/metal.html.erb</tt>.
+ #
+ # === #render is explicit!
+ # You can also alter the markup from #render. Just remember to return it.
+ #
+ # def sing
+ # render + render + render
+ # end
+ #
+ # will render three concated views.
+ #
+ # === Partials?
+ #
+ # In Cells we abandoned the term 'partial' in favor of plain 'views' - we don't need to distinguish
+ # between both terms. A cell view is both, a view and a kind of partial as it represents only a fragment
+ # of the page.
+ #
+ # Just use <tt>:view</tt> and enjoy.
+ #
+ # === Using states instead of helpers
+ #
+ # Sometimes it's useful to not only render a view but also invoke the associated state. This is
+ # especially helpful when replacing helpers. Do that with <tt>render :state</tt>.
+ #
+ # def show_cheap_item(item)
+ # render if item.price <= 1
+ # end
+ #
+ # A view could use this state in place of an odd helper.
+ #
+ # - @items.each do |item|
+ # = render({:state => :show_cheap_item}, item)
+ #
+ # This calls the state method which in turn will render its view - if the item isn't too expensive.
+ def render(*args)
+ render_view_for(self.action_name, *args)
+ end
+
+ private
+ # Renders the view belonging to the given state. Will raise ActionView::MissingTemplate
+ # if it can't find a view.
+ def render_view_for(state, *args)
+ opts = args.first.is_a?(::Hash) ? args.shift : {}
+
+ return "" if opts[:nothing]
+
+ if opts[:state]
+ opts[:text] = render_state(opts.delete(:state), *args)
+ elsif (opts.keys & [:text, :inline, :file]).blank?
+ process_opts_for(opts, state)
+ end
+
+ render_to_string(opts).html_safe # ActionView::Template::Text doesn't do that for us.
+ end
+
+
+ module ClassMethods
+ # Main entry point for #render_cell.
+ def render_cell_for(name, state, *args)
+ cell = create_cell_for(name, *args)
+ yield cell if block_given?
+
+ render_cell_state(cell, state, *args)
+ end
+
+ private
+ def render_cell_state(cell, state, *args)
+ cell.render_state(state, *args)
+ end
+ end
+ end
+end
View
@@ -85,5 +85,3 @@ def self.rails3_1?
require 'cells/rails'
require 'cells/rails_compat' # fixes a bug in Rails <3.0.4. # TODO: remove me as soon as we support 3.1, only.
require 'cell/deprecations'
-
-#Cell::Base = Cell::Rails

0 comments on commit 1d8a541

Please sign in to comment.