edge_rider 0.3.2 → 2.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (50) hide show
  1. checksums.yaml +5 -5
  2. data/.github/workflows/test.yml +99 -0
  3. data/.rspec +1 -0
  4. data/.ruby-version +1 -1
  5. data/CHANGELOG.md +45 -0
  6. data/Gemfile +1 -0
  7. data/{gemfiles/Gemfile.4.2.mysql2 → Gemfile.4.2.mysql2} +4 -3
  8. data/{gemfiles/Gemfile.4.2.mysql2.lock → Gemfile.4.2.mysql2.lock} +32 -22
  9. data/{gemfiles/Gemfile.4.2.pg → Gemfile.4.2.pg} +3 -2
  10. data/{gemfiles/Gemfile.4.2.pg.lock → Gemfile.4.2.pg.lock} +30 -20
  11. data/Gemfile.5.2.mysql2 +17 -0
  12. data/{gemfiles/Gemfile.5.1.mysql2.lock → Gemfile.5.2.mysql2.lock} +34 -22
  13. data/{gemfiles/Gemfile.5.1.mysql2 → Gemfile.5.2.pg} +7 -5
  14. data/{gemfiles/Gemfile.5.1.pg.lock → Gemfile.5.2.pg.lock} +32 -20
  15. data/{gemfiles/Gemfile.5.1.pg → Gemfile.6.1.pg} +4 -3
  16. data/Gemfile.6.1.pg.lock +78 -0
  17. data/Gemfile.lock +1 -0
  18. data/README.md +96 -91
  19. data/Rakefile +1 -1
  20. data/edge_rider.gemspec +24 -15
  21. data/lib/edge_rider/collect_column.rb +8 -15
  22. data/lib/edge_rider/origin_class.rb +2 -10
  23. data/lib/edge_rider/preload_associations.rb +7 -0
  24. data/lib/edge_rider/scoped.rb +1 -1
  25. data/lib/edge_rider/to_id_query.rb +1 -1
  26. data/lib/edge_rider/traverse_association.rb +9 -5
  27. data/lib/edge_rider/util.rb +18 -19
  28. data/lib/edge_rider/version.rb +1 -1
  29. data/lib/edge_rider.rb +0 -1
  30. metadata +24 -37
  31. data/.travis.yml +0 -68
  32. data/gemfiles/Gemfile.2.3.mysql2 +0 -16
  33. data/gemfiles/Gemfile.2.3.mysql2.lock +0 -37
  34. data/gemfiles/Gemfile.3.2.mysql2 +0 -16
  35. data/gemfiles/Gemfile.3.2.mysql2.lock +0 -62
  36. data/lib/edge_rider/to_sql.rb +0 -13
  37. data/spec/edge_rider/collect_column_spec.rb +0 -85
  38. data/spec/edge_rider/collect_ids_spec.rb +0 -108
  39. data/spec/edge_rider/origin_class_spec.rb +0 -32
  40. data/spec/edge_rider/preload_associations_spec.rb +0 -15
  41. data/spec/edge_rider/scoped_spec.rb +0 -55
  42. data/spec/edge_rider/to_id_query_spec.rb +0 -29
  43. data/spec/edge_rider/to_sql_spec.rb +0 -14
  44. data/spec/edge_rider/traverse_association_spec.rb +0 -134
  45. data/spec/edge_rider/util_spec.rb +0 -5
  46. data/spec/spec_helper.rb +0 -12
  47. data/spec/support/database.rb +0 -34
  48. data/spec/support/database.sample.yml +0 -10
  49. data/spec/support/database.travis.yml +0 -9
  50. data/spec/support/models.rb +0 -92
data/Rakefile CHANGED
@@ -7,4 +7,4 @@ rescue LoadError
7
7
  puts 'Run `gem install gemika` for additional tasks'
8
8
  end
9
9
 
10
- task :default => 'matrix:spec'
10
+ task default: 'matrix:spec'
data/edge_rider.gemspec CHANGED
@@ -1,20 +1,29 @@
1
- $:.push File.expand_path("../lib", __FILE__)
1
+ lib = File.expand_path('lib', __dir__)
2
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
2
3
  require "edge_rider/version"
3
4
 
4
- Gem::Specification.new do |s|
5
- s.name = 'edge_rider'
6
- s.version = EdgeRider::VERSION
7
- s.authors = ["Henning Koch"]
8
- s.email = 'henning.koch@makandra.de'
9
- s.homepage = 'https://github.com/makandra/edge_rider'
10
- s.summary = 'Power tools for ActiveRecord relations (scopes)'
11
- s.description = s.summary
12
- s.license = 'MIT'
5
+ Gem::Specification.new do |spec|
6
+ spec.name = 'edge_rider'
7
+ spec.version = EdgeRider::VERSION
8
+ spec.required_ruby_version = '>= 2.3.0'
9
+ spec.authors = ['Henning Koch']
10
+ spec.email = ['henning.koch@makandra.de']
13
11
 
