Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Support for multi-selectable enum field. Closes railsadminteam#833
  • Loading branch information
mshibuya committed Jul 27, 2012
1 parent 24ecbdf commit da1addf
Show file tree
Hide file tree
Showing 11 changed files with 237 additions and 141 deletions.
5 changes: 4 additions & 1 deletion app/assets/javascripts/rails_admin/ra.widgets.coffee
Expand Up @@ -26,7 +26,10 @@ $(document).live 'rails_admin.dom_ready', ->
# enumeration

$('form [data-enumeration]').each ->
$(this).filteringSelect $(this).data('options')
if $(this).is('[multiple]')
$(this).filteringMultiselect $(this).data('options')
else
$(this).filteringSelect $(this).data('options')

# fileupload

Expand Down
19 changes: 18 additions & 1 deletion app/views/rails_admin/main/_form_enumeration.html.haml
@@ -1 +1,18 @@
= form.select field.method_name, field.enum, { :include_blank => true }.reverse_merge((hdv = field.html_default_value).nil? ? {} : { :selected => hdv }), field.html_attributes.reverse_merge({ :data => { :enumeration => true }, :placeholder => t('admin.misc.search') })
- unless field.multiple?
= form.select field.method_name, field.enum, { :include_blank => true }.reverse_merge((hdv = field.html_default_value).nil? ? {} : { :selected => hdv }), field.html_attributes.reverse_merge({ :data => { :enumeration => true }, :placeholder => t('admin.misc.search') })
- else
:ruby
js_data = {
:xhr => false,
:sortable => false,
:cacheAll => true,
:regional => {
:chooseAll => t("admin.misc.chose_all"),
:chosen => t("admin.misc.chosen", :name => config.label_plural),
:clearAll => t("admin.misc.clear_all"),
:search => t("admin.misc.search"),
:up => t("admin.misc.up"),
:down => t("admin.misc.down")
}
}
= form.select field.method_name, field.enum, { :selected => field.value, :object => form.object }, field.html_attributes.reverse_merge({:data => { :filteringmultiselect => true, :options => js_data.to_json }, :multiple => true})
15 changes: 9 additions & 6 deletions lib/rails_admin/adapters/active_record.rb
Expand Up @@ -88,22 +88,17 @@ def properties
{
:name => property.name.to_sym,
:pretty_name => property.name.to_s.tr('_', ' ').capitalize,
:type => property.type,
:length => property.limit,
:nullable? => property.null,
:serial? => property.primary,
}
}.merge(type_lookup(property))
end
end

def table_name
model.table_name
end

def serialized_attributes
model.serialized_attributes.keys
end

def encoding
Rails.configuration.database_configuration[Rails.env]['encoding']
end
Expand Down Expand Up @@ -223,6 +218,14 @@ def build_statement(column, type, value, operator)
end
end

def type_lookup(property)
if model.serialized_attributes[property.name.to_s]
{:type => :serialized}
else
{:type => property.type}
end
end

def association_model_lookup(association)
if association.options[:polymorphic]
RailsAdmin::AbstractModel.polymorphic_parents(:active_record, association.name) || []
Expand Down
72 changes: 34 additions & 38 deletions lib/rails_admin/adapters/mongoid.rb
Expand Up @@ -83,57 +83,20 @@ def associations
def properties
fields = model.fields.reject{|name, field| DISABLED_COLUMN_TYPES.include?(field.type.to_s) }
fields.map do |name,field|
ar_type = {
"Array" => { :type => :serialized },
"BigDecimal" => { :type => :decimal },
"Boolean" => { :type => :boolean },
"BSON::ObjectId" => { :type => :bson_object_id, :serial? => (name == primary_key) },
"Moped::BSON::ObjectId" => { :type => :bson_object_id, :serial? => (name == primary_key) },
"Date" => { :type => :date },
"DateTime" => { :type => :datetime },
"Float" => { :type => :float },
"Hash" => { :type => :serialized },
"Integer" => { :type => :integer },
"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
),
"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
),
"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}"

{
:name => field.name.to_sym,
:length => nil,
:pretty_name => field.name.to_s.gsub('_', ' ').strip.capitalize,
:nullable? => true,
:serial? => false,
}.merge(ar_type)
}.merge(type_lookup(name, field))
end
end

def table_name
model.collection_name.to_s
end

def serialized_attributes
# Mongoid Array and Hash type columns are mapped to RA serialized type
# through type detection in self#properties.
[]
end

def encoding
'UTF-8'
end
Expand Down Expand Up @@ -277,6 +240,39 @@ def build_statement(column, type, value, operator)
end
end

def type_lookup(name, field)
{
"Array" => { :type => :serialized },
"BigDecimal" => { :type => :decimal },
"Boolean" => { :type => :boolean },
"BSON::ObjectId" => { :type => :bson_object_id, :serial? => (name == primary_key) },
"Moped::BSON::ObjectId" => { :type => :bson_object_id, :serial? => (name == primary_key) },
"Date" => { :type => :date },
"DateTime" => { :type => :datetime },
"Float" => { :type => :float },
"Hash" => { :type => :serialized },
"Integer" => { :type => :integer },
"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
),
"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
),
"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}"
end

def association_model_proc_lookup(association)
if association.polymorphic? && [:referenced_in, :belongs_to].include?(association.macro)
RailsAdmin::AbstractModel.polymorphic_parents(:mongoid, association.name) || []
Expand Down
1 change: 0 additions & 1 deletion lib/rails_admin/config/fields.rb
Expand Up @@ -85,4 +85,3 @@ def self.register_factory(&block)
require 'rails_admin/config/fields/factories/dragonfly'
require 'rails_admin/config/fields/factories/carrierwave'
require 'rails_admin/config/fields/factories/association'
require 'rails_admin/config/fields/factories/serialized'
17 changes: 0 additions & 17 deletions lib/rails_admin/config/fields/factories/serialized.rb

This file was deleted.

4 changes: 4 additions & 0 deletions lib/rails_admin/config/fields/types/enum.rb
Expand Up @@ -28,6 +28,10 @@ class Enum < RailsAdmin::Config::Fields::Base
value.presence || ' - '
end
end

register_instance_option(:multiple?) do
properties && [:serialized].include?(properties[:type])
end
end
end
end
Expand Down
8 changes: 6 additions & 2 deletions spec/dummy_app/app/active_record/user.rb
Expand Up @@ -12,10 +12,14 @@ class User < ActiveRecord::Base
# Add Paperclip support for avatars
has_attached_file :avatar, :styles => { :medium => "300x300>", :thumb => "100x100>" }

attr_accessor :delete_avatar
before_validation { self.avatar = nil if self.delete_avatar == '1' }

def attr_accessible_role
:custom_role
end

attr_accessor :delete_avatar
before_validation { self.avatar = nil if self.delete_avatar == '1' }
def roles_enum
[:admin, :user]
end
end
Expand Up @@ -143,6 +143,12 @@

describe "update with serialized objects" do
before(:each) do
RailsAdmin.config do |c|
c.model User do
configure :roles, :serialized
end
end

@user = FactoryGirl.create :user

visit edit_path(:model_name => "user", :id => @user.id)
Expand Down

0 comments on commit da1addf

Please sign in to comment.