ar_virtual_field 0.3.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: 891a7d54ceef88352b9799d13a9fa27ee5a390e0ecacc634de0140e61565a648
4
- data.tar.gz: 138333e6d5f189a1754ad35b50ac7283cabef173df02c37e2f1848bedf18d7c8
3
+ metadata.gz: ef8b11855497a6f4ed5be9d1717453133c52d9d3691000c29b83d5f3561670ec
4
+ data.tar.gz: f207d7151c03885d2698b9bf6bc91c166dee17aec39b7d2d8376d74dee5b824f
5
5
  SHA512:
6
- metadata.gz: c95f69519e4d0c4abaacfcb98c878fc7aedf964d7e9bae19e7054eb80a440284004b03251a002259ebcd7494cbe9dade1e16e9640cd99019a545c35b3882bb69
7
- data.tar.gz: 7c424b0acc36049f65192e9ebf7ecaa3f7c62b76c88785494c92cefb5184bb36eeb7e9944d68b5d918e1ffd1e52b174d397f14ce8c82475f2fc4aea39c0d6595
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,9 +29,9 @@ 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
- - `default`: A default value for the virtual field if the result is nil.
34
+ - `default`: A default value for the virtual field if the result is nil (optional).
35
35
 
36
36
  Example:
37
37
 
@@ -42,33 +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
- ## Development
64
-
65
- 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.
66
-
67
- 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).
68
-
69
68
  ## Contributing
70
69
 
71
- 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).
72
71
 
73
72
  ## License
74
73
 
@@ -76,4 +75,4 @@ The gem is available as open source under the terms of the [MIT License](https:/
76
75
 
77
76
  ## Code of Conduct
78
77
 
79
- 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.3.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,9 +11,21 @@ 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
- def virtual_field(name, scope: nil, select:, get:, default:)
28
+ def virtual_field(name, scope: nil, select:, get:, default: nil)
15
29
  name = name.to_s
16
30
  current_class = self
17
31
  unwrap_arel_expression = -> (exp) { exp.is_a?(Arel::Nodes::NodeExpression) ? exp : Arel.sql(exp) }
@@ -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.3.0
4
+ version: 0.5.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Pavel Egorov