arel-helpers 2.5.0 → 2.6.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
- SHA1:
3
- metadata.gz: f1009f54fd7f6ec015f38a4c4c37321b74aa5a23
4
- data.tar.gz: 4d7b85881fc754aa00ad80b869ca3abf6c14c6d1
2
+ SHA256:
3
+ metadata.gz: c660ae1fe4060cdb950996cc25907f683ccc785701c7cebd7c80bd426a3bb9ea
4
+ data.tar.gz: 2496cce6af26bb6c243e1947e32b800cb84b9bc3bdc134f59e3c2480521eb5f8
5
5
  SHA512:
6
- metadata.gz: 130ce322efa3f3cde98f37ce8312487f64bf9b80f597893dc98e4ca0df5ab89b1aabe9173015b45baf869388a8a5386b1fbb8797ffbe7c4b34260ce6f691e53f
7
- data.tar.gz: 75deca0bf9ac64644da21446eda134b3815627650bfd52c6087551ede0ef9369025b2dccc64cf8650e53eaeb882e938e8fdb67dfdbe0cc5ebf1200dfb08f8eec
6
+ metadata.gz: 397035a79ac5c8d9ec98858d76e028dc421d247d8c342131b4ca7b1e8bcd790698ed6400fca56ca1769785cb7dfa9a15aeae1fe0b765f7092b367c98ed5e5879
7
+ data.tar.gz: 302f2730474bd9f929f36ee9bc10a91b13cd904ccce6963bb072de61b1c21f0526ea0221e3637f4cc10745d47e9e21fb140e100ee7482eafd2b19b463062227a
data/Gemfile CHANGED
@@ -3,8 +3,10 @@ source "https://rubygems.org"
3
3
  gemspec
4
4
 
5
5
  group :development, :test do
6
- gem 'pry-nav'
7
- gem 'rake'
6
+ gem 'pry-byebug'
7
+
8
+ # lock to 10.0 until rspec is upgraded
9
+ gem 'rake', '~> 10.0'
8
10
  end
9
11
 
10
12
  group :test do
data/Rakefile CHANGED
@@ -10,7 +10,7 @@ require './lib/arel-helpers'
10
10
 
11
11
  Bundler::GemHelper.install_tasks
12
12
 
13
- task :default => :spec
13
+ task default: :spec
14
14
 
15
15
  desc 'Run specs'
16
16
  RSpec::Core::RakeTask.new do |t|
@@ -21,7 +21,7 @@ task :console do
21
21
  $:.push(File.dirname(__FILE__))
22
22
 
23
23
  require 'spec/env'
24
- require 'pry-nav'
24
+ require 'pry-byebug'
25
25
 
26
26
  ArelHelpers::Env.establish_connection
27
27
  ArelHelpers::Env.reset
@@ -0,0 +1,21 @@
1
+ # encoding: UTF-8
2
+
3
+ module ArelHelpers
4
+
5
+ module Aliases
6
+ extend ActiveSupport::Concern
7
+
8
+ module ClassMethods
9
+ def aliased_as(*args)
10
+ aliases = args.map { |name| arel_table.alias(name) }
11
+
12
+ if block_given?
13
+ yield *aliases
14
+ else
15
+ aliases
16
+ end
17
+ end
18
+ end
19
+ end
20
+
21
+ end
@@ -17,21 +17,22 @@ module ArelHelpers
17
17
  # For example, for HABTM associations, two join statements are required.
18
18
  # This method encapsulates that functionality and yields an intermediate object for chaining.
19
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::Nodes::InnerJoin, &block)
20
+ def join_association(table, association, join_type = Arel::Nodes::InnerJoin, options = {}, &block)
21
21
  if ActiveRecord::VERSION::STRING >= '5.0.0'
22
- join_association_5_0(table, association, join_type, &block)
22
+ join_association_5_0(table, association, join_type, options, &block)
23
23
  elsif ActiveRecord::VERSION::STRING >= '4.2.0'
24
- join_association_4_2(table, association, join_type, &block)
24
+ join_association_4_2(table, association, join_type, options, &block)
25
25
  elsif ActiveRecord::VERSION::STRING >= '4.1.0'
26
- join_association_4_1(table, association, join_type, &block)
26
+ join_association_4_1(table, association, join_type, options, &block)
27
27
  else
28
- join_association_3_1(table, association, join_type, &block)
28
+ join_association_3_1(table, association, join_type, options, &block)
29
29
  end
