Module: Dummy::Generators::Common

Included in:
DataGenerator
Defined in:
lib/generators/common.rb

Overview

Methods used by other dummy-related gems are grouped here Other generators will just “include Dummy::Generators::Common”

Instance Method Summary collapse

Instance Method Details

#associated_class_name(info, name) ⇒ Object



56
57
58
59
60
61
# File 'lib/generators/common.rb', line 56

def associated_class_name(info, name)
  info[:associations].each do |assoc|
    return assoc[:model] if assoc[:foreign_key] == name
  end
  false
end

#gather_associationsObject



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/generators/common.rb', line 6

def gather_associations
  @models.each_key do |model|
    model_symbol = model.to_s.underscore.pluralize.to_sym
    associations = model.reflect_on_all_associations(:belongs_to)
    
    associations.each do |assoc|
      assoc_name = assoc.name.to_s.camelcase
      assoc_options = assoc.options

      if !assoc_options.has_key?(:foreign_key)
        @models[model][:associations].push({
          :model  => assoc_name.constantize,
          :foreign_key => "#{assoc_name.underscore}_id"
        })
      else
        @models[model][:associations].push({
          :model => assoc.class_name.constantize,
          :foreign_key => assoc_options[:foreign_key]
        })
      end

      assoc_model = @models[model][:associations].last[:model]
      assoc_reflections = assoc_model.reflect_on_all_associations(:has_one)
      @models[model][:associations].pop if assoc_reflections.map(&:name).include?(model_symbol)
    end
  end
end

#generate_association_data(associated_model) ⇒ Object



63
64
65
66
# File 'lib/generators/common.rb', line 63

def generate_association_data(associated_model)
  random_record_num = rand(@models[associated_model][:record_amount])
  "#{associated_model.to_s.underscore}_#{random_record_num}"
end

#generate_record_data(name, info, column, fixtures = true) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/generators/common.rb', line 34

def generate_record_data(name, info, column, fixtures=true)
  column_name = String.new(column.name) # this shouldn't be needed, ruby bug?
  if((column_name == "id") or (["created_at", "created_on", "updated_at", "updated_on"].include?(column_name) and column.type == :datetime))
    return
  end

  associated_model = associated_class_name(info, column_name)

  if associated_model
    if fixtures
      val = ActiveRecord::Fixtures.identify(generate_association_data(associated_model))
    else
      val = generate_association_data(associated_model)
      column_name.gsub!(/_id$/, "")
    end
  else
   val = generate_regular_data(column)
  end

  {column_name => val}
end

#generate_regular_data(column) ⇒ Object



68
69
70
71
72
73
74
75
76
77
# File 'lib/generators/common.rb', line 68

def generate_regular_data(column)
  val = Dummy.magic_data(column.name, column.type)
  
  if val
    val
  else
    say_status :failed, "data generation for '#{column.name}' with type '#{column.type.to_s.downcase}'", :red
    ""
  end
end