14
- s.files = `git ls-files`.split("\n").select { |path| File.exist?(path) && !File.symlink?(path) }
15
- s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n").select { |path| File.exist?(path) && !File.symlink?(path) }
16
- s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
17
- s.require_paths = ["lib"]
12
+ spec.summary = 'Power tools for ActiveRecord relations (scopes)'
13
+ spec.description = spec.summary
14
+ spec.homepage = 'https://github.com/makandra/edge_rider'
15
+ spec.license = 'MIT'
18
16
 
19
- s.add_dependency('activerecord')
17
+ # Specify which files should be added to the gem when it is released.
18
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
19
+ spec.files = Dir.chdir(File.expand_path(__dir__)) do
20
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
21
+ end
22
+ spec.bindir = 'exe'
23
+ spec.executables = spec.files.grep(%r(^exe/)) { |f| File.basename(f) }
24
+ spec.require_paths = ['lib']
25
+
26
+ spec.add_dependency('activerecord', '>=3.2')
27
+
28
+ # Development dependencies are defined in the Gemfile (therefore no `spec.add_development_dependency` directives)
20
29
  end
@@ -4,23 +4,16 @@ module EdgeRider
4
4
  def collect_column(column_name, find_options = {})
5
5
  distinct = find_options.delete(:distinct)
6
6
  qualified_column_name = EdgeRider::Util.qualify_column_name(self, column_name)
7
- if respond_to?(:pluck) # Rails 3.2+
8
- scope = scoped({})
9
- if distinct
10
- if ActiveRecord::VERSION::MAJOR < 5
11
- scope = scope.uniq
12
- else
13
- scope = scope.distinct
14
- end
7
+
8
+ scope = scoped({})
9
+ if distinct
10
+ if ActiveRecord::VERSION::MAJOR < 5
11
+ scope = scope.uniq
12
+ else
13
+ scope = scope.distinct
15
14
  end
16
- scope.pluck(qualified_column_name)
17
- else # Rails 2
18
- select = distinct ? "DISTINCT #{qualified_column_name}" : qualified_column_name
19
- query = scoped(find_options.merge(:select => select)).to_sql
20
- raw_values = connection.select_values(query)
21
- column = columns_hash[column_name.to_s] or raise "Could not retrieve column information: #{column_name}"
22
- raw_values.collect { |value| column.type_cast(value) }
23
15
  end
16
+ scope.pluck(qualified_column_name)
24
17
  end
25
18
 
26
19
  ActiveRecord::Base.extend(self)
@@ -3,16 +3,8 @@ module EdgeRider
3
3
 
4
4
  def origin_class
5
5
  scope = scoped({})
6
- if Util.activerecord2?
7
- # Rails 2
8
- while scope.respond_to?(:proxy_scope, true)
9
- scope = scope.proxy_scope
10
- end
11
- else
12
- # Rails 3
13
- while scope.respond_to?(:klass, true)
14
- scope = scope.klass
15
- end
6
+ while scope.respond_to?(:klass, true)
7
+ scope = scope.klass
16
8
  end
17
9
  scope
18
10
  end
@@ -1,5 +1,10 @@
1
1
  module EdgeRider
2
2
  module PreloadAssociations
3
+ module PreloadAssociationsInstanceMethod
4
+ def preload_associations(*args)
5
+ self.class.preload_associations([self], *args)
6
+ end
7
+ end
3
8
 
4
9
  def preload_associations(*args)
5
10
  preloader = ActiveRecord::Associations::Preloader
@@ -21,5 +26,7 @@ module EdgeRider
21
26
  ActiveRecord::Base.send(:extend, self)
22
27
  end
23
28
 
29
+ ActiveRecord::Base.send(:include, PreloadAssociationsInstanceMethod)
30
+
24
31
  end
25
32
  end
@@ -3,7 +3,7 @@ module EdgeRider
3
3
 
4
4
  VALID_FIND_OPTIONS = [ :conditions, :include, :joins, :limit, :offset,
5
5
  :order, :select, :readonly, :group, :having, :from, :lock ]
6
-
6
+
7
7
  def scoped(options = nil)
8
8
  options ||= {}
9
9
  relation = all
@@ -3,7 +3,7 @@ module EdgeRider
3
3
 
