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
- #associated_class_name(info, name) ⇒ Object
- #gather_associations ⇒ Object
- #generate_association_data(associated_model) ⇒ Object
- #generate_record_data(name, info, column, fixtures = true) ⇒ Object
- #generate_regular_data(column) ⇒ Object
Instance Method Details
#associated_class_name(info, name) ⇒ Object
58 59 60 61 62 63 |
# File 'lib/generators/common.rb', line 58 def associated_class_name(info, name) info[:associations].each do |assoc| return assoc[:model] if assoc[:foreign_key] == name end false end |
#gather_associations ⇒ Object
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 33 34 |
# 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. if .empty? @models[model][:associations].push({ :model => assoc_name.constantize, :foreign_key => "#{assoc_name.underscore}_id" }) elsif .has_key?(:class_name) and .has_key?(:foreign_key) @models[model][:associations].push({ :model => [:class_name].constantize, # TODO: handle class_name :foreign_key => [:foreign_key] }) else next 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
65 66 67 68 |
# File 'lib/generators/common.rb', line 65 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
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/generators/common.rb', line 36 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 =~ /_at$/ and column.type == :datetime) or column_name == "id" return end associated_model = associated_class_name(info, column_name) if associated_model if fixtures val = generate_association_data(associated_model) column_name.gsub!(/_id$/, "") else val = Fixtures.identify(generate_association_data(associated_model)) end else val = generate_regular_data(column) end {column_name => val} end |
#generate_regular_data(column) ⇒ Object
70 71 72 73 74 75 76 77 78 79 |
# File 'lib/generators/common.rb', line 70 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 |