Skip to content

Commit

Permalink
Add support for lots more query methods
Browse files Browse the repository at this point in the history
  • Loading branch information
shioyama committed Nov 2, 2019
1 parent eaa479c commit dbea573
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 7 deletions.
12 changes: 8 additions & 4 deletions lib/wharel.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,16 @@

module Wharel
module QueryMethods
def where(opts = :chain, *rest, &block)
block_given? ? super(VirtualRow.build_query(self, &block)) : super
%w[where order having pluck group].each do |method_name|
module_eval <<-EOM, __FILE__, __LINE__ + 1
def #{method_name}(*, &block)
block_given? ? super(VirtualRow.build_query(self, &block)) : super
end
EOM
end

def order(*args, &block)
block_given? ? super(VirtualRow.build_query(self, &block)) : super
def or(*, &block)
block_given? ? super(model.where(VirtualRow.build_query(self, &block))) : super
end

module WhereChain
Expand Down
72 changes: 69 additions & 3 deletions spec/wharel_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,79 @@ class Comment < ActiveRecord::Base
end

describe "Relation#order" do
it "works with block format" do
post1 = Post.create(title: "Z")
post2 = Post.create(title: "a")
let!(:post1) { Post.create(title: "Z") }
let!(:post2) { Post.create(title: "a") }

it "works with block format" do
expect(Post.order { title }).to eq([post1, post2])
expect(Post.order { title.lower }).to eq([post2, post1])
end

it "works without block format" do
expect(Post.order(:title)).to eq([post1, post2])
expect(Post.order("LOWER(title)")).to eq([post2, post1])
end
end

describe 'grouping query methods' do
let!(:post1) { Post.create(title: 'foo', content: 'baz') }
let!(:post2) { Post.create(title: 'bar', content: 'baz') }
let!(:post3) { Post.create(title: 'Foo', content: 'baz') }

describe 'Relation#group' do
it 'works with block format' do
expect(Post.group { title.lower }.map(&:title)).to match_array(%w[foo bar])
end

it 'works without block format' do
expect(Post.group(:title).map(&:title)).to match_array(%w[foo Foo bar])
end
end

describe 'Relation#having' do
it 'works with block format' do
expect(
Post.group(:title).having { title.lower.eq('foo') }.map(&:title)
).to match_array(%w[foo Foo])
end

it 'works without block format' do
expect(
Post.group(:title).having("LOWER(title) = ?", 'foo').map(&:title)
).to match_array(%w[foo Foo])
end
end
end

describe 'Relation#pluck' do
let!(:post1) { Post.create(title: 'foo') }
let!(:post2) { Post.create(title: 'bar') }
let!(:post3) { Post.create(title: 'Foo') }

it 'works with block format' do
expect(Post.pluck { title.lower }).to match_array(%w[foo bar foo])
end

it 'works without block format' do
expect(Post.pluck("LOWER(title)")).to match_array(%w[foo bar foo])
end
end

describe 'Relation#or' do
let!(:post1) { Post.create(title: 'This Wharel!') }
let!(:post2) { Post.create(title: 'Not this Wharel!') }

it 'works with block format' do
expect(
Post.where { title.eq('This Wharel!') }.or { title.eq('Not this Wharel!') }
).to match_array([post1, post2])
end

it 'works without block format' do
expect(
Post.where(title: 'This Wharel!').or(Post.where(title: 'Not this Wharel!'))
).to match_array([post1, post2])
end
end

describe "WhereChain#not" do
Expand Down

0 comments on commit dbea573

Please sign in to comment.