Module: Revealer::ClassMethods

Defined in:
lib/revealer.rb

Instance Method Summary collapse

Instance Method Details

#decorate(reveal) ⇒ Object



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

def decorate(reveal)
  reveals = Array(reveal).map do |reveal|
    "#{reveal.class.name}Decorator".constantize.new(reveal)
  end
  
  reveals.many? ? reveals : reveals.first
rescue NameError
  reveal
end

#reveal(name, &block) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/revealer.rb', line 22

def reveal(name, &block)
  _name = "_#{name}"
  
  default_reveal = proc do |name|
    collection = "_#{name.to_s.pluralize}"
    
    if respond_to?(collection) && collection != name.to_s && (collection = send(collection)).respond_to?(:scoped)
      proxy = collection
    else
      proxy = name.to_s.classify.constantize
    end
    
    if id = params[:"#{name}_id"] || params[:id]
      proxy.find(id)
    else
      proxy.new
    end.tap do |reveal|
      reveal.attributes = params[name] unless request.get?
    end
  end
  
  define_method(name) do
    @_reveals ||= {}
    @_reveals[name] ||= self.class.decorate(send(_name))
  end
  
  define_method(_name) do
    @_reveals ||= {}
    @_reveals[_name] ||= if block_given?
      instance_eval(&block)
    else
      instance_exec(name, &default_reveal)
    end
  end
  
  helper_method(name)
  helper_method(_name)
  hide_action(name)
  hide_action(_name)
end