Class: LazyFixtures::AssociationManager

Inherits:
Object
  • Object
show all
Defined in:
lib/lazy_fixtures/association_manager.rb

Instance Method Summary collapse

Constructor Details

#initialize(item) ⇒ AssociationManager

Returns a new instance of AssociationManager.



3
4
5
6
# File 'lib/lazy_fixtures/association_manager.rb', line 3

def initialize(item)
  @item = item
  @klass = @item.class.name.constantize
end

Instance Method Details

#columns_infoObject



8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/lazy_fixtures/association_manager.rb', line 8

def columns_info
  return_hash = {}
  @klass.reflections.keys.map do |x|
    reflection = @klass.reflections[x]
    return_hash[x.to_s]= {
        method: x,
        macro: reflection.macro,
        klass: reflection.class_name
    }
  end
  return_hash
end

#create_belongs_to_association(klass, class_name, method) ⇒ Object



33
34
35
36
37
# File 'lib/lazy_fixtures/association_manager.rb', line 33

def create_belongs_to_association(klass, class_name, method)
  method = method == klass.downcase ? klass.downcase : method
  class_name = klass == class_name ? klass : class_name
  "association :#{method}, factory: :#{class_name.downcase}"
end

#create_has_many_associations(klass) ⇒ Object



39
40
41
42
43
# File 'lib/lazy_fixtures/association_manager.rb', line 39

def create_has_many_associations(klass)
  %Q(after(:create) do |x|
  create_list(:#{klass.downcase}, 1, #{@item.class.name.downcase}: x)
end)
end

#determine_association(association_info, class_name, method) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
# File 'lib/lazy_fixtures/association_manager.rb', line 21

def determine_association(association_info, class_name, method)
  relation = association_info[:macro]
  text = if relation == :belongs_to
           create_belongs_to_association(association_info[:klass], class_name, method)
         elsif relation == :has_many || relation == :has_and_belongs_to_many
           create_has_many_associations(association_info[:klass])
         end
  <<-EOF
#{text}
  EOF
end