Skip to content

Commit

Permalink
refactored the whole string/color/virtual mess.
Browse files Browse the repository at this point in the history
  • Loading branch information
bbenezech committed Sep 5, 2011
1 parent c95b9e2 commit fce667b
Show file tree
Hide file tree
Showing 8 changed files with 37 additions and 69 deletions.
4 changes: 3 additions & 1 deletion README.md
Expand Up @@ -32,6 +32,9 @@ Supported ORMs:

## <a name="notices">Notices</a>

`Virtual` Class is no more. :(
Just use `String` instead, or another type. There is a `virtual?` method on `Fields::Base`, that can be used to detect whereas field has properties.

`:attr_accessible` is now taken into account: restricted fields are not editable anymore, and mass-assignement security isn't bypassed anymore. Be careful if you whitelist attributes, you'll need to whitelist association 'id' methods as well : `division_id`, `player_ids`, `commentable_type`, `commentable_id`, etc.

Default scopes are now fully *active* in list views (ordering is overriden, obvisously) as they used to a while ago. This is not configurable (that would bring consistency issues with cancan scoping which brings default scope). If you don't want some default scopes in RailsAdmin, either move your scoping rules to cancan, or activate your default scope conditionnaly on user/url prefix.
Expand Down Expand Up @@ -1099,7 +1102,6 @@ RailsAdmin ships with the following field types:
* text
* time
* timestamp
* virtual *(useful for displaying data that is calculated a runtime [for example a method call on model instance])*

**Fields - Creating a custom field type**

Expand Down
2 changes: 1 addition & 1 deletion lib/rails_admin/config/fields/types/all.rb
Expand Up @@ -17,4 +17,4 @@
require 'rails_admin/config/fields/types/text'
require 'rails_admin/config/fields/types/time'
require 'rails_admin/config/fields/types/timestamp'
require 'rails_admin/config/fields/types/virtual'
require 'rails_admin/config/fields/types/color'
18 changes: 18 additions & 0 deletions lib/rails_admin/config/fields/types/color.rb
@@ -0,0 +1,18 @@
require 'rails_admin/config/fields/base'

module RailsAdmin
module Config
module Fields
module Types
class Color < RailsAdmin::Config::Fields::Base
# Register field type for the type loader
RailsAdmin::Config::Fields::Types::register(self)

register_instance_option(:partial) do
:form_colorpicker
end
end
end
end
end
end
4 changes: 2 additions & 2 deletions lib/rails_admin/config/fields/types/password.rb
@@ -1,12 +1,12 @@
require 'rails_admin/config/fields'
require 'rails_admin/config/sections/list'
require 'rails_admin/config/fields/types/virtual'
require 'rails_admin/config/fields/types/string'

module RailsAdmin
module Config
module Fields
module Types
class Password < RailsAdmin::Config::Fields::Types::Virtual
class Password < RailsAdmin::Config::Fields::Types::String
# Register field type for the type loader
RailsAdmin::Config::Fields::Types::register(self)

Expand Down
32 changes: 11 additions & 21 deletions lib/rails_admin/config/fields/types/string.rb
Expand Up @@ -5,39 +5,29 @@ module Config
module Fields
module Types
class String < RailsAdmin::Config::Fields::Base
# Register field type for the type loader
RailsAdmin::Config::Fields::Types::register(self)

# Display a colorpicker widget instead of text input.
# Todo: refactor to a dedicated field type
register_instance_option(:color?) do
false
end

register_instance_option(:help) do
text = (required? ? I18n.translate("admin.new.required") : I18n.translate("admin.new.optional")) + '. '

# Length requirement isn't necessary to display in case a colorpicker is rendered
unless color?
text += "#{length} "
text += length == 1 ? I18n.translate("admin.new.one_char") : I18n.translate("admin.new.many_chars")
text += ". "
end
text
end
RailsAdmin::Config::Fields::Types::register(self)

@view_helper = :text_field

register_instance_option(:html_attributes) do
{
:class => "#{css_class} #{has_errors? ? "errorField" : nil} #{color? ? 'color' : nil}",
:class => "#{css_class} #{has_errors? ? "errorField" : nil}",
:maxlength => length,
:size => [50, length.to_i].min,
:style => "width:#{column_width}px",
:value => value,
}
end

register_instance_option(:help) do
text = (required? ? I18n.translate("admin.new.required") : I18n.translate("admin.new.optional")) + '. '
text += "#{length} #{length == 1 ? I18n.translate("admin.new.one_char") : I18n.translate("admin.new.many_chars")}." if length.present?
text
end

register_instance_option(:partial) do
color? ? :form_colorpicker : :form_field
:form_field
end
end
end
Expand Down
40 changes: 0 additions & 40 deletions lib/rails_admin/config/fields/types/virtual.rb

This file was deleted.

2 changes: 1 addition & 1 deletion lib/rails_admin/config/has_fields.rb
Expand Up @@ -13,7 +13,7 @@ def field(name, type = nil, &block)
# Specify field as virtual if type is not specifically set and field was not
# found in default stack
if field.nil? && type.nil?
field = (@fields << RailsAdmin::Config::Fields::Types.load(:virtual).new(self, name, {})).last
field = (@fields << RailsAdmin::Config::Fields::Types.load(:string).new(self, name, {})).last

# Register a custom field type if one is provided and it is different from
# one found in default stack
Expand Down
4 changes: 1 addition & 3 deletions spec/requests/config/edit/rails_admin_config_edit_spec.rb
Expand Up @@ -725,9 +725,7 @@ def color_list
it "should show input with class color" do
RailsAdmin.config Team do
edit do
field :color do
color true
end
field :color, :color
end
end
visit new_path(:model_name => "team")
Expand Down

0 comments on commit fce667b

Please sign in to comment.