ar_virtual_field 0.3.0 → 0.5.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 +4 -4
- data/README.md +16 -17
- data/lib/ar_virtual_field/version.rb +1 -1
- data/lib/ar_virtual_field.rb +24 -5
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: ef8b11855497a6f4ed5be9d1717453133c52d9d3691000c29b83d5f3561670ec
|
4
|
+
data.tar.gz: f207d7151c03885d2698b9bf6bc91c166dee17aec39b7d2d8376d74dee5b824f
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
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
|
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
|
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
|
-
|
49
|
-
|
50
|
-
|
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
|
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
|
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
|
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
|
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).
|
data/lib/ar_virtual_field.rb
CHANGED
@@ -1,4 +1,6 @@
|
|
1
|
-
#
|
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
|
35
|
-
|
36
|
-
|
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
|