Skip to content

Commit

Permalink
Keep errors uniform
Browse files Browse the repository at this point in the history
  • Loading branch information
rzane committed Feb 3, 2017
1 parent 362601a commit 9de99e1
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 37 deletions.
13 changes: 2 additions & 11 deletions lib/baby_squeel/association.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,6 @@

module BabySqueel
class Association < Relation
class InvalidComparisonError < StandardError
end

# An Active Record association reflection
attr_reader :_reflection

Expand Down Expand Up @@ -106,18 +103,12 @@ def build_where_clause(other)
factory = relation.send(:where_clause_factory)
factory.build({ _reflection.name => other }, [])
else
raise InvalidComparisonError, <<-EOMSG.squish
You can't compare association '#{_reflection.name}'
to #{other.class}.
EOMSG
raise AssociationComparisonError.new(_reflection.name, other)
end
end
else
def build_where_clause(_)
raise NotImplementedError, <<-EOMSG.squish
Querying association '#{_reflection.name}' with '==' and '!='
is only supported for ActiveRecord 5.
EOMSG
raise AssociationComparisonNotSupportedError.new(_reflection.name)
end
end

Expand Down
16 changes: 16 additions & 0 deletions lib/baby_squeel/errors.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,20 @@ def initialize(association)
super format(MESSAGE, association: association)
end
end

class AssociationComparisonError < StandardError # :nodoc:
def initialize(name, other)
super "You can't compare association '#{name}' to #{other}."
end
end

class AssociationComparisonNotSupportedError < StandardError # :nodoc:
MESSAGE =
"Querying association '%{name}' with '==' and '!=' " \
"is only supported for ActiveRecord >=5."

def initialize(name)
super format(MESSAGE, name: name)
end
end
end
56 changes: 30 additions & 26 deletions spec/baby_squeel/association_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,40 +33,44 @@
end

describe '#==' do
before do
if ActiveRecord::VERSION::MAJOR < 5
skip "This isn't supported in ActiveRecord 4"
if ActiveRecord::VERSION::MAJOR >= 5
it 'returns an wrapped Arel::Nodes::And' do
node = association == Author.new(id: 42)
expect(node._arel.left).to be_an(Arel::Nodes::Equality)
end
end

it 'returns an wrapped Arel::Nodes::And' do
node = association == Author.new(id: 42)
expect(node._arel.left).to be_an(Arel::Nodes::Equality)
end

it 'throws for an invalid comparison' do
expect {
association == 'foo'
}.to raise_error(BabySqueel::Association::InvalidComparisonError)
it 'throws for an invalid comparison' do
expect {
association == 'foo'
}.to raise_error(BabySqueel::AssociationComparisonError)
end
else
it 'throws not supported' do
expect {
association == 'foo'
}.to raise_error(BabySqueel::AssociationComparisonNotSupportedError)
end
end
end

describe '#!=' do
before do
if ActiveRecord::VERSION::MAJOR < 5
skip "This isn't supported in ActiveRecord 4"
if ActiveRecord::VERSION::MAJOR >= 5
it 'returns some wrapped arel' do
node = association != Author.new(id: 42)
expect(node._arel.left.expr).to be_an(Arel::Nodes::NotEqual)
end
end

it 'returns some wrapped arel' do
node = association != Author.new(id: 42)
expect(node._arel.left.expr).to be_an(Arel::Nodes::NotEqual)
end

it 'throws for an invalid comparison' do
expect {
association != 'foo'
}.to raise_error(BabySqueel::Association::InvalidComparisonError)
it 'throws for an invalid comparison' do
expect {
association != 'foo'
}.to raise_error(BabySqueel::AssociationComparisonError)
end
else
it 'throws not supported' do
expect {
association != 'foo'
}.to raise_error(BabySqueel::AssociationComparisonNotSupportedError)
end
end
end

Expand Down

0 comments on commit 9de99e1

Please sign in to comment.