active_record_extended 2.0.0 → 2.0.3

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: 9934d5a90324b46dc3faa7b496724ac219a2582d264db89e64e2b9be574adab6
4
- data.tar.gz: c1359988f9a6b83148f36548e842a25cab68a191e7a839caef7e783b1415b346
3
+ metadata.gz: bbcdc3e208ac89fd2d55b8944ad7c0483e57f21a89941a4848beb548b56ce293
4
+ data.tar.gz: 10465bfe73686aae4fad33a7da0e533ed22401c68da5655654e1538a8bbb34c0
5
5
  SHA512:
6
- metadata.gz: e7385b414ef4c8d8b0c5b29eaeb70e77ba79f07828255deef5db79e99cf33f18de7bab696501711dee1f8e056ca3bdf628cdd498b341a91b5ad5cd2c766bd61c
7
- data.tar.gz: 10e8d4f65c570dba757b2cf47aaaff7f8a5538974e9c8aadb3c267e3e3fccb9508b05a8722eb5d6680937cb15827f6fa4f88d6dd219d8240c58323385fe44eb2
6
+ metadata.gz: 1ed8f4d6fe19c384bbb8add11443cc78b6f23cf4adc1808bdd0a1b6328a2dbeac22d4e0fa8fc6257dde505646d1c3613ed90711bbd36321cbb74231cf621958d
7
+ data.tar.gz: e139454fc996ad112d70332e39b002ba164d2172100f6b61f2494f090a0a3afe16c0e5e51998ab8f8cb8e839c6b0cb1104d152f821ae65d55bec4c83ec9c990a
@@ -18,8 +18,8 @@ require "active_record_extended/query_methods/inet"
18
18
  require "active_record_extended/query_methods/json"
19
19
  require "active_record_extended/query_methods/select"
20
20
 
21
- if ActiveRecord::VERSION::MAJOR == 5 && ActiveRecord::VERSION::MINOR == 1
21
+ if Gem::Requirement.new("~> 5.1.0").satisfied_by?(ActiveRecord.gem_version)
22
22
  require "active_record_extended/patch/5_1/where_clause"
23
- elsif ActiveRecord::VERSION::MAJOR >= 5
23
+ else
24
24
  require "active_record_extended/patch/5_2/where_clause"
25
25
  end
@@ -5,9 +5,11 @@ require "arel/nodes/function"
5
5
 
6
6
  module Arel
7
7
  module Nodes
