Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

#56 - adds support for find_or_instantiator_by_attributes

  • Loading branch information...
commit 33b9ad377496964ec75fcb02f055eb56bd82bfd7 1 parent 6699cdf
Artur Małecki authored
39 lib/globalize/active_record/class_methods.rb
View
@@ -98,12 +98,45 @@ def method_missing(method_id, *arguments, &block)
scope = scope.send(:"scoped_by_#{unt}", arguments[index])
end
- return scope.send(match.finder).tap do |found|
- found.is_a?(Array) ? found.map { |f| f.translations.reload } : found.translations.reload unless found.nil?
- end if match.is_a?(::ActiveRecord::DynamicFinderMatch)
+ if match.is_a?(::ActiveRecord::DynamicFinderMatch)
+ if match.instantiator? and scope.blank?
+ return scope.find_or_instantiator_by_attributes match, attribute_names, *arguments, &block
+ end
+
+ return scope.send(match.finder).tap do |found|
+ found.is_a?(Array) ? found.map { |f| f.translations.reload } : found.translations.reload unless found.nil?
+ end
+ end
return scope
end
+ def find_or_instantiator_by_attributes(match, attributes, *args)
+ options = args.size > 1 && args.last(2).all?{ |a| a.is_a?(Hash) } ? args.extract_options! : {}
+ protected_attributes_for_create, unprotected_attributes_for_create = {}, {}
+ args.each_with_index do |arg, i|
+ if arg.is_a?(Hash)
+ protected_attributes_for_create = args[i].with_indifferent_access
+ else
+ unprotected_attributes_for_create[attributes[i]] = args[i]
+ end
+ end
+
+ record = if ::ActiveRecord::VERSION::STRING < "3.1.0"
+ class_name.constantize.new do |r|
+ r.send(:attributes=, protected_attributes_for_create, true) unless protected_attributes_for_create.empty?
+ r.send(:attributes=, unprotected_attributes_for_create, false) unless unprotected_attributes_for_create.empty?
+ end
+ else
+ class_name.constantize.new(protected_attributes_for_create, options) do |r|
+ r.assign_attributes(unprotected_attributes_for_create, :without_protection => true)
+ end
+ end
+ yield(record) if block_given?
+ record.send(match.bang? ? :save! : :save) if match.instantiator.eql?(:create)
+
+ record
+ end
+
protected
def translated_attr_accessor(name)
21 test/globalize3/dynamic_finders_test.rb
View
@@ -17,6 +17,27 @@ class DynamicFindersTest < Test::Unit::TestCase
assert_equal [user], User.find_all_by_id_and_email(user.id, user.email)
end
+ test "Does not break find_or_initialize finders" do
+ user = User.create!(:name => "Jim", :email => "email@example.org")
+
+ new_user = User.find_or_initialize_by_name("Bob")
+
+ assert_equal new_user.name, "Bob"
+ assert_equal user, User.find_or_initialize_by_name_and_email("Jim", "email@example.org")
+ end
+
+ test "Does not break find_or_create finders" do
+ user = User.create!(:name => "Jim", :email => "email@example.org")
+
+ user_count_before = User.count
+ new_user = User.find_or_create_by_name_and_email("Bob", "bob@example.org")
+ assert_equal user_count_before + 1, User.count
+ assert_equal new_user.name, "Bob"
+ assert_equal new_user.email, "bob@example.org"
+
+ assert_equal user, User.find_or_create_by_name("Jim")
+ end
+
test "simple dynamic finders do work" do
foo = Post.create!(:title => 'foo')
bar = Post.create!(:title => 'bar')
Please sign in to comment.
Something went wrong with that request. Please try again.