Sha256: a5fa690dfb131a08248438e8397edd9d5ba19bd5b9d76ccbc5865b73aac1b9fe
Contents?: true
Size: 1.97 KB
Versions: 3
Compression:
Stored size: 1.97 KB
Contents
module Factory # Build actions for the model def self.build(model, &block) name = model.to_s.underscore define_method("#{name}_attributes", block) define_method("valid_#{name}_attributes") {|*args| valid_attributes_for(model, *args)} define_method("new_#{name}") {|*args| new_record(model, *args)} define_method("create_#{name}") {|*args| create_record(model, *args)} end # Get valid attributes for the model def valid_attributes_for(model, attributes = {}) name = model.to_s.underscore send("#{name}_attributes", attributes) attributes.stringify_keys! attributes end # Build an unsaved record def new_record(model, *args) attributes = valid_attributes_for(model, *args) record = model.new(attributes) attributes.each {|attr, value| record.send("#{attr}=", value) if model.accessible_attributes && !model.accessible_attributes.include?(attr) || model.protected_attributes && model.protected_attributes.include?(attr)} record end # Build and save/reload a record def create_record(model, *args) record = new_record(model, *args) record.save! record.reload record end build Article do |attributes| attributes[:author] = create_author unless attributes.include?(:author) attributes.reverse_merge!( :content => 'Thanks for visiting my blog!' ) end build Author do |attributes| attributes.reverse_merge!( :name => 'John Smith' ) end build Comment do |attributes| attributes[:commentable] = create_article unless attributes.include?(:commentable) attributes[:commenter] = create_user unless attributes.include?(:commenter) end build Page do |attributes| attributes[:author] = create_author unless attributes.include?(:author) attributes.reverse_merge!( :content => 'Some information about me...' ) end build User do |attributes| attributes.reverse_merge!( :name => 'Mr. Bean' ) end end
Version data entries
3 entries across 3 versions & 2 rubygems
Version | Path |
---|---|
pluginaweek-polymorphic_identity-0.1.0 | test/factory.rb |
polymorphic_identity-0.1.1 | test/factory.rb |
polymorphic_identity-0.1.0 | test/factory.rb |