Skip to content
  • 9 commits
  • 27 files changed
  • 0 commit comments
  • 2 contributors
View
2 LICENSE.txt
@@ -1,4 +1,4 @@
-Copyright (c) 2011 John Wood
+Copyright (c) 2012 John Wood
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
View
1 THANKS.md
@@ -6,6 +6,7 @@ following people have contributed to the project by reporting problems,
suggesting improvements, or submitting changes.
* [danielgranat](http://github.com/danielgranat)
+ * [raphaelcm](http://github.com/raphaelcm)
Patches are welcome. The primary source for this software project is [on Github](http://github.com/jwood/tenacity)
View
12 history.txt
@@ -1,3 +1,15 @@
+== 0.5.6
+
+* Bug fixes
+
+ * Fixed a few issues with regards to how ActiveRecord::Base.save behaves (raphaelcm)
+
+== 0.5.5
+
+* Bug fixes
+
+ * Modify association code to support qualified class names (Foo::Bar)
+
== 0.5.4
* Bug fixes
View
4 lib/tenacity/associate_proxy.rb
@@ -23,11 +23,11 @@ def inspect
@target.inspect
end
- def save
+ def save(*args)
if @association.readonly?
raise ReadOnlyError
else
- @target._t_save_if_dirty
+ @target._t_save_if_dirty(*args)
end
end
View
36 lib/tenacity/association.rb
@@ -68,9 +68,9 @@ def name
# Get the associated class
def associate_class(object=nil)
if @type == :t_belongs_to && polymorphic?
- Kernel.const_get(object.send(polymorphic_type))
+ qualified_const_get(object.send(polymorphic_type))
else
- @clazz ||= Kernel.const_get(@class_name)
+ @clazz ||= qualified_const_get(@class_name)
end
end
@@ -109,11 +109,39 @@ def foreign_key_constraints_enabled?
private
+ # Shamelessly copied from http://redcorundum.blogspot.com/2006/05/kernelqualifiedconstget.html
+ def qualified_const_get(clazz)
+ path = clazz.to_s.split('::')
+ from_root = path[0].empty?
+
+ if from_root
+ from_root = []
+ path = path[1..-1]
+ else
+ start_ns = ((Class === self) || (Module === self)) ? self : self.class
+ from_root = start_ns.to_s.split('::')
+ end
+
+ until from_root.empty?
+ begin
+ return (from_root + path).inject(Object) { |ns,name| ns.const_get(name) }
+ rescue NameError
+ from_root.delete_at(-1)
+ end
+ end
+
+ path.inject(Object) { |ns,name| ns.const_get(name) }
+ end
+
+ def unqualified_class_name(clazz)
+ clazz.to_s.split('::').last
+ end
+
def belongs_to_foreign_key
if polymorphic?
(name.to_s + "_id").to_sym
else
- @class_name.underscore + "_id"
+ unqualified_class_name(@class_name).underscore + "_id"
end
end
@@ -122,7 +150,7 @@ def has_x_foreign_key(clazz)
if polymorphic?
(@as.to_s + "_id").to_sym
else
- "#{ActiveSupport::Inflector.underscore(clazz)}_id"
+ "#{ActiveSupport::Inflector.underscore(unqualified_class_name(clazz))}_id"
end
end
end
View
4 lib/tenacity/orm_ext/activerecord.rb
@@ -112,8 +112,8 @@ def _t_reload
self
end
- def _t_save_if_dirty
- changed? ? save : false
+ def _t_save_if_dirty(*args)
+ changed? ? save(*args) : true
end
end
View
4 lib/tenacity/orm_ext/couchrest.rb
@@ -145,8 +145,8 @@ def _t_reload
self
end
- def _t_save_if_dirty
- save
+ def _t_save_if_dirty(*args)
+ save(*args)
end
end
View
4 lib/tenacity/orm_ext/datamapper.rb
@@ -138,8 +138,8 @@ def _t_reload
self.class._t_find(self.id)
end
- def _t_save_if_dirty
- dirty? ? save : false
+ def _t_save_if_dirty(*args)
+ dirty? ? save(*args) : true
end
private
View
4 lib/tenacity/orm_ext/mongo_mapper.rb
@@ -111,8 +111,8 @@ def _t_reload
self
end
- def _t_save_if_dirty
- changed? ? save : false
+ def _t_save_if_dirty(*args)
+ changed? ? save(*args) : true
end
end
View
4 lib/tenacity/orm_ext/mongoid.rb
@@ -113,8 +113,8 @@ def _t_reload
self
end
- def _t_save_if_dirty
- changed? ? save : false
+ def _t_save_if_dirty(*args)
+ changed? ? save(*args) : true
end
end
View
4 lib/tenacity/orm_ext/ripple.rb
@@ -139,8 +139,8 @@ def _t_reload
self
end
- def _t_save_if_dirty
- changed? ? save : false
+ def _t_save_if_dirty(*args)
+ changed? ? save(*args) : true
end
def save
View
4 lib/tenacity/orm_ext/sequel.rb
@@ -147,8 +147,8 @@ def _t_reload
self
end
- def _t_save_if_dirty
- !changed_columns.empty? ? save : false
+ def _t_save_if_dirty(*args)
+ !changed_columns.empty? ? save(*args) : true
end
end
View
4 lib/tenacity/orm_ext/toystore.rb
@@ -109,8 +109,8 @@ def _t_reload
self
end
- def _t_save_if_dirty
- changed? ? save : false
+ def _t_save_if_dirty(*args)
+ changed? ? save(*args) : true
end
end
end
View
2 lib/tenacity/version.rb
@@ -1,3 +1,3 @@
module Tenacity
- VERSION = "0.5.4"
+ VERSION = "0.5.6"
end
View
31 tenacity.gemspec
@@ -18,27 +18,28 @@ Gem::Specification.new do |s|
s.add_runtime_dependency "activesupport", "~> 2.3"
- s.add_development_dependency "bundler", "~> 1.0.0"
- s.add_development_dependency "rake", "~> 0.8.7"
- s.add_development_dependency "rcov", "~> 0.9.9"
- s.add_development_dependency "shoulda", "~> 2.11.3"
- s.add_development_dependency "mocha", "~> 0.9.10"
- s.add_development_dependency "yard", "~> 0.6.4"
+ s.add_development_dependency "bundler", ">= 1.0.0"
+ s.add_development_dependency "rake", ">= 0.8.7"
+ s.add_development_dependency "rcov", "0.9.9"
+ s.add_development_dependency "shoulda", "2.11.3"
+ s.add_development_dependency "mocha", "0.9.10"
+ s.add_development_dependency "yard", "0.6.4"
# Relational DBs
- s.add_development_dependency "sqlite3-ruby", "~> 1.3.1"
- s.add_development_dependency "activerecord", "~> 2.3"
- s.add_development_dependency "datamapper", "~> 1.0.2"
- s.add_development_dependency "dm-sqlite-adapter", "~> 1.0.2"
- s.add_development_dependency "sequel", "~> 3.19.0"
+ s.add_development_dependency "sqlite3-ruby", "1.3.1"
+ s.add_development_dependency "activerecord", "2.3.14"
+ s.add_development_dependency "datamapper", "1.0.2"
+ s.add_development_dependency "dm-sqlite-adapter", "1.0.2"
+ s.add_development_dependency "sequel", "3.19.0"
# MongoDB
- s.add_development_dependency "mongo_mapper", "~> 0.8.6"
- s.add_development_dependency "bson_ext", "~> 1.3.0"
+ s.add_development_dependency "mongo_mapper", "0.8.6"
+ s.add_development_dependency "mongo", "1.6.2"
+ s.add_development_dependency "bson_ext", "1.6.2"
# CouchDB
- s.add_development_dependency "couchrest", "~> 1.0.0"
- s.add_development_dependency "couchrest_extended_document", "~> 1.0.0"
+ s.add_development_dependency "couchrest", "1.0.0"
+ s.add_development_dependency "couchrest_extended_document", "1.0.0"
s.files = `git ls-files`.split("\n")
s.executables = `git ls-files`.split("\n").map{|f| f =~ /^bin\/(.*)/ ? $1 : nil}.compact
View
5 test/fixtures/active_record_organization.rb
@@ -0,0 +1,5 @@
+class ActiveRecordOrganization < ActiveRecord::Base
+ include Tenacity
+
+ has_many :active_record_users
+end
View
5 test/fixtures/active_record_user.rb
@@ -0,0 +1,5 @@
+class ActiveRecordUser < ActiveRecord::Base
+ include Tenacity
+
+ belongs_to :active_record_organization, :autosave => true
+end
View
9 test/fixtures/mongoid_campus_hub.rb
@@ -0,0 +1,9 @@
+require_mongoid do
+ class MongoidCampusHub
+ include Mongoid::Document
+ include Mongoid::Timestamps
+ include Tenacity
+
+ t_belongs_to :active_record_organization
+ end
+end
View
7 test/helpers/active_record_test_helper.rb
@@ -29,6 +29,13 @@
create_table :active_record_object_with_string_ids, :force => true, :id => false do |t|
t.string :id, :limit => 36, :primary => true
end
+
+ create_table :active_record_users, :force => true do |t|
+ t.integer :active_record_organization_id
+ end
+
+ create_table :active_record_organizations, :force => true do |t|
+ end
create_table :active_record_has_one_targets, :force => true do |t|
t.integer :active_record_object_id
View
21 test/orm_ext/activerecord_test.rb
@@ -98,9 +98,15 @@ class ActiveRecordTest < Test::Unit::TestCase
assert object._t_save_if_dirty
end
+ should "return true for save if valid object is not dirty" do
+ object = ActiveRecordObject.create
+ assert object.save
+ end
+
should "not save the object if it is not dirty" do
object = ActiveRecordObject.create
- assert !object._t_save_if_dirty
+ ActiveRecordObject.any_instance.stubs(:save).raises(RuntimeError.new("should not have called this"))
+ assert object._t_save_if_dirty
end
should "be able to successfully determine the id type" do
@@ -110,6 +116,19 @@ class ActiveRecordTest < Test::Unit::TestCase
class ActiveRecordObjectWithNoTable < ActiveRecord::Base; include Tenacity; end
assert_equal Integer, ActiveRecordObjectWithNoTable._t_id_type
end
+
+ require_mongoid do
+ should "successfully save if belongs_to another AR object which is assigned from a mongoid object" do
+ org = ActiveRecordOrganization.create
+ campus_hub = MongoidCampusHub.create
+ campus_hub.active_record_organization = org
+ campus_hub.save!
+ user = ActiveRecordUser.new
+ user.active_record_organization = campus_hub.active_record_organization
+ assert user.save
+ assert user.active_record_organization.save(:validate => false)
+ end
+ end
end
private
View
8 test/orm_ext/datamapper_test.rb
@@ -89,9 +89,15 @@ class DataMapperTest < Test::Unit::TestCase
assert object._t_save_if_dirty
end
+ should "return true for save if valid object is not dirty" do
+ object = DataMapperObject.create
+ assert object.save
+ end
+
should "not save the object if it is not dirty" do
object = DataMapperObject.create
- assert !object._t_save_if_dirty
+ DataMapperObject.any_instance.stubs(:save).raises(RuntimeError.new("should not have called this"))
+ assert object._t_save_if_dirty
end
should "be able to successfully determine the id type" do
View
8 test/orm_ext/mongo_mapper_test.rb
@@ -99,9 +99,15 @@ class MongoMapperTest < Test::Unit::TestCase
assert object._t_save_if_dirty
end
+ should "return true for save if valid object is not dirty" do
+ object = MongoMapperObject.create
+ assert object.save
+ end
+
should "not save the object if it is not dirty" do
object = MongoMapperObject.create
- assert !object._t_save_if_dirty
+ MongoMapperObject.any_instance.stubs(:save).raises(RuntimeError.new("should not have called this"))
+ assert object._t_save_if_dirty
end
end
View
8 test/orm_ext/mongoid_test.rb
@@ -100,9 +100,15 @@ class MongoidTest < Test::Unit::TestCase
assert object._t_save_if_dirty
end
+ should "return true for save if valid object is not dirty" do
+ object = MongoidObject.create
+ assert object.save
+ end
+
should "not save the object if it is not dirty" do
object = MongoidObject.create
- assert !object._t_save_if_dirty
+ MongoidObject.any_instance.stubs(:save).raises(RuntimeError.new("should not have called this"))
+ assert object._t_save_if_dirty
end
end
View
8 test/orm_ext/ripple_test.rb
@@ -139,9 +139,15 @@ class RippleTest < Test::Unit::TestCase
assert object._t_save_if_dirty
end
+ should "return true for save if valid object is not dirty" do
+ object = RippleObject.create
+ assert object.save
+ end
+
should "not save the object if it is not dirty" do
object = RippleObject.create
- assert !object._t_save_if_dirty
+ RippleObject.any_instance.stubs(:save).raises(RuntimeError.new("should not have called this"))
+ assert object._t_save_if_dirty
end
end
View
8 test/orm_ext/sequel_test.rb
@@ -99,9 +99,15 @@ class SequelTest < Test::Unit::TestCase
assert object._t_save_if_dirty
end
+ should "return true for save if valid object is not dirty" do
+ object = SequelObject.create
+ assert object.save
+ end
+
should "not save the object if it is not dirty" do
object = SequelObject.create
- assert !object._t_save_if_dirty
+ SequelObject.any_instance.stubs(:save).raises(RuntimeError.new("should not have called this"))
+ assert object._t_save_if_dirty
end
should "be able to successfully determine the id type" do
View
8 test/orm_ext/toystore_test.rb
@@ -101,9 +101,15 @@ class ToystoreTest < Test::Unit::TestCase
assert object._t_save_if_dirty
end
+ should "return true for save if valid object is not dirty" do
+ object = ToystoreObject.create
+ assert object.save
+ end
+
should "not save the object if it is not dirty" do
object = ToystoreObject.create
- assert !object._t_save_if_dirty
+ ToystoreObject.any_instance.stubs(:save).raises(RuntimeError.new("should not have called this"))
+ assert object._t_save_if_dirty
end
end
View
2 test/test_helper.rb
@@ -35,7 +35,7 @@ def setup_fixtures
filename =~ /.*\/(.*)\.rb/
clazz = Kernel.const_get($1.camelcase)
if clazz.respond_to?(:delete_all)
- clazz.delete_all
+ clazz.delete_all rescue true
elsif clazz.respond_to?(:db)
clazz.db["delete from #{clazz.table_name}"].delete
elsif clazz.respond_to?(:destroy!)

No commit comments for this range

Something went wrong with that request. Please try again.