Skip to content

Commit

Permalink
bump version
Browse files Browse the repository at this point in the history
  • Loading branch information
emfy0 committed Sep 17, 2024
1 parent cf3ed1f commit fc61c38
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 6 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ user.fullname # => value computed by database
- `with_#{name}`: Automatically generated scope to include the virtual field in queries. You can use this scope in your ActiveRecord queries like so:

```ruby
User.with_total_orders.where(total_orders: 5)
User.with_total_orders.where(ArVirtualField[:total_orders] => 5)
```

This will include the total_orders virtual field in the SQL query and allow filtering by it.
Expand Down
Binary file modified ar_virtual_field.gem
Binary file not shown.
27 changes: 23 additions & 4 deletions lib/ar_virtual_field.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
# frozen_string_literal: true
# frozen_string_litera: true

require "active_record"

module ArVirtualField
module HelperMethods
Expand All @@ -9,6 +11,18 @@ def self.select_append(relation, *values)

relation.select(*values)
end

def self.table_name(name)
"#{name}_outer"
end

def self.table_with_column(name)
"#{name}_outer.#{name}"
end
end

def self.[](field)
Arel.sql(HelperMethods.table_with_column(field))
end

def virtual_field(name, scope: nil, select:, get:, default: nil)
Expand All @@ -31,9 +45,14 @@ def virtual_field(name, scope: nil, select:, get:, default: nil)
scope(scope_name, scope)

scope(:"with_#{name}", -> do
scope_query = current_class.send(scope_name).select(select_lambda.().as(name), "#{table_name}.id")
new_scope = joins("LEFT JOIN (#{scope_query.to_sql}) #{name}_outer ON #{name}_outer.id = #{table_name}.id")
HelperMethods.select_append(new_scope, "#{name}_outer.#{name} AS #{name}")
scope_query = current_class
.send(scope_name)
.select(select_lambda.().as(name), "#{table_name}.id")

HelperMethods.select_append(joins(<<~SQL.squish), "#{HelperMethods.table_with_column(name)} AS #{name}")
LEFT JOIN (#{scope_query.to_sql}) #{HelperMethods.table_name(name)}
ON #{HelperMethods.table_name(name)}.id = #{table_name}.id
SQL
end)
else
scope(:"with_#{name}", -> do
Expand Down
2 changes: 1 addition & 1 deletion lib/ar_virtual_field/version.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# frozen_string_literal: true

module ArVirtualField
VERSION = "0.4.0"
VERSION = "0.5.0"
end

0 comments on commit fc61c38

Please sign in to comment.