class Formulary::HtmlForm
Constants
- FIELD_GROUP_TYPES
- FIELD_TYPES
- GROUPED_FIELD_SELECTOR
- SINGULAR_FIELD_SELECTOR
Public Class Methods
new(markup)
click to toggle source
# File lib/formulary/html_form.rb, line 16 def initialize(markup) @markup = markup fields end
Public Instance Methods
data_field_value(name, data_field)
click to toggle source
# File lib/formulary/html_form.rb, line 46 def data_field_value(name, data_field) field = find_field(name) field.blank? ? nil : field.get_value_from_data_field(data_field) end
errors()
click to toggle source
# File lib/formulary/html_form.rb, line 35 def errors fields.each_with_object({}) do |field, hash| hash[field.name] = field.error unless field.valid? end end
valid?(params)
click to toggle source
# File lib/formulary/html_form.rb, line 21 def valid?(params) params.each do |key, value| if value.kind_of?(Hash) value.each do |nested_key, nested_value| set_field_value("#{key}[#{nested_key}]", nested_value) end else set_field_value(key, value) end end fields.all?(&:valid?) end
Protected Instance Methods
build_fields()
click to toggle source
# File lib/formulary/html_form.rb, line 71 def build_fields @fields = [] build_singular_fields_from build_grouped_fields_from end
build_grouped_fields_from()
click to toggle source
# File lib/formulary/html_form.rb, line 88 def build_grouped_fields_from grouped_elements = document.css(GROUPED_FIELD_SELECTOR.strip).group_by do |element| element.attributes["name"].value end grouped_elements.each do |element_group| group_name, elements = *element_group group_klass = FIELD_GROUP_TYPES.detect { |k| k.compatible_with?(elements) } if group_klass.nil? raise UnsupportedFieldType.new("I can't handle these fields: #{elements.inspect}") end @fields << group_klass.new(self, group_name, elements) end end
build_singular_fields_from()
click to toggle source
# File lib/formulary/html_form.rb, line 78 def build_singular_fields_from document.css(SINGULAR_FIELD_SELECTOR.strip).map do |element| field_klass = FIELD_TYPES.detect { |k| k.compatible_with?(element) } if field_klass.nil? raise UnsupportedFieldType.new("I can't handle this field: #{element.inspect}") end @fields << field_klass.new(self, element) end end
document()
click to toggle source
# File lib/formulary/html_form.rb, line 53 def document @document ||= Nokogiri::HTML(@markup) end
fields()
click to toggle source
# File lib/formulary/html_form.rb, line 63 def fields @fields || build_fields end
find_field(name)
click to toggle source
# File lib/formulary/html_form.rb, line 67 def find_field(name) fields.detect { |field| field.name == name.to_s } end
set_field_value(field_name, value)
click to toggle source
# File lib/formulary/html_form.rb, line 57 def set_field_value(field_name, value) field = find_field(field_name) raise UnexpectedParameter.new("Got unexpected field '#{field_name}'") unless field field.set_value(value) end