datts_right 0.0.23 → 0.0.24

Sign up to get free protection for your applications and to get access to all the features.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.0.23
1
+ 0.0.24
data/datts_right.gemspec CHANGED
@@ -5,7 +5,7 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{datts_right}
8
- s.version = "0.0.23"
8
+ s.version = "0.0.24"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Ramon Tayag"]
@@ -47,13 +47,18 @@ Gem::Specification.new do |s|
47
47
  "spec/datts_right/create_definitions_spec.rb",
48
48
  "spec/datts_right/definition_spec.rb",
49
49
  "spec/datts_right/dynamic_attribute_definition_spec.rb",
50
+ "spec/datts_right/dynamic_attribute_details_spec.rb",
50
51
  "spec/datts_right/find_by_dynamic_attribute_spec.rb",
51
52
  "spec/datts_right/inheritance_spec.rb",
53
+ "spec/datts_right/order_by_dynamic_attribute_spec.rb",
52
54
  "spec/datts_right/read_dynamic_attribute_spec.rb",
53
55
  "spec/datts_right/remove_definition_spec.rb",
54
56
  "spec/datts_right/remove_definitions_spec.rb",
55
57
  "spec/datts_right/remove_dynamic_attribute_spec.rb",
56
58
  "spec/datts_right/where_dynamic_attribute_spec.rb",
59
+ "spec/datts_right/with_dynamic_attribute_key_spec.rb",
60
+ "spec/datts_right/with_dynamic_attribute_type_spec.rb",
61
+ "spec/datts_right/write_dynamic_attribute_spec.rb",
57
62
  "spec/datts_right_spec.rb",
58
63
  "spec/spec_helper.rb"
59
64
  ]
@@ -72,13 +77,18 @@ Gem::Specification.new do |s|
72
77
  "spec/datts_right/create_definitions_spec.rb",
73
78
  "spec/datts_right/definition_spec.rb",
74
79
  "spec/datts_right/dynamic_attribute_definition_spec.rb",
80
+ "spec/datts_right/dynamic_attribute_details_spec.rb",
75
81
  "spec/datts_right/find_by_dynamic_attribute_spec.rb",
76
82
  "spec/datts_right/inheritance_spec.rb",
83
+ "spec/datts_right/order_by_dynamic_attribute_spec.rb",
77
84
  "spec/datts_right/read_dynamic_attribute_spec.rb",
78
85
  "spec/datts_right/remove_definition_spec.rb",
79
86
  "spec/datts_right/remove_definitions_spec.rb",
80
87
  "spec/datts_right/remove_dynamic_attribute_spec.rb",
81
88
  "spec/datts_right/where_dynamic_attribute_spec.rb",
89
+ "spec/datts_right/with_dynamic_attribute_key_spec.rb",
90
+ "spec/datts_right/with_dynamic_attribute_type_spec.rb",
91
+ "spec/datts_right/write_dynamic_attribute_spec.rb",
82
92
  "spec/datts_right_spec.rb",
83
93
  "spec/spec_helper.rb"
84
94
  ]
@@ -1,4 +1,8 @@
1
1
  class Category < ActiveRecord::Base
2
2
  has_dynamic_attributes :definition => true
3
3
  has_many :pages
4
+
5
+ def definition_attributes=(new_attributes)
6
+ add_definitions(new_attributes)
7
+ end
4
8
  end
@@ -10,4 +10,7 @@ module DattsRight
10
10
 
11
11
  class NotDefinedError < DattsRightError
12
12
  end
13
+
14
+ class AttributeKeyRequired < DattsRightError
15
+ end
13
16
  end
@@ -92,10 +92,13 @@ module DattsRight
92
92
  else
93
93
  #respond_to?(:"#{k}=") ? send(:"#{k}=", v) : raise(UnknownAttributeError, "unknown attribute: #{k}")
94
94
  begin
