Class: Roda::Component::Form

Inherits:
Object
  • Object
show all
Extended by:
Delegates
Includes:
Validations
Defined in:
lib/roda/component/form.rb,
lib/roda/component/form/validations.rb

Defined Under Namespace

Modules: Delegates, Validations Classes: Attributes

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Delegates

_delegates

Methods included from Validations

#errors, #valid?, #validate

Constructor Details

#initialize(atts, options = {}) ⇒ Form

Initialize with a hash of attributes and values. If extra attributes are sent, a NoMethodError exception will be raised.

Examples:


class EditPost < Scrivener
  attr_accessor :title
  attr_accessor :body

  def validate
    assert_present :title
    assert_present :body
  end
end

edit = EditPost.new(title: "Software Tools")

edit.valid? #=> false

edit.errors[:title] #=> []
edit.errors[:body]  #=> [:not_present]

edit.body = "Recommended reading..."

edit.valid? #=> true

# Now it's safe to initialize the model.
post = Post.new(edit.attributes)
post.save


88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/roda/component/form.rb', line 88

def initialize(atts, options = {})
  @_data    = atts
  @_data    = atts.to_obj if atts.is_a? Hash
  @_options = options

  # @_attributes = Class.new(Attributes).new
  @_attributes = Attributes.new
  @_attributes.set_attr_accessors _attr_accessors
  @_attributes.set_values _data

  (defined?(_data.attributes) ? _data.attributes : _data).each do |key, val|
    send("#{key}=", val)
  end

  _form.each do |key, klass|
    opts = {}
    opts[key] = klass.new(_data.send(key)) if _data.respond_to?(key)
    @_attributes.set_values opts

    send("#{key}=", opts[key])
  end
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, &block) ⇒ Object



131
132
133
134
135
136
137
138
139
140
# File 'lib/roda/component/form.rb', line 131

def method_missing method, *args, &block
  # respond_to?(symbol, include_all=false)
  if _data.respond_to? method, true
    _data.send method, *args, &block
  else
    return if method[/\=\z/]

    super
  end
end

Class Method Details

.attr_accessor(*vars) ⇒ Object



111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/roda/component/form.rb', line 111

def self.attr_accessor(*vars)
  @_attr_accessors ||= []
  @_form ||= {}

  vars.each do |v|
    if !v.is_a? Hash
      @_attr_accessors << v unless @_attr_accessors.include? v
    else
      v = v.first

      unless @_attr_accessors.include? v.first
        @_attr_accessors << v.first
        @_form[v.first] = v.last
      end
    end
  end

  _delegates(*_attr_accessors)
end

Instance Method Details

#_attributesObject



282
283
284
# File 'lib/roda/component/form.rb', line 282

def _attributes
  @_attributes ||= {}
end

#attributesObject

Return hash of attributes and values.



143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
# File 'lib/roda/component/form.rb', line 143

def attributes
  Hash.new.tap do |atts|
    _attributes.instance_variables.each do |ivar|
      # todo: figure out why it's setting @constructor and @toString
      next if ivar == :@constructor || ivar == :@toString || ivar == :@_attributes || ivar == :@_data || ivar == :@_forms

      att = ivar[1..-1].to_sym
      atts[att] = _attributes.send(att)

      if klass = _form[att.to_s.to_sym]
        atts[att] = klass.new(atts[att]).attributes
      end
    end
  end
end

#display_errors(options = {}, &block) ⇒ Object Also known as: render_errors



191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
# File 'lib/roda/component/form.rb', line 191

def display_errors options = {}, &block
  dom = options.delete(:dom) || _dom
  d_errors = errors

  if override_errors = options[:override_errors]
    d_errors = override_errors
  end

  keys = options.delete(:keys) || (_options[:key] ? [_options[:key]] : [])

  if extra_errors = options.delete(:errors)
    extra_errors.each do |key, value|
      d_errors[key] = value
    end
  end

  d_errors.each do |key, error|
    d_keys = (keys.dup << key)

    error = error.first

    if error.is_a?(Hash)
      d_options = options.dup
      d_options[:keys] = d_keys
      d_options[:override_errors] = d_errors[key].first

      display_errors d_options, &block
    elsif !block_given? || block.call(d_keys, error) == false
      name = d_keys.each_with_index.map do |field, i|
        i != 0 ? "[#{field}]" : field
      end.join

      if tmpl = options[:tmpl]
        if client?
          field_error_dom = DOM.new(`#{tmpl.dom}[0].outerHTML`)
        else
          field_error_dom = DOM.new(tmpl.dom.to_html)
        end
      else
        field_error_dom = DOM.new('<span class="field-error"><span>')
      end

      field_error_dom.html _error_name(key, error)

      field = dom.find("[name='#{name}']")
      field.before field_error_dom.dom
    end
  end
end

#model_attributes(data = attributes) ⇒ Object



159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
# File 'lib/roda/component/form.rb', line 159

def model_attributes data = attributes
  hash = {}

  data.each do |k, v|
    if klass = _form[k.to_s.to_sym]
      d = data[k]
      d = d.attributes if d.is_a?(Form)

      f  = klass.new d
      k  = "#{k}_attributes"
      dt = f.model_attributes

      hash[k] = model_attributes dt
    elsif v.is_a? Hash
      hash[k] = model_attributes data[k]
    else
      hash[k] = v
    end
  end

  hash
end

#render_values(dom = false, key = false, data = false) ⇒ Object



242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
# File 'lib/roda/component/form.rb', line 242

def render_values dom = false, key = false, data = false
  dom = _options[:dom] unless dom
  key = _options[:key] if !key && _options.key?(:key)

  dom.find('input, select, textarea') do |element|
    name  = element['name']
    name  = name.gsub(/\A#{key}/, '') if key
    keys  = name.gsub(/\A\[/, '').gsub(/[^a-z0-9_]/, '|').gsub(/\|\|/, '|').gsub(/\|$/, '').split('|')
    value = false

    keys.each do |k|
      begin
        value = value ? value.send(k) : send(k)
      rescue
        value = ''
      end
    end

    case element.name
    when 'select'
      element.find('option') do |x|
        x['selected'] = true if x['value'] == value.to_s
      end
    when 'input'
      if %w(radio checkbox).include? element['type']
        if element['value'] == value.to_s
          element['checked'] = true
        else
          element.delete 'checked'
        end
      else
        value = sprintf('%.2f', value) if value.is_a? BigDecimal
        element['value'] = value.to_s
      end
    when 'textarea'
      element.val value.to_s
    end
  end
end

#slice(*keys) ⇒ Object



182
183
184
185
186
187
188
189
# File 'lib/roda/component/form.rb', line 182

def slice(*keys)
  Hash.new.tap do |atts|
    keys.each do |att|
      atts[att] = send(att)
      # atts[att] = _attributes.send(att)
    end
  end
end

#validate_msg(error, column) ⇒ Object



286
287
288
# File 'lib/roda/component/form.rb', line 286

def validate_msg error, column
  false
end