/
string.rb
51 lines (43 loc) · 1.61 KB
/
string.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
require 'rails_admin/config/fields/base'
module RailsAdmin
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
register_instance_option(:pretty_value) do
bindings[:view].truncate(formatted_value.to_s, :length => 60)
end
register_instance_option(:html_attributes) do
{
:class => "#{css_class} #{has_errors? ? "errorField" : nil} #{color? ? 'color' : nil}",
:maxlength => length,
:size => [50, length.to_i].min,
:style => "width:#{column_width}px",
:value => value,
}
end
register_instance_option(:partial) do
color? ? :form_colorpicker : :form_field
end
end
end
end
end
end