arel-helpers 2.0.0 → 2.0.1

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 74fd32a2a3bd64b02be9dbd561afe2add390752f
4
- data.tar.gz: 78f91129ab6b88b3b93675de7a0e96f7ac9ef716
3
+ metadata.gz: ea16c7f7e25c5cfbfffefce34a8c9533b0843476
4
+ data.tar.gz: 1166b9bbb46d3752cd1dfbbbdca2af9722a1dd71
5
5
  SHA512:
6
- metadata.gz: a8082544e84ddca08e248276682473fa8d2af08cd666fb2b76c2588cbf52546c8f9345805693dbd6929ec0a58f64e130f21a7fdb973374feb81f263a2b6fd65c
7
- data.tar.gz: 0fa70969de2f89d8fa03c2b0747ca3da4c011e9399f74b4f9222611401df399bbd24ecd7e2e577e9600de21291a5f99d06559ca0648be1c1770b27ff08f902fa
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
- included do
9
- def self.join_association(association, join_type = Arel::InnerJoin, &block)
10
- ArelHelpers.join_association(self, association, join_type, &block)
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
- end
15
-
16
- # activerecord uses JoinDependency to automagically generate inner join statements for
17
- # any type of association (belongs_to, has_many, and has_and_belongs_to_many).
18
- # For example, for HABTM associations, two join statements are required.
19
- # This method encapsulates that functionality and yields an intermediate object for chaining.
20
- # It also allows you to use an outer join instead of the default inner via the join_type arg.
21
- def self.join_association(table, association, join_type = Arel::InnerJoin, &block)
22
- if ActiveRecord::VERSION::STRING >= '4.1.0'
23
- join_association_4_1(table, association, join_type, &block)
24
- else
25
- join_association_3_1(table, association, join_type, &block)
26
- end
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
- private
28
+ private
30
29
 
31
- def self.join_association_3_1(table, association, join_type)
32
- associations = association.is_a?(Array) ? association : [association]
33
- join_dependency = ActiveRecord::Associations::JoinDependency.new(table, associations, [])
34
- manager = Arel::SelectManager.new(table)
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
- join_dependency.join_associations.each do |assoc|
37
- assoc.join_type = join_type
38
- assoc.join_to(manager)
39
- end
35
+ join_dependency.join_associations.each do |assoc|
36
+ assoc.join_type = join_type
37
+ assoc.join_to(manager)
38
+ end
40
39
 
41
- manager.join_sources.map do |assoc|
42
- if block_given?
43
- # yield |assoc_name, join_conditions|
44
- right = yield assoc.left.name.to_sym, assoc.right
45
- assoc.class.new(assoc.left, right)
46
- else
47
- assoc
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
- def self.join_association_4_1(table, association, join_type)
53
- associations = association.is_a?(Array) ? association : [association]
54
- join_dependency = ActiveRecord::Associations::JoinDependency.new(table, associations, [])
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
- join_dependency.join_constraints([]).map do |constraint|
57
- right = if block_given?
58
- yield constraint.left.name.to_sym, constraint.right
59
- else
60
- constraint.right
61
- end
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
- join_type.new(constraint.left, right)
62
+ join_type.new(constraint.left, right)
63
+ end
64
+ end
64
65
  end
65
66
  end
66
-
67
67
  end
@@ -1,5 +1,5 @@
1
1
  # encoding: UTF-8
2
2
 
3
3
  module ArelHelpers
4
- VERSION = "2.0.0"
5
- end
4
+ VERSION = "2.0.1"
5
+ end
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
@@ -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"."comment_id" = "comments"."id"'
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
- File.unlink(db_file) if File.exist?(db_file)
80
- FileUtils.mkdir_p(db_dir)
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.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-06-22 00:00:00.000000000 Z
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