better_ar 0.0.8 → 0.0.11

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.
data/HISTORY.txt CHANGED
@@ -1,3 +1,12 @@
1
+ ## 0.0.11
2
+ Ignore polymorphic associations for implcit joins
3
+
4
+ ## 0.0.10
5
+ Fix for inplicit joins
6
+
7
+ ## 0.0.9
8
+ Fix for Arel objects being passed to .all
9
+
1
10
  ## 0.0.8
2
11
  Don't extract joins for values of ActiveRecord::Base
3
12
 
data/better_ar.gemspec CHANGED
@@ -20,4 +20,5 @@ Gem::Specification.new do |s|
20
20
  s.require_paths = ["lib"]
21
21
 
22
22
  s.add_development_dependency 'activerecord', ['~> 3.0.3']
23
+ s.add_development_dependency 'sqlite3'
23
24
  end
@@ -23,14 +23,19 @@ module BetterAr
23
23
  # Optional
24
24
  # @return [ActiveRecord::Relation]
25
25
  def all(opts = {})
26
- if opts.empty?
27
- super()
28
- elsif opts.key?(:conditions)
29
- super(opts)
26
+ if opts.is_a?(Hash)
27
+ if opts.empty?
28
+ super()
29
+ elsif opts.key?(:conditions)
30
+ super(opts)
31
+ else
32
+ relation = clone
33
+ relation = better_ar_apply_sql_clauses(relation, opts)
34
+ relation = better_ar_apply_associations(relation, opts)
35
+ relation.where(opts)
36
+ end
30
37
  else
31
38
  relation = clone
32
- relation = better_ar_apply_sql_clauses(relation, opts)
33
- relation = better_ar_apply_associations(relation, opts)
34
39
  relation.where(opts)
35
40
  end
36
41
  end
@@ -71,8 +76,11 @@ module BetterAr
71
76
  end
72
77
 
73
78
  def better_ar_apply_associations(relation, opts = {})
74
- (reflect_on_all_associations.map(&:name) & better_ar_join_keys(opts)).each do |name|
75
- relation = relation.joins(name)
79
+ reflect_on_all_associations.select do |association|
80
+ !association.options[:polymorphic] &&
81
+ better_ar_join_keys(opts).include?(association.table_name.to_sym)
82
+ end.each do |association|
83
+ relation = relation.joins(association.name)
76
84
  end
77
85
 
78
86
  relation
@@ -1,3 +1,3 @@
1
1
  module BetterAr
2
- VERSION = "0.0.8"
2
+ VERSION = "0.0.11"
3
3
  end
data/test/helper.rb CHANGED
@@ -16,7 +16,7 @@ ActiveRecord::Base.establish_connection(
16
16
  :database => ':memory:'
17
17
  )
18
18
 
19
- users_table = %{CREATE TABLE users (id INTEGER PRIMARY KEY, age INTEGER, name TEXT);}
19
+ users_table = %{CREATE TABLE users (id INTEGER PRIMARY KEY, age INTEGER, name TEXT, object_type TEXT, object_id INTEGER);}
20
20
  records_table = %{CREATE TABLE records (id INTEGER PRIMARY KEY, user_id INTEGER, name TEXT);}
21
21
  reports_table = %{CREATE TABLE reports (id INTEGER PRIMARY KEY, record_id INTEGER, name TEXT);}
22
22
  ActiveRecord::Base.connection.execute(users_table)
@@ -25,6 +25,7 @@ ActiveRecord::Base.connection.execute(reports_table)
25
25
 
26
26
  class User < ActiveRecord::Base
27
27
  has_many :records
28
+ belongs_to :object, :polymorphic => true
28
29
  end
29
30
 
30
31
  class Record < ActiveRecord::Base
@@ -18,12 +18,26 @@ describe 'ActiveRecord::Relation Finder Methods' do
18
18
  test_sql.must_be_like expected_sql
19
19
  end
20
20
 
21
+ it 'finds implicit joins by reflection with singular association name' do
22
+ test_sql = Record.all(:users => { :name => 'test' }).to_sql
23
+ expected_sql = Record.joins(:user).where(:users => { :name => 'test' }).to_sql
24
+ test_sql.must_be_like expected_sql
25
+ end
26
+
21
27
  it 'will ignore implicit joins if value is an instance of ActiveRecord::Base' do
22
28
  user = User.create
23
29
  test_sql = Record.all(:user => user).to_sql
24
30
  expected_sql = Record.where(:user => user).to_sql
25
31
  test_sql.must_be_like expected_sql
26
32
  end
33
+
34
+ it 'still allows arel objects' do
35
+ user = User.create
36
+ relation = Record.arel_table[:user_id].eq(user.id)
37
+ test_sql = Record.all(relation).to_sql
38
+ expected_sql = Record.where(:user_id => user.id).to_sql
39
+ test_sql.must_be_like expected_sql
40
+ end
27
41
  end
28
42
 
29
43
  describe '.first' do
metadata CHANGED
@@ -1,12 +1,8 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: better_ar
3
3
  version: !ruby/object:Gem::Version
4
- prerelease: false
5
- segments:
6
- - 0
7
- - 0
8
- - 8
9
- version: 0.0.8
4
+ prerelease:
5
+ version: 0.0.11
10
6
  platform: ruby
11
7
  authors:
12
8
  - Brian Cardarella
@@ -14,7 +10,7 @@ autorequire:
14
10
  bindir: bin
15
11
  cert_chain: []
16
12
 
17
- date: 2011-02-15 00:00:00 -05:00
13
+ date: 2011-03-12 00:00:00 -05:00
18
14
  default_executable:
19
15
  dependencies:
20
16
  - !ruby/object:Gem::Dependency
@@ -25,13 +21,20 @@ dependencies:
25
21
  requirements:
26
22
  - - ~>
27
23
  - !ruby/object:Gem::Version
28
- segments:
29
- - 3
30
- - 0
31
- - 3
32
24
  version: 3.0.3
33
25
  type: :development
34
26
  version_requirements: *id001
27
+ - !ruby/object:Gem::Dependency
28
+ name: sqlite3
29
+ prerelease: false
30
+ requirement: &id002 !ruby/object:Gem::Requirement
31
+ none: false
32
+ requirements:
33
+ - - ">="
34
+ - !ruby/object:Gem::Version
35
+ version: "0"
36
+ type: :development
37
+ version_requirements: *id002
35
38
  description: Better Active Record finders
36
39
  email:
37
40
  - bcardarella@gmail.com
@@ -72,21 +75,17 @@ required_ruby_version: !ruby/object:Gem::Requirement
72
75
  requirements:
73
76
  - - ">="
74
77
  - !ruby/object:Gem::Version
75
- segments:
76
- - 0
77
78
  version: "0"
78
79
  required_rubygems_version: !ruby/object:Gem::Requirement
79
80
  none: false
80
81
  requirements:
81
82
  - - ">="
82
83
  - !ruby/object:Gem::Version
83
- segments:
84
- - 0
85
84
  version: "0"
86
85
  requirements: []
87
86
 
88
87
  rubyforge_project: better_ar
89
- rubygems_version: 1.3.7
88
+ rubygems_version: 1.5.3
90
89
  signing_key:
91
90
  specification_version: 3
92
91
  summary: Better Active Record finders