Permalink
Browse files

AR improvements

  * Make sure the EAV attributes make it into the serializable_hash
  * Make sure the nilified attributes are removed from the DB
  • Loading branch information...
1 parent dec95df commit 0fe3b3439fcb8a55f246b27bd3101107488f46a2 @coffeeaddict committed Apr 5, 2011
Showing with 62 additions and 10 deletions.
  1. +14 −6 README.rdoc
  2. +19 −4 lib/has_eav.rb
  3. +29 −0 test/test_has_eav.rb
View
@@ -4,7 +4,7 @@
Enabled EAV behaviour on ActiveRecord (or is at least supposed to).
-This was inspired by https://github.com/visfleet/acts_as_eav_model but is a
+This was inspired by https://github.com/visfleet/acts_as_eav_model but is a
more minimized approach and is less automated.
=== EAV
@@ -54,7 +54,7 @@ And you are good to go.
=== Usage of EAV
-For the general usage of EAV, thread wisely. Be careful and suspicious about
+For the general usage of EAV, thread wisely. Be careful and suspicious about
your needs.
=== Usage of has_eav
@@ -68,15 +68,23 @@ structure. You must define the attributes to be required.
==== Instance eav_attributes
-If you create a method +instance_eav_attributes+ in your model, has_eav will
+If you create a method +instance_eav_attributes+ in your model, has_eav will
recognize these attributes as well.
This might be useful for state machines or STI classes where you want to have
-different attributes based on the current state or type of the instance.
+different attributes based on the current state or type of the instance.
+
+== Changelog
+
+[1.1.1] * Make sure the EAV attributes make it into the serializable_hash
+ * Make sure the nilified attributes are removed from the DB
+[1.1.0] * Casting of dates and times added
+[1.0.1] * STI works
+[1.0.0] * first release
== Contributing to has_eav
-
-* Check out the latest master to make sure the feature hasn't been implemented
+
+* Check out the latest master to make sure the feature hasn't been implemented
or the bug hasn't been fixed yet
* Check out the issue tracker to make sure someone already hasn't requested
it and/or contributed it
View
@@ -108,31 +108,35 @@ def method_missing method_symbol, *args, &block
value = args[0]
if attribute
- if value
+ if !value.nil?
return attribute.send(:write_attribute, "value", value)
- elsif value.nil?
+ else
+ @eav_attributes -= [ attribute ]
return attribute.destroy
end
- else
+ elsif !value.nil?
@eav_attributes << eav_class.new(
:name => attribute_name,
:value => "#{value}"
)
return cast_eav_value(value, attribute_name)
+ else
+ return nil
+
end
elsif method_name =~ /\?$/
return ( attribute and attribute.value == true ) ? true : false
else
+ return nil if attribute and attribute.destroyed?
return attribute ?
cast_eav_value(attribute.value, attribute_name) :
nil
-
end
raise e
@@ -200,6 +204,17 @@ def self_key # :nodoc:
"#{klass.name.underscore}_id".to_sym
end
+ # make sure EAV is included in as_json, to_json and to_xml
+ #
+ def serializable_hash options=nil
+ hash = super
+ eav_attributes_list.each do |attribute|
+ hash[attribute] = self.send(attribute)
+ end
+
+ hash
+ end
+
# cast an eav value to it's desired class
def cast_eav_value value, attribute # :nodoc:
attributes = self.class_eav_attributes.stringify_keys
View
@@ -112,4 +112,33 @@ class TestHasEav < Test::Unit::TestCase
assert_equal t.to_i, p.last_update.to_i, "Time comparisson"
end
+
+ should "not create attributes with nil value" do
+ p = Post.last
+ p.author_name = nil
+
+ assert_equal [], p.eav_attributes, "No attributes where defined"
+
+ p.author_name = "name"
+
+ assert_equal 1, p.eav_attributes.count, "There is 1 eav attribute"
+
+ p.author_name = nil
+ assert_equal nil, p.author_name, "The value is nilified"
+ assert_equal [], p.eav_attributes, "There are no more attributes"
+ end
+
+ should "include EAV attributes in to_json, as_json and to_xml" do
+ p = Post.last
+ p.author_name = "The Author"
+ p.author_email = nil
+
+ hash = p.as_json
+
+ assert hash["post"].has_key?("author_name"), "The key is present"
+ assert_equal "The Author", hash["post"]["author_name"], "Value is correct"
+
+ assert hash["post"].has_key?("author_email"), "The nil key is present"
+ assert_equal nil, hash["post"]["author_email"], "Value is nil"
+ end
end

0 comments on commit 0fe3b34

Please sign in to comment.