Class: SampleModels::Creation

Inherits:
Object
  • Object
show all
Defined in:
lib/sample_models/creation.rb

Defined Under Namespace

Classes: SpecifiedAttributes, VirtualColumn

Instance Method Summary collapse

Constructor Details

#initialize(sampler, *args) ⇒ Creation

Returns a new instance of Creation.



5
6
7
8
# File 'lib/sample_models/creation.rb', line 5

def initialize(sampler, *args)
  @sampler = sampler
  @specified_attrs = SpecifiedAttributes.new(sampler, *args).result
end

Instance Method Details

#belongs_to_assoc(column_name) ⇒ Object



10
11
12
13
14
15
# File 'lib/sample_models/creation.rb', line 10

def belongs_to_assoc(column_name)
  model.belongs_to_associations.detect { |assoc|
    assoc.foreign_key == column_name ||
      assoc.foreign_type == column_name
  }
end

#deferred_belongs_to_assocsObject



17
18
19
20
21
22
23
24
25
26
27
# File 'lib/sample_models/creation.rb', line 17

def deferred_belongs_to_assocs
  @deferred_belongs_to_assocs ||= begin
    model.belongs_to_associations.select { |a|
      @instance.send(a.foreign_key).nil? &&
        !@specified_attrs.member?(a.foreign_key) &&
        !@specified_attrs.member?(a.name) && 
        !@sampler.defaults.member?(a.foreign_key) &&
        !@sampler.defaults.member?(a.name)
    }
  end
end

#modelObject



29
30
31
# File 'lib/sample_models/creation.rb', line 29

def model
  @sampler.model
end

#runObject



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/sample_models/creation.rb', line 33

def run
  attrs = @specified_attrs.clone
  @sampler.defaults.each do |attr, val|
    unless attrs.member?(attr) ||
           ((assoc = model.belongs_to_association(attr)) &&
           (attrs.member?(assoc.name) || attrs.member?(assoc.foreign_key)))
      attrs[attr] = val
    end
  end
  columns_to_fill = model.columns.clone.reject { |c|
    c.name == 'id' || timestamp?(c.name)
  }
  model.validated_attr_accessors.each do |attr|
    columns_to_fill << VirtualColumn.new(attr)
  end
  columns_to_fill.each do |column|
    unless attrs.member?(column.name) || 
           specified_association_value?(column.name) ||
           ((assoc = belongs_to_assoc(column.name)) && attrs.member?(assoc.name))
      sequence = @sampler.first_pass_attribute_sequence(column)
      attrs[column.name] = sequence.value
    end
  end
  @instance = model.new
  attrs.each do |k, v|
    @instance.send("#{k}=", v)
  end
  save!
  update_with_deferred_associations!
  @instance
end

#save!Object



65
66
67
68
69
70
71
72
73
74
# File 'lib/sample_models/creation.rb', line 65

def save!
  if @sampler.before_save
    if @sampler.before_save.arity == 1
      @sampler.before_save.call(@instance)
    else
      @sampler.before_save.call(@instance, @specified_attrs)
    end
  end
  @instance.save!
end

#specified_association_value?(column_name) ⇒ Boolean

Returns:

  • (Boolean)


76
77
78
79
80
81
82
# File 'lib/sample_models/creation.rb', line 76

def specified_association_value?(column_name)
  @specified_attrs.any? { |attr, val|
    if assoc = model.belongs_to_association(attr)
      assoc.foreign_key == column_name
    end
  }
end

#timestamp?(column_name) ⇒ Boolean

Returns:

  • (Boolean)


84
85
86
# File 'lib/sample_models/creation.rb', line 84

def timestamp?(column_name)
  %w(created_at updated_at created_on updated_on).include?(column_name)
end

#update_with_deferred_associations!Object



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/sample_models/creation.rb', line 88

def update_with_deferred_associations!
  unless deferred_belongs_to_assocs.empty?
    deferred_belongs_to_assocs.each do |a|
      if a.polymorphic?
        klass = @sampler.polymorphic_default_classes[a.name]
        klass ||= SampleModels.samplers.values.map(&:model).detect { |m|
          m != @sampler.model
        }
        @instance.send("#{a.name}=", klass.sample)
      else
        column = model.columns.detect { |c| c.name == a.foreign_key }
        @instance.send(
          "#{a.foreign_key}=", 
          @sampler.second_pass_attribute_sequence(column).next
        )
      end
    end
    save!
  end
end