extended_joins_impl 0.0.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: 403d54fe3888676bfdc11699909314b277236c1d
4
+ data.tar.gz: cfe6bd058d44fd9216c01f2d175d739150b646c4
5
+ SHA512:
6
+ metadata.gz: 44f5f7b04a8fa77b81cffdf2aa4f523eed713463695fd0b3b0f06c6324ea6d0c4fe5780518520364944afbaa56a3d6a8ebde25ed6bc7aece7b69d55293ca76d0
7
+ data.tar.gz: b462edba146c8af41d91cd1413384dbad62100b57e2572489e9b9a336d276d9930ff5baa007e47b2958b9bedcd50144cab696c06122bb31c9c5e6759f0fd05b3
data/.gitignore ADDED
@@ -0,0 +1,20 @@
1
+ *.iml
2
+ .DS_Store
3
+ .idea
4
+ *.gem
5
+ *.rbc
6
+ .bundle
7
+ .config
8
+ .yardoc
9
+ Gemfile.lock
10
+ InstalledFiles
11
+ _yardoc
12
+ coverage
13
+ doc/
14
+ lib/bundler/man
15
+ pkg
16
+ rdoc
17
+ spec/reports
18
+ test/tmp
19
+ test/version_tmp
20
+ tmp
data/.travis.yml ADDED
@@ -0,0 +1,11 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.0.0
4
+ - 1.9.3
5
+ env:
6
+ - "RAILS_VERSION=4.0.0"
7
+ - "RAILS_VERSION=3.2.0"
8
+ branches:
9
+ only:
10
+ - master
11
+ - develop
data/Gemfile ADDED
@@ -0,0 +1,17 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in extended_joins_impl.gemspec
4
+ gemspec
5
+
6
+ rails_version = ENV['RAILS_VERSION'] || 'default'
7
+
8
+ rails = case rails_version
9
+ when 'master'
10
+ { :github => 'rails/rails' }
11
+ when 'default'
12
+ '~> 3.2.0'
13
+ else
14
+ "~> #{rails_version}"
15
+ end
16
+
17
+ gem 'rails', rails
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 MnrUchida
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,65 @@
1
+ # ExtendedJoinsImpl
2
+
3
+ Syntactic sugar for using a subquery and left outer join in ActiveRecord
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'extended_joins_impl'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install extended_joins_impl
18
+
19
+ ## Usage
20
+ In these case
21
+
22
+ class User < ActiveRecord::Base
23
+ belongs_to :type
24
+ has_many :user_notes,
25
+ class_name: "Note",
26
+ primary_key: :code,
27
+ foreign_key: :user_code
28
+
29
+ end
30
+
31
+ class Type < ActiveRecord::Base
32
+ end
33
+
34
+ class Note < ActiveRecord::Base
35
+ end
36
+
37
+ # SELECT users.* FROM users INNER JOIN (SELECT * FROM types) types ON users.type_id = types.id
38
+ User.scoped.extended_joins(:inner, Type.scoped)
39
+
40
+ # SELECT users.* FROM users LEFT OUTER JOIN (SELECT * FROM types) types ON users.type_id = types.id
41
+ User.scoped.extended_joins(:outer, Type.scoped)
42
+
43
+ # SELECT users.* FROM users INNER JOIN (SELECT * FROM types WHERE id = 3) types ON users.type_id = types.id
44
+ User.scoped.extended_joins(:inner, Type.where(id: 3)
45
+
46
+ # SELECT users.* FROM users INNER JOIN (SELECT * FROM types) user_types ON users.type_id = user_types.id
47
+ User.scoped.extended_joins(:inner, Type.scoped, as: "user_types")
48
+
49
+ # SELECT users.* FROM users INNER JOIN (SELECT * FROM notes) notes ON users.code = notes.user_code
50
+ User.scoped.extended_joins(:inner, Note.scoped, on: {code: :user_code})
51
+
52
+ # SELECT users.* FROM users INNER JOIN (SELECT * FROM notes) notes ON users.id = notes.user_id AND notes.user_code = 3
53
+ User.scoped.extended_joins(:inner, Note.scoped, where: {user_code: 3})
54
+
55
+ # if alias name is same as association name
56
+ # SELECT users.* FROM users INNER JOIN (SELECT * FROM notes) notes ON users.code = notes.user_code
57
+ User.scoped.extended_joins(:inner, Note.scoped, as: "user_notes")
58
+
59
+ ## Contributing
60
+
61
+ 1. Fork it ( http://github.com/<my-github-username>/extended_joins_impl/fork )
62
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
63
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
64
+ 4. Push to the branch (`git push origin my-new-feature`)
65
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,10 @@
1
+ # coding: utf-8
2
+ require "bundler/gem_tasks"
3
+
4
+ require 'rspec/core/rake_task'
5
+
6
+ desc "Run specs"
7
+ RSpec::Core::RakeTask.new(:spec)
8
+
9
+ desc 'Default: run specs.'
10
+ task :default => :spec
@@ -0,0 +1,25 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'extended_joins_impl/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "extended_joins_impl"
8
+ spec.version = ExtendedJoinsImpl::VERSION
9
+ spec.authors = ["MnrUchida"]
10
+ spec.email = ["mukenan108@gmail.com"]
11
+ spec.summary = %q{Syntactic sugar for ActiveRecord.}
12
+ spec.description = %q{Syntactic sugar for using a subquery and left outer join in ActiveRecord}
13
+ spec.homepage = ""
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.5"
22
+ spec.add_development_dependency "rake"
23
+ spec.add_development_dependency 'sqlite3'
24
+ spec.add_development_dependency "rspec"
25
+ end
@@ -0,0 +1,3 @@
1
+ module ExtendedJoinsImpl
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,52 @@
1
+ require "extended_joins_impl/version"
2
+
3
+ module ExtendedJoinsImpl
4
+ def extended_joins(join_type, table, params={})
5
+ on_key = params[:on] || default_condition(table.table_name, params[:as])
6
+
7
+ joins_table_name = params[:as] || table.table_name
8
+ table_arel = table.arel.as(joins_table_name.to_s)
9
+
10
+ condition = nil
11
+ on_key.each do |left, right|
12
+ right_value = right.class == Symbol ? table_arel[right.to_sym] : right
13
+
14
+ a_condition = self.arel_table[left.to_sym].eq(right_value)
15
+ if condition.present?
16
+ condition = condition.and(a_condition)
17
+ else
18
+ condition = a_condition
19
+ end
20
+ end
21
+ if params[:where].present?
22
+ params[:where].each {|right, value| condition = condition.and(table_arel[right.to_sym].eq(value)) }
23
+ end
24
+
25
+ join_node = (join_type == :outer) ? Arel::Nodes::OuterJoin : Arel::Nodes::InnerJoin
26
+ join_state = join_node.new( table_arel, Arel::Nodes::On.new(condition))
27
+
28
+ self.joins(join_state.to_sql)
29
+ end
30
+
31
+ private
32
+ def default_condition(table_name, alias_name)
33
+ association = reflect_on_association(alias_name.to_sym) if alias_name.present?
34
+ if association.blank?
35
+ association = reflect_on_association(table_name.singularize.to_sym) ||
36
+ reflect_on_association(table_name.to_sym)
37
+ end
38
+
39
+ options = association.try(:options) || {}
40
+ if association.try(:macro) == :belongs_to
41
+ {(options[:foreign_key] || "#{table_name.singularize}_id") => (options[:primary_key] || :id)}
42
+ else
43
+ {(options[:primary_key] || :id) => (options[:foreign_key] || "#{self.table_name.singularize}_id".to_sym)}
44
+ end
45
+ end
46
+ end
47
+
48
+ module ActiveRecord
49
+ class Relation
50
+ include ExtendedJoinsImpl
51
+ end
52
+ end
@@ -0,0 +1,53 @@
1
+ # coding: utf-8
2
+ require 'spec_helper'
3
+ require 'extended_joins_impl'
4
+
5
+ describe ExtendedJoinsImpl do
6
+
7
+ it "inner join" do
8
+ sql = User.where(nil).extended_joins(:inner, Type.where(nil)).to_sql
9
+ expect(sql).to include "INNER JOIN"
10
+ expect(sql).to include "\"users\".\"type_id\" = types.\"id\""
11
+ end
12
+
13
+ it "left outer join" do
14
+ sql = User.where(nil).extended_joins(:outer, Type.where(nil)).to_sql
15
+ expect(sql).to include "LEFT OUTER JOIN"
16
+ expect(sql).to include "\"users\".\"type_id\" = types.\"id\""
17
+ end
18
+
19
+ it "subquery join" do
20
+ sql = User.where(nil).extended_joins(:outer, Type.where(id: 3)).to_sql
21
+ expect(sql).to include "LEFT OUTER JOIN"
22
+ expect(sql).to include "\"users\".\"type_id\" = types.\"id\""
23
+ expect(sql).to include "WHERE \"types\".\"id\" = 3"
24
+ end
25
+
26
+ it "join alias" do
27
+ sql = User.where(nil).extended_joins(:outer, Type.where(id: 3), as: "user_types").to_sql
28
+ expect(sql).to include "LEFT OUTER JOIN"
29
+ expect(sql).to include "\"users\".\"type_id\" = user_types.\"id\""
30
+ expect(sql).to include "WHERE \"types\".\"id\" = 3"
31
+ end
32
+
33
+ it "join on condition" do
34
+ sql = User.where(nil).extended_joins(:inner, Note.where(nil), on: {code: :user_code}).to_sql
35
+ expect(sql).to include "INNER JOIN"
36
+ expect(sql).to include "\"users\".\"code\" = notes.\"user_code\""
37
+ end
38
+
39
+ it "join on constant" do
40
+ sql = User.where(nil).extended_joins(:inner, Note.where(nil), where: {id: 3, user_code: 4}).to_sql
41
+ expect(sql).to include "INNER JOIN"
42
+ expect(sql).to include "\"users\".\"id\" = notes.\"user_id\""
43
+ expect(sql).to include "notes.\"id\" = 3"
44
+ expect(sql).to include "notes.\"user_code\" = 4"
45
+ end
46
+
47
+ it "join with association" do
48
+ sql = User.where(nil).extended_joins(:inner, Note.where(nil), as: "user_notes").to_sql
49
+ expect(sql).to include "INNER JOIN"
50
+ expect(sql).to include "\"users\".\"code\" = user_notes.\"user_code\""
51
+ end
52
+
53
+ end
@@ -0,0 +1,5 @@
1
+ ## coding: utf-8
2
+ require 'rubygems'
3
+ require 'active_record'
4
+ require File.expand_path('../support/schema.rb', __FILE__)
5
+ require File.expand_path('../support/models.rb', __FILE__)
@@ -0,0 +1,15 @@
1
+ # coding: utf-8
2
+ class User < ActiveRecord::Base
3
+ belongs_to :type
4
+ has_many :user_notes,
5
+ class_name: "Note",
6
+ primary_key: :code,
7
+ foreign_key: :user_code
8
+
9
+ end
10
+
11
+ class Type < ActiveRecord::Base
12
+ end
13
+
14
+ class Note < ActiveRecord::Base
15
+ end
@@ -0,0 +1,22 @@
1
+ # coding: utf-8
2
+ ActiveRecord::Base.establish_connection(
3
+ :adapter => 'sqlite3',
4
+ :database => ':memory:'
5
+ )
6
+
7
+ ActiveRecord::Schema.define do
8
+ create_table :users, :force => true do |t|
9
+ t.references :type
10
+ t.integer :code
11
+ t.timestamps
12
+ end
13
+
14
+ create_table :types, :force => true do |t|
15
+ t.timestamps
16
+ end
17
+
18
+ create_table :notes, :force => true do |t|
19
+ t.references :user
20
+ t.integer :user_code
21
+ end
22
+ end
metadata ADDED
@@ -0,0 +1,118 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: extended_joins_impl
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - MnrUchida
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-04-01 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.5'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.5'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
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: sqlite3
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec
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
+ description: Syntactic sugar for using a subquery and left outer join in ActiveRecord
70
+ email:
71
+ - mukenan108@gmail.com
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - ".gitignore"
77
+ - ".travis.yml"
78
+ - Gemfile
79
+ - LICENSE.txt
80
+ - README.md
81
+ - Rakefile
82
+ - extended_joins_impl.gemspec
83
+ - lib/extended_joins_impl.rb
84
+ - lib/extended_joins_impl/version.rb
85
+ - spec/extended_joins_impl_spec.rb
86
+ - spec/spec_helper.rb
87
+ - spec/support/models.rb
88
+ - spec/support/schema.rb
89
+ homepage: ''
90
+ licenses:
91
+ - MIT
92
+ metadata: {}
93
+ post_install_message:
94
+ rdoc_options: []
95
+ require_paths:
96
+ - lib
97
+ required_ruby_version: !ruby/object:Gem::Requirement
98
+ requirements:
99
+ - - ">="
100
+ - !ruby/object:Gem::Version
101
+ version: '0'
102
+ required_rubygems_version: !ruby/object:Gem::Requirement
103
+ requirements:
104
+ - - ">="
105
+ - !ruby/object:Gem::Version
106
+ version: '0'
107
+ requirements: []
108
+ rubyforge_project:
109
+ rubygems_version: 2.2.0
110
+ signing_key:
111
+ specification_version: 4
112
+ summary: Syntactic sugar for ActiveRecord.
113
+ test_files:
114
+ - spec/extended_joins_impl_spec.rb
115
+ - spec/spec_helper.rb
116
+ - spec/support/models.rb
117
+ - spec/support/schema.rb
118
+ has_rdoc: