has_eav 1.1.0 → 1.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -4,7 +4,7 @@
4
4
 
5
5
  Enabled EAV behaviour on ActiveRecord (or is at least supposed to).
6
6
 
7
- This was inspired by https://github.com/visfleet/acts_as_eav_model but is a
7
+ This was inspired by https://github.com/visfleet/acts_as_eav_model but is a
8
8
  more minimized approach and is less automated.
9
9
 
10
10
  === EAV
@@ -54,7 +54,7 @@ And you are good to go.
54
54
 
55
55
  === Usage of EAV
56
56
 
57
- For the general usage of EAV, thread wisely. Be careful and suspicious about
57
+ For the general usage of EAV, thread wisely. Be careful and suspicious about
58
58
  your needs.
59
59
 
60
60
  === Usage of has_eav
@@ -68,15 +68,23 @@ structure. You must define the attributes to be required.
68
68
 
69
69
  ==== Instance eav_attributes
70
70
 
71
- If you create a method +instance_eav_attributes+ in your model, has_eav will
71
+ If you create a method +instance_eav_attributes+ in your model, has_eav will
72
72
  recognize these attributes as well.
73
73
 
74
74
  This might be useful for state machines or STI classes where you want to have
75
- different attributes based on the current state or type of the instance.
75
+ different attributes based on the current state or type of the instance.
76
+
77
+ == Changelog
78
+
79
+ [1.1.1] * Make sure the EAV attributes make it into the serializable_hash
80
+ * Make sure the nilified attributes are removed from the DB
81
+ [1.1.0] * Casting of dates and times added
82
+ [1.0.1] * STI works
83
+ [1.0.0] * first release
76
84
 
77
85
  == Contributing to has_eav
78
-
79
- * Check out the latest master to make sure the feature hasn't been implemented
86
+
87
+ * Check out the latest master to make sure the feature hasn't been implemented
80
88
  or the bug hasn't been fixed yet
81
89
  * Check out the issue tracker to make sure someone already hasn't requested
82
90
  it and/or contributed it
data/VERSION CHANGED
@@ -1 +1 @@
1
- 1.1.0
1
+ 1.1.1
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{has_eav}
8
- s.version = "1.1.0"
8
+ s.version = "1.1.1"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Hartog C. de Mik"]
12
- s.date = %q{2011-03-21}
12
+ s.date = %q{2011-04-05}
13
13
  s.description = %q{Put EAV behaviour on your ActiveRecord models}
14
14
  s.email = %q{hcdm@matchvertise.com}
15
15
  s.extra_rdoc_files = [
@@ -108,15 +108,16 @@ module ActiveRecord
108
108
  value = args[0]
109
109
 
110
110
  if attribute
111
- if value
111
+ if !value.nil?
112
112
  return attribute.send(:write_attribute, "value", value)
113
113
 
114
- elsif value.nil?
114
+ else
115
+ @eav_attributes -= [ attribute ]
115
116
  return attribute.destroy
116
117
 
117
118
  end
118
119
 
119
- else
120
+ elsif !value.nil?
120
121
  @eav_attributes << eav_class.new(
121
122
  :name => attribute_name,
122
123
  :value => "#{value}"
@@ -124,15 +125,18 @@ module ActiveRecord
124
125
 
125
126
  return cast_eav_value(value, attribute_name)
126
127
 
128
+ else
129
+ return nil
130
+
127
131
  end
128
132
  elsif method_name =~ /\?$/
129
133
  return ( attribute and attribute.value == true ) ? true : false
130
134
 
131
135
  else
136
+ return nil if attribute and attribute.destroyed?
132
137
  return attribute ?
133
138
  cast_eav_value(attribute.value, attribute_name) :
134
139
  nil
135
-
136
140
  end
137
141
 
138
142
  raise e
@@ -200,6 +204,17 @@ module ActiveRecord
200
204
  "#{klass.name.underscore}_id".to_sym
201
205
  end
202
206
 
207
+ # make sure EAV is included in as_json, to_json and to_xml
208
+ #
209
+ def serializable_hash options=nil
210
+ hash = super
211
+ eav_attributes_list.each do |attribute|
212
+ hash[attribute] = self.send(attribute)
213
+ end
214
+
215
+ hash
216
+ end
217
+
203
218
  # cast an eav value to it's desired class
204
219
  def cast_eav_value value, attribute # :nodoc:
205
220
  attributes = self.class_eav_attributes.stringify_keys
@@ -112,4 +112,33 @@ class TestHasEav < Test::Unit::TestCase
112
112
 
113
113
  assert_equal t.to_i, p.last_update.to_i, "Time comparisson"
114
114
  end
115
+
116
+ should "not create attributes with nil value" do
117
+ p = Post.last
118
+ p.author_name = nil
119
+
120
+ assert_equal [], p.eav_attributes, "No attributes where defined"
121
+
122
+ p.author_name = "name"
123
+
124
+ assert_equal 1, p.eav_attributes.count, "There is 1 eav attribute"
125
+
126
+ p.author_name = nil
127
+ assert_equal nil, p.author_name, "The value is nilified"
128
+ assert_equal [], p.eav_attributes, "There are no more attributes"
129
+ end
130
+
131
+ should "include EAV attributes in to_json, as_json and to_xml" do
132
+ p = Post.last
133
+ p.author_name = "The Author"
134
+ p.author_email = nil
135
+
136
+ hash = p.as_json
137
+
138
+ assert hash["post"].has_key?("author_name"), "The key is present"
139
+ assert_equal "The Author", hash["post"]["author_name"], "Value is correct"
140
+
141
+ assert hash["post"].has_key?("author_email"), "The nil key is present"
142
+ assert_equal nil, hash["post"]["author_email"], "Value is nil"
143
+ end
115
144
  end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: has_eav
3
3
  version: !ruby/object:Gem::Version
4
- hash: 19
4
+ hash: 17
5
5
  prerelease: false
6
6
  segments:
7
7
  - 1
8
8
  - 1
9
- - 0
10
- version: 1.1.0
9
+ - 1
10
+ version: 1.1.1
11
11
  platform: ruby
12
12
  authors:
13
13
  - Hartog C. de Mik
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2011-03-21 00:00:00 +01:00
18
+ date: 2011-04-05 00:00:00 +02:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency