Class: Viewtastic::Base
- Inherits:
-
Object
- Object
- Viewtastic::Base
- Includes:
- ActionView::Helpers::TagHelper, ActionView::Helpers::TextHelper, ActionView::Helpers::UrlHelper
- Defined in:
- lib/viewtastic/base.rb
Overview
Base class for presenters. See README.md for usage.
Class Method Summary collapse
-
.activated? ⇒ Boolean
:nodoc:.
-
.controller ⇒ Object
:nodoc:.
-
.controller=(value) ⇒ Object
:nodoc:.
-
.presents(*types) ⇒ Object
Indicates which models are to be presented.
Instance Method Summary collapse
-
#controller ⇒ Object
The current controller performing the request is accessible with this.
-
#initialize(*values) ⇒ Base
constructor
Accepts arguments in two forms.
- #method_missing(method_name, *args, &block) ⇒ Object
Constructor Details
#initialize(*values) ⇒ Base
Accepts arguments in two forms. If you had a CommentPresenter that presented a Comment model and a Post model, you would write the follwoing:
-
CommentPresenter.new(:comment => Comment.new, :post => @post)
-
CommentPresenter.new(Comment.new, @post) - it will introspect on the model’s class; the order is not important.
You can even mix the two:
CommentPresenter.new(Comment.new, :post => @post)
73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/viewtastic/base.rb', line 73 def initialize(*values) keys_and_values = values. keys_and_values.each do |name, instance| send("#{name}=", instance) end values.each do |value| send("#{value.class.name.underscore}=", value) end end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method_name, *args, &block) ⇒ Object
85 86 87 88 89 90 91 92 93 94 |
# File 'lib/viewtastic/base.rb', line 85 def method_missing(method_name, *args, &block) if method_name.to_s =~ /_(path|url)$/ # Delegate all named routes to the controller controller.send(method_name, *args) elsif presented_attribute?(method_name) (method_name, *args, &block) else super end end |
Class Method Details
.activated? ⇒ Boolean
:nodoc:
60 61 62 |
# File 'lib/viewtastic/base.rb', line 60 def activated? #:nodoc: !controller.nil? end |
.controller ⇒ Object
:nodoc:
56 57 58 |
# File 'lib/viewtastic/base.rb', line 56 def controller #:nodoc: Thread.current[:viewtastic_controller] end |
.controller=(value) ⇒ Object
:nodoc:
52 53 54 |
# File 'lib/viewtastic/base.rb', line 52 def controller=(value) #:nodoc: Thread.current[:viewtastic_controller] = value end |
.presents(*types) ⇒ Object
Indicates which models are to be presented.
class CommentPresenter < Viewtastic::Base
presents :comment, :post
end
If you want to delegate messages to models without prefixing them with the model name, specify them in an Array:
class PresenterWithTwoAddresses < ActivePresenter::Base
presents :post, :comment => [:body, :created_at]
end
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/viewtastic/base.rb', line 31 def presents(*types) types_and_attributes = types. types_and_attributes.each do |name, delegates| attr_accessor name presented << name delegates.each do |msg| delegate msg, :to => name end end attr_accessor *types self.presented += types presented.each do |name| define_method("#{name}_dom_id") do |*args| controller.send(:dom_id, send(name), *args) end end end |
Instance Method Details
#controller ⇒ Object
The current controller performing the request is accessible with this.
98 99 100 |
# File 'lib/viewtastic/base.rb', line 98 def controller self.class.controller end |