aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorEmilio Tagua <miloops@gmail.com>2010-09-15 09:41:35 -0300
committerJosé Valim <jose.valim@gmail.com>2010-09-17 17:03:51 +0200
commita521a3c05a34e15b68e44d46cb78b6ced1544177 (patch)
tree840f58703994c17107014faa027bca7fbecbb15f
parent3480551e67d2bd97b50a9f95b8f0a49132c38bec (diff)
downloadrails-a521a3c05a34e15b68e44d46cb78b6ced1544177.tar.gz
rails-a521a3c05a34e15b68e44d46cb78b6ced1544177.tar.bz2
rails-a521a3c05a34e15b68e44d46cb78b6ced1544177.zip
Remove TODO and support all expected attributes.
Signed-off-by: José Valim <jose.valim@gmail.com>
-rw-r--r--activeresource/lib/active_resource/schema.rb4
-rw-r--r--activeresource/test/cases/base/schema_test.rb56
2 files changed, 47 insertions, 13 deletions
diff --git a/activeresource/lib/active_resource/schema.rb b/activeresource/lib/active_resource/schema.rb
index 5758ac9502..3fd37a9bb6 100644
--- a/activeresource/lib/active_resource/schema.rb
+++ b/activeresource/lib/active_resource/schema.rb
@@ -4,7 +4,7 @@ module ActiveResource # :nodoc:
class Schema # :nodoc:
# attributes can be known to be one of these types. They are easy to
# cast to/from.
- KNOWN_ATTRIBUTE_TYPES = %w( string integer float )
+ KNOWN_ATTRIBUTE_TYPES = %w( string text integer float decimal datetime timestamp time date binary boolean )
# An array of attribute definitions, representing the attributes that
# have been defined.
@@ -39,8 +39,6 @@ module ActiveResource # :nodoc:
# The following are the attribute types supported by Active Resource
# migrations.
- # TODO: We should eventually support all of these:
- # %w( string text integer float decimal datetime timestamp time date binary boolean ).each do |attr_type|
KNOWN_ATTRIBUTE_TYPES.each do |attr_type|
class_eval <<-EOV, __FILE__, __LINE__ + 1
def #{attr_type.to_s}(*args)
diff --git a/activeresource/test/cases/base/schema_test.rb b/activeresource/test/cases/base/schema_test.rb
index 136c827926..c27af9422e 100644
--- a/activeresource/test/cases/base/schema_test.rb
+++ b/activeresource/test/cases/base/schema_test.rb
@@ -78,14 +78,23 @@ class SchemaTest < ActiveModel::TestCase
end
test "schema should accept a simple hash" do
- new_schema = {'age' => 'integer', 'name' => 'string'}
+ new_schema = {'age' => 'integer', 'name' => 'string',
+ 'height' => 'float', 'bio' => 'text',
+ 'weight' => 'decimal', 'photo' => 'binary',
+ 'alive' => 'boolean', 'created_at' => 'timestamp',
+ 'thetime' => 'time', 'thedate' => 'date', 'mydatetime' => 'datetime'}
+
assert_nothing_raised { Person.schema = new_schema }
assert_equal new_schema, Person.schema
end
test "schema should accept a hash with simple values" do
- new_schema = {'age' => 'integer', 'name' => 'string', 'height' => 'float', 'mydatetime' => 'string'}
+ new_schema = {'age' => 'integer', 'name' => 'string',
+ 'height' => 'float', 'bio' => 'text',
+ 'weight' => 'decimal', 'photo' => 'binary',
+ 'alive' => 'boolean', 'created_at' => 'timestamp',
+ 'thetime' => 'time', 'thedate' => 'date', 'mydatetime' => 'datetime'}
assert_nothing_raised { Person.schema = new_schema }
assert_equal new_schema, Person.schema
@@ -109,7 +118,12 @@ class SchemaTest < ActiveModel::TestCase
end
test "schema should accept nil and remove the schema" do
- new_schema = {'age' => 'integer', 'name' => 'string'}
+ new_schema = {'age' => 'integer', 'name' => 'string',
+ 'height' => 'float', 'bio' => 'text',
+ 'weight' => 'decimal', 'photo' => 'binary',
+ 'alive' => 'boolean', 'created_at' => 'timestamp',
+ 'thetime' => 'time', 'thedate' => 'date', 'mydatetime' => 'datetime'}
+
assert_nothing_raised { Person.schema = new_schema }
assert_equal new_schema, Person.schema # sanity check
@@ -120,7 +134,12 @@ class SchemaTest < ActiveModel::TestCase
test "schema should be with indifferent access" do
- new_schema = {:age => :integer, 'name' => 'string'}
+ new_schema = {'age' => 'integer', 'name' => 'string',
+ 'height' => 'float', 'bio' => 'text',
+ 'weight' => 'decimal', 'photo' => 'binary',
+ 'alive' => 'boolean', 'created_at' => 'timestamp',
+ 'thetime' => 'time', 'thedate' => 'date', 'mydatetime' => 'datetime'}
+
new_schema_syms = new_schema.keys
assert_nothing_raised { Person.schema = new_schema }
@@ -156,7 +175,12 @@ class SchemaTest < ActiveModel::TestCase
test "defining a schema should return it when asked" do
assert Person.schema.blank?, "should have a blank class schema"
- new_schema = {'name' => 'string', 'age' => 'integer', 'height' => 'float', 'weight' => 'float'}
+ new_schema = {'age' => 'integer', 'name' => 'string',
+ 'height' => 'float', 'bio' => 'text',
+ 'weight' => 'decimal', 'photo' => 'binary',
+ 'alive' => 'boolean', 'created_at' => 'timestamp',
+ 'thetime' => 'time', 'thedate' => 'date', 'mydatetime' => 'datetime'}
+
assert_nothing_raised {
Person.schema = new_schema
assert_equal new_schema, Person.schema, "should have saved the schema on the class"
@@ -167,7 +191,11 @@ class SchemaTest < ActiveModel::TestCase
test "defining a schema, then fetching a model should still match the defined schema" do
# sanity checks
assert Person.schema.blank?, "should have a blank class schema"
- new_schema = {'name' => 'string', 'age' => 'integer', 'height' => 'float', 'weight' => 'float'}
+ new_schema = {'age' => 'integer', 'name' => 'string',
+ 'height' => 'float', 'bio' => 'text',
+ 'weight' => 'decimal', 'photo' => 'binary',
+ 'alive' => 'boolean', 'created_at' => 'timestamp',
+ 'thetime' => 'time', 'thedate' => 'date', 'mydatetime' => 'datetime'}
matz = Person.find(1)
assert !matz.schema.blank?, "should have some sort of schema on an instance variable"
@@ -368,12 +396,16 @@ class SchemaTest < ActiveModel::TestCase
end
test "setting schema should set known attributes on class and instance" do
- new_schema = {'age' => 'integer', 'name' => 'string'}
+ new_schema = {'age' => 'integer', 'name' => 'string',
+ 'height' => 'float', 'bio' => 'text',
+ 'weight' => 'decimal', 'photo' => 'binary',
+ 'alive' => 'boolean', 'created_at' => 'timestamp',
+ 'thetime' => 'time', 'thedate' => 'date', 'mydatetime' => 'datetime'}
assert_nothing_raised { Person.schema = new_schema }
- assert_equal new_schema.keys, Person.known_attributes
- assert_equal new_schema.keys, Person.new.known_attributes
+ assert_equal new_schema.keys.sort, Person.known_attributes.sort
+ assert_equal new_schema.keys.sort, Person.new.known_attributes.sort
end
test "known attributes on a fetched resource should return all the attributes of the instance" do
@@ -401,7 +433,11 @@ class SchemaTest < ActiveModel::TestCase
test "setting schema then fetching should add schema attributes to the intance attributes" do
# an attribute in common with fetched instance and one that isn't
- new_schema = {'name' => 'string', 'my_strange_attribute' => 'string'}
+ new_schema = {'age' => 'integer', 'name' => 'string',
+ 'height' => 'float', 'bio' => 'text',
+ 'weight' => 'decimal', 'photo' => 'binary',
+ 'alive' => 'boolean', 'created_at' => 'timestamp',
+ 'thetime' => 'time', 'thedate' => 'date', 'mydatetime' => 'datetime'}
assert_nothing_raised { Person.schema = new_schema }