Skip to content
Browse files

extract action controller and action view mixins

  • Loading branch information...
1 parent 721070c commit 52616363818f44bf7d4c318ace71b8017f3e230f @cainlevy committed Dec 7, 2011
Showing with 72 additions and 69 deletions.
  1. +4 −69 lib/components.rb
  2. +42 −0 lib/components/action_controller.rb
  3. +26 −0 lib/components/action_view.rb
View
73 lib/components.rb
@@ -2,6 +2,8 @@ module Components #:nodoc:
autoload :Base, 'components/base'
autoload :View, 'components/view'
autoload :Caching, 'components/caching'
+ autoload :ActionController, 'components/action_controller'
+ autoload :ActionView, 'components/action_view'
def self.render(name, component_args = [], options = {})
klass, method = name.split(/\/(?=[^\/]*$)/)
@@ -20,74 +22,7 @@ def self.merge_standard_component_options!(args, standard_options, arity)
args.last.reverse_merge!(standard_options) if args.last.is_a?(Hash)
end
end
-
- module ActionController
- protected
-
- # Renders the named component with the given arguments. The component name must indicate both
- # the component class and the class' action.
- #
- # Example:
- #
- # class UsersController < ApplicationController
- # def show
- # render :text => component("users/details", params[:id])
- # end
- # end
- #
- # would render:
- #
- # class UsersComponent < Components::Base
- # def details(user_id)
- # "all the important details about the user, nicely marked up"
- # end
- # end
- def component(name, *args)
- Components.render(name, args,
- :form_authenticity_token => (form_authenticity_token if protect_against_forgery?),
- :standard_component_options => standard_component_options
- )
- end
-
- # Override this method on your controller (probably your ApplicationController)
- # to define common arguments for your components. For example, you may always
- # want to provide the current user, in which case you could return {:user =>
- # current_user} from this method.
- #
- # In order to use this, your component actions should accept an options hash
- # as their last argument.
- #
- # I feel like this is a better solution than simply making request and
- # session details directly available to the components.
- def standard_component_options; end
- end
-
- module ActionView
- # Renders the named component with the given arguments. The component name must indicate both
- # the component class and the class' action.
- #
- # Example:
- #
- # /app/views/users/show.html.erb
- #
- # <%= component "users/details", @user.id %>
- #
- # would render:
- #
- # class UsersComponent < Components::Base
- # def details(user_id)
- # "all the important details about the user, nicely marked up"
- # end
- # end
- def component(name, *args)
- Components.render(name, args,
- :form_authenticity_token => (form_authenticity_token if protect_against_forgery?),
- :standard_component_options => controller.send(:standard_component_options)
- )
- end
- end
end
-ActionController::Base.class_eval do include Components::ActionController end
-ActionView::Base.class_eval do include Components::ActionView end
-
+ActionController::Base.class_eval { include Components::ActionController }
+ActionView::Base.class_eval { include Components::ActionView }
View
42 lib/components/action_controller.rb
@@ -0,0 +1,42 @@
+module Components
+ module ActionController
+ protected
+
+ # Renders the named component with the given arguments. The component name must indicate both
+ # the component class and the class' action.
+ #
+ # Example:
+ #
+ # class UsersController < ApplicationController
+ # def show
+ # render :text => component("users/details", params[:id])
+ # end
+ # end
+ #
+ # would render:
+ #
+ # class UsersComponent < Components::Base
+ # def details(user_id)
+ # "all the important details about the user, nicely marked up"
+ # end
+ # end
+ def component(name, *args)
+ Components.render(name, args,
+ :form_authenticity_token => (form_authenticity_token if protect_against_forgery?),
+ :standard_component_options => standard_component_options
+ )
+ end
+
+ # Override this method on your controller (probably your ApplicationController)
+ # to define common arguments for your components. For example, you may always
+ # want to provide the current user, in which case you could return {:user =>
+ # current_user} from this method.
+ #
+ # In order to use this, your component actions should accept an options hash
+ # as their last argument.
+ #
+ # I feel like this is a better solution than simply making request and
+ # session details directly available to the components.
+ def standard_component_options; end
+ end
+end
View
26 lib/components/action_view.rb
@@ -0,0 +1,26 @@
+module Components
+ module ActionView
+ # Renders the named component with the given arguments. The component name must indicate both
+ # the component class and the class' action.
+ #
+ # Example:
+ #
+ # /app/views/users/show.html.erb
+ #
+ # <%= component "users/details", @user.id %>
+ #
+ # would render:
+ #
+ # class UsersComponent < Components::Base
+ # def details(user_id)
+ # "all the important details about the user, nicely marked up"
+ # end
+ # end
+ def component(name, *args)
+ Components.render(name, args,
+ :form_authenticity_token => (form_authenticity_token if protect_against_forgery?),
+ :standard_component_options => controller.send(:standard_component_options)
+ )
+ end
+ end
+end

0 comments on commit 5261636

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