activerecord-deprecated_finders 1.0.1 → 1.0.2

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.
@@ -4,15 +4,17 @@ module ActiveRecord
4
4
  module InterceptDynamicInstantiators
5
5
  def method_missing(method, *args, &block)
6
6
  match = DynamicMatchers::Method.match(klass, method)
7
- sanitized_method = match.class.prefix + match.class.suffix if match
8
7
 
9
- if match && self.respond_to?(sanitized_method) && proxy_association.reflection.options[:through].present?
10
- self.send(sanitized_method, Hash[match.attribute_names.zip(args)])
11
-
12
- elsif match && match.is_a?(DynamicMatchers::Instantiator)
8
+ if match && match.is_a?(DynamicMatchers::Instantiator)
13
9
  scoping do
14
10
  klass.send(method, *args) do |record|
15
- proxy_association.add_to_target(record)
11
+
12
+ sanitized_method = match.class.prefix + match.class.suffix
13
+ if %w(find_or_create_by find_or_create_by!).include?(sanitized_method) && proxy_association.reflection.options[:through].present?
14
+ proxy_association.send(:save_through_record, record)
15
+ else
16
+ proxy_association.add_to_target(record)
17
+ end
16
18
  yield record if block_given?
17
19
  end
18
20
  end
@@ -1,5 +1,5 @@
1
1
  module ActiveRecord
2
2
  module DeprecatedFinders
3
- VERSION = "1.0.1"
3
+ VERSION = "1.0.2"
4
4
  end
5
5
  end
@@ -8,6 +8,14 @@ describe 'associations' do
8
8
  Appointment.delete_all
9
9
  end
10
10
 
11
+ it 'should respect find_by_anything method defined on the base class' do
12
+ physician = Physician.create!
13
+
14
+ ActiveSupport::Deprecation.silence do
15
+ assert_equal [], physician.patients.find_by_custom_name
16
+ end
17
+ end
18
+
11
19
  it 'find_or_create_by on has_many through should work' do
12
20
  physician = Physician.create!
13
21
  ActiveSupport::Deprecation.silence do
@@ -16,6 +24,14 @@ describe 'associations' do
16
24
  assert_equal 1, Appointment.count
17
25
  end
18
26
 
27
+ it 'find_or_create_by with bang on has_many through should work' do
28
+ physician = Physician.create!
29
+ ActiveSupport::Deprecation.silence do
30
+ physician.patients.find_or_create_by_name!('Tim')
31
+ end
32
+ assert_equal 1, Appointment.count
33
+ end
34
+
19
35
  it 'find_or_create_by on has_many should work' do
20
36
  physician = Physician.create!
21
37
  ActiveSupport::Deprecation.silence do
data/test/helper.rb CHANGED
@@ -50,6 +50,9 @@ class Appointment < ActiveRecord::Base
50
50
  end
51
51
 
52
52
  class Patient < ActiveRecord::Base
53
+ def self.find_by_custom_name
54
+ []
55
+ end
53
56
  end
54
57
 
55
58
  class Physician < ActiveRecord::Base
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: activerecord-deprecated_finders
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.1
4
+ version: 1.0.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-04-17 00:00:00.000000000 Z
12
+ date: 2013-04-29 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: minitest
@@ -136,4 +136,3 @@ test_files:
136
136
  - test/scoped_test.rb
137
137
  - test/update_all_test.rb
138
138
  - test/with_scope_test.rb
139
- has_rdoc: