Class: KSeeder::Content
- Inherits:
-
Object
- Object
- KSeeder::Content
- Defined in:
- lib/k_seeder/content.rb
Instance Method Summary collapse
- #fill ⇒ Object
- #fill_from_enum ⇒ Object
- #fill_from_fk ⇒ Object
- #fill_from_name ⇒ Object
- #fill_from_type ⇒ Object
- #fill_from_validations ⇒ Object
-
#initialize(model, field) ⇒ Content
constructor
A new instance of Content.
Constructor Details
#initialize(model, field) ⇒ Content
Returns a new instance of Content.
3 4 5 6 |
# File 'lib/k_seeder/content.rb', line 3 def initialize(model, field) @model = model @field = field end |
Instance Method Details
#fill ⇒ Object
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
# File 'lib/k_seeder/content.rb', line 8 def fill return nil if ['id', 'created_at', 'updated_at'].include?(@field.name) return fill_from_validations if has_validations? # first checking if it's a fk return fill_from_fk if is_fk? # TODO # after checking if it's an enum return fill_from_enum if is_enum? # trying to fill field based on its name content = fill_from_name return content unless content.nil? # finally filling content based on its type fill_from_type end |
#fill_from_enum ⇒ Object
35 36 37 38 |
# File 'lib/k_seeder/content.rb', line 35 def fill_from_enum return nil unless is_enum? @model.defined_enums[@field.name].values.sample end |
#fill_from_fk ⇒ Object
40 41 42 43 44 45 46 47 48 49 |
# File 'lib/k_seeder/content.rb', line 40 def fill_from_fk return nil unless is_fk? fk = @model.reflect_on_all_associations.find { |c| c.foreign_key == @field.name } fk_class = fk.name.to_s.classify.constantize fk_class.pluck(:id).sample rescue NameError # unexistant fk class nil end |
#fill_from_name ⇒ Object
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/k_seeder/content.rb', line 51 def fill_from_name case @field.name when 'name' # first checking for class name case @model.to_s when 'User' Faker::Name.name when 'City' Faker::Address.city when 'Company' Faker::Company.name when 'District' when 'State' Faker::Address.state else # defaulting to regular name Faker::Name.name end when 'email' Faker::Internet.email when 'first_name' Faker::Name.first_name when 'last_name' Faker::Name.last_name when 'phone_number' Faker::PhoneNumber.cell_phone end end |
#fill_from_type ⇒ Object
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 |
# File 'lib/k_seeder/content.rb', line 79 def fill_from_type case @field.type when :string Faker::Lorem.characters(@field.limit || 10) when :integer Faker::Number.between(1, 100) when :float Faker::Number.decimal(2) when :boolean Faker::Boolean.boolean when :date when :datetime Faker::Date.backward(365) when :text Faker::Lorem.sentence when :jsonb end end |
#fill_from_validations ⇒ Object
27 28 29 30 31 32 33 |
# File 'lib/k_seeder/content.rb', line 27 def fill_from_validations return nil unless has_validations? validation = @model.validators_on(@field.name).find { |v| !v.[:in].nil? } # returning a sample from the validation options validation.[:in].sample end |