Skip to content
Browse files

Mapped Mongoid String type to RA string/text type

  • Loading branch information...
1 parent a7ebbfa commit 82e65a69ea2717a5326bfadf667c3391a88c8144 @mshibuya committed
Showing with 77 additions and 16 deletions.
  1. +35 −16 lib/rails_admin/adapters/mongoid.rb
  2. +42 −0 spec/unit/adapters/mongoid_spec.rb
View
51 lib/rails_admin/adapters/mongoid.rb
@@ -5,6 +5,8 @@
module RailsAdmin
module Adapters
module Mongoid
+ STRING_TYPE_COLUMN_NAMES = [:name, :title, :subject]
+
def new(params = {})
AbstractObject.new(model.new)
end
@@ -65,32 +67,37 @@ def properties
@properties = model.fields.map do |name,field|
ar_type =
if name == '_type'
- :mongoid_type
+ { :type => :mongoid_type, :length => 1024 }
+ elsif field.type.to_s == 'String'
+ if (length = length_validation_lookup(name)) && length < 256
+ { :type => :string, :length => length }
+ elsif STRING_TYPE_COLUMN_NAMES.include?(name.to_sym)
+ { :type => :string, :length => 255 }
+ else
+ { :type => :text, :length => nil }
+ end
else
{
- "Array" => :string,
- "BigDecimal" => :string,
- "Boolean" => :boolean,
- "Date" => :date,
- "DateTime" => :datetime,
- "Float" => :float,
- "Hash" => :string,
- "Integer" => :integer,
- "String" => :string,
- "Time" => :datetime,
- "BSON::ObjectId" => :bson_object_id,
- "Object" => :bson_object_id
+ "Array" => { :type => :text, :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 => :string, :length => nil },
+ "Integer" => { :type => :integer, :length => nil },
+ "Time" => { :type => :datetime, :length => nil },
+ "Object" => { :type => :bson_object_id, :length => nil },
}[field.type.to_s] or raise "Need to map field #{field.type.to_s} for field name #{name} in #{model.inspect}"
end
{
:name => field.name.to_sym,
:pretty_name => field.name.to_s.gsub('_', ' ').strip.capitalize,
- :type => ar_type,
- :length => 1024,
:nullable? => true,
:serial? => false,
- }
+ }.merge(ar_type)
end
end
@@ -316,6 +323,18 @@ def association_type_lookup(macro)
raise "Unknown association type: #{macro.inspect}"
end
end
+
+ def length_validation_lookup(name)
+ shortest = model.validators.select do |validator|
+ validator.attributes.include?(name.to_sym) &&
+ validator.class == ActiveModel::Validations::LengthValidator
+ end.min{|a, b| a.options[:maximum] <=> b.options[:maximum] }
+ if shortest
+ shortest.options[:maximum]
+ else
+ false
+ end
+ end
end
end
end
View
42 spec/unit/adapters/mongoid_spec.rb
@@ -27,6 +27,11 @@ class MongoCategory
class MongoUser
include Mongoid::Document
references_one :mongo_profile
+ field :name, :type => String
+ field :message, :type => String
+ field :short_text, :type => String
+
+ validates :short_text, :length => {:maximum => 255}
end
class MongoProfile
@@ -137,4 +142,41 @@ class MongoComment
param[:parent_model_proc].call.should == [MongoBlog, MongoPost]
end
end
+
+ describe "field type detection" do
+ it "maps Mongoid column types to RA types" do
+ @user.properties.should == [
+ { :name => :_type,
+ :pretty_name => "Type",
+ :nullable? => true,
+ :serial? => false,
+ :type => :mongoid_type,
+ :length => 1024 },
+ { :name => :_id,
+ :pretty_name => "Id",
+ :nullable? => true,
+ :serial? => false,
+ :type => :bson_object_id,
+ :length => nil },
+ { :name => :name,
+ :pretty_name => "Name",
+ :nullable? => true,
+ :serial? => false,
+ :type => :string,
+ :length => 255 },
+ { :name => :message,
+ :pretty_name => "Message",
+ :nullable? => true,
+ :serial? => false,
+ :type => :text,
+ :length => nil },
+ { :name => :short_text,
+ :pretty_name => "Short text",
+ :nullable? => true,
+ :serial? => false,
+ :type => :string,
+ :length => 255 }
+ ]
+ end
+ end
end

0 comments on commit 82e65a6

Please sign in to comment.
Something went wrong with that request. Please try again.