Skip to content

Instantly share code, notes, and snippets.

@hopsoft
Created March 2, 2012 17:39
Show Gist options
  • Save hopsoft/1959900 to your computer and use it in GitHub Desktop.
Save hopsoft/1959900 to your computer and use it in GitHub Desktop.
Model extension to simplify access to seed data
# 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