Class: Bureaucrat::Fields::ChoiceField
- Defined in:
- lib/bureaucrat/fields.rb
Direct Known Subclasses
Constant Summary
Constants included from Validation::Validators
Validation::Validators::EMAIL_RE
Instance Attribute Summary
Attributes inherited from Field
#error_messages, #help_text, #hidden_widget, #initial, #label, #required, #show_hidden_initial, #widget
Instance Method Summary collapse
- #choices ⇒ Object
- #choices=(value) ⇒ Object
- #clean(value) ⇒ Object
-
#initialize(choices = [], options = {}) ⇒ ChoiceField
constructor
A new instance of ChoiceField.
- #valid_value?(value) ⇒ Boolean
Methods inherited from Field
hidden_widget, inherited, #initialize_copy, set_error, #validating, widget, #widget_attrs
Methods included from Validation::Converters
to_big_decimal, to_bool, to_float, to_integer
Methods included from Validation::Validates
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(choices = [], options = {}) ⇒ ChoiceField
Returns a new instance of ChoiceField.
367 368 369 370 371 |
# File 'lib/bureaucrat/fields.rb', line 367 def initialize(choices=[], ={}) [:required] = .fetch(:required, true) super() self.choices = choices end |
Instance Method Details
#choices ⇒ Object
373 374 375 |
# File 'lib/bureaucrat/fields.rb', line 373 def choices @choices end |
#choices=(value) ⇒ Object
377 378 379 |
# File 'lib/bureaucrat/fields.rb', line 377 def choices=(value) @choices = @widget.choices = value.to_a end |
#clean(value) ⇒ Object
381 382 383 384 385 386 387 388 389 390 391 392 393 |
# File 'lib/bureaucrat/fields.rb', line 381 def clean(value) value = super(value) value = '' if empty_value?(value) value = value.to_s return value if value.empty? validating do fail_with(:invalid_choice, :value => value) unless valid_value?(value) end value end |
#valid_value?(value) ⇒ Boolean
395 396 397 398 399 400 401 402 403 404 405 406 407 |
# File 'lib/bureaucrat/fields.rb', line 395 def valid_value?(value) @choices.each do |k, v| if v.is_a?(Array) # This is an optgroup, so look inside the group for options v.each do |k2, v2| return true if value == k2.to_s end else return true if value == k.to_s end end false end |