95
- send("#{k}=", v)
95
+ #puts "Attempt to set super #{k} to #{v}"
96
+ #puts "Checking to see if #{self.inspect} responds to #{k}= ........... #{self.class.name}##{self.class.respond_to?(:"#{k}=")}, or the record itself: #{respond_to?(:"#{k}=")}"
97
+ respond_to?(:"#{k}=") ? send(:"#{k}=", v) : raise(ActiveRecord::UnknownAttributeError, "unknown attribute: #{k}")
98
+ #send("#{k}=", v)
96
99
  #puts "Set super #{k} to #{v}"
97
- rescue NoMethodError => e
98
- #puts "NoMethodError was raised: #{e}, so we now check to see if '#{k}' is dynamic_attribute"
100
+ rescue ActiveRecord::UnknownAttributeError => e
101
+ #puts "ActiveRecord::UnknownAttributeError was raised: #{e}, so we now check to see if '#{k}' is dynamic_attribute"
99
102
  if dynamic_attribute?(k)
100
103
  write_dynamic_attribute("#{k}", v)
101
104
  else
@@ -120,22 +123,43 @@ module DattsRight
120
123
  end
121
124
 
122
125
  def add_definition(key, value)
123
- key = key.to_sym
124
- if dynamic_attributes_options[:definition]
125
- if definition[key]
126
- raise AlreadyDefined, "#{key} is already defined"
126
+ if key
127
+ key = key.to_sym
128
+ if dynamic_attributes_options[:definition]
129
+ if definition[key]
130
+ raise AlreadyDefined, "#{key} is already defined"
131
+ else
132
+ definition.merge!({key => value})
133
+ end
127
134
  else
128
- definition.merge!({key => value})
135
+ raise NoDefinitionError
129
136
  end
130
137
  else
131
- raise NoDefinitionError
138
+ raise AttributeKeyRequired
132
139
  end
133
140
  end
134
141
 
135
- def add_definitions(hash)
136
- if hash
137
- hash.each do |k, v|
138
- add_definition k, v
142
+ def add_definitions(*args)
143
+ args.compact! #remove the nil items
144
+ #puts "args after compacting: #{args.inspect}"
145
+ args.each do |item|
146
+ #puts "Working on #{item.inspect} is is a hash? (#{item.is_a?(Hash)}) or something else?"
147
+ item.each do |k, v|
148
+ #puts "Working on this k,v pair: #{k}, #{v}"
149
+ if v.is_a?(Hash) # item is like :robot => {:object_type => "text"}, :robot@ => {:object_type => "text"}
150
+ #puts "#{v} IS a hash"
151
+ add_definition k, v
152
+ else # v is not a hash; item is like {"name"=>"A key", "attr_key"=>"a_key"}, {"name"=>"B key", "attr_key"=>"b_key"}
153
+ #puts "#{v} is not a hash"
154
+ item.symbolize_keys! # {:name=>"A key", :description=>"asd", :attr_key=>"a_key"}
155
+ #puts "item is symbolized: #{item.inspect}"
156
+ attr_key = item.delete(:attr_key)
157
+ #puts "This is the attr_key: #{attr_key}"
158
+ if attr_key # we only want to work on it if there's an attr_key
159
+ attr_key = attr_key.to_sym
160
+ add_definition(attr_key, item)
161
+ end
162
+ end
139
163
  end
140
164
  end
141
165
  end
@@ -1,4 +1,8 @@
1
1
  class Page < ActiveRecord::Base
2
2
  belongs_to :category
3
3
  has_dynamic_attributes :of => :category
4
+
5
+ def virtual_attribute=(something)
6
+ puts "Setting virtual_attribute to #{something}"
7
+ end
4
8
  end
@@ -24,4 +24,8 @@ describe DattsRight, ".add_definition(key, value)" do
24
24
  it "should raise NoMethodError if it doesn't have definition => true" do
25
25
  lambda {Page.create.add_definition(:fake, :object_type => "string")}.should raise_error(DattsRight::NoDefinitionError)
26
26
  end
