lib/simple_model/attributes.rb in simple_model-1.2.2 vs lib/simple_model/attributes.rb in simple_model-1.2.3
- old
+ new
@@ -58,11 +58,11 @@
def allow_set_default?(d,k,v)
(v[:default] && v[:initialize] && (d[k].blank? && (self.class.alias_attributes[k].blank? || d.key?(self.class.alias_attributes[k]) && d[self.class.alias_attributes[k]].blank?)))
end
- module ClassMethods
+ module ClassMethods
# Creates a new instance where the attributes store is set to object
# provided, which allows one to pass a session store hash or any other
# hash-like object to be used for persistance. Typically used for modeling
# session stores for authorization or shopping carts
# EX:
@@ -78,15 +78,19 @@
new = self.new()
new.attributes = session_hash
new.set(new.send(:attributes_with_for_init,session_hash))
new
end
-
+
def alias_attributes
@alias_attributes ||= {}.with_indifferent_access
end
+ def alias_attributes=alias_attributes
+ @alias_attributes = alias_attributes
+ end
+
def defined_attributes
@defined_attributes ||= {}.with_indifferent_access
end
def defined_attributes=defined_attributes
@@ -114,11 +118,11 @@
@default_attribute_settings = default_attribute_settings
end
def add_defined_attribute(attr,options)
self.defined_attributes[attr] = options
- define_attribute_methods self.defined_attributes.keys
+ define_attribute_methods [attr]
end
# builds the setter and getter methods
def create_attribute_methods(attributes,options)
unless attributes.blank?
@@ -146,11 +150,14 @@
val = !val.blank? if val.respond_to?(:blank?)
end
val
end
end
-
+
+ # Creates setter methods for the provided attributes
+ # On set, it will mark the attribute as changed if the attributes has been
+ # initialized.
def define_setter_with_options(attr,options)
add_defined_attribute(attr,options)
options = default_attribute_settings.merge(options) if (options[:on_set].blank? || options[:after_set].blank?)
define_method("#{attr.to_s}=") do |val|
val = fetch_default_value(options[:default]) if (!options[:allow_blank] && options.key?(:default) && val.blank?)
@@ -158,11 +165,11 @@
val = options[:on_set].call(self,val)
rescue NoMethodError => e
raise ArgumentError, "#{val} could not be set for #{attr}: #{e.message}"
end
will_change = "#{attr}_will_change!".to_sym
- self.send(will_change) if (self.respond_to?(will_change) && val != self.attributes[attr])
+ self.send(will_change) if (initialized?(attr) && val != self.attributes[attr])
self.attributes[attr] = val
options[:after_set].call(self,val) if options[:after_set]
end
end
@@ -195,13 +202,43 @@
end
define_method("#{new_alias.to_s}=") do |*args, &block|
self.send("#{attribute.to_s}=",*args, &block)
end
end
+
+ # Must inherit super's defined_attributes and alias_attributes
+ # Rails 3.0 does some weird stuff with ActiveModel::Dirty so we need a
+ # hack to keep things working when a class in inherits from a super that
+ # has ActiveModel::Dirty included
+ def inherited(base)
+ # Rails 3.0 Hack
+ if (ActiveModel::VERSION::MAJOR == 3 && ActiveModel::VERSION::MINOR == 0)
+ base.send(:include, ActiveModel::Dirty)
+ base.attribute_method_suffix '_changed?', '_change', '_will_change!', '_was'
+ base.attribute_method_affix :prefix => 'reset_', :suffix => '!'
+ end
+
+ base.defined_attributes = self.defined_attributes.merge(base.defined_attributes)
+ base.alias_attributes = self.alias_attributes.merge(base.alias_attributes )
+ end
end
+ # Rails 3.0 does some weird stuff with ActiveModel::Dirty so we need a
+ # hack to keep things working when a class includes a module that has
+ # ActiveModel::Dirty included
def self.included(base)
- base.extend(Attributes::ClassMethods)
- base.send(:include, ActiveModel::Dirty) if base.is_a?(Class) # Add Dirty to the class
+ base.extend(Attributes::ClassMethods)
+ base.send(:include, ActiveModel::Dirty)
+ base.send(:include, ActiveModel::Validations)
+ base.send(:include, ActiveModel::Conversion)
+ base.extend ActiveModel::Naming
+ base.extend ActiveModel::Callbacks
+ base.send(:include, ActiveModel::Validations::Callbacks)
+
+ # Rails 3.0 Hack
+ if (ActiveModel::VERSION::MAJOR == 3 && ActiveModel::VERSION::MINOR == 0)
+ base.attribute_method_suffix '_changed?', '_change', '_will_change!', '_was'
+ base.attribute_method_affix :prefix => 'reset_', :suffix => '!'
+ end
end
end
end
\ No newline at end of file