Class: AdminAssistant::MultiFormView::Builder::SubFormBuilder

Inherits:
Object
  • Object
show all
Defined in:
lib/admin_assistant/form_view.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(object_name, object, template, options, proc, position) ⇒ SubFormBuilder

Returns a new instance of SubFormBuilder.



125
126
127
128
129
130
# File 'lib/admin_assistant/form_view.rb', line 125

def initialize(object_name, object, template, options, proc, position)
  @object_name, @object, @template, @options, @proc =
      object_name, object, template, options, proc
  @default_options = @options ? @options.slice(:index) : {}
  @prefix = ('a'..'z').to_a[position]
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(meth, *args, &block) ⇒ Object



132
133
134
135
136
137
138
139
140
141
142
143
144
145
# File 'lib/admin_assistant/form_view.rb', line 132

def method_missing(meth, *args, &block)
  if my_field_helpers.include?(meth.to_s)
    method = args.shift
    options = args.shift || {}
    @template.send(
      meth,
      "#{@object_name}[#{@prefix}]",
      method,
      objectify_options(options)
    )
  else
    super
  end
end

Instance Attribute Details

#objectObject (readonly)

Returns the value of attribute object.



123
124
125
# File 'lib/admin_assistant/form_view.rb', line 123

def object
  @object
end

#prefixObject (readonly)

Returns the value of attribute prefix.



123
124
125
# File 'lib/admin_assistant/form_view.rb', line 123

def prefix
  @prefix
end

Instance Method Details

#my_field_helpersObject



147
148
149
150
# File 'lib/admin_assistant/form_view.rb', line 147

def my_field_helpers
  ::ActionView::Helpers::FormBuilder.field_helpers -
      %w(label check_box radio_button fields_for) + ['datetime_select']
end

#objectify_options(options) ⇒ Object



152
153
154
# File 'lib/admin_assistant/form_view.rb', line 152

def objectify_options(options)
  @default_options.merge(options.merge(:object => @object))
end

#select(method, choices, options = {}, html_options = {}) ⇒ Object



156
157
158
159
160
161
# File 'lib/admin_assistant/form_view.rb', line 156

def select(method, choices, options = {}, html_options = {})
  @template.select(
    "#{@object_name}[#{@prefix}]", method, choices, 
    objectify_options(options), @default_options.merge(html_options)
  )
end