active_record_extended 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (43) hide show
  1. checksums.yaml +7 -0
  2. data/.codeclimate.yml +15 -0
  3. data/.gitignore +18 -0
  4. data/.rspec +3 -0
  5. data/.rubocop.yml +85 -0
  6. data/.ruby-gemset +1 -0
  7. data/.ruby-version +1 -0
  8. data/.travis.yml +35 -0
  9. data/CODE_OF_CONDUCT.md +74 -0
  10. data/Gemfile +17 -0
  11. data/Gemfile.lock +92 -0
  12. data/LICENSE.txt +21 -0
  13. data/README.md +45 -0
  14. data/Rakefile +102 -0
  15. data/active_record_extended.gemspec +32 -0
  16. data/bin/console +15 -0
  17. data/bin/setup +8 -0
  18. data/gemfiles/activerecord-51.gemfile +8 -0
  19. data/gemfiles/activerecord-52+.gemfile +8 -0
  20. data/gemfiles/activerecord-52.gemfile +8 -0
  21. data/lib/active_record_extended/active_record.rb +15 -0
  22. data/lib/active_record_extended/arel/nodes.rb +61 -0
  23. data/lib/active_record_extended/arel/predications.rb +41 -0
  24. data/lib/active_record_extended/arel/visitors/postgresql_decorator.rb +73 -0
  25. data/lib/active_record_extended/arel.rb +6 -0
  26. data/lib/active_record_extended/patch/5_1/where_clause.rb +11 -0
  27. data/lib/active_record_extended/patch/5_2/where_clause.rb +11 -0
  28. data/lib/active_record_extended/predicate_builder/array_handler_decorator.rb +20 -0
  29. data/lib/active_record_extended/query_methods/either.rb +60 -0
  30. data/lib/active_record_extended/query_methods/where_chain.rb +106 -0
  31. data/lib/active_record_extended/version.rb +5 -0
  32. data/lib/active_record_extended.rb +9 -0
  33. data/spec/active_record_extended_spec.rb +7 -0
  34. data/spec/query_methods/array_query_spec.rb +64 -0
  35. data/spec/query_methods/either_spec.rb +36 -0
  36. data/spec/query_methods/hash_query_spec.rb +45 -0
  37. data/spec/spec_helper.rb +28 -0
  38. data/spec/sql_inspections/arel/array_spec.rb +63 -0
  39. data/spec/sql_inspections/contains_sql_queries_spec.rb +47 -0
  40. data/spec/sql_inspections/either_sql_spec.rb +55 -0
  41. data/spec/support/database_cleaner.rb +15 -0
  42. data/spec/support/models.rb +20 -0
  43. metadata +203 -0
