better_ar 0.2.1 → 0.2.2

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
2
  SHA1:
3
- metadata.gz: c0abd9c4a43817436d719c13997751ca837f7da7
4
- data.tar.gz: 72f4114a37ef358ab72308ff1e88235527273df6
3
+ metadata.gz: 52a23877b85d47400e052e59fd302deb4b4da5fc
4
+ data.tar.gz: 56c8442e720ad9556476e504819cb575faf62f24
5
5
  SHA512:
6
- metadata.gz: f4393b539e0f57df2987caa04c9bdfc55501dcc5362e48fcd942f3f91254d4301152a0dfb16bdcf1db842e9c79f8ad131f382e3119a98adff4d20839ba833673
7
- data.tar.gz: 97695baa4aec0c7cd02b19061b217811526b5d5b7bfda2c2e4c6602bb3f0429caa3110c2d9982016b8889ca857340ebe4b688ca07c70843639fc4cdc1ab90151
6
+ metadata.gz: 5f0f2e515bde835506a7f9a878b18fefba8a61e57d8ec1cdaa3019518bdd79def8404583746f31771cd54385f218a4a87bd6414e4c425c39e9b91b337455b634
7
+ data.tar.gz: bb8f47dc62ff10d5d8ba6cf2255824ea5d9b296cd9f1e14837a605d404038bc4f23dd45f63f573b5e5fbf13d3606ead171e8c64f01935c3105ddfd09ccc69676
@@ -6,4 +6,3 @@ require 'active_record/deprecated_finders'
6
6
  require 'active_record'
7
7
  require 'better_ar/finder_methods'
8
8
  require 'better_ar/calculations'
9
- require 'better_ar/association_collection'
@@ -15,23 +15,19 @@ module BetterAr
15
15
  # @return [Integer]
16
16
  def count(column_name = nil, count_options = {})
17
17
  if column_name.is_a?(Hash)
18
- count_options = column_name
18
+ count_options = column_name.clone
19
19
  column_name = nil
20
20
  end
21
21
 
22
- if count_options.empty?
23
- super
24
- elsif count_options.key?(:conditions)
25
- super(column_name, count_options)
26
- else
27
- all(count_options).count
28
- end
22
+ all(count_options).calculate(:count, nil, {})
29
23
  end
30
24
  end
31
25
  end
32
26
 
33
27
  ActiveSupport.on_load(:active_record) do
28
+ ActiveRecord::Associations::CollectionProxy.send(:prepend, BetterAr::Calculations)
34
29
  class << ActiveRecord::Base
35
30
  prepend BetterAr::Calculations
36
31
  end
32
+ ActiveRecord::Relation.send(:prepend, BetterAr::Calculations)
37
33
  end
@@ -23,21 +23,20 @@ module BetterAr
23
23
  # Optional
24
24
  # @return [ActiveRecord::Relation]
25
25
  def all(opts = {})
26
+ unless relation = better_ar_get_relation
27
+ relation = super().clone
28
+ end
29
+
26
30
  if opts.is_a?(Hash)
27
31
  if opts.empty?
28
- super()
29
- elsif opts.key?(:conditions)
30
- super(opts)
32
+ return relation
31
33
  else
32
- relation = scoped.clone
33
34
  relation = better_ar_apply_sql_clauses(relation, opts)
34
35
  relation = better_ar_apply_associations(relation, opts)
35
- relation.where(opts)
36
36
  end
37
- else
38
- relation = scoped.clone
39
- relation.where(opts)
40
37
  end
38
+
39
+ relation.where(opts)
41
40
  end
42
41
 
43
42
  # Forces a limit of 1 on the collection
@@ -54,13 +53,7 @@ module BetterAr
54
53
  # Optional follows same convention as {#all}
55
54
  # @return [ActiveRecord::Base]
56
55
  def first(opts = {})
57
- if opts.empty?
58
- super
59
- elsif opts.key?(:conditions)
60
- super(opts)
61
- else
62
- all(opts.merge(:limit! => 1)).first
63
- end
56
+ all(opts.merge(:limit! => 1)).to_a.first
64
57
  end
65
58
 
66
59
  private
@@ -78,7 +71,7 @@ module BetterAr
78
71
  def better_ar_apply_associations(relation, opts = {})
