join_dependency 0.1.1

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: f9f66a03306a91acb3a744a210b291256dfb4de6
4
+ data.tar.gz: 42a4ab220ec1006ab0059b64381092d89740d174
5
+ SHA512:
6
+ metadata.gz: 9383a3facbd04ce15f804dc89bf9630d95d8928fbae0d561420ec71fda3b8def5e2fe0568adb56190ddc4fb584758cb4fc1a67a41e6df51917161fdbf7e0e171
7
+ data.tar.gz: 27ba5c103231dd348d918dacf1a82ab3c28f6354de370b4b2d6e78e21d58e4fa339342247be4dd2f1eabd1aa8b824f6d26a0497a9001503aa42b41b4ef1eb4e3
data/.gitignore ADDED
@@ -0,0 +1,12 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+
10
+ # rspec failure tracking
11
+ .rspec_status
12
+ /Gemfile.lock
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.travis.yml ADDED
@@ -0,0 +1,15 @@
1
+ language: ruby
2
+
3
+ rvm:
4
+ - 2.4.0
5
+
6
+ before_install: gem install bundler -v 1.16.1
7
+
8
+ env:
9
+ matrix:
10
+ - AR=4.2.10
11
+ - AR=5.0.6
12
+ - AR=5.1.5
13
+ - AR=5.2.0.rc1
14
+ - AR=latest
15
+
data/Gemfile ADDED
@@ -0,0 +1,14 @@
1
+ source "https://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in join_dependency.gemspec
4
+ gemspec
5
+
6
+ case ENV.fetch('AR', 'latest')
7
+ when 'latest'
8
+ gem 'activerecord'
9
+ when 'master'
10
+ gem 'activerecord', github: 'https://github.com/rails/rails'
11
+ else
12
+ gem 'activerecord', ENV['AR']
13
+ end
14
+
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2018 Ray Zane
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,23 @@
1
+ # JoinDependency [![Build Status](https://travis-ci.org/rzane/join_dependency.svg?branch=master)](https://travis-ci.org/rzane/join_dependency)
2
+
3
+ This is a module with a singular purpose. It creates an `ActiveRecord::Associations::JoinDependency` from an `ActiveRecord::Relation`. That's it.
4
+
5
+ ```ruby
6
+ require 'join_dependency'
7
+
8
+ relation = Post.joins(:author)
9
+ JoinDependency.from_relation(relation)
10
+ ```
11
+
12
+ And, in case you're trying to bend Active Record to your will, you can choose how certain joins get categorized:
13
+
14
+ ```ruby
15
+ JoinDependency.from_relation(relation) do |join|
16
+ case join
17
+ when Polyamorous::InnerJoin, Polyamorous::OuterJoin
18
+ :stashed_association_join
19
+ end
20
+ end
21
+ ```
22
+
23
+ This library isn't meant for people. It's a library for libraries on top of other libraries that interact with libraries.
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
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "join_dependency"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start(__FILE__)
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,29 @@
1
+ lib = File.expand_path("../lib", __FILE__)
2
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
3
+ require "join_dependency/version"
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "join_dependency"
7
+ spec.version = JoinDependency::VERSION
8
+ spec.authors = ["Ray Zane"]
9
+ spec.email = ["ray@promptworks.com"]
10
+
11
+ spec.summary = %q{Convert an ActiveRecord::Relation to a Join Dependency}
12
+ spec.description = %q{If only this were easier...}
13
+ spec.homepage = "https://github.com/rzane/join_dependency"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
17
+ f.match(%r{^(test|spec|features)/})
18
+ end
19
+ spec.bindir = "exe"
20
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
21
+ spec.require_paths = ["lib"]
22
+
23
+ spec.add_dependency 'activerecord', '>= 4.2.0'
24
+
25
+ spec.add_development_dependency 'sqlite3'
26
+ spec.add_development_dependency "bundler", "~> 1.16"
27
+ spec.add_development_dependency "rake", "~> 10.0"
28
+ spec.add_development_dependency "rspec", "~> 3.0"
29
+ end
@@ -0,0 +1,67 @@
1
+ require "active_record"
2
+ require "join_dependency/version"
3
+
4
+ module JoinDependency
5
+ class << self
6
+ def from_relation(relation, &block)
7
+ build(relation, collect_joins(relation, &block))
8
+ end
9
+
10
+ private
11
+
12
+ def collect_joins(relation, &block)
13
+ joins = []
14
+ joins += relation.joins_values
15
+ joins += relation.left_outer_joins_values if at_least?(5)
16
+
17
+ buckets = joins.group_by do |join|
18
+ case join
19
+ when String
20
+ :string_join
21
+ when Hash, Symbol, Array
22
+ :association_join
23
+ when Arel::Nodes::Join
24
+ :join_node
25
+ else
26
+ (block_given? && yield(join)) || raise("unknown class: %s" % join.class.name)
27
+ end
28
+ end
29
+ end
30
+
31
+ def build(relation, buckets)
32
+ buckets.default = []
33
+ association_joins = buckets[:association_join]
34
+ stashed_association_joins = buckets[:stashed_join]
35
+ join_nodes = buckets[:join_node].uniq
36
+ string_joins = buckets[:string_join].map(&:strip).uniq
37
+
38
+ join_list =
39
+ if at_least?(5, 2)
40
+ join_nodes + relation.send(:convert_join_strings_to_ast, string_joins)
41
+ elsif at_least?(5)
42
+ join_nodes + relation.send(:convert_join_strings_to_ast, relation.table, string_joins)
43
+ else
44
+ relation.send(:custom_join_ast, relation.table.from(relation.table), string_joins)
45
+ end
46
+
47
+ if at_least?(5, 2)
48
+ alias_tracker = ::ActiveRecord::Associations::AliasTracker.create(relation.klass.connection, relation.table.name, join_list)
49
+ join_dependency = ::ActiveRecord::Associations::JoinDependency.new(relation.klass, relation.table, association_joins, alias_tracker)
50
+ join_nodes.each do |join|
51
+ join_dependency.send(:alias_tracker).aliases[join.left.name.downcase] = 1
52
+ end
53
+ else
54
+ join_dependency = ::ActiveRecord::Associations::JoinDependency.new(relation.klass, association_joins, join_list)
55
+ join_nodes.each do |join|
56
+ join_dependency.send(:alias_tracker).aliases[join.left.name.downcase] = 1
57
+ end
58
+ end
59
+
60
+ join_dependency
61
+ end
62
+
63
+ def at_least?(major, minor = 0)
64
+ ActiveRecord::VERSION::MAJOR >= major && ActiveRecord::VERSION::MINOR >= minor
65
+ end
66
+ end
67
+ end
@@ -0,0 +1,3 @@
1
+ module JoinDependency
2
+ VERSION = "0.1.1"
3
+ end
metadata ADDED
@@ -0,0 +1,126 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: join_dependency
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.1
5
+ platform: ruby
6
+ authors:
7
+ - Ray Zane
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2018-04-25 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: 4.2.0
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: 4.2.0
27
+ - !ruby/object:Gem::Dependency
28
+ name: sqlite3
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: bundler
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '1.16'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '1.16'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rake
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '10.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '10.0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rspec
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '3.0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '3.0'
83
+ description: If only this were easier...
84
+ email:
85
+ - ray@promptworks.com
86
+ executables: []
87
+ extensions: []
88
+ extra_rdoc_files: []
89
+ files:
90
+ - ".gitignore"
91
+ - ".rspec"
92
+ - ".travis.yml"
93
+ - Gemfile
94
+ - LICENSE.txt
95
+ - README.md
96
+ - Rakefile
97
+ - bin/console
98
+ - bin/setup
99
+ - join_dependency.gemspec
100
+ - lib/join_dependency.rb
101
+ - lib/join_dependency/version.rb
102
+ homepage: https://github.com/rzane/join_dependency
103
+ licenses:
104
+ - MIT
105
+ metadata: {}
106
+ post_install_message:
107
+ rdoc_options: []
108
+ require_paths:
109
+ - lib
110
+ required_ruby_version: !ruby/object:Gem::Requirement
111
+ requirements:
112
+ - - ">="
113
+ - !ruby/object:Gem::Version
114
+ version: '0'
115
+ required_rubygems_version: !ruby/object:Gem::Requirement
116
+ requirements:
117
+ - - ">="
118
+ - !ruby/object:Gem::Version
119
+ version: '0'
120
+ requirements: []
121
+ rubyforge_project:
122
+ rubygems_version: 2.6.10
123
+ signing_key:
124
+ specification_version: 4
125
+ summary: Convert an ActiveRecord::Relation to a Join Dependency
126
+ test_files: []