Class: Ruhl::Rails::Presenter

Inherits:
Object
  • Object
show all
Includes:
ActiveRecord, Helper
Defined in:
lib/ruhl/rails/ruhl_presenter.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Helper

#form_authenticity, #i

Methods included from ActiveRecord

#define_paths, #error_messages, #error_messages?

Constructor Details

#initialize(context, obj = nil) ⇒ Presenter

Returns a new instance of Presenter.



12
13
14
15
16
17
18
19
20
# File 'lib/ruhl/rails/ruhl_presenter.rb', line 12

def initialize(context, obj = nil)
  @context = context

  # May only want to use the form helper
  if obj
    @presentee = obj
    define_paths(obj.class.name.underscore.downcase)
  end
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args) ⇒ Object



22
23
24
25
26
27
28
29
30
# File 'lib/ruhl/rails/ruhl_presenter.rb', line 22

def method_missing(name, *args)
  # Pass presenter method call to model so you don't have to
  # redefine every model method in the presenter class.
  presentee.__send__(name, *args)
rescue NoMethodError 
  # Instead of saying context.link_to('Some site', some_path)
  # can just use link_to
  context.__send__(name, *args)
end

Instance Attribute Details

#contextObject (readonly)

Returns the value of attribute context.



10
11
12
# File 'lib/ruhl/rails/ruhl_presenter.rb', line 10

def context
  @context
end

#presenteeObject (readonly)

Returns the value of attribute presentee.



10
11
12
# File 'lib/ruhl/rails/ruhl_presenter.rb', line 10

def presentee
  @presentee
end

Instance Method Details

#respond_to?(name) ⇒ Boolean

Extend scope of respond_to? to model.

Returns:

  • (Boolean)


33
34
35
36
37
38
39
# File 'lib/ruhl/rails/ruhl_presenter.rb', line 33

def respond_to?(name)  
  if super
    true
  else
    presentee.respond_to?(name)
  end
end