Skip to content

Commit

Permalink
Cleaned up Mongoid serialized fields based on changes in upstream
Browse files Browse the repository at this point in the history
  • Loading branch information
mshibuya committed Mar 8, 2012
1 parent 2c130f4 commit 8826de7
Show file tree
Hide file tree
Showing 8 changed files with 26 additions and 70 deletions.
2 changes: 1 addition & 1 deletion lib/rails_admin/adapters/active_record.rb
Expand Up @@ -85,7 +85,7 @@ def table_name
end end


def serialized_attributes def serialized_attributes
model.serialized_attributes model.serialized_attributes.keys
end end


private private
Expand Down
14 changes: 5 additions & 9 deletions lib/rails_admin/adapters/mongoid.rb
Expand Up @@ -87,14 +87,14 @@ def properties
end end
else else
{ {
"Array" => { :type => :array, :length => nil }, "Array" => { :type => :serialized, :length => nil },
"BigDecimal" => { :type => :string, :length => 1024 }, "BigDecimal" => { :type => :string, :length => 1024 },
"Boolean" => { :type => :boolean, :length => nil }, "Boolean" => { :type => :boolean, :length => nil },
"BSON::ObjectId" => { :type => :bson_object_id, :length => nil }, "BSON::ObjectId" => { :type => :bson_object_id, :length => nil },
"Date" => { :type => :date, :length => nil }, "Date" => { :type => :date, :length => nil },
"DateTime" => { :type => :datetime, :length => nil }, "DateTime" => { :type => :datetime, :length => nil },
"Float" => { :type => :float, :length => nil }, "Float" => { :type => :float, :length => nil },
"Hash" => { :type => :hash, :length => nil }, "Hash" => { :type => :serialized, :length => nil },
"Integer" => { :type => :integer, :length => nil }, "Integer" => { :type => :integer, :length => nil },
"Time" => { :type => :datetime, :length => nil }, "Time" => { :type => :datetime, :length => nil },
"Object" => { :type => :bson_object_id, :length => nil }, "Object" => { :type => :bson_object_id, :length => nil },
Expand All @@ -115,13 +115,9 @@ def table_name
end end


def serialized_attributes def serialized_attributes
@serialized_attributes ||= Hash[model.fields.map do |name, field| # Mongoid Array and Hash type columns are mapped to RA serialized type
if ['Array', 'Hash'].include? field.type.to_s # through type detection in self#properties.
[name.to_s, nil] # TODO: support Coder []
else
nil
end
end.compact]
end end


private private
Expand Down
2 changes: 1 addition & 1 deletion lib/rails_admin/config/fields/factories/serialized.rb
Expand Up @@ -8,7 +8,7 @@
# @see RailsAdmin::Config::Fields::Types::Password.column_names # @see RailsAdmin::Config::Fields::Types::Password.column_names
# @see RailsAdmin::Config::Fields.register_factory # @see RailsAdmin::Config::Fields.register_factory
RailsAdmin::Config::Fields.register_factory do |parent, properties, fields| 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) fields << RailsAdmin::Config::Fields::Types::Serialized.new(parent, properties[:name], properties)
true true
else else
Expand Down
2 changes: 0 additions & 2 deletions lib/rails_admin/config/fields/types/all.rb
@@ -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/belongs_to_association'
require 'rails_admin/config/fields/types/boolean' require 'rails_admin/config/fields/types/boolean'
require 'rails_admin/config/fields/types/bson_object_id' require 'rails_admin/config/fields/types/bson_object_id'
Expand All @@ -14,7 +13,6 @@
require 'rails_admin/config/fields/types/has_and_belongs_to_many_association' 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_many_association'
require 'rails_admin/config/fields/types/has_one_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/integer'
require 'rails_admin/config/fields/types/mongoid_type' require 'rails_admin/config/fields/types/mongoid_type'
require 'rails_admin/config/fields/types/password' require 'rails_admin/config/fields/types/password'
Expand Down
23 changes: 0 additions & 23 deletions lib/rails_admin/config/fields/types/array.rb

This file was deleted.

23 changes: 0 additions & 23 deletions lib/rails_admin/config/fields/types/hash.rb

This file was deleted.

2 changes: 1 addition & 1 deletion spec/unit/adapters/active_record_spec.rb
Expand Up @@ -343,7 +343,7 @@ class ARComment < ActiveRecord::Base
end end


it "#serialized_attributes works" do it "#serialized_attributes works" do
RailsAdmin::AbstractModel.new('User').serialized_attributes.keys.should == ["roles"] RailsAdmin::AbstractModel.new('User').serialized_attributes.should == ["roles"]
end end
end end


Expand Down
28 changes: 18 additions & 10 deletions spec/unit/adapters/mongoid_spec.rb
Expand Up @@ -162,7 +162,7 @@ class MongoComment
:pretty_name => "Array field", :pretty_name => "Array field",
:nullable? => true, :nullable? => true,
:serial? => false, :serial? => false,
:type => :array, :type => :serialized,
:length => nil }, :length => nil },
{ :name => :big_decimal_field, { :name => :big_decimal_field,
:pretty_name => "Big decimal field", :pretty_name => "Big decimal field",
Expand Down Expand Up @@ -210,7 +210,7 @@ class MongoComment
:pretty_name => "Hash field", :pretty_name => "Hash field",
:nullable? => true, :nullable? => true,
:serial? => false, :serial? => false,
:type => :hash, :type => :serialized,
:length => nil }, :length => nil },
{ :name => :integer_field, { :name => :integer_field,
:pretty_name => "Integer field", :pretty_name => "Integer field",
Expand Down Expand Up @@ -546,16 +546,24 @@ class MongoComment
it "#table_name works" do it "#table_name works" do
@abstract_model.table_name.should == 'articles' @abstract_model.table_name.should == 'articles'
end end
end


it "#serialized_attributes works" do describe "serialization" do
class MongoUser before do
include Mongoid::Document @abstract_model = RailsAdmin::AbstractModel.new('MongoidFieldTest')
field :name, :type => String @controller = RailsAdmin::MainController.new
field :array_field, :type => Array end
field :hash_field, :type => Hash
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 end
end end

0 comments on commit 8826de7

Please sign in to comment.