30
30
  end
31
31
 
32
32
  private
33
33
 
34
- def join_association_3_1(table, association, join_type)
34
+ def join_association_3_1(table, association, join_type, options = {})
35
+ aliases = options.fetch(:aliases, {})
35
36
  associations = association.is_a?(Array) ? association : [association]
36
37
  join_dependency = ActiveRecord::Associations::JoinDependency.new(table, associations, [])
37
38
  manager = Arel::SelectManager.new(table)
@@ -42,6 +43,10 @@ module ArelHelpers
42
43
  end
43
44
 
44
45
  manager.join_sources.map do |assoc|
46
+ if found_alias = find_alias(assoc.left.name, aliases)
47
+ assoc.left.table_alias = found_alias.name
48
+ end
49
+
45
50
  if block_given?
46
51
  # yield |assoc_name, join_conditions|
47
52
  right = yield assoc.left.name.to_sym, assoc.right
@@ -52,7 +57,8 @@ module ArelHelpers
52
57
  end
53
58
  end
54
59
 
55
- def join_association_4_1(table, association, join_type)
60
+ def join_association_4_1(table, association, join_type, options = {})
61
+ aliases = options.fetch(:aliases, {})
56
62
  associations = association.is_a?(Array) ? association : [association]
57
63
  join_dependency = ActiveRecord::Associations::JoinDependency.new(table, associations, [])
58
64
 
@@ -63,6 +69,10 @@ module ArelHelpers
63
69
  constraint.right
64
70
  end
65
71
 
72
+ if found_alias = find_alias(constraint.left.name, aliases)
73
+ constraint.left.table_alias = found_alias.name
74
+ end
75
+
66
76
  join_type.new(constraint.left, right)
67
77
  end
68
78
  end
@@ -72,7 +82,8 @@ module ArelHelpers
72
82
  # join_association isn't able to add to the list of bind variables
73
83
  # dynamically. To get around the problem, this method must return
74
84
  # a string.
75
- def join_association_4_2(table, association, join_type)
85
+ def join_association_4_2(table, association, join_type, options = {})
86
+ aliases = options.fetch(:aliases, [])
76
87
  associations = association.is_a?(Array) ? association : [association]
77
88
  join_dependency = ActiveRecord::Associations::JoinDependency.new(table, associations, [])
78
89
 
@@ -90,6 +101,10 @@ module ArelHelpers
90
101
  join.right
91
102
  end
92
103
 
104
+ if found_alias = find_alias(join.left.name, aliases)
105
+ join.left.table_alias = found_alias.name
106
+ end
107
+
93
108
  join_type.new(join.left, right)
94
109
  end
95
110
  end
@@ -101,7 +116,8 @@ module ArelHelpers
101
116
  join_strings.join(' ')
102
117
  end
103
118
 
104
- def join_association_5_0(table, association, join_type)
119
+ def join_association_5_0(table, association, join_type, options = {})
120
+ aliases = options.fetch(:aliases, [])
105
121
  associations = association.is_a?(Array) ? association : [association]
106
122
  join_dependency = ActiveRecord::Associations::JoinDependency.new(table, associations, [])
107
123
 
@@ -120,6 +136,10 @@ module ArelHelpers
120
136
  join.right
121
137
  end
122
138
 
139
+ if found_alias = find_alias(join.left.name, aliases)
140
+ join.left.table_alias = found_alias.name
141
+ end
142
+
123
143
  join_type.new(join.left, right)
124
144
  end
125
145
  end
@@ -138,6 +158,10 @@ module ArelHelpers
138
158
  collect = visitor.accept(node, Arel::Collectors::Bind.new)
139
159
  collect.substitute_binds(binds).join
140
160
  end
161
+
162
+ def find_alias(name, aliases)
163
+ aliases.find { |a| a.table_name == name }
164
+ end
141
165
  end
142
166
  end
143
167
  end
@@ -1,5 +1,5 @@
1
1
  # encoding: UTF-8
2
2
 
3
3
  module ArelHelpers
4
- VERSION = '2.5.0'
4
+ VERSION = '2.6.0'
5
5
  end
data/lib/arel-helpers.rb CHANGED
@@ -17,8 +17,9 @@ rescue
17
17
  end
18
18
 
19
19
  module ArelHelpers