@@ -0,0 +1,47 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "spec_helper"
4
+
5
+ RSpec.describe "Contains SQL Queries" do
6
+ let(:contains_array_regex) { /\"people\"\.\"tag_ids\" @> '\{1,2\}'/ }
7
+ let(:contains_hstore_regex) { /\"people\"\.\"data\" @> '\"nickname\"=>"Dan"'/ }
8
+ let(:contains_jsonb_regex) { /\"people\"\.\"jsonb_data\" @> '\{"nickname\":\"Dan"}'/ }
9
+ let(:contained_in_array_regex) { /\"people\"\.\"tag_ids\" <@ '\{1,2\}'/ }
10
+ let(:contained_in_hstore_regex) { /\"people\"\.\"data\" <@ '\"nickname\"=>"Dan"'/ }
11
+ let(:contained_in_jsonb_regex) { /\"people\"\.\"jsonb_data\" <@ '\{"nickname\":\"Dan"}'/ }
12
+ let(:contains_equals_regex) { /\"people\"\.\"ip\" >>= '127.0.0.1'/ }
13
+ let(:equality_regex) { /\"people\"\.\"tags\" = '\{"?working"?\}'/ }
14
+
15
+ describe ".where.contains(:column => value)" do
16
+ it "generates the appropriate where clause for array columns" do
17
+ query = Person.where.contains(tag_ids: [1, 2]).to_sql
18
+ expect(query).to match_regex(contains_array_regex)
19
+ end
20
+
21
+ it "generates the appropriate where clause for hstore columns" do
22
+ query = Person.where.contains(data: { nickname: "Dan" }).to_sql
23
+ expect(query).to match_regex(contains_hstore_regex)
24
+ end
25
+
26
+ it "generates the appropriate where clause for jsonb columns" do
27
+ query = Person.where.contains(jsonb_data: { nickname: "Dan" }).to_sql
28
+ expect(query).to match_regex(contains_jsonb_regex)
29
+ end
30
+
31
+ it "generates the appropriate where clause for hstore columns on joins" do
32
+ query = Tag.joins(:person).where.contains(people: { data: { nickname: "Dan" } }).to_sql
33
+ expect(query).to match_regex(contains_hstore_regex)
34
+ end
35
+
36
+ it "allows chaining" do
37
+ query = Person.where.contains(tag_ids: [1, 2]).where(tags: ["working"]).to_sql
38
+ expect(query).to match_regex(contains_array_regex)
39
+ expect(query).to match_regex(equality_regex)
40
+ end
41
+
42
+ it "generates the appropriate where clause for array columns on joins" do
43
+ query = Tag.joins(:person).where.contains(people: { tag_ids: [1, 2] }).to_sql
44
+ expect(query).to match_regex(contains_array_regex)
45
+ end
46
+ end
47
+ end
@@ -0,0 +1,55 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "spec_helper"
4
+
5
+ RSpec.describe "Either Methods SQL Queries" do
6
+ let(:contains_array_regex) { /\"people\"\.\"tag_ids\" @> '\{1,2\}'/ }
7
+ let(:profile_l_outer_join) { /LEFT OUTER JOIN \"profile_ls\" ON \"profile_ls\".\"person_id\" = \"people\".\"id\"/ }
8
+ let(:profile_r_outer_join) { /LEFT OUTER JOIN \"profile_rs\" ON \"profile_rs\".\"person_id\" = \"people\".\"id\"/ }
9
+ let(:where_join_case) do
10
+ "WHERE ((CASE WHEN profile_ls.person_id IS NULL"\
11
+ " THEN profile_rs.person_id"\
12
+ " ELSE profile_ls.person_id END) "\
13
+ "= people.id)"
14
+ end
15
+
16
+ let(:order_case) do
17
+ "ORDER BY "\
18
+ "(CASE WHEN profile_ls.likes IS NULL"\
19
+ " THEN profile_rs.dislikes"\
20
+ " ELSE profile_ls.likes END)"
21
+ end
22
+
23
+ describe ".either_join/2" do
24
+ it "Should contain outer joins on the provided relationships" do
25
+ query = Person.either_join(:profile_l, :profile_r).to_sql
26
+ expect(query).to match_regex(profile_l_outer_join)
27
+ expect(query).to match_regex(profile_r_outer_join)
28
+ end
29
+
30
+ it "Should contain a case statement that will conditionally alternative between tables" do
31
+ query = Person.either_join(:profile_l, :profile_r).to_sql
32
+ expect(query).to include(where_join_case)
33
+ end
34
+ end
35
+
36
+ describe ".either_order/2" do
37
+ let(:ascended_order) { Person.either_order(:asc, profile_l: :likes, profile_r: :dislikes).to_sql }
38
+ let(:descended_order) { Person.either_order(:desc, profile_l: :likes, profile_r: :dislikes).to_sql }
39
+
40
+ it "Should contain outer joins on the provided relationships" do
41
+ expect(ascended_order).to match_regex(profile_l_outer_join)
42
+ expect(ascended_order).to match_regex(profile_r_outer_join)
43
+ expect(descended_order).to match_regex(profile_l_outer_join)
44
+ expect(descended_order).to match_regex(profile_r_outer_join)
45
+ end
46
+
47
+ it "Should contain a relational ordering case statement for a relations column" do
48
+ expect(ascended_order).to include(order_case)
49
+ expect(ascended_order).to end_with("asc")
50
+
51
+ expect(descended_order).to include(order_case)
52
+ expect(descended_order).to end_with("desc")
53
+ end
54
+ end
55
+ end
@@ -0,0 +1,15 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "database_cleaner"
4
+
5
+ DatabaseCleaner.strategy = :truncation
6
+
7
+ RSpec.configure do |config|
8
+ config.before do
9
+ DatabaseCleaner.start
10
+ end
11
+
12
+ config.after do
13
+ DatabaseCleaner.clean
14
+ end
15
+ end
@@ -0,0 +1,20 @@
1
+
2
+ # frozen_string_literal: true
3
+
4
+ class Person < ActiveRecord::Base
5
+ has_many :hm_tags, class_name: "Tag"
6
+ has_one :profile_l, class_name: "ProfileL"
7
+ has_one :profile_r, class_name: "ProfileR"
8
+ end
9
+
10
+ class Tag < ActiveRecord::Base
11
+ belongs_to :person
12
+ end
13
+
14
+ class ProfileL < ActiveRecord::Base
15
+ belongs_to :person
16
+ end
17
+
18
+ class ProfileR < ActiveRecord::Base
19
+ belongs_to :person
20
+ end
metadata ADDED
@@ -0,0 +1,203 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: active_record_extended
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.1
5
+ platform: ruby
6
+ authors:
7
+ - George Protacio-Karaszi
8
+ - Dan McClain
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2018-05-05 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: activerecord
16
+ requirement: !ruby/object:Gem::Requirement
17
+ requirements:
18
+ - - ">="
19
+ - !ruby/object:Gem::Version
20
+ version: '5.1'
21
+ - - "<"
22
+ - !ruby/object:Gem::Version
23
+ version: '6.0'
24
+ type: :runtime
25
+ prerelease: false
26
+ version_requirements: !ruby/object:Gem::Requirement
27
+ requirements:
28
+ - - ">="
29
+ - !ruby/object:Gem::Version
30
+ version: '5.1'
31
+ - - "<"
32
+ - !ruby/object:Gem::Version
33
+ version: '6.0'
34
+ - !ruby/object:Gem::Dependency
35
+ name: ar_outer_joins
36
+ requirement: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '0.2'
41
+ type: :runtime
42
+ prerelease: false
43
+ version_requirements: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '0.2'
48
+ - !ruby/object:Gem::Dependency
49
+ name: pg
50
+ requirement: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '0.18'
55
+ type: :runtime
56
+ prerelease: false
57
+ version_requirements: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '0.18'
62
+ - !ruby/object:Gem::Dependency
63
+ name: bundler
64
+ requirement: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '1.16'
69
+ type: :development
70
+ prerelease: false
71
+ version_requirements: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '1.16'
76
+ - !ruby/object:Gem::Dependency
77
+ name: database_cleaner
78
+ requirement: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '1.6'
83
+ type: :development
84
+ prerelease: false
85
+ version_requirements: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: '1.6'
90
+ - !ruby/object:Gem::Dependency
91
+ name: rake
92
+ requirement: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: '10.0'
97
+ type: :development
98
+ prerelease: false
99
+ version_requirements: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - "~>"
102
+ - !ruby/object:Gem::Version
103
+ version: '10.0'
104
+ - !ruby/object:Gem::Dependency
105
+ name: rspec
106
+ requirement: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - "~>"
109
+ - !ruby/object:Gem::Version
110
+ version: '3.0'
111
+ type: :development
112
+ prerelease: false
113
+ version_requirements: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - "~>"
116
+ - !ruby/object:Gem::Version
117
+ version: '3.0'
118
+ description: Adds extended functionality to Activerecord Postgres implementation
119
+ email:
120
+ - georgekaraszi@gmail.com
121
+ - git@danmcclain.net
122
+ executables:
123
+ - console
124
+ - setup
125
+ extensions: []
126
+ extra_rdoc_files: []
127
+ files:
128
+ - ".codeclimate.yml"
129
+ - ".gitignore"
130
+ - ".rspec"
131
+ - ".rubocop.yml"
132
+ - ".ruby-gemset"
133
+ - ".ruby-version"
134
+ - ".travis.yml"
135
+ - CODE_OF_CONDUCT.md
136
+ - Gemfile
137
+ - Gemfile.lock
138
+ - LICENSE.txt
139
+ - README.md
140
+ - Rakefile
141
+ - active_record_extended.gemspec
142
+ - bin/console
143
+ - bin/setup
144
+ - gemfiles/activerecord-51.gemfile
145
+ - gemfiles/activerecord-52+.gemfile
146
+ - gemfiles/activerecord-52.gemfile
147
+ - lib/active_record_extended.rb
148
+ - lib/active_record_extended/active_record.rb
149
+ - lib/active_record_extended/arel.rb
150
+ - lib/active_record_extended/arel/nodes.rb
151
+ - lib/active_record_extended/arel/predications.rb
152
+ - lib/active_record_extended/arel/visitors/postgresql_decorator.rb
153
+ - lib/active_record_extended/patch/5_1/where_clause.rb
154
+ - lib/active_record_extended/patch/5_2/where_clause.rb
155
+ - lib/active_record_extended/predicate_builder/array_handler_decorator.rb
156
+ - lib/active_record_extended/query_methods/either.rb
157
+ - lib/active_record_extended/query_methods/where_chain.rb
158
+ - lib/active_record_extended/version.rb
159
+ - spec/active_record_extended_spec.rb
160
+ - spec/query_methods/array_query_spec.rb
161
+ - spec/query_methods/either_spec.rb
162
+ - spec/query_methods/hash_query_spec.rb
163
+ - spec/spec_helper.rb
164
+ - spec/sql_inspections/arel/array_spec.rb
165
+ - spec/sql_inspections/contains_sql_queries_spec.rb
166
+ - spec/sql_inspections/either_sql_spec.rb
167
+ - spec/support/database_cleaner.rb
168
+ - spec/support/models.rb
169
+ homepage: https://github.com/georgekaraszi/active_record_extended
170
+ licenses:
171
+ - MIT
172
+ metadata: {}
173
+ post_install_message:
174
+ rdoc_options: []
175
+ require_paths:
176
+ - lib
177
+ required_ruby_version: !ruby/object:Gem::Requirement
178
+ requirements:
179
+ - - ">="
180
+ - !ruby/object:Gem::Version
181
+ version: '0'
182
+ required_rubygems_version: !ruby/object:Gem::Requirement
183
+ requirements:
184
+ - - ">="
185
+ - !ruby/object:Gem::Version
186
+ version: '0'
187
+ requirements: []
188
+ rubyforge_project:
189
+ rubygems_version: 2.7.6
190
+ signing_key:
191
+ specification_version: 4
192
+ summary: Adds extended functionality to Activerecord Postgres implementation
193
+ test_files:
194
+ - spec/active_record_extended_spec.rb
195
+ - spec/query_methods/array_query_spec.rb
196
+ - spec/query_methods/either_spec.rb
197
+ - spec/query_methods/hash_query_spec.rb
198
+ - spec/spec_helper.rb
199
+ - spec/sql_inspections/arel/array_spec.rb
200
+ - spec/sql_inspections/contains_sql_queries_spec.rb
201
+ - spec/sql_inspections/either_sql_spec.rb
202
+ - spec/support/database_cleaner.rb
203
+ - spec/support/models.rb