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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: a871df479155905f1c6ea44b517cc09509073fd5f185d3d81385f6b523aa125e
4
- data.tar.gz: f30f5a2a4680eeb1d12affd531c8db957eddb2ae688bd8c2622efee9dba1da9b
3
+ metadata.gz: 37c2f6c3a915e35bc97e08e870940a07c867ce985dbe45e78b21797d40161acd
4
+ data.tar.gz: b230aac610a72310d56db8a074cb10e9c9fb19c99f3bd32a8b398b7d34473dd7
5
5
  SHA512:
6
- metadata.gz: f5ecb4f531ed0b073a0f894cf6ee1316c2fdaff34428f6949c85c20dc25f3aa59c212bac4be7b2e836b5e592fc373a1835466eec4f720de06e7432ec011a6973
7
- data.tar.gz: ba3978ceb24a0ca7f04028ceaeb11933949d19f823412389615b142b28d4334f72b8d310924f83e5e0a347fd2d14a08e2b1dd78c5a5457d7b28bf16cf042f009
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('total_orders = ?', 5)
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.
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module ArVirtualField
4
- VERSION = "0.8.0"
4
+ VERSION = "0.10.0"
5
5
  end
@@ -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.8.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-09-18 00:00:00.000000000 Z
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: 3.0.0
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.10
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