Created
March 2, 2012 17:39
-
-
Save hopsoft/1959900 to your computer and use it in GitHub Desktop.
Model extension to simplify access to seed data
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
# has_seed_data.rb | |
module HasSeedData | |
def self.included(model) | |
model.extend(ClassMethods) | |
model.send(:instance_variable_set, :@seeds, MyApp::Application.data_seeds[model.table_name.to_sym].seeds) | |
end | |
module ClassMethods | |
def seeds; @seeds; end | |
end | |
end | |
# usage ... | |
class FlagType < ActiveRecord::Base | |
include HasSeedData | |
end | |
# allows slightly more clear and terse access to the seed data | |
FlagType.seeds[:example][:id] | |
# vs the more vebose access methods | |
MyApp::Application.data_seeds[:flag_types][:example][:id] | |
Rails.data_seeds[:flag_types][:example][:id] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment