lib/lockbox/model.rb in lockbox-0.3.3 vs lib/lockbox/model.rb in lockbox-0.3.4
- old
+ new
@@ -123,11 +123,17 @@
attribute name, attribute_type
serialize name, JSON if options[:type] == :json
serialize name, Hash if options[:type] == :hash
elsif !attributes_to_define_after_schema_loads.key?(name.to_s)
- attribute name, :string
+ # when migrating it's best to specify the type directly
+ # however, we can try to use the original type if its already defined
+ if attributes_to_define_after_schema_loads.key?(original_name.to_s)
+ attribute name, attributes_to_define_after_schema_loads[original_name.to_s].first
+ else
+ attribute name, :string
+ end
end
define_method("#{name}_was") do
send(name) # writes attribute when not already set
super()
@@ -328,12 +334,26 @@
message
end
if options[:migrating]
- before_validation do
- send("#{name}=", send(original_name)) if send("#{original_name}_changed?")
+ # TODO reuse module
+ m = Module.new do
+ define_method "#{original_name}=" do |value|
+ result = super(value)
+ send("#{name}=", send(original_name))
+ result
+ end
+
+ unless activerecord
+ define_method "reset_#{original_name}!" do
+ result = super()
+ send("#{name}=", send(original_name))
+ result
+ end
+ end
end
+ prepend m
end
end
end
end