ar_virtual_field 0.8.0 → 0.10.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 +1 -1
- data/lib/ar_virtual_field/version.rb +1 -1
- data/lib/ar_virtual_field.rb +18 -0
- metadata +4 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 37c2f6c3a915e35bc97e08e870940a07c867ce985dbe45e78b21797d40161acd
|
4
|
+
data.tar.gz: b230aac610a72310d56db8a074cb10e9c9fb19c99f3bd32a8b398b7d34473dd7
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 66a22de0b4143d35f1edd9d9ade22e29560fb4a33b50f235c320f3e2459bbf639100006797cc0b134478431b0513a054b7e1bb2d5a253ea95629db3d3f266831
|
7
|
+
data.tar.gz: c26fdb44be918bc59a50ba08786f180dd692171fa5a1fa16c078052b02f2ea16be60ff7c93e340276188ab3aff07c56e90e810a20c151adb0411bf890057e309
|
data/README.md
CHANGED
@@ -60,7 +60,7 @@ user.fullname # => value computed by ruby
|
|
60
60
|
- `with_#{name}`: Automatically generated scope to include the virtual field in queries. You can use this scope in your ActiveRecord queries like so:
|
61
61
|
|
62
62
|
```ruby
|
63
|
-
User.with_total_orders.where(
|
63
|
+
User.with_total_orders.where(User.virtual_fields[:total_orders].eq(5))
|
64
64
|
```
|
65
65
|
|
66
66
|
This will include the total_orders virtual field in the SQL query and allow filtering by it.
|
data/lib/ar_virtual_field.rb
CHANGED
@@ -21,7 +21,21 @@ module ArVirtualField
|
|
21
21
|
end
|
22
22
|
end
|
23
23
|
|
24
|
+
class FieldsData < Hash
|
25
|
+
def [](field)
|
26
|
+
statement = super(field.to_sym)
|
27
|
+
raise "Unknown virtual field `#{field}`" unless statement
|
28
|
+
statement.()
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
def virtual_fields
|
33
|
+
@ar_virtual_fields || FieldsData.new
|
34
|
+
end
|
35
|
+
|
24
36
|
def virtual_field(name, scope: nil, select:, get:, default: nil)
|
37
|
+
@ar_virtual_fields ||= FieldsData.new
|
38
|
+
|
25
39
|
name = name.to_s
|
26
40
|
current_class = self
|
27
41
|
unwrap_arel_expression = -> (exp) { exp.is_a?(Arel::Nodes::NodeExpression) ? exp : Arel.sql(exp) }
|
@@ -36,6 +50,8 @@ module ArVirtualField
|
|
36
50
|
end
|
37
51
|
|
38
52
|
if scope
|
53
|
+
@ar_virtual_fields[name.to_sym] = -> { Arel.sql(HelperMethods.table_with_column(name)) }
|
54
|
+
|
39
55
|
scope_name = :"_scope_#{name}"
|
40
56
|
|
41
57
|
scope(scope_name, scope)
|
@@ -55,6 +71,8 @@ module ArVirtualField
|
|
55
71
|
SQL
|
56
72
|
end)
|
57
73
|
else
|
74
|
+
@ar_virtual_fields[name.to_sym] = -> { select_lambda.() }
|
75
|
+
|
58
76
|
scope(:"with_#{name}", -> do
|
59
77
|
HelperMethods.select_append(self, select_lambda.().as(name))
|
60
78
|
end)
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: ar_virtual_field
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.10.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Pavel Egorov
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2024-
|
11
|
+
date: 2024-12-11 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activerecord
|
@@ -77,14 +77,14 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
77
77
|
requirements:
|
78
78
|
- - ">="
|
79
79
|
- !ruby/object:Gem::Version
|
80
|
-
version:
|
80
|
+
version: 2.7.0
|
81
81
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
82
82
|
requirements:
|
83
83
|
- - ">="
|
84
84
|
- !ruby/object:Gem::Version
|
85
85
|
version: '0'
|
86
86
|
requirements: []
|
87
|
-
rubygems_version: 3.4.
|
87
|
+
rubygems_version: 3.4.19
|
88
88
|
signing_key:
|
89
89
|
specification_version: 4
|
90
90
|
summary: Provide an easy mechanism to define virtual fields within an ActiveRecord
|