Class: Bureaucrat::Fields::Field

Inherits:
Object
  • Object
show all
Includes:
Validation::Converters, Validation::Validators
Defined in:
lib/bureaucrat/fields.rb

Constant Summary

Constants included from Validation::Validators

Validation::Validators::EMAIL_RE

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Validation::Converters

to_big_decimal, to_bool, to_float, to_integer

Methods included from Validation::Validates

fail_with

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(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, self.class.widget)

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

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

Class Attribute Details

.default_error_messagesObject (readonly)

Returns the value of attribute default_error_messages.



64
65
66
# File 'lib/bureaucrat/fields.rb', line 64

def default_error_messages
  @default_error_messages
end

Instance Attribute Details

#error_messagesObject

Returns the value of attribute error_messages.



95
96
97
# File 'lib/bureaucrat/fields.rb', line 95

def error_messages
  @error_messages
end

#help_textObject

Returns the value of attribute help_text.



95
96
97
# File 'lib/bureaucrat/fields.rb', line 95

def help_text
  @help_text
end

#hidden_widgetObject

Returns the value of attribute hidden_widget.



95
96
97
# File 'lib/bureaucrat/fields.rb', line 95

def hidden_widget
  @hidden_widget
end

#initialObject

Returns the value of attribute initial.



95
96
97
# File 'lib/bureaucrat/fields.rb', line 95

def initial
  @initial
end

#labelObject

Returns the value of attribute label.



95
96
97
# File 'lib/bureaucrat/fields.rb', line 95

def label
  @label
end

#requiredObject

Returns the value of attribute required.



95
96
97
# File 'lib/bureaucrat/fields.rb', line 95

def required
  @required
end

#show_hidden_initialObject

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

#widgetObject

Returns the value of attribute widget.



95
96
97
# File 'lib/bureaucrat/fields.rb', line 95

def widget
  @widget
end

Class Method Details

.hidden_widget(hidden_widget = nil) ⇒ Object



76
77
78
79
# File 'lib/bureaucrat/fields.rb', line 76

def hidden_widget(hidden_widget=nil)
  @hidden_widget = hidden_widget unless hidden_widget.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.widget        widget
  c.hidden_widget hidden_widget
  default_error_messages.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 widget(widget=nil)
  @widget = widget unless widget.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.error_messages.dup
end

#validatingObject



113
114
115
116
117
118
119
# File 'lib/bureaucrat/fields.rb', line 113

def validating
  yield
rescue Validation::ValidationError => error
  tpl = error_messages.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 widget_attrs(widget)
  # Override to add field specific attributes
end