arel-helpers 2.0.0 → 2.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/History.txt +6 -1
- data/Rakefile +13 -0
- data/lib/arel-helpers.rb +4 -0
- data/lib/arel-helpers/join_association.rb +46 -46
- data/lib/arel-helpers/version.rb +2 -2
- data/spec/env.rb +40 -0
- data/spec/env/migrations.rb +42 -0
- data/spec/env/models.rb +29 -0
- data/spec/join_association_spec.rb +1 -1
- data/spec/spec_helper.rb +7 -71
- metadata +5 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: ea16c7f7e25c5cfbfffefce34a8c9533b0843476
|
4
|
+
data.tar.gz: 1166b9bbb46d3752cd1dfbbbdca2af9722a1dd71
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: d84deff5d029af7d5309206e2ad6d085b606cdf4e8f71f75339ccd1dad828f03fdf625021110fcbf20768fb7d77122877a649d9eaea533fc043acdd9f683d28f
|
7
|
+
data.tar.gz: e3fa334933b5792e55d222696401c53bda1ace66ec973da7c27b5fe738f8506269434265cceda4aefb38b71c1bc771288520ebebbdd391004bd0ee088d26deef
|
data/History.txt
CHANGED
@@ -12,4 +12,9 @@
|
|
12
12
|
|
13
13
|
== 2.0.0
|
14
14
|
|
15
|
-
* Turning JoinAssociation into an ActiveSupport::Concern (breaks backwards compatibility).
|
15
|
+
* Turning JoinAssociation into an ActiveSupport::Concern (breaks backwards compatibility).
|
16
|
+
|
17
|
+
== 2.0.1
|
18
|
+
|
19
|
+
* Define ArelHelpers.join_association so people can use join_association functionality without relying on autoloading. (@peeja, github issue #8)
|
20
|
+
|
data/Rakefile
CHANGED
@@ -16,3 +16,16 @@ desc 'Run specs'
|
|
16
16
|
RSpec::Core::RakeTask.new do |t|
|
17
17
|
t.pattern = './spec/**/*_spec.rb'
|
18
18
|
end
|
19
|
+
|
20
|
+
task :console do
|
21
|
+
$:.push(File.dirname(__FILE__))
|
22
|
+
|
23
|
+
require 'spec/env'
|
24
|
+
require 'pry-nav'
|
25
|
+
|
26
|
+
ArelHelpers::Env.establish_connection
|
27
|
+
ArelHelpers::Env.reset
|
28
|
+
ArelHelpers::Env.migrate
|
29
|
+
|
30
|
+
Pry.start
|
31
|
+
end
|
data/lib/arel-helpers.rb
CHANGED
@@ -6,4 +6,8 @@ module ArelHelpers
|
|
6
6
|
autoload :JoinAssociation, "arel-helpers/join_association"
|
7
7
|
autoload :ArelTable, "arel-helpers/arel_table"
|
8
8
|
autoload :QueryBuilder, "arel-helpers/query_builder"
|
9
|
+
|
10
|
+
def self.join_association(*args, &block)
|
11
|
+
ArelHelpers::JoinAssociation.join_association(*args, &block)
|
12
|
+
end
|
9
13
|
end
|
@@ -5,63 +5,63 @@ module ArelHelpers
|
|
5
5
|
module JoinAssociation
|
6
6
|
extend ActiveSupport::Concern
|
7
7
|
|
8
|
-
|
9
|
-
def
|
10
|
-
ArelHelpers.join_association(self,
|
8
|
+
module ClassMethods
|
9
|
+
def join_association(*args, &block)
|
10
|
+
ArelHelpers::JoinAssociation.join_association(self, *args, &block)
|
11
11
|
end
|
12
12
|
end
|
13
13
|
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
end
|
14
|
+
class << self
|
15
|
+
# activerecord uses JoinDependency to automagically generate inner join statements for
|
16
|
+
# any type of association (belongs_to, has_many, and has_and_belongs_to_many).
|
17
|
+
# For example, for HABTM associations, two join statements are required.
|
18
|
+
# This method encapsulates that functionality and yields an intermediate object for chaining.
|
19
|
+
# It also allows you to use an outer join instead of the default inner via the join_type arg.
|
20
|
+
def join_association(table, association, join_type = Arel::InnerJoin, &block)
|
21
|
+
if ActiveRecord::VERSION::STRING >= '4.1.0'
|
22
|
+
join_association_4_1(table, association, join_type, &block)
|
23
|
+
else
|
24
|
+
join_association_3_1(table, association, join_type, &block)
|
25
|
+
end
|
26
|
+
end
|
28
27
|
|
29
|
-
|
28
|
+
private
|
30
29
|
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
30
|
+
def join_association_3_1(table, association, join_type)
|
31
|
+
associations = association.is_a?(Array) ? association : [association]
|
32
|
+
join_dependency = ActiveRecord::Associations::JoinDependency.new(table, associations, [])
|
33
|
+
manager = Arel::SelectManager.new(table)
|
35
34
|
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
35
|
+
join_dependency.join_associations.each do |assoc|
|
36
|
+
assoc.join_type = join_type
|
37
|
+
assoc.join_to(manager)
|
38
|
+
end
|
40
39
|
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
40
|
+
manager.join_sources.map do |assoc|
|
41
|
+
if block_given?
|
42
|
+
# yield |assoc_name, join_conditions|
|
43
|
+
right = yield assoc.left.name.to_sym, assoc.right
|
44
|
+
assoc.class.new(assoc.left, right)
|
45
|
+
else
|
46
|
+
assoc
|
47
|
+
end
|
48
|
+
end
|
48
49
|
end
|
49
|
-
end
|
50
|
-
end
|
51
50
|
|
52
|
-
|
53
|
-
|
54
|
-
|
51
|
+
def join_association_4_1(table, association, join_type)
|
52
|
+
associations = association.is_a?(Array) ? association : [association]
|
53
|
+
join_dependency = ActiveRecord::Associations::JoinDependency.new(table, associations, [])
|
55
54
|
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
55
|
+
join_dependency.join_constraints([]).map do |constraint|
|
56
|
+
right = if block_given?
|
57
|
+
yield constraint.left.name.to_sym, constraint.right
|
58
|
+
else
|
59
|
+
constraint.right
|
60
|
+
end
|
62
61
|
|
63
|
-
|
62
|
+
join_type.new(constraint.left, right)
|
63
|
+
end
|
64
|
+
end
|
64
65
|
end
|
65
66
|
end
|
66
|
-
|
67
67
|
end
|
data/lib/arel-helpers/version.rb
CHANGED
data/spec/env.rb
ADDED
@@ -0,0 +1,40 @@
|
|
1
|
+
# encoding: UTF-8
|
2
|
+
|
3
|
+
require 'env/models'
|
4
|
+
require 'env/migrations'
|
5
|
+
|
6
|
+
module ArelHelpers
|
7
|
+
class Env
|
8
|
+
class << self
|
9
|
+
|
10
|
+
def db_dir
|
11
|
+
@db_dir ||= File.join(File.dirname(File.dirname(__FILE__)), "tmp")
|
12
|
+
end
|
13
|
+
|
14
|
+
def db_file
|
15
|
+
@db_file ||= File.join(db_dir, "test.sqlite3")
|
16
|
+
end
|
17
|
+
|
18
|
+
def establish_connection
|
19
|
+
ActiveRecord::Base.establish_connection(
|
20
|
+
:adapter => "sqlite3",
|
21
|
+
:database => db_file
|
22
|
+
)
|
23
|
+
end
|
24
|
+
|
25
|
+
def migrate
|
26
|
+
CreatePostsTable.new.change
|
27
|
+
CreateCommentsTable.new.change
|
28
|
+
CreateAuthorsTable.new.change
|
29
|
+
CreateFavoritesTable.new.change
|
30
|
+
CreateCollabPostsTable.new.change
|
31
|
+
end
|
32
|
+
|
33
|
+
def reset
|
34
|
+
File.unlink(db_file) if File.exist?(db_file)
|
35
|
+
FileUtils.mkdir_p(db_dir)
|
36
|
+
end
|
37
|
+
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
@@ -0,0 +1,42 @@
|
|
1
|
+
# encoding: UTF-8
|
2
|
+
|
3
|
+
class CreatePostsTable < ActiveRecord::Migration
|
4
|
+
def change
|
5
|
+
create_table :posts do |t|
|
6
|
+
t.column :title, :string
|
7
|
+
end
|
8
|
+
end
|
9
|
+
end
|
10
|
+
|
11
|
+
class CreateCommentsTable < ActiveRecord::Migration
|
12
|
+
def change
|
13
|
+
create_table :comments do |t|
|
14
|
+
t.references :post
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
class CreateAuthorsTable < ActiveRecord::Migration
|
20
|
+
def change
|
21
|
+
create_table :authors do |t|
|
22
|
+
t.references :comment
|
23
|
+
t.references :collab_posts
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
class CreateFavoritesTable < ActiveRecord::Migration
|
29
|
+
def change
|
30
|
+
create_table :favorites do |t|
|
31
|
+
t.references :post
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
class CreateCollabPostsTable < ActiveRecord::Migration
|
37
|
+
def change
|
38
|
+
create_table :collab_posts do |t|
|
39
|
+
t.references :authors
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
data/spec/env/models.rb
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
# encoding: UTF-8
|
2
|
+
|
3
|
+
class Post < ActiveRecord::Base
|
4
|
+
include ArelHelpers::ArelTable
|
5
|
+
has_many :comments
|
6
|
+
has_many :favorites
|
7
|
+
end
|
8
|
+
|
9
|
+
class Comment < ActiveRecord::Base
|
10
|
+
include ArelHelpers::ArelTable
|
11
|
+
belongs_to :post
|
12
|
+
belongs_to :author
|
13
|
+
end
|
14
|
+
|
15
|
+
class Author < ActiveRecord::Base
|
16
|
+
include ArelHelpers::ArelTable
|
17
|
+
has_one :comment
|
18
|
+
has_and_belongs_to_many :collab_posts
|
19
|
+
end
|
20
|
+
|
21
|
+
class Favorite < ActiveRecord::Base
|
22
|
+
include ArelHelpers::ArelTable
|
23
|
+
belongs_to :post
|
24
|
+
end
|
25
|
+
|
26
|
+
class CollabPost < ActiveRecord::Base
|
27
|
+
include ArelHelpers::ArelTable
|
28
|
+
has_and_belongs_to_many :authors
|
29
|
+
end
|
@@ -26,7 +26,7 @@ describe ArelHelpers do
|
|
26
26
|
.joins(ArelHelpers.join_association(Post, :comments))
|
27
27
|
.joins(ArelHelpers.join_association(Comment, :author))
|
28
28
|
.to_sql.should ==
|
29
|
-
'SELECT "posts".* FROM "posts" INNER JOIN "comments" ON "comments"."post_id" = "posts"."id" INNER JOIN "authors" ON "authors"."
|
29
|
+
'SELECT "posts".* FROM "posts" INNER JOIN "comments" ON "comments"."post_id" = "posts"."id" INNER JOIN "authors" ON "authors"."id" = "comments"."author_id"'
|
30
30
|
end
|
31
31
|
|
32
32
|
it "should be able to handle multiple associations" do
|
data/spec/spec_helper.rb
CHANGED
@@ -1,10 +1,14 @@
|
|
1
1
|
# encoding: UTF-8
|
2
2
|
|
3
|
+
$:.push(File.dirname(__FILE__))
|
4
|
+
|
3
5
|
require 'rspec'
|
4
6
|
require 'arel-helpers'
|
5
7
|
require 'fileutils'
|
6
8
|
require 'pry-nav'
|
7
9
|
|
10
|
+
require 'env'
|
11
|
+
|
8
12
|
def silence(&block)
|
9
13
|
original_stdout = $stdout
|
10
14
|
$stdout = StringIO.new
|
@@ -15,80 +19,12 @@ def silence(&block)
|
|
15
19
|
end
|
16
20
|
end
|
17
21
|
|
18
|
-
class Post < ActiveRecord::Base
|
19
|
-
include ArelHelpers::ArelTable
|
20
|
-
has_many :comments
|
21
|
-
has_many :favorites
|
22
|
-
end
|
23
|
-
|
24
|
-
class Comment < ActiveRecord::Base
|
25
|
-
include ArelHelpers::ArelTable
|
26
|
-
belongs_to :post
|
27
|
-
has_one :author
|
28
|
-
end
|
29
|
-
|
30
|
-
class Author < ActiveRecord::Base
|
31
|
-
include ArelHelpers::ArelTable
|
32
|
-
belongs_to :comment
|
33
|
-
end
|
34
|
-
|
35
|
-
class Favorite < ActiveRecord::Base
|
36
|
-
include ArelHelpers::ArelTable
|
37
|
-
belongs_to :post
|
38
|
-
end
|
39
|
-
|
40
|
-
class CreatePostsTable < ActiveRecord::Migration
|
41
|
-
def change
|
42
|
-
create_table :posts do |t|
|
43
|
-
t.column :title, :string
|
44
|
-
end
|
45
|
-
end
|
46
|
-
end
|
47
|
-
|
48
|
-
class CreateCommentsTable < ActiveRecord::Migration
|
49
|
-
def change
|
50
|
-
create_table :comments do |t|
|
51
|
-
t.references :post
|
52
|
-
end
|
53
|
-
end
|
54
|
-
end
|
55
|
-
|
56
|
-
class CreateAuthorsTable < ActiveRecord::Migration
|
57
|
-
def change
|
58
|
-
create_table :authors do |t|
|
59
|
-
t.references :comment
|
60
|
-
end
|
61
|
-
end
|
62
|
-
end
|
63
|
-
|
64
|
-
class CreateFavoritesTable < ActiveRecord::Migration
|
65
|
-
def change
|
66
|
-
create_table :favorites do |t|
|
67
|
-
t.references :post
|
68
|
-
end
|
69
|
-
end
|
70
|
-
end
|
71
|
-
|
72
22
|
RSpec.configure do |config|
|
73
23
|
config.mock_with :rr
|
74
24
|
|
75
|
-
db_dir = File.join(File.dirname(File.dirname(__FILE__)), "tmp")
|
76
|
-
db_file = File.join(db_dir, "test.sqlite3")
|
77
|
-
|
78
25
|
config.before(:each) do
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
ActiveRecord::Base.establish_connection(
|
83
|
-
:adapter => "sqlite3",
|
84
|
-
:database => db_file
|
85
|
-
)
|
86
|
-
|
87
|
-
silence do
|
88
|
-
CreatePostsTable.new.change
|
89
|
-
CreateCommentsTable.new.change
|
90
|
-
CreateAuthorsTable.new.change
|
91
|
-
CreateFavoritesTable.new.change
|
92
|
-
end
|
26
|
+
ArelHelpers::Env.establish_connection
|
27
|
+
ArelHelpers::Env.reset
|
28
|
+
silence { ArelHelpers::Env.migrate }
|
93
29
|
end
|
94
30
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: arel-helpers
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 2.0.
|
4
|
+
version: 2.0.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Cameron Dutro
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-
|
11
|
+
date: 2014-07-28 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activerecord
|
@@ -49,6 +49,9 @@ files:
|
|
49
49
|
- lib/arel-helpers/query_builder.rb
|
50
50
|
- lib/arel-helpers/version.rb
|
51
51
|
- spec/arel_table_spec.rb
|
52
|
+
- spec/env.rb
|
53
|
+
- spec/env/migrations.rb
|
54
|
+
- spec/env/models.rb
|
52
55
|
- spec/join_association_spec.rb
|
53
56
|
- spec/query_builder_spec.rb
|
54
57
|
- spec/spec_helper.rb
|