Class: Formalizer

Inherits:
Object
  • Object
show all
Defined in:
lib/formalizer/form.rb,
lib/formalizer/utils.rb,
lib/formalizer/errors.rb,
lib/formalizer/version.rb,
lib/formalizer/form_field.rb,
lib/formalizer/formalizer.rb

Defined Under Namespace

Classes: ConfigError, FieldNotFound, FileNotFound, Form, FormField, FormFieldParamMissing, FormFillError, FormTemplateError, NotUniqueId, PathOrHtmlRequired, RequiredField, Utils, WrongFileContent, WrongFormTemplate, WrongValue, WrongValueClass

Constant Summary collapse

VERSION =
"1.0.5"
EMPTY_CONFIG =

EMPTY_CONFIG constant used to initialize config if not given from YAML file

{
  form_fields: {},
  forms: {}
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config_file = 'formalizer.yml') ⇒ Formalizer

Constructor Tries loading configuration from YAML file, falling back to an empty config Caching form fields and form targets (a target can be a path or actual html)



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/formalizer/formalizer.rb', line 22

def initialize config_file = 'formalizer.yml'
  load_config config_file

  @form_fields = {}
  @form_sources = {}
  @forms = {}
  @tags = {form_fields: {}, forms: {}}
  @config[:form_fields].each do |key, value|
    add_form_field value.merge(id: key)
  end

  @config[:forms].each do |key, value|
    add_form key, value
  end
end

Instance Attribute Details

#form_fieldsObject (readonly)

Returns the value of attribute form_fields.



14
15
16
# File 'lib/formalizer/formalizer.rb', line 14

def form_fields
  @form_fields
end

#form_sourcesObject (readonly)

Returns the value of attribute form_sources.



14
15
16
# File 'lib/formalizer/formalizer.rb', line 14

def form_sources
  @form_sources
end

#formsObject (readonly)

Returns the value of attribute forms.



14
15
16
# File 'lib/formalizer/formalizer.rb', line 14

def forms
  @forms
end

#tagsObject (readonly)

Returns the value of attribute tags.



14
15
16
# File 'lib/formalizer/formalizer.rb', line 14

def tags
  @tags
end

Instance Method Details

#add_form(form_id, form_hash) ⇒ Object

Adds a form source (path or html) to the forms hash Validates that the id is unique

  • form_id - a unique id to the form

  • form_hash - a hash containing form path/html and tags

Raises:



61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/formalizer/formalizer.rb', line 61

def add_form form_id, form_hash
  # unique id validation
  raise NotUniqueId unless @form_sources[form_id].nil?
  raise TypeError, 'form_hash should be Hash' unless (form_hash.is_a?Hash)
  raise Formalizer::PathOrHtmlRequired, 'form_hash[:path] required' if form_hash[:path].nil?

  # adding to hash
  @form_sources[form_id] = form_hash[:path]

  # handling tags
  add_tags :forms, form_hash

  true
end

#add_form_field(form_field_hash) ⇒ Object

Adds a FormField object to the hash Aalidates that the id is unique

  • form_field_hash - a hash describing the form field.

    See examples at the end of formalizer/form_field.rb
    

Raises:



44
45
46
47
48
49
50
51
52
53
# File 'lib/formalizer/formalizer.rb', line 44

def add_form_field form_field_hash
  # unique id validation
  raise NotUniqueId unless @form_fields[form_field_hash[:id]].nil?

  # adding to hash
  @form_fields[form_field_hash[:id]] = FormField.new(form_field_hash)

  # handling tags
  add_tags :form_fields, form_field_hash
end

#export_form_to_html(form_id, tag = nil) ⇒ Object

Exports a form to html

  • form_id - the form id to export

  • tag - narrows down the passed fields list to fields under the specified tag.

    If nil - all fields will be passed to form
    


115
116
117
118
# File 'lib/formalizer/formalizer.rb', line 115

def export_form_to_html form_id, tag = nil
  form = bind_form_fields(form_id, tag)
  return form.export_to_html
end

