From dbbae5e00e49d3a69dc10978e38299e3f28dd1e1 Mon Sep 17 00:00:00 2001 From: Pratik Naik Date: Sun, 7 Dec 2008 03:27:53 +0100 Subject: Merge with docrails --- activerecord/lib/active_record/base.rb | 56 ++++++++++++++++------ .../abstract/connection_specification.rb | 2 + .../connection_adapters/mysql_adapter.rb | 15 +++--- activerecord/lib/active_record/migration.rb | 4 +- activerecord/lib/active_record/schema_dumper.rb | 2 + activerecord/lib/active_record/validations.rb | 16 ++++--- 6 files changed, 67 insertions(+), 28 deletions(-) (limited to 'activerecord') diff --git a/activerecord/lib/active_record/base.rb b/activerecord/lib/active_record/base.rb index 8f8ed241d5..5d614442c3 100755 --- a/activerecord/lib/active_record/base.rb +++ b/activerecord/lib/active_record/base.rb @@ -389,6 +389,8 @@ module ActiveRecord #:nodoc: # So it's possible to assign a logger to the class through Base.logger= which will then be used by all # instances in the current object space. class Base + ## + # :singleton-method: # Accepts a logger conforming to the interface of Log4r or the default Ruby 1.8+ Logger class, which is then passed # on to any new database connections made and which can be retrieved on both a class and instance level by calling +logger+. cattr_accessor :logger, :instance_writer => false @@ -414,7 +416,9 @@ module ActiveRecord #:nodoc: end @@subclasses = {} - + + ## + # :singleton-method: # Contains the database configuration - as is typically stored in config/database.yml - # as a Hash. # @@ -443,6 +447,8 @@ module ActiveRecord #:nodoc: cattr_accessor :configurations, :instance_writer => false @@configurations = {} + ## + # :singleton-method: # Accessor for the prefix type that will be prepended to every primary key column name. The options are :table_name and # :table_name_with_underscore. If the first is specified, the Product class will look for "productid" instead of "id" as # the primary column. If the latter is specified, the Product class will look for "product_id" instead of "id". Remember @@ -450,34 +456,46 @@ module ActiveRecord #:nodoc: cattr_accessor :primary_key_prefix_type, :instance_writer => false @@primary_key_prefix_type = nil + ## + # :singleton-method: # Accessor for the name of the prefix string to prepend to every table name. So if set to "basecamp_", all # table names will be named like "basecamp_projects", "basecamp_people", etc. This is a convenient way of creating a namespace # for tables in a shared database. By default, the prefix is the empty string. cattr_accessor :table_name_prefix, :instance_writer => false @@table_name_prefix = "" + ## + # :singleton-method: # Works like +table_name_prefix+, but appends instead of prepends (set to "_basecamp" gives "projects_basecamp", # "people_basecamp"). By default, the suffix is the empty string. cattr_accessor :table_name_suffix, :instance_writer => false @@table_name_suffix = "" + ## + # :singleton-method: # Indicates whether table names should be the pluralized versions of the corresponding class names. # If true, the default table name for a Product class will be +products+. If false, it would just be +product+. # See table_name for the full rules on table/class naming. This is true, by default. cattr_accessor :pluralize_table_names, :instance_writer => false @@pluralize_table_names = true + ## + # :singleton-method: # Determines whether to use ANSI codes to colorize the logging statements committed by the connection adapter. These colors # make it much easier to overview things during debugging (when used through a reader like +tail+ and on a black background), but # may complicate matters if you use software like syslog. This is true, by default. cattr_accessor :colorize_logging, :instance_writer => false @@colorize_logging = true + ## + # :singleton-method: # Determines whether to use Time.local (using :local) or Time.utc (using :utc) when pulling dates and times from the database. # This is set to :local by default. cattr_accessor :default_timezone, :instance_writer => false @@default_timezone = :local + ## + # :singleton-method: # Specifies the format to use when dumping the database schema with Rails' # Rakefile. If :sql, the schema is dumped as (potentially database- # specific) SQL statements. If :ruby, the schema is dumped as an @@ -487,6 +505,8 @@ module ActiveRecord #:nodoc: cattr_accessor :schema_format , :instance_writer => false @@schema_format = :ruby + ## + # :singleton-method: # Specify whether or not to use timestamps for migration numbers cattr_accessor :timestamped_migrations , :instance_writer => false @@timestamped_migrations = true @@ -640,16 +660,24 @@ module ActiveRecord #:nodoc: connection.select_all(sanitize_sql(sql), "#{name} Load").collect! { |record| instantiate(record) } end - # Checks whether a record exists in the database that matches conditions given. These conditions - # can either be a single integer representing a primary key id to be found, or a condition to be - # matched like using ActiveRecord#find. + + # Returns true if a record exists in the table that matches the +id+ or + # conditions given, or false otherwise. The argument can take four forms: + # + # * Integer - Finds the record with this primary key. + # * String - Finds the record with a primary key corresponding to this + # string (such as '5'). + # * Array - Finds the record that matches these +find+-style conditions + # (such as ['color = ?', 'red']). + # * Hash - Finds the record that matches these +find+-style conditions + # (such as {:color => 'red'}). # - # The +id_or_conditions+ parameter can be an Integer or a String if you want to search the primary key - # column of the table for a matching id, or if you're looking to match against a condition you can use - # an Array or a Hash. + # For more information about specifying conditions as a Hash or Array, + # see the Conditions section in the introduction to ActiveRecord::Base. # - # Possible gotcha: You can't pass in a condition as a string e.g. "name = 'Jamie'", this would be - # sanitized and then queried against the primary key column as "id = 'name = \'Jamie" + # Note: You can't pass in a condition as a string (like name = + # 'Jamie'), since it would be sanitized and then queried against + # the primary key column, like id = 'name = \'Jamie\''. # # ==== Examples # Person.exists?(5) @@ -2320,15 +2348,15 @@ module ActiveRecord #:nodoc: # object. The default implementation returns this record's id as a String, # or nil if this record's unsaved. # - # For example, suppose that you have a Users model, and that you have a - # map.resources :users route. Normally, +users_path+ will - # construct an URI with the user object's 'id' in it: + # For example, suppose that you have a User model, and that you have a + # map.resources :users route. Normally, +user_path+ will + # construct a path with the user object's 'id' in it: # # user = User.find_by_name('Phusion') # user_path(user) # => "/users/1" # - # You can override +to_param+ in your model to make +users_path+ construct - # an URI using the user's name instead of the user's id: + # You can override +to_param+ in your model to make +user_path+ construct + # a path using the user's name instead of the user's id: # # class User < ActiveRecord::Base # def to_param # overridden diff --git a/activerecord/lib/active_record/connection_adapters/abstract/connection_specification.rb b/activerecord/lib/active_record/connection_adapters/abstract/connection_specification.rb index a968fc0fd3..ccb79f547a 100644 --- a/activerecord/lib/active_record/connection_adapters/abstract/connection_specification.rb +++ b/activerecord/lib/active_record/connection_adapters/abstract/connection_specification.rb @@ -7,6 +7,8 @@ module ActiveRecord end end + ## + # :singleton-method: # The connection handler cattr_accessor :connection_handler, :instance_writer => false @@connection_handler = ConnectionAdapters::ConnectionHandler.new diff --git a/activerecord/lib/active_record/connection_adapters/mysql_adapter.rb b/activerecord/lib/active_record/connection_adapters/mysql_adapter.rb index 1e452ae88a..c3cbdc8c2f 100644 --- a/activerecord/lib/active_record/connection_adapters/mysql_adapter.rb +++ b/activerecord/lib/active_record/connection_adapters/mysql_adapter.rb @@ -156,13 +156,16 @@ module ActiveRecord # * :sslcapath - Necessary to use MySQL with an SSL connection. # * :sslcipher - Necessary to use MySQL with an SSL connection. # - # By default, the MysqlAdapter will consider all columns of type tinyint(1) - # as boolean. If you wish to disable this emulation (which was the default - # behavior in versions 0.13.1 and earlier) you can add the following line - # to your environment.rb file: - # - # ActiveRecord::ConnectionAdapters::MysqlAdapter.emulate_booleans = false class MysqlAdapter < AbstractAdapter + + ## + # :singleton-method: + # By default, the MysqlAdapter will consider all columns of type tinyint(1) + # as boolean. If you wish to disable this emulation (which was the default + # behavior in versions 0.13.1 and earlier) you can add the following line + # to your environment.rb file: + # + # ActiveRecord::ConnectionAdapters::MysqlAdapter.emulate_booleans = false cattr_accessor :emulate_booleans self.emulate_booleans = true diff --git a/activerecord/lib/active_record/migration.rb b/activerecord/lib/active_record/migration.rb index 1d843fff28..15350cf1e1 100644 --- a/activerecord/lib/active_record/migration.rb +++ b/activerecord/lib/active_record/migration.rb @@ -130,7 +130,9 @@ module ActiveRecord # To run migrations against the currently configured database, use # rake db:migrate. This will update the database by running all of the # pending migrations, creating the schema_migrations table - # (see "About the schema_migrations table" section below) if missing. + # (see "About the schema_migrations table" section below) if missing. It will also + # invoke the db:schema:dump task, which will update your db/schema.rb file + # to match the structure of your database. # # To roll the database back to a previous migration version, use # rake db:migrate VERSION=X where X is the version to which diff --git a/activerecord/lib/active_record/schema_dumper.rb b/activerecord/lib/active_record/schema_dumper.rb index 2181bdf2dd..557a554966 100644 --- a/activerecord/lib/active_record/schema_dumper.rb +++ b/activerecord/lib/active_record/schema_dumper.rb @@ -7,6 +7,8 @@ module ActiveRecord class SchemaDumper #:nodoc: private_class_method :new + ## + # :singleton-method: # A list of tables which should not be dumped to the schema. # Acceptable values are strings as well as regexp. # This setting is only used if ActiveRecord::Base.schema_format == :ruby diff --git a/activerecord/lib/active_record/validations.rb b/activerecord/lib/active_record/validations.rb index 9220eae4d1..dae00aa7db 100644 --- a/activerecord/lib/active_record/validations.rb +++ b/activerecord/lib/active_record/validations.rb @@ -494,18 +494,20 @@ module ActiveRecord # The first_name attribute must be in the object and it cannot be blank. # # If you want to validate the presence of a boolean field (where the real values are true and false), - # you will want to use validates_inclusion_of :field_name, :in => [true, false] - # This is due to the way Object#blank? handles boolean values. false.blank? # => true + # you will want to use validates_inclusion_of :field_name, :in => [true, false]. + # + # This is due to the way Object#blank? handles boolean values: false.blank? # => true. # # Configuration options: # * message - A custom error message (default is: "can't be blank"). - # * on - Specifies when this validation is active (default is :save, other options :create, :update). + # * on - Specifies when this validation is active (default is :save, other options :create, + # :update). # * if - Specifies a method, proc or string to call to determine if the validation should - # occur (e.g. :if => :allow_validation, or :if => Proc.new { |user| user.signup_step > 2 }). The - # method, proc or string should return or evaluate to a true or false value. + # occur (e.g. :if => :allow_validation, or :if => Proc.new { |user| user.signup_step > 2 }). + # The method, proc or string should return or evaluate to a true or false value. # * unless - Specifies a method, proc or string to call to determine if the validation should - # not occur (e.g. :unless => :skip_validation, or :unless => Proc.new { |user| user.signup_step <= 2 }). The - # method, proc or string should return or evaluate to a true or false value. + # not occur (e.g. :unless => :skip_validation, or :unless => Proc.new { |user| user.signup_step <= 2 }). + # The method, proc or string should return or evaluate to a true or false value. # def validates_presence_of(*attr_names) configuration = { :on => :save } -- cgit v1.2.3 From 091e6f791aa3324b2c7f8c8c4cd2fce12b689cc8 Mon Sep 17 00:00:00 2001 From: Ben Symonds Date: Mon, 8 Dec 2008 14:11:55 +0000 Subject: Change field_changed? method to handle the case where a nullable integer column is changed from 0 to '0' [#1530 state:committed] Signed-off-by: Jeremy Kemper --- activerecord/lib/active_record/dirty.rb | 4 ++-- activerecord/test/cases/dirty_test.rb | 12 ++++++++++++ 2 files changed, 14 insertions(+), 2 deletions(-) (limited to 'activerecord') diff --git a/activerecord/lib/active_record/dirty.rb b/activerecord/lib/active_record/dirty.rb index ae573799ae..a1760875ba 100644 --- a/activerecord/lib/active_record/dirty.rb +++ b/activerecord/lib/active_record/dirty.rb @@ -151,12 +151,12 @@ module ActiveRecord def field_changed?(attr, old, value) if column = column_for_attribute(attr) - if column.type == :integer && column.null && (old.nil? || old == 0) + if column.type == :integer && column.null && (old.nil? || old == 0) && value.blank? # For nullable integer columns, NULL gets stored in database for blank (i.e. '') values. # Hence we don't record it as a change if the value changes from nil to ''. # If an old value of 0 is set to '' we want this to get changed to nil as otherwise it'll # be typecast back to 0 (''.to_i => 0) - value = nil if value.blank? + value = nil else value = column.type_cast(value) end diff --git a/activerecord/test/cases/dirty_test.rb b/activerecord/test/cases/dirty_test.rb index 39d38c4e1e..10cdbdc622 100644 --- a/activerecord/test/cases/dirty_test.rb +++ b/activerecord/test/cases/dirty_test.rb @@ -68,6 +68,18 @@ class DirtyTest < ActiveRecord::TestCase end end + def test_nullable_integer_zero_to_string_zero_not_marked_as_changed + pirate = Pirate.new + pirate.parrot_id = 0 + pirate.catchphrase = 'arrr' + assert pirate.save! + + assert !pirate.changed? + + pirate.parrot_id = '0' + assert !pirate.changed? + end + def test_zero_to_blank_marked_as_changed pirate = Pirate.new pirate.catchphrase = "Yarrrr, me hearties" -- cgit v1.2.3 From c3b87dd4c99ba47d316bd03f99b5e23af1fe0ed8 Mon Sep 17 00:00:00 2001 From: Akira Matsuda Date: Thu, 30 Oct 2008 17:43:08 +0900 Subject: Make word separator in AR validation error messages configurable. [#1294 state:committed] Signed-off-by: Jeremy Kemper --- activerecord/lib/active_record/validations.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'activerecord') diff --git a/activerecord/lib/active_record/validations.rb b/activerecord/lib/active_record/validations.rb index dae00aa7db..4b275ddd70 100644 --- a/activerecord/lib/active_record/validations.rb +++ b/activerecord/lib/active_record/validations.rb @@ -205,7 +205,7 @@ module ActiveRecord else #key = :"activerecord.att.#{@base.class.name.underscore.to_sym}.#{attr}" attr_name = @base.class.human_attribute_name(attr) - full_messages << attr_name + ' ' + message + full_messages << attr_name + I18n.t('activerecord.errors.format.separator', :default => ' ') + message end end end -- cgit v1.2.3 From 9b22e56d89a55460a18d5f38adf9fee919f52266 Mon Sep 17 00:00:00 2001 From: Jeremy Kemper Date: Mon, 8 Dec 2008 15:59:42 -0800 Subject: Changelog entry for #1294 --- activerecord/CHANGELOG | 2 ++ 1 file changed, 2 insertions(+) (limited to 'activerecord') diff --git a/activerecord/CHANGELOG b/activerecord/CHANGELOG index cca70f1fb7..423a0bd0cc 100644 --- a/activerecord/CHANGELOG +++ b/activerecord/CHANGELOG @@ -1,5 +1,7 @@ *2.3.0/3.0* +* I18n the word separator for error messages. Introduces the activerecord.errors.format.separator translation key. #1294 [Akira Matsuda] + * Add :having as a key to find and the relevant associations. [miloops] * Added default_scope to Base #1381 [Paweł Kondzior]. Example: -- cgit v1.2.3 From e8c4939fb3366472021c1af1331bfdfe5d7a5d75 Mon Sep 17 00:00:00 2001 From: Jeremy Kemper Date: Tue, 9 Dec 2008 11:17:11 -0800 Subject: Benchmark.ms --- activerecord/lib/active_record/base.rb | 4 ++-- .../lib/active_record/connection_adapters/abstract_adapter.rb | 10 +++++----- 2 files changed, 7 insertions(+), 7 deletions(-) (limited to 'activerecord') diff --git a/activerecord/lib/active_record/base.rb b/activerecord/lib/active_record/base.rb index 5d614442c3..a23518b357 100755 --- a/activerecord/lib/active_record/base.rb +++ b/activerecord/lib/active_record/base.rb @@ -1417,8 +1417,8 @@ module ActiveRecord #:nodoc: def benchmark(title, log_level = Logger::DEBUG, use_silence = true) if logger && logger.level <= log_level result = nil - seconds = Benchmark.realtime { result = use_silence ? silence { yield } : yield } - logger.add(log_level, "#{title} (#{'%.1f' % (seconds * 1000)}ms)") + ms = Benchmark.ms { result = use_silence ? silence { yield } : yield } + logger.add(log_level, '%s (%.1fms)' % [title, ms]) result else yield diff --git a/activerecord/lib/active_record/connection_adapters/abstract_adapter.rb b/activerecord/lib/active_record/connection_adapters/abstract_adapter.rb index cab77fc031..bfafcfb3ab 100755 --- a/activerecord/lib/active_record/connection_adapters/abstract_adapter.rb +++ b/activerecord/lib/active_record/connection_adapters/abstract_adapter.rb @@ -160,9 +160,9 @@ module ActiveRecord @open_transactions -= 1 end - def log_info(sql, name, seconds) + def log_info(sql, name, ms) if @logger && @logger.debug? - name = "#{name.nil? ? "SQL" : name} (#{sprintf("%.1f", seconds * 1000)}ms)" + name = '%s (%.1fms)' % [name || 'SQL', ms] @logger.debug(format_log_entry(name, sql.squeeze(' '))) end end @@ -171,9 +171,9 @@ module ActiveRecord def log(sql, name) if block_given? result = nil - seconds = Benchmark.realtime { result = yield } - @runtime += seconds - log_info(sql, name, seconds) + ms = Benchmark.ms { result = yield } + @runtime += ms + log_info(sql, name, ms) result else log_info(sql, name, 0) -- cgit v1.2.3 From 355f41d8aafd75d76db25cdda4736e0052b0605c Mon Sep 17 00:00:00 2001 From: Frederick Cheung Date: Tue, 9 Dec 2008 20:36:24 +0000 Subject: Rework ActiveSupport::OrderedHash to make lookups faster [#1352 state:committed] Signed-off-by: Jeremy Kemper --- activerecord/test/cases/calculations_test.rb | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) (limited to 'activerecord') diff --git a/activerecord/test/cases/calculations_test.rb b/activerecord/test/cases/calculations_test.rb index 8bd0dd0f6e..080f6a7007 100644 --- a/activerecord/test/cases/calculations_test.rb +++ b/activerecord/test/cases/calculations_test.rb @@ -171,8 +171,9 @@ class CalculationsTest < ActiveRecord::TestCase Account.expects(:columns).at_least_once.returns([column]) c = Account.count(:all, :group => :firm) - assert_equal Firm, c.first.first.class - assert_equal 1, c.first.last + first_key = c.keys.first + assert_equal Firm, first_key.class + assert_equal 1, c[first_key] end end -- cgit v1.2.3 From 05f2183747c8e75c9e8bbaadb9573b4bdf41ecfc Mon Sep 17 00:00:00 2001 From: Emilio Tagua Date: Tue, 2 Dec 2008 14:12:09 -0300 Subject: Fix: counter_cache should decrement on deleting associated records. [#1195 state:committed] Signed-off-by: Jeremy Kemper --- .../associations/association_collection.rb | 3 +++ .../cases/associations/has_many_associations_test.rb | 20 ++++++++++++++++++++ .../has_many_through_associations_test.rb | 14 ++++++++++++++ 3 files changed, 37 insertions(+) (limited to 'activerecord') diff --git a/activerecord/lib/active_record/associations/association_collection.rb b/activerecord/lib/active_record/associations/association_collection.rb index 0ff91fbdf8..0ad58010b2 100644 --- a/activerecord/lib/active_record/associations/association_collection.rb +++ b/activerecord/lib/active_record/associations/association_collection.rb @@ -202,6 +202,9 @@ module ActiveRecord records.each do |record| @target.delete(record) + if respond_to?(:cached_counter_attribute_name) && @owner[cached_counter_attribute_name] + @owner.class.decrement_counter(cached_counter_attribute_name, @owner.send(@owner.class.primary_key)) + end callback(:after_remove, record) end end diff --git a/activerecord/test/cases/associations/has_many_associations_test.rb b/activerecord/test/cases/associations/has_many_associations_test.rb index 816ceb6855..1f8b297e81 100644 --- a/activerecord/test/cases/associations/has_many_associations_test.rb +++ b/activerecord/test/cases/associations/has_many_associations_test.rb @@ -552,6 +552,18 @@ class HasManyAssociationsTest < ActiveRecord::TestCase assert_equal 0, companies(:first_firm).clients_of_firm(true).size end + def test_deleting_updates_counter_cache + post = Post.first + + post.comments.delete(post.comments.first) + post.reload + assert_equal post.comments(true).size, post.comments_count + + post.comments.delete(post.comments.first) + post.reload + assert_equal 0, post.comments_count + end + def test_deleting_before_save new_firm = Firm.new("name" => "A New Firm, Inc.") new_client = new_firm.clients_of_firm.build("name" => "Another Client") @@ -605,6 +617,14 @@ class HasManyAssociationsTest < ActiveRecord::TestCase end end + def test_clearing_updates_counter_cache + post = Post.first + + post.comments.clear + post.reload + assert_equal 0, post.comments_count + end + def test_clearing_a_dependent_association_collection firm = companies(:first_firm) client_id = firm.dependent_clients_of_firm.first.id diff --git a/activerecord/test/cases/associations/has_many_through_associations_test.rb b/activerecord/test/cases/associations/has_many_through_associations_test.rb index a07f4bcbdd..ba3428a508 100644 --- a/activerecord/test/cases/associations/has_many_through_associations_test.rb +++ b/activerecord/test/cases/associations/has_many_through_associations_test.rb @@ -3,6 +3,9 @@ require 'models/post' require 'models/person' require 'models/reader' require 'models/comment' +require 'models/tag' +require 'models/tagging' +require 'models/author' class HasManyThroughAssociationsTest < ActiveRecord::TestCase fixtures :posts, :readers, :people, :comments, :authors @@ -84,6 +87,17 @@ class HasManyThroughAssociationsTest < ActiveRecord::TestCase assert posts(:welcome).reload.people(true).empty? end + def test_deleting_updates_counter_cache + taggable = Tagging.first.taggable + taggable.taggings.push(Tagging.new) + taggable.reload + assert_equal 1, taggable.taggings_count + + taggable.taggings.delete(taggable.taggings.first) + taggable.reload + assert_equal 0, taggable.taggings_count + end + def test_replace_association assert_queries(4){posts(:welcome);people(:david);people(:michael); posts(:welcome).people(true)} -- cgit v1.2.3 From 953954302682e1ae2f56cfc2ed04debc737c206c Mon Sep 17 00:00:00 2001 From: Jeremy Kemper Date: Wed, 10 Dec 2008 11:01:04 -0800 Subject: Add ActiveRecord::VERSION autoload --- activerecord/lib/active_record.rb | 2 ++ 1 file changed, 2 insertions(+) (limited to 'activerecord') diff --git a/activerecord/lib/active_record.rb b/activerecord/lib/active_record.rb index 348e5b94af..1aaf456c0f 100644 --- a/activerecord/lib/active_record.rb +++ b/activerecord/lib/active_record.rb @@ -37,6 +37,8 @@ module ActiveRecord [Base, DynamicFinderMatch, ConnectionAdapters::AbstractAdapter] end + autoload :VERSION, 'active_record/version' + autoload :ActiveRecordError, 'active_record/base' autoload :ConnectionNotEstablished, 'active_record/base' -- cgit v1.2.3 From 96b815d7e81b9cef912ef94c96dca923fe43b0ba Mon Sep 17 00:00:00 2001 From: Emilio Tagua Date: Wed, 10 Dec 2008 16:04:29 -0300 Subject: Fix test names collision. [#1549 state:committed] Signed-off-by: Jeremy Kemper --- activerecord/test/cases/validations_i18n_test.rb | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'activerecord') diff --git a/activerecord/test/cases/validations_i18n_test.rb b/activerecord/test/cases/validations_i18n_test.rb index f59e3f7001..e893a704f1 100644 --- a/activerecord/test/cases/validations_i18n_test.rb +++ b/activerecord/test/cases/validations_i18n_test.rb @@ -506,7 +506,7 @@ class ActiveRecordValidationsI18nTests < ActiveSupport::TestCase # validates_length_of :is w/o mocha - def test_validates_length_of_within_finds_custom_model_key_translation + def test_validates_length_of_is_finds_custom_model_key_translation I18n.backend.store_translations 'en', :activerecord => {:errors => {:models => {:topic => {:attributes => {:title => {:wrong_length => 'custom message'}}}}}} I18n.backend.store_translations 'en', :activerecord => {:errors => {:messages => {:wrong_length => 'global message'}}} @@ -515,7 +515,7 @@ class ActiveRecordValidationsI18nTests < ActiveSupport::TestCase assert_equal 'custom message', @topic.errors.on(:title) end - def test_validates_length_of_within_finds_global_default_translation + def test_validates_length_of_is_finds_global_default_translation I18n.backend.store_translations 'en', :activerecord => {:errors => {:messages => {:wrong_length => 'global message'}}} Topic.validates_length_of :title, :is => 5 @@ -525,7 +525,7 @@ class ActiveRecordValidationsI18nTests < ActiveSupport::TestCase # validates_uniqueness_of w/o mocha - def test_validates_length_of_within_finds_custom_model_key_translation + def test_validates_length_of_is_finds_custom_model_key_translation I18n.backend.store_translations 'en', :activerecord => {:errors => {:models => {:topic => {:attributes => {:title => {:wrong_length => 'custom message'}}}}}} I18n.backend.store_translations 'en', :activerecord => {:errors => {:messages => {:wrong_length => 'global message'}}} @@ -534,7 +534,7 @@ class ActiveRecordValidationsI18nTests < ActiveSupport::TestCase assert_equal 'custom message', @topic.errors.on(:title) end - def test_validates_length_of_within_finds_global_default_translation + def test_validates_length_of_is_finds_global_default_translation I18n.backend.store_translations 'en', :activerecord => {:errors => {:messages => {:wrong_length => 'global message'}}} Topic.validates_length_of :title, :is => 5 -- cgit v1.2.3 From aa5cdb0d47fb5484bfdde8244df7efeb2175bf3a Mon Sep 17 00:00:00 2001 From: Bruce Krysiak Date: Mon, 8 Dec 2008 16:38:04 -0800 Subject: Added a :camelize option to ActiveRecord and Hash to_xml serialization and from_xml deserialization Signed-off-by: Michael Koziarski --- .../active_record/serializers/xml_serializer.rb | 23 +++++++++++++++------- activerecord/test/cases/xml_serialization_test.rb | 7 +++++++ 2 files changed, 23 insertions(+), 7 deletions(-) (limited to 'activerecord') diff --git a/activerecord/lib/active_record/serializers/xml_serializer.rb b/activerecord/lib/active_record/serializers/xml_serializer.rb index d171b742f5..4749823b94 100644 --- a/activerecord/lib/active_record/serializers/xml_serializer.rb +++ b/activerecord/lib/active_record/serializers/xml_serializer.rb @@ -23,11 +23,12 @@ module ActiveRecord #:nodoc: # # # This behavior can be controlled with :only, :except, - # :skip_instruct, :skip_types and :dasherize. + # :skip_instruct, :skip_types, :dasherize and :camelize . # The :only and :except options are the same as for the # +attributes+ method. The default is to dasherize all column names, but you - # can disable this setting :dasherize to +false+. To not have the - # column type included in the XML output set :skip_types to +true+. + # can disable this setting :dasherize to +false+. Setting :camelize + # to +true+ will camelize all column names - this also overrides :dasherize. + # To not have the column type included in the XML output set :skip_types to +true+. # # For instance: # @@ -178,13 +179,22 @@ module ActiveRecord #:nodoc: def root root = (options[:root] || @record.class.to_s.underscore).to_s - dasherize? ? root.dasherize : root + reformat_name(root) end def dasherize? !options.has_key?(:dasherize) || options[:dasherize] end + def camelize? + options.has_key?(:camelize) && options[:camelize] + end + + def reformat_name(name) + name = name.camelize if camelize? + dasherize? ? name.dasherize : name + end + def serializable_attributes serializable_attribute_names.collect { |name| Attribute.new(name, @record) } end @@ -212,7 +222,7 @@ module ActiveRecord #:nodoc: def add_tag(attribute) builder.tag!( - dasherize? ? attribute.name.dasherize : attribute.name, + reformat_name(attribute.name), attribute.value.to_s, attribute.decorations(!options[:skip_types]) ) @@ -220,8 +230,7 @@ module ActiveRecord #:nodoc: def add_associations(association, records, opts) if records.is_a?(Enumerable) - tag = association.to_s - tag = tag.dasherize if dasherize? + tag = reformat_name(association.to_s) if records.empty? builder.tag!(tag, :type => :array) else diff --git a/activerecord/test/cases/xml_serialization_test.rb b/activerecord/test/cases/xml_serialization_test.rb index 63f48865cc..39c6ea820d 100644 --- a/activerecord/test/cases/xml_serialization_test.rb +++ b/activerecord/test/cases/xml_serialization_test.rb @@ -31,6 +31,13 @@ class XmlSerializationTest < ActiveRecord::TestCase assert_match %r{ 'xml_contact', :camelize => true + assert_match %r{^}, @xml + assert_match %r{$}, @xml + assert_match %r{ Date: Wed, 10 Dec 2008 14:48:12 -0800 Subject: Revert "Fix: counter_cache should decrement on deleting associated records." [#1196 state:open] This reverts commit 05f2183747c8e75c9e8bbaadb9573b4bdf41ecfc. --- .../associations/association_collection.rb | 3 --- .../cases/associations/has_many_associations_test.rb | 20 -------------------- .../has_many_through_associations_test.rb | 14 -------------- 3 files changed, 37 deletions(-) (limited to 'activerecord') diff --git a/activerecord/lib/active_record/associations/association_collection.rb b/activerecord/lib/active_record/associations/association_collection.rb index 0ad58010b2..0ff91fbdf8 100644 --- a/activerecord/lib/active_record/associations/association_collection.rb +++ b/activerecord/lib/active_record/associations/association_collection.rb @@ -202,9 +202,6 @@ module ActiveRecord records.each do |record| @target.delete(record) - if respond_to?(:cached_counter_attribute_name) && @owner[cached_counter_attribute_name] - @owner.class.decrement_counter(cached_counter_attribute_name, @owner.send(@owner.class.primary_key)) - end callback(:after_remove, record) end end diff --git a/activerecord/test/cases/associations/has_many_associations_test.rb b/activerecord/test/cases/associations/has_many_associations_test.rb index 1f8b297e81..816ceb6855 100644 --- a/activerecord/test/cases/associations/has_many_associations_test.rb +++ b/activerecord/test/cases/associations/has_many_associations_test.rb @@ -552,18 +552,6 @@ class HasManyAssociationsTest < ActiveRecord::TestCase assert_equal 0, companies(:first_firm).clients_of_firm(true).size end - def test_deleting_updates_counter_cache - post = Post.first - - post.comments.delete(post.comments.first) - post.reload - assert_equal post.comments(true).size, post.comments_count - - post.comments.delete(post.comments.first) - post.reload - assert_equal 0, post.comments_count - end - def test_deleting_before_save new_firm = Firm.new("name" => "A New Firm, Inc.") new_client = new_firm.clients_of_firm.build("name" => "Another Client") @@ -617,14 +605,6 @@ class HasManyAssociationsTest < ActiveRecord::TestCase end end - def test_clearing_updates_counter_cache - post = Post.first - - post.comments.clear - post.reload - assert_equal 0, post.comments_count - end - def test_clearing_a_dependent_association_collection firm = companies(:first_firm) client_id = firm.dependent_clients_of_firm.first.id diff --git a/activerecord/test/cases/associations/has_many_through_associations_test.rb b/activerecord/test/cases/associations/has_many_through_associations_test.rb index ba3428a508..a07f4bcbdd 100644 --- a/activerecord/test/cases/associations/has_many_through_associations_test.rb +++ b/activerecord/test/cases/associations/has_many_through_associations_test.rb @@ -3,9 +3,6 @@ require 'models/post' require 'models/person' require 'models/reader' require 'models/comment' -require 'models/tag' -require 'models/tagging' -require 'models/author' class HasManyThroughAssociationsTest < ActiveRecord::TestCase fixtures :posts, :readers, :people, :comments, :authors @@ -87,17 +84,6 @@ class HasManyThroughAssociationsTest < ActiveRecord::TestCase assert posts(:welcome).reload.people(true).empty? end - def test_deleting_updates_counter_cache - taggable = Tagging.first.taggable - taggable.taggings.push(Tagging.new) - taggable.reload - assert_equal 1, taggable.taggings_count - - taggable.taggings.delete(taggable.taggings.first) - taggable.reload - assert_equal 0, taggable.taggings_count - end - def test_replace_association assert_queries(4){posts(:welcome);people(:david);people(:michael); posts(:welcome).people(true)} -- cgit v1.2.3 From 455c7f9e37fda2969e52698b766413fc735eb488 Mon Sep 17 00:00:00 2001 From: Frederick Cheung Date: Wed, 10 Dec 2008 20:57:19 +0000 Subject: Don't use the transaction instance method so that people with has_one/belongs_to :transaction aren't fubared [#1551 state:committed] Signed-off-by: Jeremy Kemper --- activerecord/lib/active_record/transactions.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'activerecord') diff --git a/activerecord/lib/active_record/transactions.rb b/activerecord/lib/active_record/transactions.rb index 27b5aca18f..0a27ea980e 100644 --- a/activerecord/lib/active_record/transactions.rb +++ b/activerecord/lib/active_record/transactions.rb @@ -147,7 +147,7 @@ module ActiveRecord end def save_with_transactions! #:nodoc: - rollback_active_record_state! { transaction { save_without_transactions! } } + rollback_active_record_state! { self.class.transaction { save_without_transactions! } } end # Reset id and @new_record if the transaction rolls back. @@ -175,7 +175,7 @@ module ActiveRecord # instance. def with_transaction_returning_status(method, *args) status = nil - transaction do + self.class.transaction do status = send(method, *args) raise ActiveRecord::Rollback unless status end -- cgit v1.2.3