Module: Perspectives::ControllerAdditions::ClassMethods

Defined in:
lib/perspectives/controller_additions.rb

Instance Method Summary collapse

Instance Method Details

#perspectives_actions(options = {}) ⇒ Object



111
112
113
114
115
116
117
118
# File 'lib/perspectives/controller_additions.rb', line 111

def perspectives_actions(options = {})
  self.perspectives_enabled_actions = options.slice(:only, :except).each_with_object({}) do |(k, v), h|
    h[k] = Array(v).map(&:to_s)
  end

  respond_to :html, :json, options
  self.responder = Perspectives::Responder
end

#resolve_perspective_class_name(name) ⇒ Object



137
138
139
# File 'lib/perspectives/controller_additions.rb', line 137

def resolve_perspective_class_name(name)
  Perspectives.resolve_partial_class_name(controller_name.camelize, name)
end

#wrapped_with(perspective, options = {}) ⇒ Object



120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/perspectives/controller_additions.rb', line 120

def wrapped_with(perspective, options = {})
  perspective_klass = resolve_perspective_class_name(perspective)

  options[:only] = Array(options[:only]).map(&:to_s) if options[:only]
  options[:except] = Array(options[:except]).map(&:to_s) if options[:except]

  options[:if] ||= lambda { |c| c.params[perspective_klass.id_param].present? }
  options[:args] ||= lambda do |controller, perspective|
    {
      perspective_klass.active_record_klass.name.underscore => perspective_klass.active_record_klass.find(controller.params[perspective_klass.id_param]),
      options.fetch(:as, controller_name.underscore.singularize) => perspective
    }
  end

  self.perspectives_wrapping += [[perspective_klass, options]]
end