Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[1] Wharel: exposed arel, block with argument #2

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion activerecord/lib/active_record/querying.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ module Querying
:destroy_all, :delete_all, :update_all, :touch_all, :destroy_by, :delete_by,
:find_each, :find_in_batches, :in_batches,
:select, :reselect, :order, :reorder, :group, :limit, :offset, :joins, :left_joins, :left_outer_joins,
:where, :rewhere, :invert_where, :preload, :extract_associated, :eager_load, :includes, :from, :lock, :readonly,
:where, :wharel, :rewhere, :invert_where, :preload, :extract_associated, :eager_load, :includes, :from, :lock, :readonly,
:and, :or, :annotate, :optimizer_hints, :extending,
:having, :create_with, :distinct, :references, :none, :unscope, :merge, :except, :only,
:count, :average, :minimum, :maximum, :sum, :calculate,
Expand Down
31 changes: 31 additions & 0 deletions activerecord/lib/active_record/relation/query_composer.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# frozen_string_literal: true

module ActiveRecord
class QueryComposer
def initialize(model)
@model = model
@arel_table = model.arel_table
@reflections = model._reflections
define_attribute_accessors
end

def method_missing(name, *_args)
if reflections.key?(name.to_s)
self.class.new(reflections[name.to_s].klass)
else
super
end
end

private
attr_reader :model, :arel_table, :reflections

def define_attribute_accessors
model.attribute_names.each do |attr|
define_singleton_method attr do
arel_table[attr]
end
end
end
end
end
6 changes: 6 additions & 0 deletions activerecord/lib/active_record/relation/query_methods.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

require "active_record/relation/from_clause"
require "active_record/relation/query_attribute"
require "active_record/relation/query_composer"
require "active_record/relation/where_clause"
require "active_model/forbidden_attributes_protection"
require "active_support/core_ext/array/wrap"
Expand Down Expand Up @@ -671,6 +672,11 @@ def where!(opts, *rest) # :nodoc:
self
end

def wharel(&block)
conditions = yield QueryComposer.new(self)
where(conditions)
end

# Allows you to change a previously set where condition for a given attribute, instead of appending to that condition.
#
# Post.where(trashed: true).where(trashed: false)
Expand Down
16 changes: 16 additions & 0 deletions activerecord/test/cases/relations_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,16 @@ def test_multivalue_where
assert_equal 1, posts.to_a.size
end

def test_wharel
posts = Post.wharel { |post| post.author_id.not_eq(1).and(post.id.gt(2)) }
assert_equal 6, posts.to_a.size
end

def test_wharel_can_chain
posts = Post.wharel { |post| post.author_id.gteq(1) }.wharel { |post| post.id.lteq(4) }
assert_equal 3, posts.to_a.size
end

def test_scoped
topics = Topic.all
assert_kind_of ActiveRecord::Relation, topics
Expand Down Expand Up @@ -514,6 +524,12 @@ def test_finding_with_hash_conditions_on_joined_table
assert_equal companies(:rails_core), firms.first
end

def tests_finding_with_block_on_joined_table
firms = DependentFirm.joins(:account).wharel { |firm| firm.name.eq("RailsCore").and(firm.accounts.credit_limit.in(55..60)) }.to_a
assert_equal 1, firms.size
assert_equal companies(:rails_core), firms.first
end

def test_find_all_with_join
developers_on_project_one = Developer.joins("LEFT JOIN developers_projects ON developers.id = developers_projects.developer_id").
where("project_id=1").to_a
Expand Down