class TemplateForm::BaseInput

Public Class Methods

new(builder, attribute_name, options) click to toggle source
# File lib/template_form/base_input.rb, line 6
def initialize(builder, attribute_name, options)
  @builder = builder
  @attribute_name = attribute_name

  @form_type = options.delete(:form_type) || builder.form_type

  # Use the `:label` option to override the default label text.
  # Use `label: false` to indicate no label should be shown (check `has_label` in the template).
  @has_label = !(options.has_key?(:label) && options[:label] == false)
  @label_text = options.delete(:label) || ''
  @label_options = Hash.new { |h,k| h[k] = '' }.update(options.delete(:label_options) || {})

  @hint_text = options.delete(:hint) || ''

  data_attributes = (options.delete(:data) || {}).transform_keys { |k| "data-#{k}" }

  @options = options
  @options.merge! data_attributes
  @options[:class] ||= ''
end

Public Instance Methods

render() click to toggle source
# File lib/template_form/base_input.rb, line 28
def render
  template.render(
    builder,
    attribute_name: attribute_name,

    has_label:      has_label,
    label_text:     label_text,
    label_options:  label_options,

    hint_text:      hint_text,

    options:        options,
    errors:         errors
  ).html_safe
end

Private Instance Methods

detect_template_path(name) click to toggle source
# File lib/template_form/base_input.rb, line 80
def detect_template_path(name)
  paths(name).map { |p| Pathname.glob(p).first }.detect &:itself
end
errors() click to toggle source
# File lib/template_form/base_input.rb, line 61
def errors
  builder.object ? builder.object.errors : {}
end
locations() click to toggle source
# File lib/template_form/base_input.rb, line 88
def locations
  [ Pathname.new("#{Rails.root}/app/forms"),
    Pathname.new("#{__dir__}/templates") ]
end
paths(name) click to toggle source
# File lib/template_form/base_input.rb, line 84
def paths(name)
  locations.map { |p| p / form_type.to_s / "#{name}.html.*" }
end
template() click to toggle source
# File lib/template_form/base_input.rb, line 65
def template
  Tilt.new template_file
end
template_file() click to toggle source
# File lib/template_form/base_input.rb, line 69
def template_file
  detect_template_path(template_name) or
    raise TemplateForm::MissingTemplateError,
    "No template found at: #{paths(template_name).join ', '}"
end
template_name() click to toggle source

TemplateForm::TextInput -> text_input

# File lib/template_form/base_input.rb, line 76
def template_name
  self.class.name.demodulize.underscore
end