#export_form_to_pdf(form_id, tag = nil) ⇒ Object

Exports a form to pdf

  • form_id - the form id to export

  • tag - narrows down the passed fields list to fields under the specified tag.

    If nil - all fields will be passed to form
    


103
104
105
106
# File 'lib/formalizer/formalizer.rb', line 103

def export_form_to_pdf form_id, tag = nil
  form = bind_form_fields(form_id, tag)
  return form.export_to_pdf
end

#fill_field(field_id, field_value) ⇒ Object

Fills one form field with content

  • field_id - the field id

  • field_value - value to fill the field

Raises:



81
82
83
84
# File 'lib/formalizer/formalizer.rb', line 81

def fill_field field_id, field_value
  raise FieldNotFound, "field #{field_id} not found" if @form_fields[field_id].nil?
  @form_fields[field_id].value = field_value
end

#fill_fields(field_params) ⇒ Object

Fills multiple form fields with content

  • field_params - Hash of field_id: field_value objects to fill multuple fields

Raises:

  • (TypeError)


90
91
92
93
94
95
# File 'lib/formalizer/formalizer.rb', line 90

def fill_fields field_params
  raise TypeError, 'field_params should be a Hash' unless (field_params.is_a?Hash)
  field_params.each do |field_id, field_value|
    fill_field(field_id, field_value)
  end
end

#generate_fields_form(locale = I18n.locale, form_action = '', tag = nil) ⇒ Object

Generates an HTML form with fields to fill.

  • locale - relevant with enum/multiple type fields, defaults to default app locale

  • form_action - generated form can have an action. Defaults to ”

  • tag - narrows down thefields list to those under the specified tag. Defaults to all fields



157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
# File 'lib/formalizer/formalizer.rb', line 157

def generate_fields_form locale = I18n.locale, form_action = '', tag = nil
  fields = fields_by_tag(tag)
  action = form_action.empty? ? '' : " action=\"#{form_action}\""
  html = Nokogiri::HTML("<body><form#{action} method='POST'><fieldset></fieldset></form></body>")
  fields.each do |key, field|
    wrapper = Nokogiri::XML::Node.new('div', html)
    label = Nokogiri::XML::Node.new('label', wrapper)
    label['for'] = field.id
    label.content = "#{field.name(locale)}:"
    rendered_field = field.render_html(wrapper, locale)
    label.parent = wrapper
    rendered_field.parent = wrapper
    wrapper.parent = html.at_css('fieldset')
  end
  submit_container = Nokogiri::XML::Node.new('div', html)
  submit_types = %w(HTML PDF)
  %w(reset submit submit).each_with_index do |control_type, index|
    reset_submit = Nokogiri::XML::Node.new('input', submit_container)
    reset_submit['type'] = control_type
    if control_type == 'submit'
      reset_submit['name'] = "export_#{submit_types[index - 1]}"
      reset_submit['value'] = "Export as #{submit_types[index - 1]}"
    end
    reset_submit.parent = submit_container
  end
  submit_container.parent = html.at_css('fieldset')
  html.to_s
end

#load_config(config_file) ⇒ Object

Loads config from YAML file

  • config_file - path to the config file, absolute or relative to app’s ‘config’ path

Raises an error if YAML file is ill-formatted Puts a warning if not YAML file not found



126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
# File 'lib/formalizer/formalizer.rb', line 126

def load_config config_file
  # searching in paths
  @config = nil
  begin
    config_file_content = Utils::find_file(config_file) # will raise an exception if not found
    @config ||= YAML.load(config_file_content)
  rescue FileNotFound
    # ok, no config... not a disaster
    puts "YAML Not found in #{File.expand_path(config_file)}"
  rescue Psych::SyntaxError
    # config found but ill-formatted YAML
    raise "YAML Syntax Error in #{file_path}"
  end

  # Fallback to an empty config
  @config ||= EMPTY_CONFIG

  validate_config

  # YAML is translated to quoted keys, FormField expects symbols:
  @config.deep_symbolize_keys!

end