aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord
diff options
context:
space:
mode:
authorEmilio Tagua <miloops@gmail.com>2009-06-09 10:29:55 -0300
committerEmilio Tagua <miloops@gmail.com>2009-06-09 10:29:55 -0300
commit103b282130dd340143654801430aed787da4c9c6 (patch)
treeeddbe800d02f1a3c23f6266b808e74656af31f82 /activerecord
parentfd3c55f09fdfb45c33a5383af2c0b9ddf8f63e90 (diff)
parenta94e7d7897a300a95d5d5a00c5efc573b42bcb58 (diff)
downloadrails-103b282130dd340143654801430aed787da4c9c6.tar.gz
rails-103b282130dd340143654801430aed787da4c9c6.tar.bz2
rails-103b282130dd340143654801430aed787da4c9c6.zip
Merge commit 'rails/master'
Diffstat (limited to 'activerecord')
-rw-r--r--activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb16
-rw-r--r--activerecord/lib/active_record/serialization.rb5
-rw-r--r--activerecord/lib/active_record/serializers/json_serializer.rb32
-rw-r--r--activerecord/lib/active_record/validations.rb1
-rwxr-xr-xactiverecord/test/cases/base_test.rb71
-rw-r--r--activerecord/test/cases/json_serialization_test.rb8
-rw-r--r--activerecord/test/schema/oracle_specific_schema.rb19
7 files changed, 89 insertions, 63 deletions
diff --git a/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb b/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb
index 763db3900d..90a90a0a9b 100644
--- a/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb
+++ b/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb
@@ -288,7 +288,13 @@ module ActiveRecord
# Escapes binary strings for bytea input to the database.
def escape_bytea(value)
- if PGconn.respond_to?(:escape_bytea)
+ if @connection.respond_to?(:escape_bytea)
+ self.class.instance_eval do
+ define_method(:escape_bytea) do |value|
+ @connection.escape_bytea(value) if value
+ end
+ end
+ elsif PGconn.respond_to?(:escape_bytea)
self.class.instance_eval do
define_method(:escape_bytea) do |value|
PGconn.escape_bytea(value) if value
@@ -377,7 +383,13 @@ module ActiveRecord
# Quotes strings for use in SQL input in the postgres driver for better performance.
def quote_string(s) #:nodoc:
- if PGconn.respond_to?(:escape)
+ if @connection.respond_to?(:escape)
+ self.class.instance_eval do
+ define_method(:quote_string) do |s|
+ @connection.escape(s)
+ end
+ end
+ elsif PGconn.respond_to?(:escape)
self.class.instance_eval do
define_method(:quote_string) do |s|
PGconn.escape(s)
diff --git a/activerecord/lib/active_record/serialization.rb b/activerecord/lib/active_record/serialization.rb
index 7959f2b510..23d085bea9 100644
--- a/activerecord/lib/active_record/serialization.rb
+++ b/activerecord/lib/active_record/serialization.rb
@@ -3,8 +3,9 @@ module ActiveRecord #:nodoc:
class Serializer #:nodoc:
attr_reader :options
- def initialize(record, options = {})
- @record, @options = record, options.dup
+ def initialize(record, options = nil)
+ @record = record
+ @options = options ? options.dup : {}
end
# To replicate the behavior in ActiveRecord#attributes,
diff --git a/activerecord/lib/active_record/serializers/json_serializer.rb b/activerecord/lib/active_record/serializers/json_serializer.rb
index 67e2b2abb3..e49cf59494 100644
--- a/activerecord/lib/active_record/serializers/json_serializer.rb
+++ b/activerecord/lib/active_record/serializers/json_serializer.rb
@@ -1,4 +1,5 @@
require 'active_support/json'
+require 'active_support/core_ext/module/model_naming'
module ActiveRecord #:nodoc:
module Serialization
@@ -74,36 +75,17 @@ module ActiveRecord #:nodoc:
# "title": "Welcome to the weblog"},
# {"comments": [{"body": "Don't think too hard"}],
# "title": "So I was thinking"}]}
- def to_json(options = {})
- json = JsonSerializer.new(self, options).to_s
- if include_root_in_json
- "{#{self.class.json_class_name}:#{json}}"
- else
- json
- end
+ def encode_json(encoder)
+ hash = Serializer.new(self, encoder.options).serializable_record
+ hash = { self.class.model_name.element => hash } if include_root_in_json
+ ActiveSupport::JSON.encode(hash)
end
+ def as_json(options = nil) self end #:nodoc:
+
def from_json(json)
self.attributes = ActiveSupport::JSON.decode(json)
self
end
-
- private
- # For compatibility with ActiveSupport::JSON.encode
- def rails_to_json(options, *args)
- to_json(options)
- end
-
- class JsonSerializer < ActiveRecord::Serialization::Serializer #:nodoc:
- def serialize
- ActiveSupport::JSON.encode(serializable_record)
- end
- end
-
- module ClassMethods
- def json_class_name
- @json_class_name ||= name.demodulize.underscore.inspect
- end
- end
end
end
diff --git a/activerecord/lib/active_record/validations.rb b/activerecord/lib/active_record/validations.rb
index 85b65ecf1a..a150ba4acf 100644
--- a/activerecord/lib/active_record/validations.rb
+++ b/activerecord/lib/active_record/validations.rb
@@ -40,6 +40,7 @@ module ActiveRecord
full_messages = []
each do |attribute, messages|
+ messages = Array.wrap(messages)
next if messages.empty?
if attribute == :base
diff --git a/activerecord/test/cases/base_test.rb b/activerecord/test/cases/base_test.rb
index 59aa6953e3..96270118d8 100755
--- a/activerecord/test/cases/base_test.rb
+++ b/activerecord/test/cases/base_test.rb
@@ -146,7 +146,12 @@ class BasicsTest < ActiveRecord::TestCase
def test_read_attributes_before_type_cast_on_datetime
developer = Developer.find(:first)
- assert_equal developer.created_at.to_s(:db) , developer.attributes_before_type_cast["created_at"]
+ # Oracle adapter returns Time before type cast
+ unless current_adapter?(:OracleAdapter)
+ assert_equal developer.created_at.to_s(:db) , developer.attributes_before_type_cast["created_at"]
+ else
+ assert_equal developer.created_at.to_s(:db) , developer.attributes_before_type_cast["created_at"].to_s(:db)
+ end
end
def test_hash_content
@@ -682,21 +687,24 @@ class BasicsTest < ActiveRecord::TestCase
end
end
- def test_update_all_ignores_order_without_limit_from_association
- author = authors(:david)
- assert_nothing_raised do
- assert_equal author.posts_with_comments_and_categories.length, author.posts_with_comments_and_categories.update_all([ "body = ?", "bulk update!" ])
+ # Oracle UPDATE does not support ORDER BY
+ unless current_adapter?(:OracleAdapter)
+ def test_update_all_ignores_order_without_limit_from_association
+ author = authors(:david)
+ assert_nothing_raised do
+ assert_equal author.posts_with_comments_and_categories.length, author.posts_with_comments_and_categories.update_all([ "body = ?", "bulk update!" ])
+ end
end
- end
- def test_update_all_with_order_and_limit_updates_subset_only
- author = authors(:david)
- assert_nothing_raised do
- assert_equal 1, author.posts_sorted_by_id_limited.size
- assert_equal 2, author.posts_sorted_by_id_limited.find(:all, :limit => 2).size
- assert_equal 1, author.posts_sorted_by_id_limited.update_all([ "body = ?", "bulk update!" ])
- assert_equal "bulk update!", posts(:welcome).body
- assert_not_equal "bulk update!", posts(:thinking).body
+ def test_update_all_with_order_and_limit_updates_subset_only
+ author = authors(:david)
+ assert_nothing_raised do
+ assert_equal 1, author.posts_sorted_by_id_limited.size
+ assert_equal 2, author.posts_sorted_by_id_limited.find(:all, :limit => 2).size
+ assert_equal 1, author.posts_sorted_by_id_limited.update_all([ "body = ?", "bulk update!" ])
+ assert_equal "bulk update!", posts(:welcome).body
+ assert_not_equal "bulk update!", posts(:thinking).body
+ end
end
end
@@ -1117,22 +1125,25 @@ class BasicsTest < ActiveRecord::TestCase
Topic.skip_time_zone_conversion_for_attributes = []
end
- def test_multiparameter_attributes_on_time_only_column_with_time_zone_aware_attributes_does_not_do_time_zone_conversion
- ActiveRecord::Base.time_zone_aware_attributes = true
- ActiveRecord::Base.default_timezone = :utc
- Time.zone = ActiveSupport::TimeZone[-28800]
- attributes = {
- "bonus_time(1i)" => "2000", "bonus_time(2i)" => "1", "bonus_time(3i)" => "1",
- "bonus_time(4i)" => "16", "bonus_time(5i)" => "24"
- }
- topic = Topic.find(1)
- topic.attributes = attributes
- assert_equal Time.utc(2000, 1, 1, 16, 24, 0), topic.bonus_time
- assert topic.bonus_time.utc?
- ensure
- ActiveRecord::Base.time_zone_aware_attributes = false
- ActiveRecord::Base.default_timezone = :local
- Time.zone = nil
+ # Oracle, and Sybase do not have a TIME datatype.
+ unless current_adapter?(:OracleAdapter, :SybaseAdapter)
+ def test_multiparameter_attributes_on_time_only_column_with_time_zone_aware_attributes_does_not_do_time_zone_conversion
+ ActiveRecord::Base.time_zone_aware_attributes = true
+ ActiveRecord::Base.default_timezone = :utc
+ Time.zone = ActiveSupport::TimeZone[-28800]
+ attributes = {
+ "bonus_time(1i)" => "2000", "bonus_time(2i)" => "1", "bonus_time(3i)" => "1",
+ "bonus_time(4i)" => "16", "bonus_time(5i)" => "24"
+ }
+ topic = Topic.find(1)
+ topic.attributes = attributes
+ assert_equal Time.utc(2000, 1, 1, 16, 24, 0), topic.bonus_time
+ assert topic.bonus_time.utc?
+ ensure
+ ActiveRecord::Base.time_zone_aware_attributes = false
+ ActiveRecord::Base.default_timezone = :local
+ Time.zone = nil
+ end
end
def test_multiparameter_attributes_on_time_with_empty_seconds
diff --git a/activerecord/test/cases/json_serialization_test.rb b/activerecord/test/cases/json_serialization_test.rb
index 0ffbc9bf3b..54bc8e2343 100644
--- a/activerecord/test/cases/json_serialization_test.rb
+++ b/activerecord/test/cases/json_serialization_test.rb
@@ -170,18 +170,18 @@ class DatabaseConnectedJsonEncodingTest < ActiveRecord::TestCase
def test_should_allow_only_option_for_list_of_authors
authors = [@david, @mary]
- assert_equal %([{"name":"David"},{"name":"Mary"}]), authors.to_json(:only => :name)
+ assert_equal %([{"name":"David"},{"name":"Mary"}]), ActiveSupport::JSON.encode(authors, :only => :name)
end
def test_should_allow_except_option_for_list_of_authors
authors = [@david, @mary]
- assert_equal %([{"id":1},{"id":2}]), authors.to_json(:except => [:name, :author_address_id, :author_address_extra_id])
+ assert_equal %([{"id":1},{"id":2}]), ActiveSupport::JSON.encode(authors, :except => [:name, :author_address_id, :author_address_extra_id])
end
def test_should_allow_includes_for_list_of_authors
authors = [@david, @mary]
- json = authors.to_json(
+ json = ActiveSupport::JSON.encode(authors,
:only => :name,
:include => {
:posts => { :only => :id }
@@ -200,6 +200,6 @@ class DatabaseConnectedJsonEncodingTest < ActiveRecord::TestCase
2 => @mary
}
- assert_equal %({"1":{"name":"David"}}), authors_hash.to_json(:only => [1, :name])
+ assert_equal %({"1":{"name":"David"}}), ActiveSupport::JSON.encode(authors_hash, :only => [1, :name])
end
end
diff --git a/activerecord/test/schema/oracle_specific_schema.rb b/activerecord/test/schema/oracle_specific_schema.rb
new file mode 100644
index 0000000000..2d87f34625
--- /dev/null
+++ b/activerecord/test/schema/oracle_specific_schema.rb
@@ -0,0 +1,19 @@
+ActiveRecord::Schema.define do
+
+ execute "drop table test_oracle_defaults" rescue nil
+ execute "drop sequence test_oracle_defaults_seq" rescue nil
+
+ execute <<-SQL
+create table test_oracle_defaults (
+ id integer not null primary key,
+ test_char char(1) default 'X' not null,
+ test_string varchar2(20) default 'hello' not null,
+ test_int integer default 3 not null
+)
+ SQL
+
+ execute <<-SQL
+create sequence test_oracle_defaults_seq minvalue 10000
+ SQL
+
+end