Skip to content

Commit

Permalink
Added model specific custom attributes (#47)
Browse files Browse the repository at this point in the history
* Added model specific custom attributes

* use dig

* use name instead of email
  • Loading branch information
keshavbiswa authored Jul 21, 2024
1 parent 92c393e commit fb91543
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 11 deletions.
14 changes: 14 additions & 0 deletions lib/generators/seedie/templates/seedie_initializer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,19 @@
# config.default_count = 10

config.custom_attributes[:email] = "{{Faker::Internet.unique.email}}"
# Model-Specific Custom Attributes
#
# Use the prepare_custom_attributes_for method to initialize the custom_attributes hash
# for the specified models. This ensures that you can safely set model-specific custom
# attributes without encountering NoMethodError.
#
# Example:
# config.prepare_custom_attributes_for :user, :account
#
# Now you can set custom attributes for these models:
# config.custom_attributes[:user][:email] = "[email protected]"
# config.custom_attributes[:account][:name] = "{{Faker::Business.name}}"
#
# Add more custom attributes here
#
end
18 changes: 18 additions & 0 deletions lib/seedie/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,23 @@ def initialize
@default_count = nil
@custom_attributes = {}
end

# Prepares the custom_attributes hash for the specified models.
#
# This method ensures that the necessary keys exist in the custom_attributes hash.
# This prevents NoMethodError when setting model-specific custom attributes.
#
# Example usage:
# config.prepare_custom_attributes_for :user, :account
#
# Then this will work:
# config.custom_attributes[:user][:name] = "Name"
# config.custom_attributes[:account][:email] = "[email protected]"
#
def prepare_custom_attributes_for(*models)
models.inject(@custom_attributes) do |hash, key|
hash[key] ||= {}
end
end
end
end
11 changes: 10 additions & 1 deletion lib/seedie/field_values/faker_builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ def initialize(name, column, validations)
end

def build_faker_constant
return @seedie_config_custom_attributes[@name.to_sym] if @seedie_config_custom_attributes.key?(@name.to_sym)
custom_attribute = fetch_custom_attribute
return custom_attribute if fetch_custom_attribute

@unique_prefix = "unique." if has_validation?(:uniqueness)

Expand All @@ -39,6 +40,14 @@ def build_faker_constant

private

def fetch_custom_attribute
if @seedie_config_custom_attributes[@name.to_sym].is_a?(Hash)
return @seedie_config_custom_attributes.dig(@name.to_sym, @column.name.to_sym)
end

@seedie_config_custom_attributes[@name.to_sym]
end

def add_faker_class_and_method(type)
case type
when :string, :text, :citext
Expand Down
41 changes: 31 additions & 10 deletions spec/seedie/field_values/faker_builder_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,7 @@
RSpec.describe Seedie::FieldValues::FakerBuilder do
describe "#build_faker_constant" do
context "when there is a custom attribute in the configuration" do
let(:column) { double("column", type: :string) }
let(:validations) { [] }
let(:faker_builder) { described_class.new("name", column, validations) }

before do
Seedie.configure do |config|
config.custom_attributes[:name] = "custom_attribute"
end
end

# Need to clean this up else it will affect other tests
after do
Expand All @@ -22,8 +14,37 @@
end
end

it "returns the custom attribute" do
expect(faker_builder.build_faker_constant).to eq("custom_attribute")
context "when there is no model specific custom attribute" do
let(:column) { double("column", type: :string) }
let(:faker_builder) { described_class.new("name", column, validations) }

before do
Seedie.configure do |config|
config.custom_attributes[:name] = "custom_attribute"
end
end

it "returns the custom attribute" do
expect(faker_builder.build_faker_constant).to eq("custom_attribute")
end
end

context "when there is a model specific custom attribute" do
let(:column) { double("column", name: :name, type: :string) }
let(:faker_builder) { described_class.new("user", column, validations) }

before do
Seedie.configure do |config|
config.prepare_custom_attributes_for :user

config.custom_attributes[:user][:name] = "model_specific_custom_attribute"
config.custom_attributes[:name] = "global_custom_attribute"
end
end

it "returns the model specific custom attribute" do
expect(faker_builder.build_faker_constant).to eq("model_specific_custom_attribute")
end
end
end

Expand Down

0 comments on commit fb91543

Please sign in to comment.