Class: PresenterObject::Base
- Inherits:
-
Object
- Object
- PresenterObject::Base
- Defined in:
- lib/presenter_object/base.rb
Overview
Presenter superclass. Create your own presenter by sub-classing PresenterObject::Base and declaring the name of the model it will present.
e.g:
class DocumentPresenter << PresenterObject::Base
presents :document
.. add instance methods here ..
end
Create a presenter instance by giving it a model instance e.g:
document = DocumentPresenter.new Document.find(params[:id])
document.sender # can call any document model methods...
document.pretty_sender # ...as well as any presenter defined method
document.class # is the wrapped object's class, helps it behave like models do in forms, link helpers etc.
Instance Attribute Summary collapse
-
#class ⇒ Object
readonly
Returns the value of attribute class.
-
#object ⇒ Object
readonly
Returns the value of attribute object.
-
#to_param ⇒ Object
readonly
Returns the value of attribute to_param.
-
#view_context ⇒ Object
readonly
Returns the value of attribute view_context.
Class Method Summary collapse
Instance Method Summary collapse
-
#initialize(object, view_context = nil) ⇒ Base
constructor
A new instance of Base.
- #method_missing(name, *args, &block) ⇒ Object
- #respond_to?(name, include_private = false) ⇒ Boolean
Constructor Details
#initialize(object, view_context = nil) ⇒ Base
Returns a new instance of Base.
37 38 39 40 41 42 |
# File 'lib/presenter_object/base.rb', line 37 def initialize(object, view_context = nil) @object = object @class = object.class # impersonate object's class @to_param = object.id # let link helpers know the object's id @view_context = view_context end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(name, *args, &block) ⇒ Object
44 45 46 47 48 49 50 51 52 |
# File 'lib/presenter_object/base.rb', line 44 def method_missing(name, *args, &block) if @object.respond_to? name @object.send name, *args, &block elsif @view_context.respond_to? name @view_context.send name, *args, &block else super end end |
Instance Attribute Details
#class ⇒ Object (readonly)
Returns the value of attribute class.
35 36 37 |
# File 'lib/presenter_object/base.rb', line 35 def class @class end |
#object ⇒ Object (readonly)
Returns the value of attribute object.
35 36 37 |
# File 'lib/presenter_object/base.rb', line 35 def object @object end |
#to_param ⇒ Object (readonly)
Returns the value of attribute to_param.
35 36 37 |
# File 'lib/presenter_object/base.rb', line 35 def to_param @to_param end |
#view_context ⇒ Object (readonly)
Returns the value of attribute view_context.
35 36 37 |
# File 'lib/presenter_object/base.rb', line 35 def view_context @view_context end |
Class Method Details
.presented_class ⇒ Object
26 27 28 |
# File 'lib/presenter_object/base.rb', line 26 def presented_class @presented_class ||= @class_name.constantize end |
.presenters ⇒ Object
30 31 32 |
# File 'lib/presenter_object/base.rb', line 30 def presenters @@presenters ||= {} end |
.presents(class_name) ⇒ Object
21 22 23 24 |
# File 'lib/presenter_object/base.rb', line 21 def presents(class_name) @class_name = class_name.to_s.classify presenters[@class_name] = self end |
Instance Method Details
#respond_to?(name, include_private = false) ⇒ Boolean
54 55 56 57 58 59 60 |
# File 'lib/presenter_object/base.rb', line 54 def respond_to?(name, include_private = false) if @object.respond_to?(name, include_private) || @view_context.respond_to?(name, include_private) true else super end end |