ar_virtual_field 0.1.0 → 0.3.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: 4ca9b36a6e1b3b4432979509a61c611962b2a696ea0494bc27b856c57436fa29
4
- data.tar.gz: 85d952b60c94fbe1be24cefec6432364fac2f144f739040a92575eddfa7ee556
3
+ metadata.gz: 891a7d54ceef88352b9799d13a9fa27ee5a390e0ecacc634de0140e61565a648
4
+ data.tar.gz: 138333e6d5f189a1754ad35b50ac7283cabef173df02c37e2f1848bedf18d7c8
5
5
  SHA512:
6
- metadata.gz: d072a6c0ee93c4d07114bb8bc9066000c19655484c8a8379e44e2f6bf0819fadd2a5d960078c9241f5dcc72c0202accb250f31b331bf001251c1a89c3051a9b9
7
- data.tar.gz: fe351b6577315a727b74e6dbe8c6cf4f685ecf0785002b2fc1e067e1cac9c633e0b64654fe9f31142d7a7b220998b41cf6c95ca094a6939edca9188eb5588ea6
6
+ metadata.gz: c95f69519e4d0c4abaacfcb98c878fc7aedf964d7e9bae19e7054eb80a440284004b03251a002259ebcd7494cbe9dade1e16e9640cd99019a545c35b3882bb69
7
+ data.tar.gz: 7c424b0acc36049f65192e9ebf7ecaa3f7c62b76c88785494c92cefb5184bb36eeb7e9944d68b5d918e1ffd1e52b174d397f14ce8c82475f2fc4aea39c0d6595
data/README.md CHANGED
@@ -29,7 +29,7 @@ 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 or a lambda returning an SQL string) to define how the field is computed.
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
34
  - `default`: A default value for the virtual field if the result is nil.
35
35
 
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module ArVirtualField
4
- VERSION = "0.1.0"
4
+ VERSION = "0.3.0"
5
5
  end
@@ -11,7 +11,7 @@ module ArVirtualField
11
11
  end
12
12
  end
13
13
 
14
- def virtual_field(name, scope:, select: nil, get:, default:)
14
+ def virtual_field(name, scope: nil, select:, get:, default:)
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) }
@@ -31,15 +31,9 @@ module ArVirtualField
31
31
  scope(scope_name, scope)
32
32
 
33
33
  scope(:"with_#{name}", -> do
34
- scope_query = current_class.send(scope_name)
35
-
36
- if scope_query.group_values.present?
37
- scope_query = scope_query.reselect(select_lambda.().as(name), "#{table_name}.id")
38
- new_scope = joins("LEFT JOIN (#{scope_query.to_sql}) #{name}_outer ON #{name}_outer.id = #{table_name}.id")
39
- HelperMethods.select_append(new_scope, "#{name}_outer.#{name} AS #{name}")
40
- else
41
- HelperMethods.select_append(send(scope_name), select_lambda.().as(name))
42
- end
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}")
43
37
  end)
44
38
  else
45
39
  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.1.0
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Pavel Egorov