surus 0.4.0 → 0.4.1
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGELOG.md +8 -0
- data/lib/surus/array/scope.rb +9 -3
- data/lib/surus/version.rb +1 -1
- data/spec/array/scope_spec.rb +18 -10
- data/spec/database_structure.sql +7 -0
- data/spec/spec_helper.rb +4 -0
- metadata +2 -2
data/CHANGELOG.md
CHANGED
@@ -1,3 +1,11 @@
|
|
1
|
+
# 0.4.1 (February 27, 2013)
|
2
|
+
|
3
|
+
* Fix array_has and array_has_any with varchar[]
|
4
|
+
|
5
|
+
# 0.4.0 (February 22, 2013)
|
6
|
+
|
7
|
+
* Added JSON generation with find_json and all_json
|
8
|
+
|
1
9
|
# 0.3.2 (March 10, 2012)
|
2
10
|
|
3
11
|
* No changes. Had to bump version to get around partially failed upload to RubyGems.org.
|
data/lib/surus/array/scope.rb
CHANGED
@@ -8,9 +8,9 @@ module Surus
|
|
8
8
|
# User.array_has(:permissions, "manage_users", "manage_roles")
|
9
9
|
# User.array_has(:permissions, ["manage_users", "manage_roles"])
|
10
10
|
def array_has(column, *values)
|
11
|
-
where("#{connection.quote_column_name(column)} @> ARRAY[?]", values.flatten)
|
11
|
+
where("#{connection.quote_column_name(column)} @> ARRAY[?]#{array_cast(column)}", values.flatten)
|
12
12
|
end
|
13
|
-
|
13
|
+
|
14
14
|
# Adds where condition that requires column to contain any values
|
15
15
|
#
|
16
16
|
# Examples:
|
@@ -18,7 +18,13 @@ module Surus
|
|
18
18
|
# User.array_has_any(:permissions, "manage_users", "manage_roles")
|
19
19
|
# User.array_has_any(:permissions, ["manage_users", "manage_roles"])
|
20
20
|
def array_has_any(column, *values)
|
21
|
-
where("#{connection.quote_column_name(column)} && ARRAY[?]", values.flatten)
|
21
|
+
where("#{connection.quote_column_name(column)} && ARRAY[?]#{array_cast(column)}", values.flatten)
|
22
|
+
end
|
23
|
+
|
24
|
+
private
|
25
|
+
def array_cast(column_name)
|
26
|
+
column = columns_hash[column_name.to_s]
|
27
|
+
"::#{column.sql_type}"
|
22
28
|
end
|
23
29
|
end
|
24
30
|
end
|
data/lib/surus/version.rb
CHANGED
data/spec/array/scope_spec.rb
CHANGED
@@ -6,52 +6,60 @@ describe Surus::Array::Scope do
|
|
6
6
|
context "array_has" do
|
7
7
|
let!(:match) { TextArrayRecord.create! :texts => %w{a b} }
|
8
8
|
let!(:missing_element) { TextArrayRecord.create! :texts => %w{a} }
|
9
|
-
|
9
|
+
|
10
10
|
def self.shared_examples
|
11
11
|
it { should include(match) }
|
12
12
|
it { should_not include(missing_element) }
|
13
13
|
it { should_not include(empty) }
|
14
14
|
end
|
15
|
-
|
15
|
+
|
16
16
|
context "with one element" do
|
17
17
|
subject { TextArrayRecord.array_has(:texts, "b").all }
|
18
18
|
shared_examples
|
19
19
|
end
|
20
|
-
|
20
|
+
|
21
21
|
context "with array of elements" do
|
22
22
|
subject { TextArrayRecord.array_has(:texts, ["a", "b"]).all }
|
23
23
|
shared_examples
|
24
24
|
end
|
25
|
-
|
25
|
+
|
26
26
|
context "with multiple elements" do
|
27
27
|
subject { TextArrayRecord.array_has(:texts, "a", "b").all }
|
28
28
|
shared_examples
|
29
29
|
end
|
30
30
|
end
|
31
|
-
|
31
|
+
|
32
32
|
context "array_has_any" do
|
33
33
|
let!(:match) { TextArrayRecord.create! :texts => %w{a b} }
|
34
34
|
let!(:missing_element) { TextArrayRecord.create! :texts => %w{a} }
|
35
|
-
|
35
|
+
|
36
36
|
def self.shared_examples
|
37
37
|
it { should include(match) }
|
38
38
|
it { should_not include(missing_element) }
|
39
39
|
it { should_not include(empty) }
|
40
40
|
end
|
41
|
-
|
41
|
+
|
42
42
|
context "with one element" do
|
43
43
|
subject { TextArrayRecord.array_has_any(:texts, "b").all }
|
44
44
|
shared_examples
|
45
45
|
end
|
46
|
-
|
46
|
+
|
47
47
|
context "with array of elements" do
|
48
48
|
subject { TextArrayRecord.array_has_any(:texts, ["b", "c"]).all }
|
49
49
|
shared_examples
|
50
50
|
end
|
51
|
-
|
51
|
+
|
52
52
|
context "with multiple elements" do
|
53
53
|
subject { TextArrayRecord.array_has_any(:texts, "b", "c").all }
|
54
54
|
shared_examples
|
55
55
|
end
|
56
|
-
end
|
56
|
+
end
|
57
|
+
|
58
|
+
it "casts between varchar[] and text[]" do
|
59
|
+
record = VarcharArrayRecord.create! :varchars => %w{a b}
|
60
|
+
expect(VarcharArrayRecord.array_has_any(:varchars, "a")).to include(record)
|
61
|
+
expect(VarcharArrayRecord.array_has_any(:varchars, "c")).to_not include(record)
|
62
|
+
expect(VarcharArrayRecord.array_has_any(:varchars, "b", "c")).to include(record)
|
63
|
+
expect(VarcharArrayRecord.array_has_any(:varchars, "c")).to_not include(record)
|
64
|
+
end
|
57
65
|
end
|
data/spec/database_structure.sql
CHANGED
data/spec/spec_helper.rb
CHANGED
@@ -15,6 +15,10 @@ class TextArrayRecord < ActiveRecord::Base
|
|
15
15
|
serialize :texts, Surus::Array::TextSerializer.new
|
16
16
|
end
|
17
17
|
|
18
|
+
class VarcharArrayRecord < ActiveRecord::Base
|
19
|
+
serialize :varchars, Surus::Array::TextSerializer.new
|
20
|
+
end
|
21
|
+
|
18
22
|
class IntegerArrayRecord < ActiveRecord::Base
|
19
23
|
serialize :integers, Surus::Array::IntegerSerializer.new
|
20
24
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: surus
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.4.
|
4
|
+
version: 0.4.1
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2013-02-
|
12
|
+
date: 2013-02-27 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: pg
|