ar_virtual_field 0.4.0 → 0.6.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 96ceb9c69afbfc00eb75bc1691516f8ad7c3cb2aea3a4e51bd2fc9beac2bbd81
4
- data.tar.gz: d124facbe4f4ca52e3eb2272c057883e5805db4219466f34792347d36d99257e
3
+ metadata.gz: ffc1272f024fe7a86c06dbe98014598a79749c0e3f0b3f73aabc1abc93371e1a
4
+ data.tar.gz: 53662047fab832aafbcba2c18852bdea1938c27d79544a2346f0b242ef822cc0
5
5
  SHA512:
6
- metadata.gz: 11b6d4800d29f4ffbae7db65c65919d77aa55550ee05b29567aa4d8a58bc3da68bbb3f49d2b4d1f59f03a42979ad5b7693629c7f12902a78e5558a5ed4123eab
7
- data.tar.gz: 1e2f0e7fdb8e74a606ad6859ff8d3cd7703f94ea551a4f65ae80290e0185bc87dc17b1e59d2eee2298c560aeace5c1bf38053cd7b4a260dad6383c7b7173c113
6
+ metadata.gz: 195918203bffe10d882d03a950b8b89181b5cfdcebdbe700838883f006750c56fdd47c46b3ec184ea4a69155d70c0b2e49c367591e2027294bdaddfa856a9a78
7
+ data.tar.gz: 89de4af73dc65462a02fd4dc966fe518373c11b23b0dc2e21e6e1979932123868dcd65afbe55ea9d8cb434db6e85495444fe9b4490cfa9665411e26c857c06cd
data/README.md CHANGED
@@ -14,7 +14,7 @@ gem 'ar_virtual_field'
14
14
 
15
15
  ## Usage
16
16
 
17
- ### Defining a Virtual Field
17
+ ### Defining a virtual field
18
18
 
19
19
  To define a virtual field, use the virtual_field method in your model:
20
20
 
@@ -29,7 +29,7 @@ virtual_field :virtual_attribute,
29
29
  Parameters:
30
30
  - `name`: The name of the virtual field.
31
31
  - `scope`: A lambda defining a scope that fetches the virtual field value (optional).
32
- - `select`: SQL selection logic (can be a `string | arel_node` or a lambda returning an SQL `string | arel_node`) to define how the field is computed.
32
+ - `select`: SQL selection logic (can be a string or arel node or a lambda returning a string or arel_node) to define how the field is computed.
33
33
  - `get`: A method to retrieve the value of the virtual field when the field isn't fetched via SQL.
34
34
  - `default`: A default value for the virtual field if the result is nil (optional).
35
35
 
@@ -42,43 +42,32 @@ class User < ApplicationRecord
42
42
  select: -> { "COUNT(orders.id)" },
43
43
  get: -> { orders.count },
44
44
  default: 0
45
+
46
+ virtual_field :fullname,
47
+ select: -> { "name || surname" },
48
+ get: -> { "#{name}#{surname}" }
45
49
  end
46
- ```
47
50
 
48
- ### Using the scope in queries:
49
- ```ruby
50
- users_with_orders = User.with_total_orders
51
+ users_with_orders = User.with_total_orders # queries database once
52
+
53
+ user = users_with_orders.first
54
+ user.total_orders # => value computed by database
55
+ user.fullname # => value computed by database
51
56
  ```
52
57
 
53
- ### Scopes and Querying:
58
+ ### Scopes and querying:
54
59
 
55
60
  - `with_#{name}`: Automatically generated scope to include the virtual field in queries. You can use this scope in your ActiveRecord queries like so:
56
61
 
57
62
  ```ruby
58
- User.with_total_orders.where(total_orders: 5)
63
+ User.with_total_orders.where(ArVirtualField[:total_orders] => 5)
59
64
  ```
60
65
 
61
66
  This will include the total_orders virtual field in the SQL query and allow filtering by it.
62
67
 
63
- ### Also it can be used without scope:
64
-
65
- ```ruby
66
- class User < ApplicationRecord
67
- virtual_field :fullname,
68
- select: -> { "name || surname" },
69
- get: -> { "#{name}#{surname}" }
70
- end
71
- ```
72
-
73
- ## Development
74
-
75
- After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
76
-
77
- To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
78
-
79
68
  ## Contributing
80
69
 
81
- Bug reports and pull requests are welcome on GitHub at https://github.com//ar_virtual_field. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com//ar_virtual_field/blob/main/CODE_OF_CONDUCT.md).
70
+ Bug reports and pull requests are welcome on GitHub at https://github.com/emfy0/ar_virtual_field. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/emfy0/ar_virtual_field/blob/main/CODE_OF_CONDUCT.md).
82
71
 
83
72
  ## License
84
73
 
@@ -86,4 +75,4 @@ The gem is available as open source under the terms of the [MIT License](https:/
86
75
 
87
76
  ## Code of Conduct
88
77
 
89
- Everyone interacting in the ArVirtualField project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com//ar_virtual_field/blob/main/CODE_OF_CONDUCT.md).
78
+ Everyone interacting in the ArVirtualField project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/emfy0/ar_virtual_field/blob/main/CODE_OF_CONDUCT.md).
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module ArVirtualField
4
- VERSION = "0.4.0"
4
+ VERSION = "0.6.0"
5
5
  end
@@ -1,4 +1,6 @@
1
- # frozen_string_literal: true
1
+ # frozen_string_litera: true
2
+
3
+ require "active_record"
2
4
 
3
5
  module ArVirtualField
4
6
  module HelperMethods
@@ -9,6 +11,22 @@ module ArVirtualField
9
11
 
10
12
  relation.select(*values)
11
13
  end
14
+
15
+ def self.table_name(name)
16
+ sanitize_table_name("#{name}_outer")
17
+ end
18
+
19
+ def self.table_with_column(name)
20
+ sanitize_table_name("#{name}_outer.#{name}")
21
+ end
22
+
23
+ def self.sanitize_table_name(table)
24
+ ActiveRecord::Base.connection.quote_table_name(table)
25
+ end
26
+ end
27
+
28
+ def self.[](field)
29
+ Arel.sql(HelperMethods.table_with_column(field))
12
30
  end
13
31
 
14
32
  def virtual_field(name, scope: nil, select:, get:, default: nil)
@@ -31,9 +49,14 @@ module ArVirtualField
31
49
  scope(scope_name, scope)
32
50
 
33
51
  scope(:"with_#{name}", -> do
34
- scope_query = current_class.send(scope_name).select(select_lambda.().as(name), "#{table_name}.id")
35
- new_scope = joins("LEFT JOIN (#{scope_query.to_sql}) #{name}_outer ON #{name}_outer.id = #{table_name}.id")
36
- HelperMethods.select_append(new_scope, "#{name}_outer.#{name} AS #{name}")
52
+ scope_query = current_class
53
+ .send(scope_name)
54
+ .select(select_lambda.().as(name), "#{table_name}.id")
55
+
56
+ HelperMethods.select_append(joins(<<~SQL.squish), "#{HelperMethods.table_with_column(name)} AS #{name}")
57
+ LEFT JOIN (#{scope_query.to_sql}) #{HelperMethods.table_name(name)}
58
+ ON #{HelperMethods.table_name(name)}.id = #{table_name}.id
59
+ SQL
37
60
  end)
38
61
  else
39
62
  scope(:"with_#{name}", -> do
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ar_virtual_field
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.0
4
+ version: 0.6.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Pavel Egorov