Module: PresenterPattern::API

Defined in:
lib/presenter-pattern.rb

Defined Under Namespace

Modules: ClassMethods

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.[](*formats) ⇒ Object



23
24
25
26
27
28
29
30
31
# File 'lib/presenter-pattern.rb', line 23

def self.[](*formats)
  custom_api = self.dup
  custom_api.class_eval do
    define_singleton_method :included do |host_class|
      do_included(host_class, *formats)
    end
  end
  custom_api
end

.do_included(host_class, *formats) ⇒ Object



43
44
45
46
# File 'lib/presenter-pattern.rb', line 43

def self.do_included(host_class, *formats)
  host_class.respond_to *formats
  host_class.extend PresenterPattern::API::ClassMethods
end

.included(host_class) ⇒ Object



33
34
35
# File 'lib/presenter-pattern.rb', line 33

def self.included(host_class)
  do_included(host_class, :xml, :json)
end

Instance Method Details

#respond_opts(options) ⇒ Object



69
70
71
72
# File 'lib/presenter-pattern.rb', line 69

def respond_opts(options)
  @respond_opts ||= {}
  @respond_opts.merge! options
end

#send_action(method_name, *args) ⇒ Object

enforces simple api response

each action must return the data meant for their response

  • this overwrites ActionController::Metal::ImplicitRender



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/presenter-pattern.rb', line 53

def send_action(method_name, *args)
  #call the action, and store return value

  rval = self.send(method_name, *args)

  unless response_body
    #set the view variable with the return value

    name = self.class.instance_variable_get(:@view_var_name)
    name = name.pluralize if name && method_name.to_s == "index" #pluralize the @view_var_name if it was set at the class and this is GET/index

    name ||= "data"
    self.instance_variable_set :"@#{name}", rval

    #always follow responder pattern passing in the action's return value unless already rendered

    respond_with rval, (@respond_opts || {})
  end
end