Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Renames Arel array_overlap to overlap

  • Loading branch information...
commit 6c30be7f13f2b27ecf07f8bdd3e92b7ab62d1648 1 parent dae3222
@danmcclain danmcclain authored
View
2  lib/postgres_ext/active_record/relation/query_methods.rb
@@ -9,7 +9,7 @@ def initialize(scope)
def overlap(opts)
opts.each do |key, value|
- @scope = @scope.where(arel_table[key].array_overlap(value))
+ @scope = @scope.where(arel_table[key].overlap(value))
end
@scope
end
View
4 lib/postgres_ext/arel/nodes/array_nodes.rb
@@ -2,7 +2,7 @@
module Arel
module Nodes
- class ArrayOverlap < Arel::Nodes::Binary
+ class Overlap < Arel::Nodes::Binary
def operator; '&&' end
end
@@ -10,4 +10,4 @@ class ArrayContains < Arel::Nodes::Binary
def operator; '@>' end
end
end
-end
+end
View
4 lib/postgres_ext/arel/predications.rb
@@ -18,8 +18,8 @@ def contains_or_equals(other)
Nodes::ContainsEquals.new self, other
end
- def array_overlap(other)
- Nodes::ArrayOverlap.new self, other
+ def overlap(other)
+ Nodes::Overlap.new self, other
end
def array_contains(other)
View
8 lib/postgres_ext/arel/visitors/visitor.rb
@@ -20,12 +20,8 @@ def visit_Arel_Nodes_ContainsEquals o
"#{visit o.left} >>= #{visit o.right}"
end
- def visit_Arel_Nodes_ArrayOverlap o
- if Array === o.right
- "#{visit o.left} && #{visit o.right}"
- else
- "#{visit o.left} && #{visit o.right}"
- end
+ def visit_Arel_Nodes_Overlap o
+ "#{visit o.left} && #{visit o.right}"
end
def visit_Arel_Nodes_ArrayContains o
View
80 spec/arel/array_spec.rb
@@ -1,95 +1,77 @@
require 'spec_helper'
describe 'Array Column Predicates' do
- let!(:adapter) { ActiveRecord::Base.connection }
-
- before do
- adapter.create_table :arel_arrays, :force => true do |t|
- t.string :tags, :array => true
- t.integer :tag_ids, :array => true
- end
-
- class ArelArray < ActiveRecord::Base
- attr_accessible :tags
- end
- end
-
- after do
- adapter.drop_table :arel_arrays
- Object.send(:remove_const, :ArelArray)
- end
-
describe 'Array Overlap' do
- it 'converts Arel array_overlap statment' do
- arel_table = ArelArray.arel_table
+ it 'converts Arel overlap statment' do
+ arel_table = Person.arel_table
- arel_table.where(arel_table[:tags].array_overlap(['tag','tag 2'])).to_sql.should match /&& '\{"tag","tag 2"\}'/
+ arel_table.where(arel_table[:tags].overlap(['tag','tag 2'])).to_sql.should match /&& '\{"tag","tag 2"\}'/
end
- it 'converts Arel array_overlap statment' do
- arel_table = ArelArray.arel_table
+ it 'converts Arel overlap statment' do
+ arel_table = Person.arel_table
- arel_table.where(arel_table[:tag_ids].array_overlap([1,2])).to_sql.should match /&& '\{1,2\}'/
+ arel_table.where(arel_table[:tag_ids].overlap([1,2])).to_sql.should match /&& '\{1,2\}'/
end
it 'works with count (and other predicates)' do
- arel_table = ArelArray.arel_table
+ arel_table = Person.arel_table
- ArelArray.where(arel_table[:tag_ids].array_overlap([1,2])).count.should eq 0
+ Person.where(arel_table[:tag_ids].overlap([1,2])).count.should eq 0
end
it 'returns matched records' do
- one = ArelArray.create!(:tags => ['one'])
- two = ArelArray.create!(:tags => ['two'])
- arel_table = ArelArray.arel_table
+ one = Person.create!(:tags => ['one'])
+ two = Person.create!(:tags => ['two'])
+ arel_table = Person.arel_table
- query = arel_table.where(arel_table[:tags].array_overlap(['one'])).project(Arel.sql('*'))
- ArelArray.find_by_sql(query.to_sql).should include(one)
+ query = arel_table.where(arel_table[:tags].overlap(['one'])).project(Arel.sql('*'))
+ Person.find_by_sql(query.to_sql).should include(one)
- query = arel_table.where(arel_table[:tags].array_overlap(['two'])).project(Arel.sql('*'))
- ArelArray.find_by_sql(query.to_sql).should include(two)
+ query = arel_table.where(arel_table[:tags].overlap(['two'])).project(Arel.sql('*'))
+ Person.find_by_sql(query.to_sql).should include(two)
- query = arel_table.where(arel_table[:tags].array_overlap(['two','one'])).project(Arel.sql('*'))
- ArelArray.find_by_sql(query.to_sql).should include(two)
- ArelArray.find_by_sql(query.to_sql).should include(one)
+ query = arel_table.where(arel_table[:tags].overlap(['two','one'])).project(Arel.sql('*'))
+ Person.find_by_sql(query.to_sql).should include(two)
+ Person.find_by_sql(query.to_sql).should include(one)
end
end
describe 'Array Contains' do
it 'converts Arel array_contains statement and escapes strings' do
- arel_table = ArelArray.arel_table
+ arel_table = Person.arel_table
arel_table.where(arel_table[:tags].array_contains(['tag','tag 2'])).to_sql.should match /@> '\{"tag","tag 2"\}'/
end
it 'converts Arel array_contains statement with numbers' do
- arel_table = ArelArray.arel_table
+ arel_table = Person.arel_table
arel_table.where(arel_table[:tag_ids].array_contains([1,2])).to_sql.should match /@> '\{1,2\}'/
end
it 'works with count (and other predicates)' do
- arel_table = ArelArray.arel_table
+ arel_table = Person.arel_table
- ArelArray.where(arel_table[:tag_ids].array_contains([1,2])).count.should eq 0
+ Person.where(arel_table[:tag_ids].array_contains([1,2])).count.should eq 0
end
it 'returns matched records' do
- one = ArelArray.create!(:tags => ['one', 'two', 'three'])
- two = ArelArray.create!(:tags => ['one', 'three'])
- arel_table = ArelArray.arel_table
+ one = Person.create!(:tags => ['one', 'two', 'three'])
+ two = Person.create!(:tags => ['one', 'three'])
+ arel_table = Person.arel_table
query = arel_table.where(arel_table[:tags].array_contains(['one', 'two'])).project(Arel.sql('*'))
- ArelArray.find_by_sql(query.to_sql).should include one
- ArelArray.find_by_sql(query.to_sql).should_not include two
+ Person.find_by_sql(query.to_sql).should include one
+ Person.find_by_sql(query.to_sql).should_not include two
query = arel_table.where(arel_table[:tags].array_contains(['one', 'three'])).project(Arel.sql('*'))
- ArelArray.find_by_sql(query.to_sql).should include one
- ArelArray.find_by_sql(query.to_sql).should include two
+ Person.find_by_sql(query.to_sql).should include one
+ Person.find_by_sql(query.to_sql).should include two
query = arel_table.where(arel_table[:tags].array_contains(['two'])).project(Arel.sql('*'))
- ArelArray.find_by_sql(query.to_sql).should include one
- ArelArray.find_by_sql(query.to_sql).should_not include two
+ Person.find_by_sql(query.to_sql).should include one
+ Person.find_by_sql(query.to_sql).should_not include two
end
end
end
View
2  spec/dummy/app/models/person.rb
@@ -1,3 +1,3 @@
class Person < ActiveRecord::Base
- attr_accessible :ip
+ attr_accessible :ip, :tags
end
View
2  spec/dummy/db/migrate/20120501163758_create_people.rb
@@ -3,7 +3,7 @@ def change
create_table :people do |t|
t.inet :ip
t.cidr :subnet
- t.inet :tag_ids, :array => true
+ t.integer :tag_ids, :array => true
t.string :tags, :array => true
t.timestamps
View
2  spec/dummy/db/schema.rb
@@ -20,7 +20,7 @@
create_table "people", :force => true do |t|
t.inet "ip"
t.cidr "subnet"
- t.inet "tag_ids", :array => true
+ t.integer "tag_ids", :array => true
t.string "tags", :array => true
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
View
4 spec/models/array_spec.rb
@@ -222,14 +222,14 @@ class User < ActiveRecord::Base
end
end
- describe 'array_overlap' do
+ describe 'overlap' do
it "works" do
arel = User.arel_table
User.create(:nick_names => ['this'])
x = User.create
x.nick_names = ["s'o{m}e", 'thing']
x.save
- u = User.where(arel[:nick_names].array_overlap(["s'o{m}e"]))
+ u = User.where(arel[:nick_names].overlap(["s'o{m}e"]))
u.first.should_not be_nil
u.first.nick_names.should eq ["s'o{m}e", 'thing']
end
Please sign in to comment.
Something went wrong with that request. Please try again.