Sha256: 5b88f084c6d4790db63755d2f0a126a022613e3102d3c87080a69d8402bfee25

Contents?: true

Size: 1.4 KB

Versions: 1

Compression:

Stored size: 1.4 KB

Contents

# frozen_string_literal: true
module Formtastic

  # Modify existing inputs, subclass them, or create your own from scratch.
  # @example
  # !!!shell
  #   $ rails generate formtastic:input HatSize

  # @example Define input name using underscore convention
  # !!!shell
  #   $ rails generate formtastic:input hat_size

  # @example Override an existing input behavior
  # !!!shell
  #   $ rails generate formtastic:input string --extend

  # @example Extend an existing input behavior
  # !!!shell
  #   $ rails generate formtastic:input FlexibleText --extend string
  class InputGenerator < Rails::Generators::NamedBase

    argument :name, :type => :string, :required => true, :banner => 'FILE_NAME'

    source_root File.expand_path('../../../templates', __FILE__)

    class_option :extend

    def create
      normalize_file_name
      define_extension_sentence
      template "input.rb", "app/inputs/#{name.underscore}_input.rb"
    end

    protected

    def normalize_file_name
      name.chomp!("Input")  if name.ends_with?("Input")
      name.chomp!("_input") if name.ends_with?("_input")
      name.chomp!("input")  if name.ends_with?("input")
    end

    def define_extension_sentence
      @extension_sentence = "< Formtastic::Inputs::#{name.camelize}Input" if options[:extend] == "extend"
      @extension_sentence ||= "< Formtastic::Inputs::#{options[:extend].camelize}Input" if options[:extend]
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
formtastic-5.0.0 lib/generators/formtastic/input/input_generator.rb