Class: BrainDamage::HasMany

Inherits:
Relation show all
Defined in:
lib/generators/brain_damage/lib/relation/has_many.rb

Constant Summary

Constants inherited from Relation

Relation::VALID_RELATION_TYPES

Instance Method Summary collapse

Methods inherited from Relation

#add_options_to_line, #class_name, create, is_valid_relation?, #nested_on, #resource_name_according_to_foreign

Constructor Details

#initialize(options = {}) ⇒ HasMany

Returns a new instance of HasMany.



5
6
7
8
# File 'lib/generators/brain_damage/lib/relation/has_many.rb', line 5

def initialize(options = {})
  @options = options
  @name = @options[:field].name
end

Instance Method Details

#model_linesObject



10
11
12
# File 'lib/generators/brain_damage/lib/relation/has_many.rb', line 10

def model_lines
  [relationship_line, nested_attributes_line, validates_associated_line ]
end

#nested_attributes_lineObject



19
20
21
# File 'lib/generators/brain_damage/lib/relation/has_many.rb', line 19

def nested_attributes_line
  "accepts_nested_attributes_for :#{@name}, reject_if: proc { |attributes| attributes.values.uniq.first == '' }".indent unless @options[:skip_nested_form]
end

#relationship_lineObject



14
15
16
17
# File 'lib/generators/brain_damage/lib/relation/has_many.rb', line 14

def relationship_line
  line = "has_many :#{@name}".indent
  add_options_to_line line, @options.slice(:class_name, :join_table, :as, :dependent, :foreign_key, :through)
end

#validates_associated_lineObject



23
24
25
# File 'lib/generators/brain_damage/lib/relation/has_many.rb', line 23

def validates_associated_line
  "validates_associated :#{@name}".indent unless @options[:skip_nested_form]
end

#white_listObject



27
28
29
30
31
32
33
# File 'lib/generators/brain_damage/lib/relation/has_many.rb', line 27

def white_list
  if @options[:white_list]
    return ":#{@name.to_s.pluralize}_attributes => #{@options[:white_list].inspect}"
  else
    return ":#{@name.to_s.singularize}_ids => []"
  end
end