27
+
28
+ it "should raise AttributeKeyRequired if no attr_key is passed" do
29
+ lambda {Page.create.add_definition(nil, {})}.should raise_error(DattsRight::AttributeKeyRequired)
30
+ end
27
31
  end
@@ -12,6 +12,14 @@ describe DattsRight, ".add_definitions(hash)" do
12
12
  c.add_definitions(:robot => {:object_type => "text"}, :another => {:object_type => "string"})
13
13
  end
14
14
 
15
+ it "should accept 'flat' hashes in an array (straight from a form)" do
16
+ # [{"name"=>"A key", "description"=>"asd", "attr_key"=>"a_key"}, {"name"=>"A key", "description"=>"asd", "attr_key"=>"b_key"}]
17
+ c = Category.create
18
+ c.should_receive(:add_definition).with(:a_key, {:name=>"A key"})
19
+ c.should_receive(:add_definition).with(:b_key, {:name=>"B key"})
20
+ c.add_definitions({"name"=>"A key", "attr_key"=>"a_key"}, {"name"=>"B key", "attr_key"=>"b_key"})
21
+ end
22
+
15
23
  it "should not explode if nil is passed" do
16
24
  lambda {Category.create.add_definitions(nil)}.should_not raise_error(NoMethodError)
17
25
  end
@@ -0,0 +1,17 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
2
+
3
+ describe DattsRight, ".dynamic_attribute_details(key)" do
4
+ before do
5
+ reset_database
6
+ @page = Page.create
7
+ end
8
+
9
+ it "should be aliased by datt_details" do
10
+ Page.instance_method(:dynamic_attribute_details).should == Page.instance_method(:datt_details)
11
+ end
12
+
13
+ it "should allow access to the dynamic_attribute" do
14
+ @page.add_dynamic_attribute(:price, "integer")
15
+ @page.dynamic_attribute_details(:price).class.should == DynamicAttribute
16
+ end
17
+ end
@@ -0,0 +1,44 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
2
+
3
+ describe DattsRight, "order_by_dynamic_attribute" do
4
+ before do
5
+ reset_database
6
+ @page = Page.create
7
+ end
8
+
9
+ it "should be aliased by order_by_datt" do
10
+ @page.add_dynamic_attribute(:price, "integer")
11
+ @page.write_dynamic_attribute :price, 2
12
+ @page.save
13
+ Page.order_by_dynamic_attribute("price", "integer").should == Page.order_by_datt("price", "integer")
14
+ end
15
+
16
+ it "should default to asc" do
17
+ @page.add_dynamic_attribute(:price, "integer")
18
+ @page.write_dynamic_attribute :price, 2
19
+ @page.save
20
+ @page_2 = Page.create
21
+ @page_2.add_dynamic_attribute(:price, "integer")
22
+ @page_2.write_dynamic_attribute :price, 1
23
+ @page_2.save
24
+ @page_3 = Page.create
25
+ @page_3.add_dynamic_attribute(:price, "float")
26
+ @page_3.write_dynamic_attribute :price, 3.0
27
+ @page_3.save
28
+ @page_4 = Page.create
29
+ @page_4.add_dynamic_attribute(:price, "float")
30
+ @page_4.write_dynamic_attribute :price, 3.5
31
+ @page_4.save
32
+
33
+ # What if different records have price but they are of different object_type?
34
+ # page_1.read_dynamic_attribute(:price) is an "integer" and it's saved in "integer_value" column
35
+ # page_2.read_dynamic_attribute(:price) is a "float" and it's saved in "float_value"
36
+ #
37
+ # How would you work on Page.order_by_dynamic_attribute("price")?
38
+ #
39
+ # Answer 1: we could require passing of the object_type in the order method
40
+ # This way, we know which column to look at => order_by_dynamic_attribute("price", "integer")
41
+ Page.order_by_dynamic_attribute("price", "integer").should == [@page_2, @page]
42
+ Page.order_by_dynamic_attribute("price DESC", "float").should == [@page_4, @page_3]
43
+ end
44
+ end
@@ -0,0 +1,13 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
2
+
3
+ describe ".with_dynamic_attribute_key" do
4
+ before do
5
+ reset_database
6
+ @page = Page.create
7
+ end
8
+
9
+ it "should be aliased by .with_datt_key" do
10
+ @page.add_dynamic_attribute(:price, "float")
11
+ Page.with_dynamic_attribute_key(:price).should == Page.with_datt_key(:price)
12
+ end
13
+ end
@@ -0,0 +1,13 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
2
+
3
+ describe ".with_dynamic_attribute_type" do
4
+ before do
5
+ reset_database
6
+ @page = Page.create
7
+ end
8
+
9
+ it "should be aliased by .with_datt_type" do
10
+ @page.add_dynamic_attribute(:price, "float")
11
+ Page.with_dynamic_attribute_type("float").should == Page.with_datt_type("float")
12
+ end
13
+ end
@@ -0,0 +1,7 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
2
+
3
+ describe DattsRight, ".write_dynamic_attribute" do
4
+ it "should be aliased by write_datt" do
5
+ Page.instance_method(:write_dynamic_attribute).should == Page.instance_method(:write_datt)
6
+ end
7
+ end
@@ -143,75 +143,6 @@ describe DattsRight do
143
143
  @page.read_dynamic_attribute(:price).should == 200
