Sha256: 9a1c751c91e4c6b5e2ff0729f669da445f535e926491300163f385fb5f7c1e9d

Contents?: true

Size: 1.88 KB

Versions: 97

Compression:

Stored size: 1.88 KB

Contents

module Account::FormsHelper
  PRESENCE_VALIDATORS = [ActiveRecord::Validations::PresenceValidator, ActiveModel::Validations::PresenceValidator]

  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

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

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

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

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

  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

  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

  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

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

  def current_fields_namespace
    @fields_namespaces&.last
  end
end

Version data entries

97 entries across 97 versions & 2 rubygems

Version Path
bullet_train-fields-1.6.38 app/helpers/account/forms_helper.rb
bullet_train-fields-1.6.37 app/helpers/account/forms_helper.rb
bullet_train-fields-1.6.36 app/helpers/account/forms_helper.rb
bullet_train-fields-1.6.35 app/helpers/account/forms_helper.rb
bullet_train-fields-1.6.34 app/helpers/account/forms_helper.rb
bullet_train-fields-1.6.33 app/helpers/account/forms_helper.rb
bullet_train-fields-1.6.32 app/helpers/account/forms_helper.rb
bullet_train-fields-1.6.31 app/helpers/account/forms_helper.rb
bullet_train-1.6.30 app/helpers/account/forms_helper.rb
bullet_train-1.6.29 app/helpers/account/forms_helper.rb
bullet_train-1.6.28 app/helpers/account/forms_helper.rb
bullet_train-1.6.27 app/helpers/account/forms_helper.rb
bullet_train-1.6.26 app/helpers/account/forms_helper.rb
bullet_train-1.6.25 app/helpers/account/forms_helper.rb
bullet_train-1.6.24 app/helpers/account/forms_helper.rb
bullet_train-1.6.23 app/helpers/account/forms_helper.rb
bullet_train-1.6.22 app/helpers/account/forms_helper.rb
bullet_train-1.6.21 app/helpers/account/forms_helper.rb
bullet_train-1.6.20 app/helpers/account/forms_helper.rb
bullet_train-1.6.19 app/helpers/account/forms_helper.rb