ar_virtual_field 0.3.0 → 0.4.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +11 -1
- data/lib/ar_virtual_field/version.rb +1 -1
- data/lib/ar_virtual_field.rb +1 -1
- 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: 96ceb9c69afbfc00eb75bc1691516f8ad7c3cb2aea3a4e51bd2fc9beac2bbd81
|
4
|
+
data.tar.gz: d124facbe4f4ca52e3eb2272c057883e5805db4219466f34792347d36d99257e
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 11b6d4800d29f4ffbae7db65c65919d77aa55550ee05b29567aa4d8a58bc3da68bbb3f49d2b4d1f59f03a42979ad5b7693629c7f12902a78e5558a5ed4123eab
|
7
|
+
data.tar.gz: 1e2f0e7fdb8e74a606ad6859ff8d3cd7703f94ea551a4f65ae80290e0185bc87dc17b1e59d2eee2298c560aeace5c1bf38053cd7b4a260dad6383c7b7173c113
|
data/README.md
CHANGED
@@ -31,7 +31,7 @@ Parameters:
|
|
31
31
|
- `scope`: A lambda defining a scope that fetches the virtual field value (optional).
|
32
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.
|
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
|
|
@@ -60,6 +60,16 @@ User.with_total_orders.where(total_orders: 5)
|
|
60
60
|
|
61
61
|
This will include the total_orders virtual field in the SQL query and allow filtering by it.
|
62
62
|
|
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
|
+
|
63
73
|
## Development
|
64
74
|
|
65
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.
|
data/lib/ar_virtual_field.rb
CHANGED
@@ -11,7 +11,7 @@ module ArVirtualField
|
|
11
11
|
end
|
12
12
|
end
|
13
13
|
|
14
|
-
def virtual_field(name, scope: nil, select:, get:, default:)
|
14
|
+
def virtual_field(name, scope: nil, select:, get:, default: nil)
|
15
15
|
name = name.to_s
|
16
16
|
current_class = self
|
17
17
|
unwrap_arel_expression = -> (exp) { exp.is_a?(Arel::Nodes::NodeExpression) ? exp : Arel.sql(exp) }
|