Class: Bureaucrat::Fields::Field
- Inherits:
-
Object
- Object
- Bureaucrat::Fields::Field
- Defined in:
- lib/bureaucrat/fields.rb
Direct Known Subclasses
BigDecimalField, BooleanField, CharField, ChoiceField, ComboField, FileField, IntegerField
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.
-
#validators ⇒ Object
Returns the value of attribute validators.
-
#widget ⇒ Object
Returns the value of attribute widget.
Instance Method Summary collapse
-
#bound_data(data, initial) ⇒ Object
The data to be displayed when rendering for a bound form.
- #clean(value) ⇒ Object
-
#default_error_messages ⇒ Object
Default error messages for this kind of field.
-
#default_hidden_widget ⇒ Object
Default hidden widget for this kind of field.
-
#default_validators ⇒ Object
Default validators for this kind of field.
-
#default_widget ⇒ Object
Default widget for this kind of field.
-
#initialize(options = {}) ⇒ Field
constructor
A new instance of Field.
- #initialize_copy(original) ⇒ Object
-
#populate_object(object, name, value) ⇒ Object
Populates object.name if posible.
- #prepare_value(value) ⇒ Object
- #run_validators(value) ⇒ Object
- #to_object(value) ⇒ Object
- #validate(value) ⇒ Object
-
#widget_attrs(widget) ⇒ Object
List of attributes to add on the widget.
Constructor Details
#initialize(options = {}) ⇒ Field
Returns a new instance of Field.
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/bureaucrat/fields.rb', line 53 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, ) @widget = @widget.new if @widget.is_a?(Class) @widget.attrs.update((@widget)) @widget.is_required = @required @hidden_widget = .fetch(:hidden_widget, ) @hidden_widget = @hidden_widget.new if @hidden_widget.is_a?(Class) @error_messages = . merge(.fetch(:error_messages, {})) @validators = default_validators + .fetch(:validators, []) end |
Instance Attribute Details
#error_messages ⇒ Object
Returns the value of attribute error_messages.
51 52 53 |
# File 'lib/bureaucrat/fields.rb', line 51 def @error_messages end |
#help_text ⇒ Object
Returns the value of attribute help_text.
51 52 53 |
# File 'lib/bureaucrat/fields.rb', line 51 def help_text @help_text end |
#hidden_widget ⇒ Object
Returns the value of attribute hidden_widget.
51 52 53 |
# File 'lib/bureaucrat/fields.rb', line 51 def @hidden_widget end |
#initial ⇒ Object
Returns the value of attribute initial.
51 52 53 |
# File 'lib/bureaucrat/fields.rb', line 51 def initial @initial end |
#label ⇒ Object
Returns the value of attribute label.
51 52 53 |
# File 'lib/bureaucrat/fields.rb', line 51 def label @label end |
#required ⇒ Object
Returns the value of attribute required.
51 52 53 |
# File 'lib/bureaucrat/fields.rb', line 51 def required @required end |
#show_hidden_initial ⇒ Object
Returns the value of attribute show_hidden_initial.
51 52 53 |
# File 'lib/bureaucrat/fields.rb', line 51 def show_hidden_initial @show_hidden_initial end |
#validators ⇒ Object
Returns the value of attribute validators.
51 52 53 |
# File 'lib/bureaucrat/fields.rb', line 51 def validators @validators end |
#widget ⇒ Object
Returns the value of attribute widget.
51 52 53 |
# File 'lib/bureaucrat/fields.rb', line 51 def @widget end |
Instance Method Details
#bound_data(data, initial) ⇒ Object
The data to be displayed when rendering for a bound form
149 150 151 |
# File 'lib/bureaucrat/fields.rb', line 149 def bound_data(data, initial) data end |
#clean(value) ⇒ Object
141 142 143 144 145 146 |
# File 'lib/bureaucrat/fields.rb', line 141 def clean(value) value = to_object(value) validate(value) run_validators(value) value end |
#default_error_messages ⇒ Object
Default error messages for this kind of field. Override on subclasses to add or replace messages
75 76 77 78 79 80 |
# File 'lib/bureaucrat/fields.rb', line 75 def { required: 'This field is required', invalid: 'Enter a valid value' } end |
#default_hidden_widget ⇒ Object
Default hidden widget for this kind of field. Override on subclasses to customize.
93 94 95 |
# File 'lib/bureaucrat/fields.rb', line 93 def Widgets::HiddenInput end |
#default_validators ⇒ Object
Default validators for this kind of field.
83 84 85 |
# File 'lib/bureaucrat/fields.rb', line 83 def default_validators [] end |
#default_widget ⇒ Object
Default widget for this kind of field. Override on subclasses to customize.
88 89 90 |
# File 'lib/bureaucrat/fields.rb', line 88 def Widgets::TextInput end |
#initialize_copy(original) ⇒ Object
167 168 169 170 171 172 173 174 175 176 177 178 179 |
# File 'lib/bureaucrat/fields.rb', line 167 def initialize_copy(original) super(original) @initial = original.initial begin @initial = @initial.dup rescue TypeError # non-clonable end @label = original.label && original.label.dup @widget = original. && original..dup @validators = original.validators.dup @error_messages = original..dup end |
#populate_object(object, name, value) ⇒ Object
Populates object.name if posible
159 160 161 162 163 164 165 |
# File 'lib/bureaucrat/fields.rb', line 159 def populate_object(object, name, value) setter = :"#{name}=" if object.respond_to?(setter) object.send(setter, value) end end |
#prepare_value(value) ⇒ Object
97 98 99 |
# File 'lib/bureaucrat/fields.rb', line 97 def prepare_value(value) value end |
#run_validators(value) ⇒ Object
111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 |
# File 'lib/bureaucrat/fields.rb', line 111 def run_validators(value) if Validators.empty_value?(value) return end errors = [] validators.each do |v| begin v.call(value) rescue ValidationError => e if e.code && .has_key?(e.code) = [e.code] if e.params = Utils.format_string(, e.params) end errors << else errors += e. end end end unless errors.empty? raise ValidationError.new(errors) end end |
#to_object(value) ⇒ Object
101 102 103 |
# File 'lib/bureaucrat/fields.rb', line 101 def to_object(value) value end |
#validate(value) ⇒ Object
105 106 107 108 109 |
# File 'lib/bureaucrat/fields.rb', line 105 def validate(value) if required && Validators.empty_value?(value) raise ValidationError.new([:required]) end end |
#widget_attrs(widget) ⇒ Object
List of attributes to add on the widget. Override to add field specific attributes
154 155 156 |
# File 'lib/bureaucrat/fields.rb', line 154 def () {} end |