Class: Bureaucrat::Fields::Field

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

Instance Attribute Summary collapse

Instance Method Summary collapse

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(options={})
  @required = options.fetch(:required, true)
  @show_hidden_initial = options.fetch(:show_hidden_initial, false)
  @label = options[:label]
  @initial = options[:initial]
  @help_text = options.fetch(:help_text, '')
  @widget = options.fetch(:widget, default_widget)

  @widget = @widget.new if @widget.is_a?(Class)
  @widget.attrs.update(widget_attrs(@widget))
  @widget.is_required = @required

  @hidden_widget = options.fetch(:hidden_widget, default_hidden_widget)
  @hidden_widget = @hidden_widget.new if @hidden_widget.is_a?(Class)

  @error_messages = default_error_messages.
    merge(options.fetch(:error_messages, {}))

  @validators = default_validators + options.fetch(:validators, [])
end

Instance Attribute Details

#error_messagesObject

Returns the value of attribute error_messages.



51
52
53
# File 'lib/bureaucrat/fields.rb', line 51

def error_messages
  @error_messages
end

#help_textObject

Returns the value of attribute help_text.



51
52
53
# File 'lib/bureaucrat/fields.rb', line 51

def help_text
  @help_text
end

#hidden_widgetObject

Returns the value of attribute hidden_widget.



51
52
53
# File 'lib/bureaucrat/fields.rb', line 51

def hidden_widget
  @hidden_widget
end

#initialObject

Returns the value of attribute initial.



51
52
53
# File 'lib/bureaucrat/fields.rb', line 51

def initial
  @initial
end

#labelObject

Returns the value of attribute label.



51
52
53
# File 'lib/bureaucrat/fields.rb', line 51

def label
  @label
end

#requiredObject

Returns the value of attribute required.



51
52
53
# File 'lib/bureaucrat/fields.rb', line 51

def required
  @required
end

#show_hidden_initialObject

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

#validatorsObject

Returns the value of attribute validators.



51
52
53
# File 'lib/bureaucrat/fields.rb', line 51

def validators
  @validators
end

#widgetObject

Returns the value of attribute widget.



51
52
53
# File 'lib/bureaucrat/fields.rb', line 51

def widget
  @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_messagesObject

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 default_error_messages
  {
    required: 'This field is required',
    invalid: 'Enter a valid value'
  }
end

#default_hidden_widgetObject

Default hidden widget for this kind of field. Override on subclasses to customize.



93
94
95
# File 'lib/bureaucrat/fields.rb', line 93

def default_hidden_widget
  Widgets::HiddenInput
end

#default_validatorsObject

Default validators for this kind of field.



83
84
85
# File 'lib/bureaucrat/fields.rb', line 83

def default_validators
  []
end

#default_widgetObject

Default widget for this kind of field. Override on subclasses to customize.



88
89
90
# File 'lib/bureaucrat/fields.rb', line 88

def default_widget
  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.widget && original.widget.dup
  @validators = original.validators.dup
  @error_messages = original.error_messages.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 && error_messages.has_key?(e.code)
        message = error_messages[e.code]

        if e.params
          message = Utils.format_string(message, e.params)
        end

        errors << message
      else
        errors += e.messages
      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(error_messages[: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 widget_attrs(widget)
  {}
end