20
- autoload :JoinAssociation, "arel-helpers/join_association"
20
+ autoload :Aliases, "arel-helpers/aliases"
21
21
  autoload :ArelTable, "arel-helpers/arel_table"
22
+ autoload :JoinAssociation, "arel-helpers/join_association"
22
23
  autoload :QueryBuilder, "arel-helpers/query_builder"
23
24
 
24
25
  def self.join_association(*args, &block)
@@ -0,0 +1,42 @@
1
+ # encoding: UTF-8
2
+
3
+ require 'spec_helper'
4
+
5
+ describe ArelHelpers::Aliases do
6
+ describe "#aliased_as" do
7
+ it "yields an alias when passed a block" do
8
+ Post.aliased_as('foo') do |foo_alias|
9
+ expect(foo_alias).to be_a(Arel::Nodes::TableAlias)
10
+ expect(foo_alias.name).to eq('foo')
11
+ end
12
+ end
13
+
14
+ it "is capable of yielding multiple aliases" do
15
+ Post.aliased_as('foo', 'bar') do |foo_alias, bar_alias|
16
+ expect(foo_alias).to be_a(Arel::Nodes::TableAlias)
17
+ expect(foo_alias.name).to eq('foo')
18
+
19
+ expect(bar_alias).to be_a(Arel::Nodes::TableAlias)
20
+ expect(bar_alias.name).to eq('bar')
21
+ end
22
+ end
23
+
24
+ it "returns an alias when not passed a block" do
25
+ aliases = Post.aliased_as('foo')
26
+ expect(aliases.size).to eq(1)
27
+ expect(aliases[0]).to be_a(Arel::Nodes::TableAlias)
28
+ expect(aliases[0].name).to eq('foo')
29
+ end
30
+
31
+ it "is capable of returning multiple aliases" do
32
+ aliases = Post.aliased_as('foo', 'bar')
33
+ expect(aliases.size).to eq(2)
34
+
35
+ expect(aliases[0]).to be_a(Arel::Nodes::TableAlias)
36
+ expect(aliases[0].name).to eq('foo')
37
+
38
+ expect(aliases[1]).to be_a(Arel::Nodes::TableAlias)
39
+ expect(aliases[1].name).to eq('bar')
40
+ end
41
+ end
42
+ end
data/spec/env/models.rb CHANGED
@@ -2,29 +2,34 @@
2
2
 
3
3
  class Post < ActiveRecord::Base
4
4
  include ArelHelpers::ArelTable
5
+ include ArelHelpers::Aliases
5
6
  has_many :comments
6
7
  has_many :favorites
7
8
  end
8
9
 
9
10
  class Comment < ActiveRecord::Base
10
11
  include ArelHelpers::ArelTable
12
+ include ArelHelpers::Aliases
11
13
  belongs_to :post
12
14
  belongs_to :author
13
15
  end
14
16
 
15
17
  class Author < ActiveRecord::Base
16
18
  include ArelHelpers::ArelTable
19
+ include ArelHelpers::Aliases
17
20
  has_one :comment
18
21
  has_and_belongs_to_many :collab_posts
19
22
  end
20
23
 
21
24
  class Favorite < ActiveRecord::Base
22
25
  include ArelHelpers::ArelTable
26
+ include ArelHelpers::Aliases
23
27
  belongs_to :post
24
28
  end
25
29
 
26
30
  class CollabPost < ActiveRecord::Base
27
31
  include ArelHelpers::ArelTable
32
+ include ArelHelpers::Aliases
28
33
  has_and_belongs_to_many :authors
29
34
  end
30
35
 
data/spec/env.rb CHANGED
@@ -1,7 +1,7 @@
1
1
  # encoding: UTF-8
2
2
 
3
- require 'env/models'
4
3
  require 'env/migrations'
4
+ require 'env/models'
5
5
 
6
6
  module ArelHelpers
7
7
  class Env
@@ -65,6 +65,22 @@ describe ArelHelpers do
65
65
  'SELECT "collab_posts".* FROM "collab_posts" INNER JOIN "authors_collab_posts" ON "authors_collab_posts"."collab_post_id" = "collab_posts"."id" INNER JOIN "authors" ON "authors"."id" = "authors_collab_posts"."author_id"'
66
66
  end
67
67
 
