Skip to content
Browse files

Set field visibility of Mongoid's _id and _type through RailsAdmin::C…

…onfig#default_hidden_fields
  • Loading branch information...
1 parent 6d32552 commit 80d8914e01c64127f9c360906c61e8cc909b4fc3 @mshibuya mshibuya committed
View
21 lib/rails_admin/adapters/mongoid.rb
@@ -14,13 +14,8 @@ def new(params = {})
def get(id)
begin
- id = BSON::ObjectId(id) unless id.is_a?(BSON::ObjectId)
- rescue BSON::InvalidObjectId
- return nil
- end
- if object = model.where(:_id=>id).first
- AbstractObject.new object
- else
+ AbstractObject.new(model.find(id))
+ rescue BSON::InvalidObjectId, ::Mongoid::Errors::DocumentNotFound
nil
end
end
@@ -90,24 +85,22 @@ def properties
"Float" => { :type => :float },
"Hash" => { :type => :serialized },
"Integer" => { :type => :integer },
- "Object" => lambda do
+ "Object" => (
if associations.find{|a| a[:type] == :belongs_to && a[:foreign_key] == name.to_sym}
{ :type => :bson_object_id }
else
{ :type => :string, :length => 255 }
end
- end.call,
- "String" => lambda do
- if name == '_type'
- { :type => :mongoid_type, :length => nil }
- elsif (length = length_validation_lookup(name)) && length < 256
+ ),
+ "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 }
end
- end.call,
+ ),
"Symbol" => { :type => :string, :length => 255 },
"Time" => { :type => :datetime },
}[field.type.to_s] or raise "Need to map field #{field.type.to_s} for field name #{name} in #{model.inspect}"
View
5 lib/rails_admin/config.rb
@@ -277,8 +277,9 @@ def reset
@audit = nil
@current_user = nil
@default_hidden_fields = {}
- @default_hidden_fields[:edit] = [:id, :created_at, :created_on, :deleted_at, :updated_at, :updated_on, :deleted_on]
- @default_hidden_fields[:show] = [:id, :created_at, :created_on, :deleted_at, :updated_at, :updated_on, :deleted_on]
+ @default_hidden_fields[:base] = [:_type]
+ @default_hidden_fields[:edit] = [:id, :_id, :created_at, :created_on, :deleted_at, :updated_at, :updated_on, :deleted_on]
+ @default_hidden_fields[:show] = [:id, :_id, :created_at, :created_on, :deleted_at, :updated_at, :updated_on, :deleted_on]
@default_items_per_page = 20
@default_search_operator = 'default'
@attr_accessible_role = nil
View
1 lib/rails_admin/config/fields/types/all.rb
@@ -14,7 +14,6 @@
require 'rails_admin/config/fields/types/has_many_association'
require 'rails_admin/config/fields/types/has_one_association'
require 'rails_admin/config/fields/types/integer'
-require 'rails_admin/config/fields/types/mongoid_type'
require 'rails_admin/config/fields/types/password'
require 'rails_admin/config/fields/types/polymorphic_association'
require 'rails_admin/config/fields/types/string'
View
25 lib/rails_admin/config/fields/types/mongoid_type.rb
@@ -1,25 +0,0 @@
-require 'rails_admin/config/fields/types/string'
-
-module RailsAdmin
- module Config
- module Fields
- module Types
- class MongoidType < RailsAdmin::Config::Fields::Types::String
- # Register field type for the type loader
- RailsAdmin::Config::Fields::Types::register(self)
-
- register_instance_option(:label) do
- "Type"
- end
-
- register_instance_option(:visible) do
- false
- end
- end
- end
- end
- end
-end
-
-
-
View
1 spec/orm/mongoid.rb
@@ -12,6 +12,7 @@ def column(name, sql_type = 'string', default = nil, null = true)
:string => String,
:text => String,
:date => Date,
+ :datetime => DateTime,
}[sql_type], :default => default
end
end
View
2 spec/unit/adapters/mongoid_spec.rb
@@ -262,7 +262,7 @@ class MongoEmbedded
:pretty_name => "Type",
:nullable? => true,
:serial? => false,
- :type => :mongoid_type,
+ :type => :text,
:length => nil },
{ :name => :array_field,
:pretty_name => "Array field",
View
21 spec/unit/config/fields/base_spec.rb
@@ -315,4 +315,25 @@ class CommentReversed < Tableless
RailsAdmin.config(Comment).fields.find{|f|f.name == :commentable}.associated_collection('').should be_empty
end
end
+
+ describe '#visible?' do
+ it "should be false when fields have specific name " do
+ class FieldVisibilityTest < Tableless
+ column :id, :integer
+ column :_id, :integer
+ column :_type, :string
+ column :name, :string
+ column :created_at, :datetime
+ column :updated_at, :datetime
+ column :deleted_at, :datetime
+ column :created_on, :datetime
+ column :updated_on, :datetime
+ column :deleted_on, :datetime
+ end
+ RailsAdmin.config(FieldVisibilityTest).base.fields.select{|f| f.visible? }.map(&:name).should =~ [:_id, :created_at, :created_on, :deleted_at, :deleted_on, :id, :name, :updated_at, :updated_on]
+ RailsAdmin.config(FieldVisibilityTest).list.fields.select{|f| f.visible? }.map(&:name).should =~ [:_id, :created_at, :created_on, :deleted_at, :deleted_on, :id, :name, :updated_at, :updated_on]
+ RailsAdmin.config(FieldVisibilityTest).edit.fields.select{|f| f.visible? }.map(&:name).should =~ [:name]
+ RailsAdmin.config(FieldVisibilityTest).show.fields.select{|f| f.visible? }.map(&:name).should =~ [:name]
+ end
+ end
end

0 comments on commit 80d8914

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