Skip to content

Commit

Permalink
Allow boolean attributes to be referenced without operator in where.has
Browse files Browse the repository at this point in the history
To explicitly allow all of:
```ruby
Post.where.has { published }
Post.where.has { published & (view_count >= 1) }
Post.where.has { (view_count >= 1) & published }
```

Fixes rzane#129
  • Loading branch information
owst committed Jan 8, 2024
1 parent c8f77f9 commit 5844bb4
Show file tree
Hide file tree
Showing 5 changed files with 87 additions and 3 deletions.
8 changes: 7 additions & 1 deletion lib/baby_squeel/active_record/where_chain.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,13 @@ module WhereChain
# Constructs Arel for ActiveRecord::Base#where using the DSL.
def has(&block)
arel = DSL.evaluate(@scope, &block)
@scope.where!(arel) unless arel.blank?

unless arel.blank?
arel = Operators::Grouping.coerce_boolean_attribute("where.has", arel)

@scope.where!(arel)
end

@scope
end
end
Expand Down
8 changes: 8 additions & 0 deletions lib/baby_squeel/nodes/attribute.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,14 @@ def initialize(parent, name)
super(parent._table[@name])
end

def &(other)
Operators::Grouping.coerce_boolean_attribute(:&, self).and(other)
end

def |(other)
Operators::Grouping.coerce_boolean_attribute(:|, self).or(other)
end

def in(rel)
if rel.is_a? ::ActiveRecord::Relation
Nodes.wrap ::Arel::Nodes::In.new(self, sanitize_relation(rel))
Expand Down
20 changes: 18 additions & 2 deletions lib/baby_squeel/operators.rb
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,24 @@ def op(operator, other)

module Grouping
extend ArelAliasing
arel_alias :&, :and
arel_alias :|, :or

def self.coerce_boolean_attribute(op, node)
return node unless node.is_a?(Arel::Attributes::Attribute)

unless node.type_caster.type == :boolean
raise ArgumentError, "non-boolean attribute #{node.name} passed to #{op}"
end

Arel::Nodes::Equality.new(node, Arel::Nodes::True.new)
end

def &(other)
self.and(Grouping.coerce_boolean_attribute(:&, other))
end

def |(other)
self.or(Grouping.coerce_boolean_attribute(:|, other))
end
end

module Matching
Expand Down
12 changes: 12 additions & 0 deletions spec/integration/__snapshots__/where_chain_spec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -61,3 +61,15 @@
"posts"."author_id" = 42
"#where.has wheres an association using #!= 1": SELECT "posts".* FROM "posts" WHERE
"posts"."author_id" != 42
"#where.has when using a boolean column coerces a plain boolean column reference to equality at the top-level 1": SELECT
"authors".* FROM "authors" WHERE "authors"."ugly" = 1
"#where.has when using a boolean column coerces a plain boolean column reference to equality on the LHS of an AND 1": SELECT
"authors".* FROM "authors" WHERE "authors"."ugly" = 1 AND "authors"."id" = 1
"#where.has when using a boolean column coerces a plain boolean column reference to equality on the RHS of an AND 1": SELECT
"authors".* FROM "authors" WHERE "authors"."id" = 1 AND "authors"."ugly" = 1
"#where.has when using a boolean column coerces a negated plain boolean column reference to equality at the top-level 1": SELECT
"authors".* FROM "authors"
"#where.has when using a boolean column coerces a plain boolean column reference to equality on the LHS of an OR 1": SELECT
"authors".* FROM "authors" WHERE ("authors"."ugly" = 1 OR "authors"."id" = 1)
"#where.has when using a boolean column coerces a plain boolean column reference to equality on the RHS of an OR 1": SELECT
"authors".* FROM "authors" WHERE ("authors"."id" = 1 OR "authors"."ugly" = 1)
42 changes: 42 additions & 0 deletions spec/integration/where_chain_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,48 @@

expect(bs.to_sql).to eq(ar.to_sql)
end

context 'when using a boolean column' do
it 'coerces a plain boolean column reference to equality at the top-level' do
expect(Author.where.has { ugly }).to match_sql_snapshot
end

it 'raises with a plain non-boolean column reference at the top-level' do
expect { Author.where.has { id } }.to raise_error(ArgumentError)
end

it 'coerces a plain boolean column reference to equality on the LHS of an AND' do
expect(Author.where.has { ugly & (id == 1) }).to match_sql_snapshot
end

it 'raises with a plain non-boolean column reference on the LHS of an AND' do
expect { Author.where.has { id & (id == 1)} }.to raise_error(ArgumentError)
end

it 'coerces a plain boolean column reference to equality on the RHS of an AND' do
expect(Author.where.has { (id == 1) & ugly }).to match_sql_snapshot
end

it 'raises with a plain non-boolean column reference on the RHS of an AND' do
expect { Author.where.has { (id == 1) & id } }.to raise_error(ArgumentError)
end

it 'coerces a plain boolean column reference to equality on the LHS of an OR' do
expect(Author.where.has { ugly | (id == 1) }).to match_sql_snapshot
end

it 'raises with a plain non-boolean column reference on the LHS of an OR' do
expect { Author.where.has { id | (id == 1)} }.to raise_error(ArgumentError)
end

it 'coerces a plain boolean column reference to equality on the RHS of an OR' do
expect(Author.where.has { (id == 1) | ugly }).to match_sql_snapshot
end

it 'raises with a plain non-boolean column reference on the RHS of an OR' do
expect { Author.where.has { (id == 1) | id } }.to raise_error(ArgumentError)
end
end
end

describe '#where_values_hash' do
Expand Down

0 comments on commit 5844bb4

Please sign in to comment.