Module: Puffer::Controller::Dsl::ClassMethods

Defined in:
lib/puffer/controller/dsl.rb

Instance Method Summary collapse

Instance Method Details

#collection(&block) ⇒ Object



35
36
37
38
# File 'lib/puffer/controller/dsl.rb', line 35

def collection &block
  _collections.controller = self
  block.bind(_collections).call if block_given?
end

#define_fieldset(*actions) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/puffer/controller/dsl.rb', line 40

def define_fieldset *actions
  options = actions.extract_options!
  fallbacks = Array.wrap(options.delete(:fallbacks)).map(&:to_sym)

  actions.each do |action|
    self._fieldset_fallbacks[action] = [action] + fallbacks

    class_attribute "_#{action}_fields"
    send "_#{action}_fields=", Puffer::FieldSet.new unless send("_#{action}_fields?")
    helper_method "#{action}_fields"

    self.class.instance_eval do
      define_method action do |&block|
        @_super_fields = send("_#{action}_fields")
        @_fields = send("_#{action}_fields=", Puffer::FieldSet.new(action))
        block.call if block
        remove_instance_variable :@_fields
        remove_instance_variable :@_super_fields
      end

      define_method "#{action}_fields" do
        actions = self._fieldset_fallbacks[action].dup
        last = actions.pop
        actions.map do |action|
          send("_#{action}_fields").presence
        end.compact.first || send("_#{last}_fields")
      end
    end

    define_method "#{action}_fields" do
      self.class.send "#{action}_fields"
    end
  end
end

#field(name, options = {}, &block) ⇒ Object



79
80
81
# File 'lib/puffer/controller/dsl.rb', line 79

def field name, options = {}, &block
  field = @_fields.field(name, model, options, &block) if @_fields
end

#fields(set) ⇒ Object



75
76
77
# File 'lib/puffer/controller/dsl.rb', line 75

def fields set
  send "#{set}_fields"
end

#inherited(klass) ⇒ Object



23
24
25
26
27
28
# File 'lib/puffer/controller/dsl.rb', line 23

def inherited klass
  klass._members = _members.dup
  klass._collections = _collections.dup
  klass._fieldset_fallbacks = Marshal.load(Marshal.dump(_fieldset_fallbacks))
  super
end

#member(&block) ⇒ Object



30
31
32
33
# File 'lib/puffer/controller/dsl.rb', line 30

def member &block
  _members.controller = self
  block.bind(_members).call if block_given?
end

#super_fieldsObject



83
84
85
# File 'lib/puffer/controller/dsl.rb', line 83

def super_fields
  @_super_fields.copy_to @_fields, model if @_super_fields && @_fields
end