Skip to content
Browse files

Remove explicit array_contains where extension, use contains instead

Removed the array_contains where extension and changed the existing
contains extension to check the type of the column. It will use either
array_contains or the INET contains depending on whether the column is
an array or not.
  • Loading branch information...
1 parent ab3b6e0 commit 8203c9702ba37fb874cfbebc417c9a969d0c1b5d @jagregory jagregory committed
View
9 docs/querying.md
@@ -55,15 +55,14 @@ ARRAY[1,2,3] @> ARRAY[2,3]
-- t
```
-Postgres_ext extends the `ActiveRecord::Relation.where` method by
-adding an `array_contains` method. To make a contains query, you
-can do:
+Postgres\_ext extends the `ActiveRecord::Relation.where` method by
+adding a `contains` method. To make a contains query, you can do:
```ruby
-User.where.array_contains(:nick_names => ['Bob', 'Fred'])
+User.where.contains(:nick_names => ['Bob', 'Fred'])
```
-Postgres_ext defines `array_contains`, an [Arel](https://github.com/rails/arel)
+Postgres\_ext defines `array_contains`, an [Arel](https://github.com/rails/arel)
predicate for the `@>` operator. This is utilized by the
`where.array_contains` call above.
View
15 lib/postgres_ext/active_record/relation/query_methods.rb
@@ -14,13 +14,6 @@ def overlap(opts)
@scope
end
- def array_contains(opts)
- opts.each do |key, value|
- @scope = @scope.where(arel_table[key].array_contains(value))
- end
- @scope
- end
-
def contained_within(opts)
opts.each do |key, value|
@scope = @scope.where(arel_table[key].contained_within(value))
@@ -39,7 +32,13 @@ def contained_within_or_equals(opts)
def contains(opts)
opts.each do |key, value|
- @scope = @scope.where(arel_table[key].contains(value))
+ column_definition = @scope.engine.columns.find { |col| col.name == key.to_s }
+
+ if column_definition.respond_to?(:array) && column_definition.array
+ @scope = @scope.where(arel_table[key].array_contains(value))
+ else
+ @scope = @scope.where(arel_table[key].contains(value))
+ end
end
@scope
View
6 spec/queries/array_queries_spec.rb
@@ -28,14 +28,14 @@
end
end
- describe '.where.array_contains(:column => value)' do
+ describe '.where.contains(:column => value)' do
it 'generates the appropriate where clause' do
- query = Person.where.array_contains(:tag_ids => [1,2])
+ query = Person.where.contains(:tag_ids => [1,2])
query.to_sql.should match contains_regex
end
it 'allows chaining' do
- query = Person.where.array_contains(:tag_ids => [1,2]).where(:tags => ['working']).to_sql
+ query = Person.where.contains(:tag_ids => [1,2]).where(:tags => ['working']).to_sql
query.should match contains_regex
query.should match equality_regex

0 comments on commit 8203c97

Please sign in to comment.
Something went wrong with that request. Please try again.