Permalink
Browse files

Cleaned up Mongoid serialized fields based on changes in upstream

  • Loading branch information...
mshibuya committed Mar 8, 2012
1 parent 2c130f4 commit 8826de7fc6dc0aa0c80ce5da5a8184485a8f106d
@@ -85,7 +85,7 @@ def table_name
end
def serialized_attributes
- model.serialized_attributes
+ model.serialized_attributes.keys
end
private
@@ -87,14 +87,14 @@ def properties
end
else
{
- "Array" => { :type => :array, :length => nil },
+ "Array" => { :type => :serialized, :length => nil },
"BigDecimal" => { :type => :string, :length => 1024 },
"Boolean" => { :type => :boolean, :length => nil },
"BSON::ObjectId" => { :type => :bson_object_id, :length => nil },
"Date" => { :type => :date, :length => nil },
"DateTime" => { :type => :datetime, :length => nil },
"Float" => { :type => :float, :length => nil },
- "Hash" => { :type => :hash, :length => nil },
+ "Hash" => { :type => :serialized, :length => nil },
"Integer" => { :type => :integer, :length => nil },
"Time" => { :type => :datetime, :length => nil },
"Object" => { :type => :bson_object_id, :length => nil },
@@ -115,13 +115,9 @@ def table_name
end
def serialized_attributes
- @serialized_attributes ||= Hash[model.fields.map do |name, field|
- if ['Array', 'Hash'].include? field.type.to_s
- [name.to_s, nil] # TODO: support Coder
- else
- nil
- end
- end.compact]
+ # Mongoid Array and Hash type columns are mapped to RA serialized type
+ # through type detection in self#properties.
+ []
end
private
@@ -8,7 +8,7 @@
# @see RailsAdmin::Config::Fields::Types::Password.column_names
# @see RailsAdmin::Config::Fields.register_factory
RailsAdmin::Config::Fields.register_factory do |parent, properties, fields|
- if parent.abstract_model.serialized_attributes.keys.include?(properties[:name].to_s)
+ if parent.abstract_model.serialized_attributes.include?(properties[:name].to_s)
fields << RailsAdmin::Config::Fields::Types::Serialized.new(parent, properties[:name], properties)
true
else
@@ -1,4 +1,3 @@
-require 'rails_admin/config/fields/types/array'
require 'rails_admin/config/fields/types/belongs_to_association'
require 'rails_admin/config/fields/types/boolean'
require 'rails_admin/config/fields/types/bson_object_id'
@@ -14,7 +13,6 @@
require 'rails_admin/config/fields/types/has_and_belongs_to_many_association'
require 'rails_admin/config/fields/types/has_many_association'
require 'rails_admin/config/fields/types/has_one_association'
-require 'rails_admin/config/fields/types/hash'
require 'rails_admin/config/fields/types/integer'
require 'rails_admin/config/fields/types/mongoid_type'
require 'rails_admin/config/fields/types/password'
@@ -1,23 +0,0 @@
-require 'rails_admin/config/fields/types/text'
-
-module RailsAdmin
- module Config
- module Fields
- module Types
- class Array < RailsAdmin::Config::Fields::Types::Text
- # Register field type for the type loader
- RailsAdmin::Config::Fields::Types::register(self)
-
- @view_helper = :text_area
-
- register_instance_option(:html_default_value) do
- YAML.dump(value)
- end
- end
- end
- end
- end
-end
-
-
-
@@ -1,23 +0,0 @@
-require 'rails_admin/config/fields/types/text'
-
-module RailsAdmin
- module Config
- module Fields
- module Types
- class Hash < RailsAdmin::Config::Fields::Types::Text
- # Register field type for the type loader
- RailsAdmin::Config::Fields::Types::register(self)
-
- @view_helper = :text_area
-
- register_instance_option(:html_default_value) do
- YAML.dump(value)
- end
- end
- end
- end
- end
-end
-
-
-
@@ -343,7 +343,7 @@ class ARComment < ActiveRecord::Base
end
it "#serialized_attributes works" do
- RailsAdmin::AbstractModel.new('User').serialized_attributes.keys.should == ["roles"]
+ RailsAdmin::AbstractModel.new('User').serialized_attributes.should == ["roles"]
end
end
@@ -162,7 +162,7 @@ class MongoComment
:pretty_name => "Array field",
:nullable? => true,
:serial? => false,
- :type => :array,
+ :type => :serialized,
:length => nil },
{ :name => :big_decimal_field,
:pretty_name => "Big decimal field",
@@ -210,7 +210,7 @@ class MongoComment
:pretty_name => "Hash field",
:nullable? => true,
:serial? => false,
- :type => :hash,
+ :type => :serialized,
:length => nil },
{ :name => :integer_field,
:pretty_name => "Integer field",
@@ -546,16 +546,24 @@ class MongoComment
it "#table_name works" do
@abstract_model.table_name.should == 'articles'
end
+ end
- it "#serialized_attributes works" do
- class MongoUser
- include Mongoid::Document
- field :name, :type => String
- field :array_field, :type => Array
- field :hash_field, :type => Hash
- end
+ describe "serialization" do
+ before do
+ @abstract_model = RailsAdmin::AbstractModel.new('MongoidFieldTest')
+ @controller = RailsAdmin::MainController.new
+ end
+
+ it "accepts array value" do
+ params = {:array_field => '[1,3]'}
+ @controller.send(:sanitize_params_for!, 'create', @abstract_model.config, params)
+ params[:array_field].should == [1, 3]
+ end
- RailsAdmin::AbstractModel.new('MongoUser').serialized_attributes.keys.should == ["array_field", "hash_field"]
+ it "accepts hash value" do
+ params = {:hash_field => '{a: 1, b: 3}'}
+ @controller.send(:sanitize_params_for!, 'create', @abstract_model.config, params)
+ params[:hash_field].should == {"a"=>1, "b"=>3}
end
end
end

0 comments on commit 8826de7

Please sign in to comment.