Module: Account::FormsHelper

Defined in:
app/helpers/account/forms_helper.rb

Constant Summary collapse

PRESENCE_VALIDATORS =
[ActiveRecord::Validations::PresenceValidator, ActiveModel::Validations::PresenceValidator]

Instance Method Summary collapse

Instance Method Details

#current_fields_namespaceObject



62
63
64
# File 'app/helpers/account/forms_helper.rb', line 62

def current_fields_namespace
  @fields_namespaces&.last
end

#flush_content_for(name) ⇒ Object



12
13
14
15
16
# File 'app/helpers/account/forms_helper.rb', line 12

def flush_content_for(name)
  content_for name, flush: true do
    ""
  end
end

#if_present(string) ⇒ Object



26
27
28
# File 'app/helpers/account/forms_helper.rb', line 26

def if_present(string)
  string.present? ? string : nil
end

#labels_for(form, method) ⇒ Object



34
35
36
37
38
# File 'app/helpers/account/forms_helper.rb', line 34

def labels_for(form, method)
  keys = [:placeholder, :label, :help, :options_help]
  path = [model_key(form), current_fields_namespace || :fields, method].compact
  Struct.new(*keys).new(*keys.map { |key| t((path + [key]).join("."), default: "").presence })
end

#legacy_label_for(form, method) ⇒ Object



48
49
50
51
52
53
# File 'app/helpers/account/forms_helper.rb', line 48

def legacy_label_for(form, method)
  # e.g. 'scaffolding/things.labels.name'
  key = "#{model_key(form)}.labels.#{method}"
  #  e.g. 'scaffolding/things.labels.name' or 'scaffolding.things.labels.name' or nil
  t(key, default: "").presence || t(key.tr("/", "."), default: "").presence
end

#model_key(form) ⇒ Object



30
31
32
# File 'app/helpers/account/forms_helper.rb', line 30

def model_key(form)
  form.object.class.name.pluralize.underscore
end

#options_for(form, method) ⇒ Object



40
41
42
43
44
45
46
# File 'app/helpers/account/forms_helper.rb', line 40

def options_for(form, method)
  # e.g. "scaffolding/completely_concrete/tangible_things.fields.text_area_value.options"
  path = [model_key(form), current_fields_namespace || :fields, method, :options]
  options = t(path.compact.join("."))
  return options unless options.is_a?(Hash)
  options.stringify_keys
end

#options_with_labels(options, namespace) ⇒ Object



18
19
20
21
22
23
24
# File 'app/helpers/account/forms_helper.rb', line 18

def options_with_labels(options, namespace)
  hash = {}
  options.each do |option|
    hash[option] = t([namespace, option].join("."))
  end
  hash
end

#presence_validated?(object, attribute) ⇒ Boolean

Returns:

  • (Boolean)


4
5
6
7
8
9
10
# File 'app/helpers/account/forms_helper.rb', line 4

def presence_validated?(object, attribute)
  validators = object.class.validators
  validators.select! do |validator|
    PRESENCE_VALIDATORS.include?(validator.class) && validator.attributes.include?(attribute)
  end
  validators.any?
end

#within_fields_namespace(namespace) ⇒ Object



55
56
57
58
59
60
# File 'app/helpers/account/forms_helper.rb', line 55

def within_fields_namespace(namespace)
  @fields_namespaces ||= []
  @fields_namespaces << namespace
  yield
  @fields_namespaces.pop
end