Class: Bureaucrat::Fields::Field
- Inherits:
-
Object
- Object
- Bureaucrat::Fields::Field
- Includes:
- Validation::Converters, Validation::Validators
- Defined in:
- lib/bureaucrat/fields.rb
Direct Known Subclasses
BigDecimalField, BooleanField, CharField, ChoiceField, FileField, FloatField, IntegerField
Constant Summary
Constants included from Validation::Validators
Validation::Validators::EMAIL_RE
Class Attribute Summary collapse
-
.default_error_messages ⇒ Object
readonly
Returns the value of attribute default_error_messages.
Instance Attribute Summary collapse
-
#error_messages ⇒ Object
Returns the value of attribute error_messages.
-
#help_text ⇒ Object
Returns the value of attribute help_text.
-
#hidden_widget ⇒ Object
Returns the value of attribute hidden_widget.
-
#initial ⇒ Object
Returns the value of attribute initial.
-
#label ⇒ Object
Returns the value of attribute label.
-
#required ⇒ Object
Returns the value of attribute required.
-
#show_hidden_initial ⇒ Object
Returns the value of attribute show_hidden_initial.
-
#widget ⇒ Object
Returns the value of attribute widget.
Class Method Summary collapse
- .hidden_widget(hidden_widget = nil) ⇒ Object
-
.inherited(c) ⇒ Object
Copy field properties to the child class.
- .set_error(key, template) ⇒ Object
- .widget(widget = nil) ⇒ Object
Instance Method Summary collapse
- #clean(value) ⇒ Object
-
#initialize(options = {}) ⇒ Field
constructor
A new instance of Field.
- #initialize_copy(original) ⇒ Object
- #validating ⇒ Object
- #widget_attrs(widget) ⇒ Object
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(options = {}) ⇒ Field
Returns a new instance of Field.
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 |
# File 'lib/bureaucrat/fields.rb', line 97 def initialize(={}) @required = .fetch(:required, true) @show_hidden_initial = .fetch(:show_hidden_initial, false) @label = [:label] @initial = [:initial] @help_text = .fetch(:help_text, '') @widget = .fetch(:widget, self.class.) @widget = @widget.new if @widget.is_a?(Class) extra_attrs = (@widget) @widget.attrs.update(extra_attrs) if extra_attrs @error_messages = self.class.. merge(.fetch(:error_messages, {})) end |
Class Attribute Details
.default_error_messages ⇒ Object (readonly)
Returns the value of attribute default_error_messages.
64 65 66 |
# File 'lib/bureaucrat/fields.rb', line 64 def @default_error_messages end |
Instance Attribute Details
#error_messages ⇒ Object
Returns the value of attribute error_messages.
95 96 97 |
# File 'lib/bureaucrat/fields.rb', line 95 def @error_messages end |
#help_text ⇒ Object
Returns the value of attribute help_text.
95 96 97 |
# File 'lib/bureaucrat/fields.rb', line 95 def help_text @help_text end |
#hidden_widget ⇒ Object
Returns the value of attribute hidden_widget.
95 96 97 |
# File 'lib/bureaucrat/fields.rb', line 95 def @hidden_widget end |
#initial ⇒ Object
Returns the value of attribute initial.
95 96 97 |
# File 'lib/bureaucrat/fields.rb', line 95 def initial @initial end |
#label ⇒ Object
Returns the value of attribute label.
95 96 97 |
# File 'lib/bureaucrat/fields.rb', line 95 def label @label end |
#required ⇒ Object
Returns the value of attribute required.
95 96 97 |
# File 'lib/bureaucrat/fields.rb', line 95 def required @required end |
#show_hidden_initial ⇒ Object
Returns the value of attribute show_hidden_initial.
95 96 97 |
# File 'lib/bureaucrat/fields.rb', line 95 def show_hidden_initial @show_hidden_initial end |
#widget ⇒ Object
Returns the value of attribute widget.
95 96 97 |
# File 'lib/bureaucrat/fields.rb', line 95 def @widget end |
Class Method Details
.hidden_widget(hidden_widget = nil) ⇒ Object
76 77 78 79 |
# File 'lib/bureaucrat/fields.rb', line 76 def (=nil) @hidden_widget = unless .nil? @hidden_widget end |
.inherited(c) ⇒ Object
Copy field properties to the child class
82 83 84 85 86 |
# File 'lib/bureaucrat/fields.rb', line 82 def inherited(c) c. c. .each {|k, v| c.set_error k, v} end |
.set_error(key, template) ⇒ Object
66 67 68 69 |
# File 'lib/bureaucrat/fields.rb', line 66 def set_error(key, template) @default_error_messages ||= {} @default_error_messages[key] = template end |
.widget(widget = nil) ⇒ Object
71 72 73 74 |
# File 'lib/bureaucrat/fields.rb', line 71 def (=nil) @widget = unless .nil? @widget end |
Instance Method Details
#clean(value) ⇒ Object
121 122 123 124 |
# File 'lib/bureaucrat/fields.rb', line 121 def clean(value) validating { is_present(value) if @required } value end |
#initialize_copy(original) ⇒ Object
130 131 132 133 134 135 |
# File 'lib/bureaucrat/fields.rb', line 130 def initialize_copy(original) super(original) @initial = original.initial ? original.initial.dup : original.initial @label = original.label ? original.label.dup : original.label @error_messages = original..dup end |
#validating ⇒ Object
113 114 115 116 117 118 119 |
# File 'lib/bureaucrat/fields.rb', line 113 def validating yield rescue Validation::ValidationError => error tpl = .fetch(error.error_code, error.error_code.to_s) msg = Utils.format_string(tpl, error.parameters) raise FieldValidationError.new(msg) end |
#widget_attrs(widget) ⇒ Object
126 127 128 |
# File 'lib/bureaucrat/fields.rb', line 126 def () # Override to add field specific attributes end |