ar_virtual_field 0.4.0 → 0.5.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 96ceb9c69afbfc00eb75bc1691516f8ad7c3cb2aea3a4e51bd2fc9beac2bbd81
4
- data.tar.gz: d124facbe4f4ca52e3eb2272c057883e5805db4219466f34792347d36d99257e
3
+ metadata.gz: ef8b11855497a6f4ed5be9d1717453133c52d9d3691000c29b83d5f3561670ec
4
+ data.tar.gz: f207d7151c03885d2698b9bf6bc91c166dee17aec39b7d2d8376d74dee5b824f
5
5
  SHA512:
6
- metadata.gz: 11b6d4800d29f4ffbae7db65c65919d77aa55550ee05b29567aa4d8a58bc3da68bbb3f49d2b4d1f59f03a42979ad5b7693629c7f12902a78e5558a5ed4123eab
7
- data.tar.gz: 1e2f0e7fdb8e74a606ad6859ff8d3cd7703f94ea551a4f65ae80290e0185bc87dc17b1e59d2eee2298c560aeace5c1bf38053cd7b4a260dad6383c7b7173c113
6
+ metadata.gz: 7aae58b16112adb85dfa95874b1a794049e86a1ac5436a40278825e285bd5004fd7ff4c4ad3c6457a4bc791878c5bea1d4ed223e1de8d41aa719a0d0562228ef
7
+ data.tar.gz: ef0031c1df9b311373af9a12a4dfab15e4628ad87b79dfed6214a4cc5e94e0c72b4a0453cd930a43a2ac7b214721a489d6c86cf66a9bd41db915464b29f1207d
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.5.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,18 @@ module ArVirtualField
9
11
 
10
12
  relation.select(*values)
11
13
  end
14
+
15
+ def self.table_name(name)
16
+ "#{name}_outer"
17
+ end
18
+
19
+ def self.table_with_column(name)
20
+ "#{name}_outer.#{name}"
21
+ end
22
+ end
23
+
24
+ def self.[](field)
25
+ Arel.sql(HelperMethods.table_with_column(field))
12
26
  end
13
27
 
14
28
  def virtual_field(name, scope: nil, select:, get:, default: nil)
@@ -31,9 +45,14 @@ module ArVirtualField
31
45
  scope(scope_name, scope)
32
46
 
33
47
  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}")
48
+ scope_query = current_class
49
+ .send(scope_name)
50
+ .select(select_lambda.().as(name), "#{table_name}.id")
51
+
52
+ HelperMethods.select_append(joins(<<~SQL.squish), "#{HelperMethods.table_with_column(name)} AS #{name}")
53
+ LEFT JOIN (#{scope_query.to_sql}) #{HelperMethods.table_name(name)}
54
+ ON #{HelperMethods.table_name(name)}.id = #{table_name}.id
55
+ SQL
37
56
  end)
38
57
  else
39
58
  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.5.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Pavel Egorov