8
+ if Gem::Requirement.new("< 6.1").satisfied_by?(ActiveRecord.gem_version)
9
+ ["Contains", "Overlaps"].each { |binary_node_name| const_set(binary_node_name, Class.new(::Arel::Nodes::Binary)) }
10
+ end
11
+
8
12
  [
9
- "Overlap",
10
- "Contains",
11
13
  "ContainsHStore",
12
14
  "ContainsArray",
13
15
  "ContainedInArray"
@@ -14,9 +14,10 @@ module Arel
14
14
  Arel::Nodes::Equality.new(Nodes.build_quoted(other, self), all_tags_function)
15
15
  end
16
16
 
17
- def overlap(other)
18
- Nodes::Overlap.new(self, Nodes.build_quoted(other, self))
17
+ def overlaps(other)
18
+ Nodes::Overlaps.new(self, Nodes.build_quoted(other, self))
19
19
  end
20
+ alias overlap overlaps
20
21
 
21
22
  def contains(other)
22
23
  Nodes::Contains.new self, Nodes.build_quoted(other, self)
@@ -9,7 +9,7 @@ module ActiveRecordExtended
9
9
 
10
10
  # rubocop:disable Naming/MethodName
11
11
 
12
- def visit_Arel_Nodes_Overlap(object, collector)
12
+ def visit_Arel_Nodes_Overlaps(object, collector)
13
13
  infix_value object, collector, " && "
14
14
  end
15
15
 
@@ -52,6 +52,7 @@ module ActiveRecordExtended
52
52
  def xor_field_options_for_associations(associations)
53
53
  associations.each_with_object({}) do |association_name, options|
54
54
  reflection = reflect_on_association(association_name)
55
+ reflection = reflection.through_reflection if reflection.through_reflection?
55
56
  options[reflection.table_name] = reflection.foreign_key
56
57
  end
57
58
  end
@@ -107,7 +107,7 @@ module ActiveRecordExtended
107
107
  end
108
108
 
109
109
  def union(opts = :chain, *args)
110
- return UnionChain.new(spawn) if opts == :chain
110
+ return UnionChain.new(spawn) if :chain == opts
111
111
 
112
112
  opts.nil? ? self : spawn.union!(opts, *args, chain_method: __callee__)
113
113
  end
@@ -121,7 +121,7 @@ module ActiveRecordExtended
121
121
  def union!(opts = :chain, *args, chain_method: :union)
122
122
  union_chain = UnionChain.new(self)
123
123
  chain_method ||= :union
124
- return union_chain if opts == :chain
124
+ return union_chain if :chain == opts
125
125
 
126
126
  union_chain.public_send(chain_method, *([opts] + args))
127
127
  end
@@ -5,9 +5,10 @@ module ActiveRecordExtended
5
5
  # Finds Records that have an array column that contain any a set of values
6
6
  # User.where.overlap(tags: [1,2])
7
7
  # # SELECT * FROM users WHERE tags && {1,2}
8
- def overlap(opts, *rest)
9
- substitute_comparisons(opts, rest, Arel::Nodes::Overlap, "overlap")
8
+ def overlaps(opts, *rest)
9
+ substitute_comparisons(opts, rest, Arel::Nodes::Overlaps, "overlap")
10
10
  end
11
+ alias overlap overlaps
11
12
 
12
13
  # Finds Records that contain an element in an array column
13
14
  # User.where.any(tags: 3)
@@ -113,14 +113,14 @@ module ActiveRecordExtended
113
113
 
114
114
  # @param [Hash, WithCTE] opts
115
115
  def with(opts = :chain, *rest)
116
- return WithChain.new(spawn) if opts == :chain
116
+ return WithChain.new(spawn) if :chain == opts
117
117
 
118
118
  opts.blank? ? self : spawn.with!(opts, *rest)
119
119
  end
120
120
 
121
121
  # @param [Hash, WithCTE] opts
122
122
  def with!(opts = :chain, *_rest)
123
- return WithChain.new(self) if opts == :chain
123
+ return WithChain.new(self) if :chain == opts
124
124
 
125
125
  tap do |scope|
126
126
  scope.cte ||= WithCTE.new(self)
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module ActiveRecordExtended
4
- VERSION = "2.0.0"
4
+ VERSION = "2.0.3"
5
5
  end
@@ -23,6 +23,17 @@ RSpec.describe "Active Record Either Methods" do
23
23
  expect(query).to_not include(three)
24
24
  end
25
25
  end
26
+
27
+ context "Through association .either_joins/2" do
28
+ let!(:four) { User.create! }
29
+ let!(:group) { Group.create!(users: [four]) }
30
+
31
+ it "Should only only return records that belong to profile L or has group" do
32
+ query = User.either_joins(:profile_l, :groups)
33
+ expect(query).to include(one, four)
34
+ expect(query).to_not include(three, two)
35
+ end
36
+ end
26
37
  end
27
38
 
28
39
  describe ".either_order/2" do
@@ -31,6 +31,22 @@ RSpec.describe "Either Methods SQL Queries" do
31
31
  query = User.either_join(:profile_l, :profile_r).to_sql
32
32
  expect(query).to include(where_join_case)
33
33
  end
34
+
35
+ context "Through association .either_joins/2" do
36
+ let!(:four) { User.create! }
37
+ let!(:group) { Group.create!(users: [four]) }
38
+ let(:where_join_through_case) do
39
+ "WHERE ((CASE WHEN profile_ls.user_id IS NULL"\
40
+ " THEN groups_users.user_id"\
41
+ " ELSE profile_ls.user_id END) "\
42
+ "= users.id)"
43
+ end
44
+
45
+ it "Should contain a case statement that will conditionally alternative between tables" do
46
+ query = User.either_join(:profile_l, :groups).to_sql
47
+ expect(query).to include(where_join_through_case)
48
+ end
49
+ end
34
50
  end
35
51
 
36
52
  describe ".either_order/2" do
@@ -5,6 +5,8 @@ class ApplicationRecord < ActiveRecord::Base
5
5
  end
6
6
 
7
7
  class User < ApplicationRecord
8
+ has_many :groups_users, class_name: "GroupsUser"
9
+ has_many :groups, through: :groups_users, dependent: :destroy
8
10
  has_many :hm_tags, class_name: "Tag"
9
11
  has_one :profile_l, class_name: "ProfileL"
10
12
  has_one :profile_r, class_name: "ProfileR"
@@ -66,3 +68,13 @@ class VersionControl < ApplicationRecord
66
68
  # t.jsonb :source, default: {}, null: false
67
69
  #
68
70
  end
71
+
72
+ class Group < ApplicationRecord
73
+ has_many :groups_users, class_name: "GroupsUser"
74
+ has_many :users, through: :groups_users, dependent: :destroy
75
+ end
76
+
77
+ class GroupsUser < ApplicationRecord
78
+ belongs_to :user
79
+ belongs_to :group
80
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: active_record_extended
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.0.0
4
+ version: 2.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - George Protacio-Karaszi
@@ -10,7 +10,7 @@ authors:
10
10
  autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
- date: 2020-12-22 00:00:00.000000000 Z
13
+ date: 2021-06-25 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: activerecord