Module: DecentExposure::DefaultExposure

Defined in:
lib/decent_exposure/default_exposure.rb

Class Method Summary collapse

Class Method Details

.included(klass) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/decent_exposure/default_exposure.rb', line 4

def self.included(klass)
  klass.extend(DecentExposure)
  klass.send(:include, DecentExposure::ControllerInstanceMethods)
  if klass.respond_to?(:class_attribute)
    klass.class_attribute(:_default_exposure)
  else
    klass.superclass_delegating_accessor(:_default_exposure)
  end
  if klass.respond_to?(:default_exposure)
    klass.default_exposure do |name|
      collection = name.to_s.pluralize
      if respond_to?(collection) && collection != name.to_s && send(collection).respond_to?(:scoped)
        proxy = send(collection)
      else
        proxy = name.to_s.classify.constantize
      end

      if id = params["#{name}_id"] || params[:id]
        proxy.find(id).tap do |r|
          r.attributes = params[name] unless request.get?
        end
      else
        proxy.new(params[name])
      end
    end
  end
end