4
4
  def to_id_query
5
5
  ids = collect_ids
6
- EdgeRider::Util.exclusive_query(self, :id => ids)
6
+ EdgeRider::Util.exclusive_query(self, id: ids)
7
7
  end
8
8
 
9
9
  ActiveRecord::Base.send(:extend, self)
@@ -8,7 +8,7 @@ module EdgeRider
8
8
 
9
9
  scope = scoped({})
10
10
 
11
- associations.each_with_index do |association, index|
11
+ associations.each do |association|
12
12
 
13
13
  reflection = scope.reflect_on_association(association) or raise UnknownAssociation, "Could not find association: #{self.name}##{association}"
14
14
  foreign_key = reflection.respond_to?(:foreign_key) ? reflection.foreign_key : reflection.primary_key_name
@@ -18,14 +18,18 @@ module EdgeRider
18
18
  raise NotImplementedError if reflection.options[:conditions] or (reflection.respond_to?(:scope) && reflection.scope)
19
19
 
20
20
  if reflection.macro == :belongs_to # belongs_to
21
- ids = scope.collect_column(foreign_key, :distinct => true)
22
- scope = EdgeRider::Util.exclusive_query(reflection.klass, :id => ids)
21
+ ids = scope.collect_column(foreign_key, distinct: true)
22
+ scope = EdgeRider::Util.exclusive_query(reflection.klass, id: ids)
23
23
  elsif reflection.macro == :has_many || reflection.macro == :has_one
24
24
  if reflection.through_reflection # has_many :through
25
25
  scope = scope.traverse_association(reflection.through_reflection.name, reflection.source_reflection.name)
26
26
  else # has_many or has_one
27
- ids = scope.collect_ids
28
- scope = EdgeRider::Util.exclusive_query(reflection.klass, foreign_key => ids)
27
+ conditions = {}
28
+ # A polymorphic association has a type attribute, e.g. record_type, that needs to be added to condition.
29
+ conditions[reflection.type] = self.name if reflection.type.present?
30
+ conditions[foreign_key] = scope.collect_ids
31
+
32
+ scope = EdgeRider::Util.exclusive_query(reflection.klass, conditions)
29
33
  end
30
34
  else
31
35
  raise UnsupportedAssociation, "Unsupport association type: #{reflection.macro}"
@@ -7,21 +7,21 @@ module EdgeRider
7
7
  def qualify_column_name(model, column_name)
8
8
  column_name = column_name.to_s
9
9
  unless column_name.include?('.')
10
- quoted_table_name = model.connection.quote_table_name(model.table_name)
11
- quoted_column_name = model.connection.quote_column_name(column_name)
12
- column_name = "#{quoted_table_name}.#{quoted_column_name}"
10
+ column_name = if ActiveRecord::VERSION::MAJOR < 4
11
+ quoted_table_name = model.connection.quote_table_name(model.table_name)
12
+ quoted_column_name = model.connection.quote_column_name(column_name)
13
+ "#{quoted_table_name}.#{quoted_column_name}"
14
+ else
15
+ # Rails 4+ will quote correctly and Rails 5.2 will print
16
+ # deprecation warnings if there are surplus quotes
17
+ "#{model.table_name}.#{column_name}"
18
+ end
13
19
  end
14
20
  column_name
15
21
  end
16
22
 
17
23
  def exclusive_query(model, conditions)
18
- if activerecord2?
19
- model.send(:with_exclusive_scope) do
20
- model.scoped(:conditions => conditions)
21
- end
22
- else
23
- model.unscoped.where(conditions)
24
- end
24
+ model.unscoped.where(conditions)
25
25
  end
26
26
 
27
27
  def scope?(object)
@@ -29,13 +29,16 @@ module EdgeRider
29
29
  end
30
30
 
31
31
  def define_scope(klass, name, lambda)
32
- if ActiveRecord::VERSION::MAJOR < 4 # Rails 2/3
33
- scope_definition = ActiveRecord::VERSION::MAJOR < 3 ? :named_scope : :scope
34
- klass.send scope_definition, name, lambda
32
+ if ActiveRecord::VERSION::MAJOR == 3
33
+ klass.send :scope, name, lambda
35
34
  else
36
35
  klass.send :scope, name, lambda { |*args|
37
36
  options = lambda.call(*args)
38
- klass.scoped(options.slice :conditions)
37
+ if ActiveRecord::VERSION::MAJOR < 6
38
+ klass.scoped(options.slice :conditions)
39
+ else
40
+ scoped(options.slice :conditions)
41
+ end
39
42
  }
40
43
  end
41
44
  end
