Module: CaseForm::Associations

Included in:
FormBuilder
Defined in:
lib/case_form/associations.rb

Instance Method Summary collapse

Instance Method Details

#association(method, *args, &block) ⇒ Object Also known as: belongs_to, has_one, has_many, habtm

Raises:

  • (ArgumentError)


4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/case_form/associations.rb', line 4

def association(method, *args, &block)
  association = object.class.reflect_on_association(method)
  raise(ArgumentError, "Association :#{method} not found in #{object.class} model") unless association
  
  options = args.extract_options!
  
  if options.has_key?(:as) && association.macro != :has_one
    type, input_types = options.delete(:as), [:checkbox, :radio, :select]
    raise(ArgumentError, "Unknown input type: #{type}. Available types: #{input_types.join(', ')}") unless input_types.include?(type.to_sym)
    send(type, method, options)
  elsif block_given? || object.respond_to?("#{method}_attributes=")
    Element::NestedModel.new(self, method, options, &block).generate
  else
    specified_association(method, options)
  end
end

#case_fields_for(record_or_name_or_array, *args, &block) ⇒ Object



33
34
35
36
37
# File 'lib/case_form/associations.rb', line 33

def case_fields_for(record_or_name_or_array, *args, &block)
  options = args.extract_options!
  options[:builder] = CaseForm::FormBuilder
  fields_for(record_or_name_or_array, *(args << options), &block)
end

#destroy_object(options = {}) ⇒ Object



29
30
31
# File 'lib/case_form/associations.rb', line 29

def destroy_object(options={})
  Element::DestructorHandle.new(self, options).generate
end

#new_object(method, options = {}) ⇒ Object



25
26
27
# File 'lib/case_form/associations.rb', line 25

def new_object(method, options={})
  Element::GeneratorHandle.new(self, method, options).generate
end