Sha256: f7e132de5d9efc6e9f547ac1c09752e7c0aac43b748bb4ae3b6a8c578bd9b52b
Contents?: true
Size: 1.9 KB
Versions: 3
Compression:
Stored size: 1.9 KB
Contents
module E9Attributes::Model extend ActiveSupport::Concern module ClassMethods # # By default, it reformats the attributes passed into association # names and defines them. To add a record attribute without defining # an association name, pass the association names literally and specify # the option :skip_name_format. This will cause the method to skip adding # associations for any attribute not ending in "_attributes", which you # must add manually, e.g. # # has_many :users # accepts_nested_attributes_for :users, :allow_destroy => true # def has_record_attributes(*attributes) options = attributes.extract_options! options.symbolize_keys! class_inheritable_accessor :record_attributes attributes.flatten! attributes.map!(&:to_s) unless options[:skip_name_format] attributes.map! do |a| a = a.classify a = a =~ /Attribute$/ ? a : "#{a}Attribute" a.constantize rescue next a.underscore.pluralize end.compact end self.record_attributes = attributes has_many :record_attributes, :as => :record self.record_attributes.select {|r| r =~ /attributes$/ }.each do |association_name| has_many association_name.to_sym, :class_name => association_name.classify, :as => :record accepts_nested_attributes_for association_name.to_sym, :allow_destroy => true, :reject_if => :reject_record_attribute? end end end def build_all_record_attributes self.class.record_attributes.each do |attr| params_method = "#{attr}_build_parameters" build_params = self.class.send(params_method) if self.class.respond_to?(params_method) send(attr).send(:build, build_params || {}) end end protected def reject_record_attribute?(attributes) attributes.keys.member?('value') && attributes['value'].blank? end end
Version data entries
3 entries across 3 versions & 1 rubygems
Version | Path |
---|---|
e9_attributes-0.0.6 | lib/e9_attributes/model.rb |
e9_attributes-0.0.5 | lib/e9_attributes/model.rb |
e9_attributes-0.0.4 | lib/e9_attributes/model.rb |