Class: Bureaucrat::Fields::CharField

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

Direct Known Subclasses

EmailField, IPAddressField, RegexField, SlugField

Instance Attribute Summary collapse

Attributes inherited from Field

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

Instance Method Summary collapse

Methods inherited from Field

#bound_data, #clean, #default_error_messages, #default_hidden_widget, #default_validators, #default_widget, #initialize_copy, #populate_object, #prepare_value, #run_validators, #validate

Constructor Details

#initialize(options = {}) ⇒ CharField

Returns a new instance of CharField.



186
187
188
189
190
191
192
193
194
195
196
197
198
# File 'lib/bureaucrat/fields.rb', line 186

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

  if @min_length
    validators << Validators::MinLengthValidator.new(@min_length)
  end

  if @max_length
    validators << Validators::MaxLengthValidator.new(@max_length)
  end
end

Instance Attribute Details

#max_lengthObject

Returns the value of attribute max_length.



184
185
186
# File 'lib/bureaucrat/fields.rb', line 184

def max_length
  @max_length
end

#min_lengthObject

Returns the value of attribute min_length.



184
185
186
# File 'lib/bureaucrat/fields.rb', line 184

def min_length
  @min_length
end

Instance Method Details

#to_object(value) ⇒ Object



200
201
202
203
204
205
206
# File 'lib/bureaucrat/fields.rb', line 200

def to_object(value)
  if Validators.empty_value?(value)
    ''
  else
    value
  end
end

#widget_attrs(widget) ⇒ Object



208
209
210
211
212
213
214
215
# File 'lib/bureaucrat/fields.rb', line 208

def widget_attrs(widget)
  super(widget).tap do |attrs|
    if @max_length && (widget.kind_of?(Widgets::TextInput) ||
                       widget.kind_of?(Widgets::PasswordInput))
      attrs.merge(maxlength: @max_length.to_s)
    end
  end
end