Sha256: dd12617f37ec392cf069a13c75ea2d074fc61e28b770a70038d84d20bfb684a8

Contents?: true

Size: 1.49 KB

Versions: 10

Compression:

Stored size: 1.49 KB

Contents

require 'rails_admin/config/fields/types/string_like'

module RailsAdmin
  module Config
    module Fields
      module Types
        class String < StringLike
          RailsAdmin::Config::Fields::Types.register(self)

          def input_size
            [50, length.to_i].reject(&:zero?).min
          end

          register_instance_option :html_attributes do
            {
              required: required?,
              maxlength: length,
              size: input_size,
            }
          end

          def generic_help
            text = (required? ? I18n.translate('admin.form.required') : I18n.translate('admin.form.optional')) + '. '
            if valid_length.present? && valid_length[:is].present?
              text += "#{I18n.translate('admin.form.char_length_of').capitalize} #{valid_length[:is]}."
            else
              max_length = [length, valid_length[:maximum] || nil].compact.min
              min_length = [0, valid_length[:minimum] || nil].compact.max
              if max_length
                text +=
                  if min_length == 0
                    "#{I18n.translate('admin.form.char_length_up_to').capitalize} #{max_length}."
                  else
                    "#{I18n.translate('admin.form.char_length_of').capitalize} #{min_length}-#{max_length}."
                  end
              end
            end
            text
          end

          register_instance_option :partial do
            :form_field
          end
        end
      end
    end
  end
end

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
rails_admin-2.3.1 lib/rails_admin/config/fields/types/string.rb
rails_admin-2.3.0 lib/rails_admin/config/fields/types/string.rb
rails_admin-2.2.1 lib/rails_admin/config/fields/types/string.rb
rails_admin-2.2.0 lib/rails_admin/config/fields/types/string.rb
rails_admin-2.1.1 lib/rails_admin/config/fields/types/string.rb
rails_admin-2.1.0 lib/rails_admin/config/fields/types/string.rb
rails_admin-2.0.2 lib/rails_admin/config/fields/types/string.rb
rails_admin-2.0.1 lib/rails_admin/config/fields/types/string.rb
rails_admin-2.0.0 lib/rails_admin/config/fields/types/string.rb
rails_admin-2.0.0.rc lib/rails_admin/config/fields/types/string.rb