@@ -49,7 +52,7 @@ module EdgeRider
49
52
  # but Rails 2 does not have it.
50
53
  other_options = options.slice!(:conditions)
51
54
  scope = lambda { |*args| scoped(options) }
52
- owner.send association, target, scope, other_options
55
+ owner.send association, target, scope, **other_options
53
56
  end
54
57
  end
55
58
 
@@ -57,10 +60,6 @@ module EdgeRider
57
60
  ActiveRecord::VERSION::MAJOR
58
61
  end
59
62
 
60
- def activerecord2?
61
- active_record_version < 3
62
- end
63
-
64
63
  def rspec_version
65
64
  if defined?(Spec)
66
65
  1
@@ -1,3 +1,3 @@
1
1
  module EdgeRider
2
- VERSION = '0.3.2'
2
+ VERSION = '2.0.0'
3
3
  end
data/lib/edge_rider.rb CHANGED
@@ -12,4 +12,3 @@ require 'edge_rider/origin_class'
12
12
  require 'edge_rider/scoped'
13
13
  require 'edge_rider/traverse_association'
14
14
  require 'edge_rider/to_id_query'
15
- require 'edge_rider/to_sql'
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: edge_rider
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.2
4
+ version: 2.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Henning Koch
8
8
  autorequire:
9
- bindir: bin
9
+ bindir: exe
10
10
  cert_chain: []
11
- date: 2017-05-29 00:00:00.000000000 Z
11
+ date: 2021-08-25 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord
@@ -16,39 +16,42 @@ dependencies:
16
16
  requirements:
17
17
  - - ">="
18
18
  - !ruby/object:Gem::Version
19
- version: '0'
19
+ version: '3.2'
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
24
  - - ">="
25
25
  - !ruby/object:Gem::Version
26
- version: '0'
26
+ version: '3.2'
27
27
  description: Power tools for ActiveRecord relations (scopes)
28
- email: henning.koch@makandra.de
28
+ email:
29
+ - henning.koch@makandra.de
29
30
  executables: []
30
31
  extensions: []
31
32
  extra_rdoc_files: []
32
33
  files:
34
+ - ".github/workflows/test.yml"
33
35
  - ".gitignore"
36
+ - ".rspec"
34
37
  - ".ruby-version"
35
- - ".travis.yml"
38
+ - CHANGELOG.md
39
+ - Gemfile
40
+ - Gemfile.4.2.mysql2
41
+ - Gemfile.4.2.mysql2.lock
42
+ - Gemfile.4.2.pg
43
+ - Gemfile.4.2.pg.lock
44
+ - Gemfile.5.2.mysql2
45
+ - Gemfile.5.2.mysql2.lock
46
+ - Gemfile.5.2.pg
47
+ - Gemfile.5.2.pg.lock
48
+ - Gemfile.6.1.pg
49
+ - Gemfile.6.1.pg.lock
50
+ - Gemfile.lock
36
51
  - LICENSE
37
52
  - README.md
38
53
  - Rakefile
39
54
  - edge_rider.gemspec
40
- - gemfiles/Gemfile.2.3.mysql2
41
- - gemfiles/Gemfile.2.3.mysql2.lock
42
- - gemfiles/Gemfile.3.2.mysql2
43
- - gemfiles/Gemfile.3.2.mysql2.lock
44
- - gemfiles/Gemfile.4.2.mysql2
45
- - gemfiles/Gemfile.4.2.mysql2.lock
46
- - gemfiles/Gemfile.4.2.pg
47
- - gemfiles/Gemfile.4.2.pg.lock
48
- - gemfiles/Gemfile.5.1.mysql2
49
- - gemfiles/Gemfile.5.1.mysql2.lock
50
- - gemfiles/Gemfile.5.1.pg
51
- - gemfiles/Gemfile.5.1.pg.lock
52
55
  - lib/edge_rider.rb
53
56
  - lib/edge_rider/collect_column.rb
54
57
  - lib/edge_rider/collect_ids.rb
@@ -57,24 +60,9 @@ files:
57
60
  - lib/edge_rider/preload_associations.rb
58
61
  - lib/edge_rider/scoped.rb
59
62
  - lib/edge_rider/to_id_query.rb
60
- - lib/edge_rider/to_sql.rb
61
63
  - lib/edge_rider/traverse_association.rb
62
64
  - lib/edge_rider/util.rb
63
65
  - lib/edge_rider/version.rb