79
72
  reflect_on_all_associations.select do |association|
80
73
  !association.options[:polymorphic] &&
81
- better_ar_join_keys(opts).include?(association.table_name.to_sym)
74
+ better_ar_join_keys(opts).include?(association.table_name)
82
75
  end.each do |association|
83
76
  relation = relation.joins(association.name)
84
77
  end
@@ -87,7 +80,15 @@ module BetterAr
87
80
  end
88
81
 
89
82
  def better_ar_join_keys(opts = {})
90
- opts.keys.select { |key| !opts[key].kind_of?(ActiveRecord::Base) }
83
+ opts.keys.select { |key| !opts[key].kind_of?(ActiveRecord::Base) }.map { |k| k.to_s.pluralize }
84
+ end
85
+
86
+ def better_ar_get_relation
87
+ if self.kind_of?(ActiveRecord::Associations::Association)
88
+ @association.scope
89
+ elsif self.kind_of?(ActiveRecord::Relation)
90
+ self.clone
91
+ end
91
92
  end
92
93
 
93
94
  end
@@ -97,6 +98,5 @@ ActiveSupport.on_load(:active_record) do
97
98
  class << ActiveRecord::Base
98
99
  prepend BetterAr::FinderMethods
99
100
  end
100
-
101
- ActiveRecord::Associations::CollectionProxy::ActiveRecord_Associations_CollectionProxy_Record.send(:prepend, BetterAr::FinderMethods)
101
+ ActiveRecord::Relation.send(:prepend, BetterAr::FinderMethods)
102
102
  end
@@ -1,3 +1,3 @@
1
1
  module BetterAr
2
- VERSION = "0.2.1"
2
+ VERSION = "0.2.2"
3
3
  end
@@ -12,7 +12,13 @@ describe 'ActiveRecord::Relation Finder Methods' do
12
12
  test_sql.must_be_like expected_sql
13
13
  end
14
14
 
15
- it 'finds implicit joins by reflection' do
15
+ it 'finds implicit joins by reflection on singular associations' do
16
+ test_sql = User.all(:profile => { :name => 'test' }).to_sql
17
+ expected_sql = User.joins(:profile).where(:profile => { :name => 'test' }).to_sql
18
+ test_sql.must_be_like expected_sql
19
+ end
20
+
21
+ it 'finds implicit joins by reflection on plural associations' do
16
22
  test_sql = User.all(:records => { :name => 'test' }).to_sql
17
23
  expected_sql = User.joins(:records).where(:records => { :name => 'test' }).to_sql
18
24
  test_sql.must_be_like expected_sql
@@ -55,42 +61,6 @@ describe 'ActiveRecord::Relation Finder Methods' do
55
61
  User.count(:age => 10).must_equal 1
56
62
  end
57
63
  end
58
-
59
- describe 'hash contains :conditions' do
60
- before do
61
- @expected = User.create(:name => 'test')
62
- @user = User.create(:name => 'no test')
63
- end
64
-
65
- describe 'with conditions' do
66
- it 'falls back for .all' do
67
- User.all(:conditions => "name = 'test'").must_equal [@expected]
68
- end
69
-
70
- it 'falls back for .first' do
71
- User.first(:conditions => "name = 'test'").must_equal @expected
72
- end
73
-
74
- it 'falls back for .count' do
75
- User.count(:conditions => "name = 'test'").must_equal 1
76
- end
77
- end
78
-
79
- describe 'without conditions' do
80
- it 'falls back for .all' do
81
- User.all.must_equal [@expected, @user]
82
- end
83
-
84
- it 'falls back for .first' do
85
- User.first.must_equal @expected
86
- end
87
-
88
- it 'falls back for .count' do
89
- User.count.must_equal 2
90
- end
91
- end
92
- end
93
-
94
64
  end
95
65
 
96
66
  describe 'ActiveRecord::Associations::AssociationCollection Finder Methods' do
@@ -130,36 +100,4 @@ describe 'ActiveRecord::Associations::AssociationCollection Finder Methods' do
130
100
  @user.records.count(:name => 'one').must_equal 1
131
101
  end
132
102
  end
