activerecord_nested_scope 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 166d91ad4f5ed349c3bade4dc78b7b03a5d23dbd
4
+ data.tar.gz: 41a8f213a1b5c800ecf0ab98a986a6f2f90e5e48
5
+ SHA512:
6
+ metadata.gz: ae33da5f637a1a078f147a5b9f0a14152c73e01a046f5f8a71c57475d111be6c4f686d3a4900582c3583954d579cf03944e8ec8b36c71742ac829204185169c2
7
+ data.tar.gz: 406a41e50108e30684e84c69fc89347603f8b612dd56a611a01f5509521b2d7aea7bfb0968b4c1597cb5951a6bdd9c0d847f27e25d94d2664e5ec59e3b5a9c83
data/.gitignore ADDED
@@ -0,0 +1,15 @@
1
+ .bundle/
2
+ .yardoc
3
+ .project
4
+ Gemfile.lock
5
+ _yardoc/
6
+ coverage/
7
+ doc/
8
+ pkg/
9
+ spec/reports/
10
+ spec/dummy/config/database.yml
11
+ spec/dummy/db/schema.rb
12
+ spec/dummy/db/*.sqlite3
13
+ spec/dummy/log/*.log
14
+ spec/dummy/tmp/*
15
+ tmp/
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.travis.yml ADDED
@@ -0,0 +1,25 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.3
4
+ - 2.4
5
+ - 2.5
6
+ services:
7
+ - mysql
8
+ - postgresql
9
+ addons:
10
+ postgresql: "9.5"
11
+ env:
12
+ - DATABASE=sqlite
13
+ - DATABASE=mysql
14
+ - DATABASE=postgresql
15
+ gemfile:
16
+ - gemfiles/rails50.gemfile
17
+ - gemfiles/rails51.gemfile
18
+ - gemfiles/rails52.gemfile
19
+ before_script:
20
+ - cd spec/dummy
21
+ - bundle exec rake db:create RAILS_ENV=test
22
+ - bundle exec rake db:migrate RAILS_ENV=test
23
+ - bundle exec rake db:seed RAILS_ENV=test
24
+ - cd ../..
25
+ script: bundle exec rspec
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2017-2018 Yoshikazu Kaneta
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,102 @@
1
+ # ActiveRecordNestedScope
2
+
3
+ An ActiveRecord extension to build nested scopes through pre-defined associations.
4
+
5
+ ## Dependencies
6
+
7
+ * ruby 2.3+
8
+ * rails 5.0+ (activerecord and activesupport)
9
+
10
+ ## Installation
11
+
12
+ Add this line to your application's Gemfile:
13
+
14
+ ```ruby
15
+ gem 'activerecord_nested_scope'
16
+ ```
17
+
18
+ Then execute:
19
+
20
+ $ bundle
21
+
22
+ ## Usage
23
+
24
+ For example, define `in_group` scope using nested_scope as follows:
25
+
26
+ ```ruby
27
+ class Group < ActiveRecord::Base
28
+ nested_scope :in_group # root scope
29
+ ...
30
+ end
31
+
32
+ class User < ActiveRecord::Base
33
+ belongs_to :group
34
+ nested_scope :in_group, through: :group # User belongs to Group
35
+ ...
36
+ end
37
+
38
+ class UserConfig < ActiveRecord::Base
39
+ belongs_to :user
40
+ nested_scope :in_group, through: :user # UserConfig belongs to Group through User
41
+ ...
42
+ end
43
+ ```
44
+
45
+ `in_group` scope generates SQL as follows:
46
+
47
+ ```ruby
48
+ User.in_group(id: 1)
49
+ #=> SELECT "users".* FROM "users" WHERE "users"."group_id" IN (
50
+ # SELECT "groups"."id" FROM "groups" WHERE "groups"."id" = 1)
51
+
52
+ UserConfig.in_group(id: 1)
53
+ #=> SELECT "user_configs".* FROM "user_configs" WHERE "user_configs"."user_id" IN (
54
+ # SELECT "users"."id" FROM "users" WHERE "users"."group_id" IN (
55
+ # SELECT "groups"."id" FROM "groups" WHERE "groups"."id" = 1))
56
+ ```
57
+
58
+ ### Polymorphic association
59
+
60
+ If you define a polymorphic association like that,
61
+
62
+ ```ruby
63
+ class Name < ActiveRecord::Base
64
+ belongs_to :data, polymorphic: true
65
+ nested_scope :in_group, through: :data
66
+ end
67
+ ```
68
+
69
+ `in_group` scope generates union of subqueries for each polymorpchic type:
70
+
71
+ ```ruby
72
+ Name.in_group(id: 1)
73
+ #=> SELECT "names"."data_type" FROM "names" GROUP BY "names"."data_type"
74
+ # SELECT "names".* FROM (
75
+ # SELECT "names".* FROM "names" WHERE "names"."data_type" = 'Group' AND "names"."data_id" IN (SELECT "groups"."id" FROM "groups" WHERE "groups"."id" = 1) UNION
76
+ # SELECT "names".* FROM "names" WHERE "names"."data_type" = 'Manager' AND "names"."data_id" IN (SELECT "managers"."id" FROM "managers" WHERE "managers"."id" IN (SELECT "groups"."manager_id" FROM "groups" WHERE "groups"."id" = 1)) UNION
77
+ # SELECT "names".* FROM "names" WHERE "names"."data_type" = 'Supervisor' AND "names"."data_id" IN (SELECT "supervisors"."id" FROM "supervisors" WHERE "supervisors"."id" IN (SELECT "managers"."supervisor_id" FROM "managers" WHERE "managers"."id" IN (SELECT "groups"."manager_id" FROM "groups" WHERE "groups"."id" = 1) ORDER BY "managers"."id" ASC)) UNION
78
+ # SELECT "names".* FROM "names" WHERE "names"."data_type" = 'User' AND "names"."data_id" IN (SELECT "users"."id" FROM "users" WHERE "users"."group_id" IN (SELECT "groups"."id" FROM "groups" WHERE "groups"."id" = 1))) AS names
79
+ ```
80
+
81
+ Note that the first SQL is executed to load `data_type`, and the second SQL is built using loaded `data_type` variations.
82
+
83
+ ### Scope examples
84
+
85
+ ```ruby
86
+ # pass a integer
87
+ User.in_group(1)
88
+
89
+ # pass a hash
90
+ User.in_group(id: 1)
91
+
92
+ # pass an AR relation
93
+ User.in_group(Group.where(id: 1))
94
+ ```
95
+
96
+ ## Contributing
97
+
98
+ Bug reports and pull requests are welcome at https://github.com/kanety/activerecord_nested_scope.
99
+
100
+ ## License
101
+
102
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
@@ -0,0 +1,32 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'activerecord_nested_scope/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "activerecord_nested_scope"
8
+ spec.version = ActiveRecordNestedScope::VERSION
9
+ spec.authors = ["Yoshikazu Kaneta"]
10
+ spec.email = ["kaneta@sitebridge.co.jp"]
11
+ spec.summary = %q{An ActiveRecord extension to build nested scopes}
12
+ spec.description = %q{An ActiveRecord extension to build nested scopes through pre-defined associations}
13
+ spec.homepage = "https://github.com/kanety/activerecord_nested_scope"
14
+
15
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
16
+ spec.bindir = "exe"
17
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
18
+ spec.require_paths = ["lib"]
19
+
20
+ spec.add_dependency "activerecord", ">= 5.0"
21
+ spec.add_dependency "activesupport", ">= 5.0"
22
+
23
+ spec.add_development_dependency "rails", ">= 5.0"
24
+ spec.add_development_dependency "active_record_union"
25
+ spec.add_development_dependency "mysql2"
26
+ spec.add_development_dependency "pg"
27
+ spec.add_development_dependency "sqlite3"
28
+ spec.add_development_dependency "rspec-rails"
29
+ spec.add_development_dependency "simplecov"
30
+ spec.add_development_dependency "pry-rails"
31
+ spec.add_development_dependency "pry-byebug"
32
+ end
@@ -0,0 +1,8 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gem "rails", "~> 5.0.0"
4
+ gem "sqlite3", "~> 1.3.6"
5
+ gem "mysql2", "~> 0.4.4"
6
+ gem "pg", "~> 0.18"
7
+
8
+ gemspec path: "../"
@@ -0,0 +1,8 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gem "rails", "~> 5.1.0"
4
+ gem "sqlite3", "~> 1.3.6"
5
+ gem "mysql2", "~> 0.4.4"
6
+ gem "pg", "~> 0.18"
7
+
8
+ gemspec path: "../"
@@ -0,0 +1,5 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gem "rails", "~> 5.2.0.rc1"
4
+
5
+ gemspec path: "../"
@@ -0,0 +1,3 @@
1
+ require 'activerecord_nested_scope/version'
2
+ require 'activerecord_nested_scope/extension'
3
+ require 'activerecord_nested_scope/railtie' if defined?(Rails)
@@ -0,0 +1,93 @@
1
+ module ActiveRecordNestedScope
2
+ class Builder
3
+ def initialize(klass, name, args)
4
+ @klass = klass
5
+ @name = name
6
+ @args = args
7
+ end
8
+
9
+ def build
10
+ build_for(@klass)
11
+ end
12
+
13
+ private
14
+
15
+ def build_for(klass)
16
+ if klass._nested_scope_options
17
+ if (through = klass._nested_scope_options[@name][:through])
18
+ if (ref = klass.reflect_on_association(through))
19
+ build_relation(klass, ref)
20
+ else
21
+ raise ArgumentError.new("can't find reflection #{through} in #{klass}")
22
+ end
23
+ else
24
+ root_relation(klass)
25
+ end
26
+ else
27
+ klass.none
28
+ end
29
+ end
30
+
31
+ def build_relation(klass, ref)
32
+ case ref.class.to_s
33
+ when 'ActiveRecord::Reflection::HasManyReflection', 'ActiveRecord::Reflection::HasOneReflection'
34
+ has_many_relation(klass, ref)
35
+ when 'ActiveRecord::Reflection::BelongsToReflection'
36
+ if ref.polymorphic?
37
+ belongs_to_polymorphic_relation(klass, ref)
38
+ else
39
+ belongs_to_relation(klass, ref)
40
+ end
41
+ else
42
+ raise ArgumentError.new("unsupported reflection: #{ref} in #{klass}")
43
+ end
44
+ end
45
+
46
+ def has_many_relation(klass, ref)
47
+ klass.where(klass.primary_key => build_for(ref.klass).merge(scoped(ref.klass, ref.scope)).select(ref.foreign_key))
48
+ end
49
+
50
+ def belongs_to_relation(klass, ref)
51
+ klass.where(ref.foreign_key => build_for(ref.klass).merge(scoped(ref.klass, ref.scope)).select(klass.primary_key))
52
+ end
53
+
54
+ def belongs_to_polymorphic_relation(klass, ref)
55
+ types = klass.unscoped.group(ref.foreign_type).pluck(ref.foreign_type)
56
+ rels = types.map { |type|
57
+ if (parent = type.safe_constantize)
58
+ klass.where(ref.foreign_type => type, ref.foreign_key => build_for(parent).merge(scoped(parent, ref.scope)))
59
+ else
60
+ klass.none
61
+ end
62
+ }
63
+ union(klass, rels)
64
+ end
65
+
66
+ def root_relation(klass)
67
+ if @args.is_a?(Integer) || @args.is_a?(ActiveRecord::Base)
68
+ klass.where(klass.primary_key => @args)
69
+ elsif @args.is_a?(ActiveRecord::Relation)
70
+ klass.all.merge(@args)
71
+ elsif @args
72
+ klass.where(@args)
73
+ else
74
+ klass.all
75
+ end
76
+ end
77
+
78
+ def scoped(klass, scope)
79
+ rel = klass.all
80
+ rel = rel.instance_eval(&scope) if scope
81
+ rel
82
+ end
83
+
84
+ def union(klass, rels)
85
+ if defined? ActiveRecordUnion
86
+ rels.reduce(:union)
87
+ else
88
+ union = rels.map { |rel| "#{rel.to_sql}" }.reject(&:empty?).join(' UNION ')
89
+ klass.from(Arel.sql("(#{union}) AS #{klass.table_name}"))
90
+ end
91
+ end
92
+ end
93
+ end
@@ -0,0 +1,23 @@
1
+ require 'active_support'
2
+ require_relative 'builder'
3
+
4
+ module ActiveRecordNestedScope
5
+ module Extension
6
+ extend ActiveSupport::Concern
7
+
8
+ included do
9
+ class_attribute :_nested_scope_options
10
+ end
11
+
12
+ class_methods do
13
+ def nested_scope(name, options = {})
14
+ self._nested_scope_options ||= {}
15
+ self._nested_scope_options[name] = options
16
+
17
+ scope name, ->(args) {
18
+ ActiveRecordNestedScope::Builder.new(self, name, args).build
19
+ }
20
+ end
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,7 @@
1
+ module ActiveRecordNestedScope
2
+ class Railtie < Rails::Railtie
3
+ ActiveSupport.on_load :active_record do
4
+ ActiveRecord::Base.send :include, ActiveRecordNestedScope::Extension
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,3 @@
1
+ module ActiveRecordNestedScope
2
+ VERSION = '1.0.0'
3
+ end
metadata ADDED
@@ -0,0 +1,214 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: activerecord_nested_scope
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Yoshikazu Kaneta
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2018-02-24 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: activerecord
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '5.0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '5.0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: activesupport
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '5.0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '5.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rails
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '5.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '5.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: active_record_union
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: mysql2
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: pg
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: sqlite3
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ - !ruby/object:Gem::Dependency
112
+ name: rspec-rails
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - ">="
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - ">="
123
+ - !ruby/object:Gem::Version
124
+ version: '0'
125
+ - !ruby/object:Gem::Dependency
126
+ name: simplecov
127
+ requirement: !ruby/object:Gem::Requirement
128
+ requirements:
129
+ - - ">="
130
+ - !ruby/object:Gem::Version
131
+ version: '0'
132
+ type: :development
133
+ prerelease: false
134
+ version_requirements: !ruby/object:Gem::Requirement
135
+ requirements:
136
+ - - ">="
137
+ - !ruby/object:Gem::Version
138
+ version: '0'
139
+ - !ruby/object:Gem::Dependency
140
+ name: pry-rails
141
+ requirement: !ruby/object:Gem::Requirement
142
+ requirements:
143
+ - - ">="
144
+ - !ruby/object:Gem::Version
145
+ version: '0'
146
+ type: :development
147
+ prerelease: false
148
+ version_requirements: !ruby/object:Gem::Requirement
149
+ requirements:
150
+ - - ">="
151
+ - !ruby/object:Gem::Version
152
+ version: '0'
153
+ - !ruby/object:Gem::Dependency
154
+ name: pry-byebug
155
+ requirement: !ruby/object:Gem::Requirement
156
+ requirements:
157
+ - - ">="
158
+ - !ruby/object:Gem::Version
159
+ version: '0'
160
+ type: :development
161
+ prerelease: false
162
+ version_requirements: !ruby/object:Gem::Requirement
163
+ requirements:
164
+ - - ">="
165
+ - !ruby/object:Gem::Version
166
+ version: '0'
167
+ description: An ActiveRecord extension to build nested scopes through pre-defined
168
+ associations
169
+ email:
170
+ - kaneta@sitebridge.co.jp
171
+ executables: []
172
+ extensions: []
173
+ extra_rdoc_files: []
174
+ files:
175
+ - ".gitignore"
176
+ - ".rspec"
177
+ - ".travis.yml"
178
+ - Gemfile
179
+ - LICENSE
180
+ - README.md
181
+ - Rakefile
182
+ - activerecord_nested_scope.gemspec
183
+ - gemfiles/rails50.gemfile
184
+ - gemfiles/rails51.gemfile
185
+ - gemfiles/rails52.gemfile
186
+ - lib/activerecord_nested_scope.rb
187
+ - lib/activerecord_nested_scope/builder.rb
188
+ - lib/activerecord_nested_scope/extension.rb
189
+ - lib/activerecord_nested_scope/railtie.rb
190
+ - lib/activerecord_nested_scope/version.rb
191
+ homepage: https://github.com/kanety/activerecord_nested_scope
192
+ licenses: []
193
+ metadata: {}
194
+ post_install_message:
195
+ rdoc_options: []
196
+ require_paths:
197
+ - lib
198
+ required_ruby_version: !ruby/object:Gem::Requirement
199
+ requirements:
200
+ - - ">="
201
+ - !ruby/object:Gem::Version
202
+ version: '0'
203
+ required_rubygems_version: !ruby/object:Gem::Requirement
204
+ requirements:
205
+ - - ">="
206
+ - !ruby/object:Gem::Version
207
+ version: '0'
208
+ requirements: []
209
+ rubyforge_project:
210
+ rubygems_version: 2.6.12
211
+ signing_key:
212
+ specification_version: 4
213
+ summary: An ActiveRecord extension to build nested scopes
214
+ test_files: []