64
- - spec/edge_rider/collect_column_spec.rb
65
- - spec/edge_rider/collect_ids_spec.rb
66
- - spec/edge_rider/origin_class_spec.rb
67
- - spec/edge_rider/preload_associations_spec.rb
68
- - spec/edge_rider/scoped_spec.rb
69
- - spec/edge_rider/to_id_query_spec.rb
70
- - spec/edge_rider/to_sql_spec.rb
71
- - spec/edge_rider/traverse_association_spec.rb
72
- - spec/edge_rider/util_spec.rb
73
- - spec/spec_helper.rb
74
- - spec/support/database.rb
75
- - spec/support/database.sample.yml
76
- - spec/support/database.travis.yml
77
- - spec/support/models.rb
78
66
  homepage: https://github.com/makandra/edge_rider
79
67
  licenses:
80
68
  - MIT
@@ -87,15 +75,14 @@ required_ruby_version: !ruby/object:Gem::Requirement
87
75
  requirements:
88
76
  - - ">="
89
77
  - !ruby/object:Gem::Version
90
- version: '0'
78
+ version: 2.3.0
91
79
  required_rubygems_version: !ruby/object:Gem::Requirement
92
80
  requirements:
93
81
  - - ">="
94
82
  - !ruby/object:Gem::Version
95
83
  version: '0'
96
84
  requirements: []
97
- rubyforge_project:
98
- rubygems_version: 2.4.5.1
85
+ rubygems_version: 3.2.26
99
86
  signing_key:
100
87
  specification_version: 4
101
88
  summary: Power tools for ActiveRecord relations (scopes)
data/.travis.yml DELETED
@@ -1,68 +0,0 @@
1
- language: ruby
2
-
3
- rvm:
4
- - '1.8.7'
5
- - '2.1.8'
6
- - '2.2.4'
7
- - '2.3.1'
8
- - '2.4.1'
9
-
10
- gemfile:
11
- - 'gemfiles/Gemfile.2.3.mysql2'
12
- - 'gemfiles/Gemfile.3.2.mysql2'
13
- - 'gemfiles/Gemfile.4.2.mysql2'
14
- - 'gemfiles/Gemfile.4.2.pg'
15
- - 'gemfiles/Gemfile.5.1.mysql2'
16
- - 'gemfiles/Gemfile.5.1.pg'
17
-
18
- matrix:
19
- exclude:
20
- - gemfile: 'gemfiles/Gemfile.2.3.mysql2'
21
- rvm: '2.1.8'
22
- - gemfile: 'gemfiles/Gemfile.2.3.mysql2'
23
- rvm: '2.2.4'
24
- - gemfile: 'gemfiles/Gemfile.3.2.mysql2'
25
- rvm: '2.3.1'
26
- - gemfile: 'gemfiles/Gemfile.2.3.mysql2'
27
- rvm: '2.3.1'
28
- - gemfile: 'gemfiles/Gemfile.2.3.mysql2'
29
- rvm: '2.4.1'
30
- - gemfile: 'gemfiles/Gemfile.3.2.mysql2'
31
- rvm: '2.4.1'
32
- - gemfile: 'gemfiles/Gemfile.4.2.mysql2'
33
- rvm: '1.8.7'
34
- - gemfile: 'gemfiles/Gemfile.4.2.pg'
35
- rvm: '1.8.7'
36
- - gemfile: 'gemfiles/Gemfile.4.2.mysql2'
37
- rvm: '2.4.1'
38
- - gemfile: 'gemfiles/Gemfile.4.2.pg'
39
- rvm: '2.4.1'
40
- - gemfile: 'gemfiles/Gemfile.5.1.mysql2'
41
- rvm: '1.8.7'
42
- - gemfile: 'gemfiles/Gemfile.5.1.mysql2'
43
- rvm: '2.1.8'
44
- - gemfile: 'gemfiles/Gemfile.5.1.pg'
45
- rvm: '1.8.7'
46
- - gemfile: 'gemfiles/Gemfile.5.1.pg'
47
- rvm: '2.1.8'
48
-
49
- services:
50
- - mysql
51
-
52
- install:
53
- # Shave yaks
54
- - gem uninstall bundler -I -x --force
55
- # Old Travis CI bundler explodes when lockfile version doesn't match recently bumped version
56
- - gem install bundler --version='=1.12.5'
57
- # This is the default Travis CI install step
58
- - bundle install --jobs=3 --retry=3 --deployment --path=${BUNDLE_PATH:-vendor/bundle}
59
-
60
- before_script:
61
- - psql -c 'create database edge_rider_test;' -U postgres
62
- - mysql -e 'create database IF NOT EXISTS edge_rider_test;'
63
-
64
- script: bundle exec rake current_rspec
65
-
66
- notifications:
67
- email:
68
- - fail@makandra.de