hstore_accessor 0.6.0 → 0.6.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +12 -0
- data/Rakefile +3 -0
- data/lib/hstore_accessor/macro.rb +7 -4
- data/lib/hstore_accessor/version.rb +1 -1
- data/spec/hstore_accessor_spec.rb +36 -16
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 7c01e590bd0c9ae91fc49ddc4dc1c2b687292d6e
|
4
|
+
data.tar.gz: cca0f9e8ff8bcb3ab3081123e43568393b6006b4
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 10e8633baf66a5119571d8d47d1e1617cdb223529671596ac84e8547240fd2cc004f9620795ad3166965fe69d4afe4f482eff5429a0a692bea7cb828895dff37
|
7
|
+
data.tar.gz: a241e6f63c92b4e3e11d8677c8846fd274a94b7bffeacf45c7294fc92fa3ce473ff2318d910fe164765d11945f2cead9fbe681c24c5367bbfdae27bef8f9529d
|
data/README.md
CHANGED
@@ -78,6 +78,18 @@ In the above example you can continue to interact with the fields using
|
|
78
78
|
their full name but when saved to the database the field will be set
|
79
79
|
using the `store_key`.
|
80
80
|
|
81
|
+
Additionally, dirty tracking is implemented in the same way that normal
|
82
|
+
`ActiveRecord` fields work.
|
83
|
+
|
84
|
+
```ruby
|
85
|
+
p.color #=> "green"
|
86
|
+
p.color = "blue"
|
87
|
+
p.changed? #=> true
|
88
|
+
p.color_changed? #=> true
|
89
|
+
p.color_was #=> "green"
|
90
|
+
p.color_changes #=> ["green", "blue"]
|
91
|
+
```
|
92
|
+
|
81
93
|
### Scopes
|
82
94
|
|
83
95
|
The `hstore_accessor` macro also creates scopes for `string`, `integer`,
|
data/Rakefile
CHANGED
@@ -24,9 +24,12 @@ module HstoreAccessor
|
|
24
24
|
raise Serialization::InvalidDataTypeError unless Serialization::VALID_TYPES.include?(data_type)
|
25
25
|
|
26
26
|
field_methods.send(:define_method, "#{key}=") do |value|
|
27
|
-
|
28
|
-
|
29
|
-
send(
|
27
|
+
casted_value = TypeHelpers.cast(data_type, value)
|
28
|
+
serialized_value = serialize(data_type, casted_value)
|
29
|
+
unless send(key) == casted_value
|
30
|
+
send(:attribute_will_change!, key)
|
31
|
+
send("#{hstore_attribute}_will_change!")
|
32
|
+
end
|
30
33
|
send("#{hstore_attribute}=", (send(hstore_attribute) || {}).merge(store_key.to_s => serialized_value))
|
31
34
|
end
|
32
35
|
|
@@ -76,7 +79,7 @@ module HstoreAccessor
|
|
76
79
|
when :array
|
77
80
|
send(:scope, "#{key}_eq", -> value { where("#{query_field} = ?", value.join(Serialization::SEPARATOR)) })
|
78
81
|
send(:scope, "#{key}_contains", -> value do
|
79
|
-
where("string_to_array(#{query_field}, '#{Serialization::SEPARATOR}') @> string_to_array(?, '#{Serialization::SEPARATOR}')", Array[value].flatten)
|
82
|
+
where("string_to_array(#{query_field}, '#{Serialization::SEPARATOR}') @> string_to_array(?, '#{Serialization::SEPARATOR}')", Array[value].flatten.join(Serialization::SEPARATOR))
|
80
83
|
end)
|
81
84
|
end
|
82
85
|
end
|
@@ -4,6 +4,7 @@ require "active_support/all"
|
|
4
4
|
FIELDS = {
|
5
5
|
color: :string,
|
6
6
|
price: :integer,
|
7
|
+
published: { data_type: :boolean, store_key: "p" },
|
7
8
|
weight: { data_type: :float, store_key: "w" },
|
8
9
|
popular: :boolean,
|
9
10
|
build_timestamp: :time,
|
@@ -99,7 +100,7 @@ describe HstoreAccessor do
|
|
99
100
|
it "uses 'present?' to determine return value" do
|
100
101
|
stub = double(present?: :result_of_present)
|
101
102
|
expect(stub).to receive(:present?)
|
102
|
-
product.
|
103
|
+
allow(product).to receive_messages(color: stub)
|
103
104
|
expect(product.color?).to eq(:result_of_present)
|
104
105
|
end
|
105
106
|
|
@@ -109,34 +110,34 @@ describe HstoreAccessor do
|
|
109
110
|
product.popular = true
|
110
111
|
product.save
|
111
112
|
product.reload
|
112
|
-
expect(product.popular?).to
|
113
|
+
expect(product.popular?).to be true
|
113
114
|
end
|
114
115
|
|
115
116
|
it "return the state for false boolean fields" do
|
116
117
|
product.popular = false
|
117
118
|
product.save
|
118
119
|
product.reload
|
119
|
-
expect(product.popular?).to
|
120
|
+
expect(product.popular?).to be false
|
120
121
|
end
|
121
122
|
|
122
123
|
it "return true for boolean field set via hash using real boolean" do
|
123
124
|
product.options = { "popular" => true }
|
124
|
-
expect(product.popular?).to
|
125
|
+
expect(product.popular?).to be true
|
125
126
|
end
|
126
127
|
|
127
128
|
it "return false for boolean field set via hash using real boolean" do
|
128
129
|
product.options = { "popular" => false }
|
129
|
-
expect(product.popular?).to
|
130
|
+
expect(product.popular?).to be false
|
130
131
|
end
|
131
132
|
|
132
133
|
it "return true for boolean field set via hash using string" do
|
133
134
|
product.options = { "popular" => "true" }
|
134
|
-
expect(product.popular?).to
|
135
|
+
expect(product.popular?).to be true
|
135
136
|
end
|
136
137
|
|
137
138
|
it "return false for boolean field set via hash using string" do
|
138
139
|
product.options = { "popular" => "false" }
|
139
|
-
expect(product.popular?).to
|
140
|
+
expect(product.popular?).to be false
|
140
141
|
end
|
141
142
|
end
|
142
143
|
|
@@ -239,6 +240,8 @@ describe HstoreAccessor do
|
|
239
240
|
it "contains" do
|
240
241
|
expect(Product.tags_contains("tag2").to_a).to eq [product_a, product_b]
|
241
242
|
expect(Product.tags_contains(["tag2", "tag3"]).to_a).to eq [product_a, product_b]
|
243
|
+
expect(Product.tags_contains(["tag1", "tag2", "tag3"]).to_a).to eq [product_a]
|
244
|
+
expect(Product.tags_contains(["tag1", "tag2", "tag3", "tag4"]).to_a).to eq []
|
242
245
|
end
|
243
246
|
|
244
247
|
end
|
@@ -378,20 +381,20 @@ describe HstoreAccessor do
|
|
378
381
|
product.popular = 'true'
|
379
382
|
product.save
|
380
383
|
product.reload
|
381
|
-
expect(product.popular).to
|
384
|
+
expect(product.popular).to be true
|
382
385
|
end
|
383
386
|
|
384
387
|
it "when a real boolean is passed" do
|
385
388
|
product.popular = true
|
386
389
|
product.save
|
387
390
|
product.reload
|
388
|
-
expect(product.popular).to
|
391
|
+
expect(product.popular).to be true
|
389
392
|
end
|
390
393
|
|
391
394
|
end
|
392
395
|
|
393
396
|
it "setters call the _will_change! method of the store attribute" do
|
394
|
-
product.
|
397
|
+
expect(product).to receive(:options_will_change!)
|
395
398
|
product.color = "green"
|
396
399
|
end
|
397
400
|
|
@@ -422,11 +425,17 @@ describe HstoreAccessor do
|
|
422
425
|
it "type casts boolean values" do
|
423
426
|
ActiveRecord::ConnectionAdapters::Column::TRUE_VALUES.each do |value|
|
424
427
|
product.popular = value
|
425
|
-
expect(product.popular).to
|
428
|
+
expect(product.popular).to be true
|
429
|
+
|
430
|
+
product.published = value
|
431
|
+
expect(product.published).to be true
|
426
432
|
end
|
427
433
|
ActiveRecord::ConnectionAdapters::Column::FALSE_VALUES.each do |value|
|
428
434
|
product.popular = value
|
429
|
-
expect(product.popular).to
|
435
|
+
expect(product.popular).to be false
|
436
|
+
|
437
|
+
product.published = value
|
438
|
+
expect(product.published).to be false
|
430
439
|
end
|
431
440
|
end
|
432
441
|
end
|
@@ -442,7 +451,7 @@ describe HstoreAccessor do
|
|
442
451
|
end
|
443
452
|
|
444
453
|
def color
|
445
|
-
super.downcase
|
454
|
+
super.try(:downcase)
|
446
455
|
end
|
447
456
|
end
|
448
457
|
end
|
@@ -475,11 +484,22 @@ describe HstoreAccessor do
|
|
475
484
|
let(:product) { Product.new }
|
476
485
|
|
477
486
|
it "<attr>_changed? should return the expected value" do
|
478
|
-
expect(product.color_changed?).to
|
487
|
+
expect(product.color_changed?).to be false
|
479
488
|
product.color = "ORANGE"
|
480
|
-
expect(product.color_changed?).to
|
489
|
+
expect(product.color_changed?).to be true
|
490
|
+
product.save
|
491
|
+
expect(product.color_changed?).to be false
|
492
|
+
product.color = "ORANGE"
|
493
|
+
expect(product.color_changed?).to be false
|
494
|
+
|
495
|
+
expect(product.price_changed?).to be false
|
496
|
+
product.price = 100
|
497
|
+
expect(product.price_changed?).to be true
|
481
498
|
product.save
|
482
|
-
expect(product.
|
499
|
+
expect(product.price_changed?).to be false
|
500
|
+
product.price = "100"
|
501
|
+
expect(product.price).to be 100
|
502
|
+
expect(product.price_changed?).to be false
|
483
503
|
end
|
484
504
|
|
485
505
|
it "<attr>_was should return the expected value" do
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: hstore_accessor
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.6.
|
4
|
+
version: 0.6.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Joe Hirn
|
@@ -10,7 +10,7 @@ authors:
|
|
10
10
|
autorequire:
|
11
11
|
bindir: bin
|
12
12
|
cert_chain: []
|
13
|
-
date: 2014-
|
13
|
+
date: 2014-08-17 00:00:00.000000000 Z
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|
16
16
|
name: activesupport
|