133
-
134
- describe 'hash contains :conditions' do
135
- describe 'with conditions' do
136
- it 'falls back for .all' do
137
- @user.records.all(:conditions => "name = 'one'").must_equal [@record_1]
138
- end
139
-
140
- it 'falls back for .first' do
141
- @user.records.create(:name => 'one')
142
- @user.records.first(:conditions => "name = 'one'").must_equal @record_1
143
- end
144
-
145
- # Note a valid test... need to rewrite.
146
- # it 'falls back for .count' do
147
- # @user.records.count(:conditions => "name = 'one'").must_equal 1
148
- # end
149
- end
150
-
151
- describe 'without conditions' do
152
- it 'falls back for .all' do
153
- @user.records.all.must_equal [@record_1, @record_2]
154
- end
155
-
156
- it 'falls back for .first' do
157
- @user.records.first.must_equal @record_1
158
- end
159
-
160
- it 'falls back for .count' do
161
- @user.records.count.must_equal 2
162
- end
163
- end
164
- end
165
103
  end
@@ -17,12 +17,14 @@ ActiveRecord::Base.establish_connection(
17
17
  users_table = %{CREATE TABLE users (id INTEGER PRIMARY KEY, age INTEGER, name TEXT, object_type TEXT, object_id INTEGER);}
18
18
  records_table = %{CREATE TABLE records (id INTEGER PRIMARY KEY, user_id INTEGER, name TEXT);}
19
19
  reports_table = %{CREATE TABLE reports (id INTEGER PRIMARY KEY, record_id INTEGER, name TEXT);}
20
+ profiles_table = %{CREATE TABLE profiles (id INTEGER PRIMARY KEY, user_id INTEGER, title TEXT);}
20
21
  ActiveRecord::Base.connection.execute(users_table)
21
22
  ActiveRecord::Base.connection.execute(records_table)
22
23
  ActiveRecord::Base.connection.execute(reports_table)
23
24
 
24
25
  class User < ActiveRecord::Base
25
26
  has_many :records
27
+ has_one :profile
26
28
  belongs_to :object, :polymorphic => true
27
29
  end
28
30
 
@@ -35,3 +37,6 @@ class Report < ActiveRecord::Base
35
37
  belongs_to :record
36
38
  end
37
39
 
40
+ class Profile < ActiveRecord::Base
41
+ belongs_to :user
42
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: better_ar
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.1
4
+ version: 0.2.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Brian Cardarella
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-12-11 00:00:00.000000000 Z
11
+ date: 2013-12-13 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord
@@ -69,7 +69,6 @@ files:
69
69
  - TODO
70
70
  - better_ar.gemspec
71
71
  - lib/better_ar.rb
72
- - lib/better_ar/association_collection.rb
73
72
  - lib/better_ar/calculations.rb
74
73
  - lib/better_ar/finder_methods.rb
75
74
  - lib/better_ar/version.rb
@@ -1,49 +0,0 @@
1
- module BetterAr
2
- module AssociationCollection
3
- # Allows for the same interface as {BetterAr::Relation#first} on association collections
4
- #
5
- # example:
6
- # User.first.records.first(:level => 2, :order! => :name)
7
- #
8
- # @param [Hash]
9
- # Optional
10
- # @returns [ActiveRecord::Base]
11
- def first(opts = {})
12
- if opts.empty?
13
- super
14
- elsif opts.key?(:conditions)
15
- super(opts)
16
- else
17
- scoped.first(opts)
18
- end
19
- end
20
-
21
- # Allows for the same interface as {BetterAr::Relation#count} on association collections
22
- #
23
- # example:
24
- # User.first.records.count(:level => 2)
25
- #
26
- # @param [Hash]
27
- # Optional
28
- # @returns [Integer]
29
- def count(column_name = nil, count_options = {})
30
- if column_name.is_a?(Hash)
31
- count_options = column_name
32
- column_name = nil
33
- end
34
-
35
- if count_options.empty?
36
- super
37
- elsif count_options.key?(:conditions)
38
- super(column_name, count_options)
39
- else
40
- scope.where(count_options).count
41
- end
42
- end
43
- end
44
- end
45
-
46
-
47
- ActiveSupport.on_load(:active_record) do
48
- ActiveRecord::Associations::CollectionAssociation.send(:prepend, BetterAr::AssociationCollection)
49
- end