144
144
  end
145
145
 
146
- describe "order_by_dynamic_attribute" do
147
- it "should be aliased by order_by_datt" do
148
- @page.add_dynamic_attribute(:price, "integer")
149
- @page.write_dynamic_attribute :price, 2
150
- @page.save
151
- Page.order_by_dynamic_attribute("price", "integer").should == Page.order_by_datt("price", "integer")
152
- end
153
-
154
- it "should default to asc" do
155
- @page.add_dynamic_attribute(:price, "integer")
156
- @page.write_dynamic_attribute :price, 2
157
- @page.save
158
- @page_2 = Page.create
159
- @page_2.add_dynamic_attribute(:price, "integer")
160
- @page_2.write_dynamic_attribute :price, 1
161
- @page_2.save
162
- @page_3 = Page.create
163
- @page_3.add_dynamic_attribute(:price, "float")
164
- @page_3.write_dynamic_attribute :price, 3.0
165
- @page_3.save
166
- @page_4 = Page.create
167
- @page_4.add_dynamic_attribute(:price, "float")
168
- @page_4.write_dynamic_attribute :price, 3.5
169
- @page_4.save
170
-
171
- # What if different records have price but they are of different object_type?
172
- # page_1.read_dynamic_attribute(:price) is an "integer" and it's saved in "integer_value" column
173
- # page_2.read_dynamic_attribute(:price) is a "float" and it's saved in "float_value"
174
- #
175
- # How would you work on Page.order_by_dynamic_attribute("price")?
176
- #
177
- # Answer 1: we could require passing of the object_type in the order method
178
- # This way, we know which column to look at => order_by_dynamic_attribute("price", "integer")
179
- Page.order_by_dynamic_attribute("price", "integer").should == [@page_2, @page]
180
- Page.order_by_dynamic_attribute("price DESC", "float").should == [@page_4, @page_3]
181
- end
182
- end
183
-
184
- describe ".write_dynamic_attribute" do
185
- it "should be aliased by write_datt" do
186
- Page.instance_method(:write_dynamic_attribute).should == Page.instance_method(:write_datt)
187
- end
188
- end
189
-
190
- describe ".with_dynamic_attribute_key" do
191
- it "should be aliased by .with_datt_key" do
192
- @page.add_dynamic_attribute(:price, "float")
193
- Page.with_dynamic_attribute_key(:price).should == Page.with_datt_key(:price)
194
- end
195
- end
196
-
197
- describe ".with_dynamic_attribute_type" do
198
- it "should be aliased by .with_datt_type" do
199
- @page.add_dynamic_attribute(:price, "float")
200
- Page.with_dynamic_attribute_type("float").should == Page.with_datt_type("float")
201
- end
202
- end
203
-
204
- describe ".dynamic_attribute_details(key)" do
205
- it "should be aliased by datt_details" do
206
- Page.instance_method(:dynamic_attribute_details).should == Page.instance_method(:datt_details)
207
- end
208
-
209
- it "should allow access to the dynamic_attribute" do
210
- @page.add_dynamic_attribute(:price, "integer")
211
- @page.dynamic_attribute_details(:price).class.should == DynamicAttribute
212
- end
213
- end
214
-
215
146
  describe "when loading from database" do
