Class: Bureaucrat::Fields::CharField

Inherits:
Field
  • Object
show all
Defined in:
lib/bureaucrat/fields.rb

Direct Known Subclasses

RegexField

Constant Summary

Constants included from Validation::Validators

Validation::Validators::EMAIL_RE

Instance Attribute Summary collapse

Attributes inherited from Field

#error_messages, #help_text, #hidden_widget, #initial, #label, #required, #show_hidden_initial, #widget

Instance Method Summary collapse

Methods inherited from Field

hidden_widget, inherited, #initialize_copy, set_error, #validating, widget

Methods included from Validation::Converters

to_big_decimal, to_bool, to_float, to_integer

Methods included from Validation::Validates

fail_with

Methods included from Validation::Validators

empty_value?, has_max_decimal_places, has_max_digits, has_max_length, has_max_whole_digits, has_min_length, included_in, is_array, is_email, is_not_greater_than, is_not_lesser_than, is_present, is_true, matches_regex, not_empty

Constructor Details

#initialize(options = {}) ⇒ CharField

Returns a new instance of CharField.



145
146
147
148
149
# File 'lib/bureaucrat/fields.rb', line 145

def initialize(options={})
  @max_length = options.delete(:max_length)
  @min_length = options.delete(:min_length)
  super(options)
end

Instance Attribute Details

#max_lengthObject

Returns the value of attribute max_length.



143
144
145
# File 'lib/bureaucrat/fields.rb', line 143

def max_length
  @max_length
end

#min_lengthObject

Returns the value of attribute min_length.



143
144
145
# File 'lib/bureaucrat/fields.rb', line 143

def min_length
  @min_length
end

Instance Method Details

#clean(value) ⇒ Object



157
158
159
160
161
162
163
164
165
166
167
# File 'lib/bureaucrat/fields.rb', line 157

def clean(value)
  super(value)
  return '' if empty_value?(value)

  validating do
      has_max_length(value, @max_length) if @max_length
      has_min_length(value, @min_length) if @min_length
    end

  value
end

#widget_attrs(widget) ⇒ Object



151
152
153
154
155
# File 'lib/bureaucrat/fields.rb', line 151

def widget_attrs(widget)
  return if @max_length.nil?
  return {:maxlength => @max_length.to_s} if
    widget.kind_of?(Widgets::TextInput) || widget.kind_of?(Widgets::PasswordInput)
end