Sha256: f94464a875edac914ce2da9e70e7a7d07de8af616481f4a0675b9a42184360ca

Contents?: true

Size: 1.18 KB

Versions: 2

Compression:

Stored size: 1.18 KB

Contents

# A text field that is used for the Name fields of content.
#
# @example <%= f.name as: :name %>
#
# Add the following behaviors above and beyond
#   1. Will generate a slug if the class requires it. (Requires a as: :path field to work)
#   2. If no label is specified, it shows a larger than normal input which spans the full row.
#   3. Labels are turned off by default.
class NameInput < SimpleForm::Inputs::TextInput

  def initialize(*args)
    super(*args)
    options[:label] = false if options[:label].nil?
    options[:placeholder] = "Name" if options[:placeholder].nil?
  end

  def input
    add_slug_source_for_content_that_needs_it

    unless options[:label]
      input_html_options[:class] << 'input-block-level input-xxlarge'
    end

    @builder.text_field(attribute_name, input_html_options).html_safe
  end

  protected

  def add_slug_source_for_content_that_needs_it
    if should_autogenerate_slug?
      input_html_options[:class] << 'slug-source'
    end
  end

  def should_autogenerate_slug?
    content_requires_slug_field? && (object.new_record? || (object.name.blank? && object.slug.blank?))
  end

  def content_requires_slug_field?
    object.class.requires_slug?
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
browsercms-4.0.0.rc1 app/inputs/name_input.rb
browsercms-4.0.0.beta app/inputs/name_input.rb