68
+ it "allows adding a custom alias to the joined table" do
69
+ Comment.aliased_as(:foo) do |foo|
70
+ Post.joins(ArelHelpers.join_association(Post, :comments, Arel::Nodes::InnerJoin, aliases: [foo])).to_sql.should ==
71
+ 'SELECT "posts".* FROM "posts" INNER JOIN "comments" "foo" ON "foo"."post_id" = "posts"."id"'
72
+ end
73
+ end
74
+
75
+ it "allows aliasing multiple associations" do
76
+ Comment.aliased_as(:foo) do |foo|
77
+ Favorite.aliased_as(:bar) do |bar|
78
+ Post.joins(ArelHelpers.join_association(Post, [:comments, :favorites], Arel::Nodes::InnerJoin, aliases: [foo, bar])).to_sql.should ==
79
+ 'SELECT "posts".* FROM "posts" INNER JOIN "comments" "foo" ON "foo"."post_id" = "posts"."id" INNER JOIN "favorites" "bar" ON "bar"."post_id" = "posts"."id"'
80
+ end
81
+ end
82
+ end
83
+
68
84
  it 'handles polymorphic through associations' do
69
85
  relation = Location.joins(
70
86
  ArelHelpers.join_association(Location, :community_tickets)
data/spec/spec_helper.rb CHANGED
@@ -5,7 +5,7 @@ $:.push(File.dirname(__FILE__))
5
5
  require 'rspec'
6
6
  require 'arel-helpers'
7
7
  require 'fileutils'
8
- require 'pry-nav'
8
+ require 'pry-byebug'
9
9
 
10
10
  require 'env'
11
11
 
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.5.0
4
+ version: 2.6.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Cameron Dutro
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-10-07 00:00:00.000000000 Z
11
+ date: 2018-01-18 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord
@@ -39,16 +39,17 @@ extensions: []
39
39
  extra_rdoc_files: []
40
40
  files:
41
41
  - Gemfile
42
- - History.txt
43
42
  - README.md
44
43
  - Rakefile
45
44
  - arel-helpers.gemspec
46
45
  - lib/arel-helpers.rb
46
+ - lib/arel-helpers/aliases.rb
47
47
  - lib/arel-helpers/arel_table.rb
48
48
  - lib/arel-helpers/ext/collection_proxy.rb
49
49
  - lib/arel-helpers/join_association.rb
50
50
  - lib/arel-helpers/query_builder.rb
51
51
  - lib/arel-helpers/version.rb
52
+ - spec/aliases_spec.rb
52
53
  - spec/arel_table_spec.rb
53
54
  - spec/env.rb
54
55
  - spec/env/migrations.rb
@@ -76,7 +77,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
76
77
  version: '0'
77
78
  requirements: []
78
79
  rubyforge_project:
79
- rubygems_version: 2.6.11
80
+ rubygems_version: 2.7.3
80
81
  signing_key:
81
82
  specification_version: 4
82
83
  summary: Useful tools to help construct database queries with ActiveRecord and Arel.
data/History.txt DELETED
@@ -1,49 +0,0 @@
1
- == 1.0.0
2
-
3
- * Birthday! Includes join_association and arel_table helpers.
4
-
5
- == 1.1.0
6
-
7
- * Adding the QueryBuilder class.
8
-
9
- == 1.2.0
10
-
11
- * Adding Rails 4 support.
12
-
13
- == 2.0.0
14
-
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
-
21
- == 2.0.2
22
-
23
- * Fix issue causing CollectionProxy#[] to return Arel::Attribute objects instead of model instances. See https://github.com/camertron/arel-helpers/pull/11
24
-
25
- == 2.1.0
26
-
27
- * Adding support for Rails 4.2 (@hasghari, github issue #12)
28
-
29
- == 2.1.1
30
-
31
- * Fixing issue causing ArelTable instances to get returned when accessing records inside an ActiveRecord::Relation. (@svoynow, #18)
32
-
33
- == 2.2.0
34
-
35
- * Adding polymorphic join support for Rails 4.2.
36
-
37
- == 2.3.0
38
-
39
- * Adding support for Rails 5 (@vkill #24, @camertron #26)
40
-
41
- == 2.4.0
42
-
43
- * Adding support for Rails 5.1 (@hasghari #30)
44
-
45
- == 2.5.0
46
-
47
- * Add license information to gemspec so it is parsed by verifiers (@petergoldstein #31)
48
- * Update QueryBuilder#reflect to create deep copy of builder (@wycleffsean #32)
49
-