Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1150 from larrymjordan/input-generators
Add custom input generator support
- Loading branch information
Showing
4 changed files
with
205 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
class <%= name.camelize %>Input <%= @extension_sentence %> | ||
<%- if !options[:extend] -%> | ||
include Formtastic::Inputs::Base | ||
<%- end -%> | ||
|
||
<%- if !options[:extend] || (options[:extend] == "extend") -%> | ||
def to_html | ||
# Add your custom input definition here. | ||
<%- if options[:extend] == "extend" -%> | ||
super | ||
<%- end -%> | ||
end | ||
<%- else -%> | ||
def input_html_options | ||
# Add your custom input extension here. | ||
end | ||
<%- end -%> | ||
end |
124 changes: 124 additions & 0 deletions
124
spec/generators/formtastic/input/input_generator_spec.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
require 'spec_helper' | ||
|
||
require 'generators/formtastic/input/input_generator' | ||
|
||
describe Formtastic::InputGenerator do | ||
include FormtasticSpecHelper | ||
|
||
destination File.expand_path("../../../../../tmp", __FILE__) | ||
|
||
before do | ||
prepare_destination | ||
end | ||
|
||
after do | ||
FileUtils.rm_rf(File.expand_path("../../../../../tmp", __FILE__)) | ||
end | ||
|
||
describe 'without file name' do | ||
it 'should raise Thor::RequiredArgumentMissingError' do | ||
lambda { run_generator }.should raise_error(Thor::RequiredArgumentMissingError) | ||
end | ||
end | ||
|
||
describe "input generator with underscore definition" do | ||
before { run_generator %w(hat_size)} | ||
|
||
describe 'generate an input in its respective folder' do | ||
subject{ file('app/inputs/hat_size_input.rb')} | ||
it { should exist} | ||
it { should contain "class HatSizeInput"} | ||
it { should contain "def to_html"} | ||
it { should contain "include Formtastic::Inputs::Base"} | ||
it { should_not contain "super"} | ||
end | ||
end | ||
|
||
describe "input generator with camelcase definition" do | ||
before { run_generator %w(HatSize)} | ||
|
||
describe 'generate an input in its respective folder' do | ||
subject{ file('app/inputs/hat_size_input.rb')} | ||
it { should exist} | ||
it { should contain "class HatSizeInput"} | ||
end | ||
end | ||
|
||
describe "input generator with camelcase Input name sufixed" do | ||
before { run_generator %w(HatSizeInput)} | ||
|
||
describe 'generate an input in its respective folder' do | ||
subject{ file('app/inputs/hat_size_input.rb')} | ||
it { should exist} | ||
it { should contain "class HatSizeInput"} | ||
end | ||
end | ||
|
||
describe "input generator with underscore _input name sufixed" do | ||
before { run_generator %w(hat_size_input)} | ||
|
||
describe 'generate an input in its respective folder' do | ||
subject{ file('app/inputs/hat_size_input.rb')} | ||
it { should exist} | ||
it { should contain "class HatSizeInput"} | ||
end | ||
end | ||
|
||
describe "input generator with underscore input name sufixed" do | ||
before { run_generator %w(hat_sizeinput)} | ||
|
||
describe 'generate an input in its respective folder' do | ||
subject{ file('app/inputs/hat_size_input.rb')} | ||
it { should exist} | ||
it { should contain "class HatSizeInput"} | ||
end | ||
end | ||
|
||
describe "override an existing input using extend" do | ||
before { run_generator %w(string --extend)} | ||
|
||
describe 'app/inputs/string_input.rb' do | ||
subject{ file('app/inputs/string_input.rb')} | ||
it { should exist } | ||
it { should contain "class StringInput < Formtastic::Inputs::StringInput" } | ||
it { should contain "def to_html" } | ||
it { should_not contain "include Formtastic::Inputs::Base" } | ||
it { should contain "super" } | ||
it { should_not contain "def input_html_options" } | ||
end | ||
end | ||
|
||
describe "extend an existing input" do | ||
before { run_generator %w(FlexibleText --extend string)} | ||
|
||
describe 'app/inputs/flexible_text_input.rb' do | ||
subject{ file('app/inputs/flexible_text_input.rb')} | ||
it { should contain "class FlexibleTextInput < Formtastic::Inputs::StringInput" } | ||
it { should contain "def input_html_options" } | ||
it { should_not contain "include Formtastic::Inputs::Base" } | ||
it { should_not contain "def to_html" } | ||
end | ||
end | ||
|
||
describe "provide a slashed namespace" do | ||
before { run_generator %w(stuff/foo)} | ||
|
||
describe 'app/inputs/stuff/foo_input.rb' do | ||
subject{ file('app/inputs/stuff/foo_input.rb')} | ||
it {should exist} | ||
it { should contain "class Stuff::FooInput" } | ||
it { should contain "include Formtastic::Inputs::Base" } | ||
end | ||
end | ||
|
||
describe "provide a camelized namespace" do | ||
before { run_generator %w(Stuff::Foo)} | ||
|
||
describe 'app/inputs/stuff/foo_input.rb' do | ||
subject{ file('app/inputs/stuff/foo_input.rb')} | ||
it {should exist} | ||
it { should contain "class Stuff::FooInput" } | ||
it { should contain "include Formtastic::Inputs::Base" } | ||
end | ||
end | ||
end |