216
147
  it "should load the dynamic attributes for use" do
217
148
  @page.add_datt(:body, "text")
@@ -243,4 +174,8 @@ describe DattsRight do
243
174
  @page.save
244
175
  @page.body.should == "dude"
245
176
  end
177
+
178
+ it "should still allow virtual attributes to work (for mass assignment)" do
179
+ lambda {Category.create.update_attributes(:definition_attributes => {"attr_key" => "some_attr", "object_type" => "string"}, :name => "hello")}.should_not raise_error(ActiveRecord::UnknownAttributeError)
180
+ end
246
181
  end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: datts_right
3
3
  version: !ruby/object:Gem::Version
4
- hash: 49
4
+ hash: 47
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
8
  - 0
9
- - 23
10
- version: 0.0.23
9
+ - 24
10
+ version: 0.0.24
11
11
  platform: ruby
12
12
  authors:
13
13
  - Ramon Tayag
@@ -178,13 +178,18 @@ files:
178
178
  - spec/datts_right/create_definitions_spec.rb
179
179
  - spec/datts_right/definition_spec.rb
180
180
  - spec/datts_right/dynamic_attribute_definition_spec.rb
181
+ - spec/datts_right/dynamic_attribute_details_spec.rb
181
182
  - spec/datts_right/find_by_dynamic_attribute_spec.rb
182
183
  - spec/datts_right/inheritance_spec.rb
184
+ - spec/datts_right/order_by_dynamic_attribute_spec.rb
183
185
  - spec/datts_right/read_dynamic_attribute_spec.rb
184
186
  - spec/datts_right/remove_definition_spec.rb
185
187
  - spec/datts_right/remove_definitions_spec.rb
186
188
  - spec/datts_right/remove_dynamic_attribute_spec.rb
187
189
  - spec/datts_right/where_dynamic_attribute_spec.rb
190
+ - spec/datts_right/with_dynamic_attribute_key_spec.rb
191
+ - spec/datts_right/with_dynamic_attribute_type_spec.rb
192
+ - spec/datts_right/write_dynamic_attribute_spec.rb
188
193
  - spec/datts_right_spec.rb
189
194
  - spec/spec_helper.rb
190
195
  has_rdoc: true
@@ -231,12 +236,17 @@ test_files:
231
236
  - spec/datts_right/create_definitions_spec.rb
232
237
  - spec/datts_right/definition_spec.rb
233
238
  - spec/datts_right/dynamic_attribute_definition_spec.rb
239
+ - spec/datts_right/dynamic_attribute_details_spec.rb
234
240
  - spec/datts_right/find_by_dynamic_attribute_spec.rb
235
241
  - spec/datts_right/inheritance_spec.rb
242
+ - spec/datts_right/order_by_dynamic_attribute_spec.rb
236
243
  - spec/datts_right/read_dynamic_attribute_spec.rb
237
244
  - spec/datts_right/remove_definition_spec.rb
238
245
  - spec/datts_right/remove_definitions_spec.rb
239
246
  - spec/datts_right/remove_dynamic_attribute_spec.rb
240
247
  - spec/datts_right/where_dynamic_attribute_spec.rb
248
+ - spec/datts_right/with_dynamic_attribute_key_spec.rb
249
+ - spec/datts_right/with_dynamic_attribute_type_spec.rb
250
+ - spec/datts_right/write_dynamic_attribute_spec.rb
241
251
  - spec/datts_right_spec.rb
242
252
  - spec/spec_helper.rb