-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added model specific custom attributes (#47)
* Added model specific custom attributes * use dig * use name instead of email
- Loading branch information
1 parent
92c393e
commit fb91543
Showing
4 changed files
with
73 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters