Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ test/dummy/db/*.sqlite3-journal
test/dummy/log/*.log
test/dummy/tmp/
test/dummy/.sass-cache
tmp
25 changes: 25 additions & 0 deletions lib/rails/generators/form/form_generator.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
require 'rails/generators'
require 'rails/generators/named_base'

module Rails
module Generators # :nodoc:
class FormGenerator < Rails::Generators::NamedBase # :nodoc:
desc 'This generator creates an active form file at app/forms'

check_class_collision suffix: 'Form'

hook_for :test_framework

argument :attributes, type: :array, default: [], banner: "field1 field2 field3"

def self.default_generator_root
File.dirname(__FILE__)
end

def create_job_file
template 'form.rb', File.join('app/forms', class_path, "#{file_name}_form.rb")
end

end
end
end
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
require 'rails/generators'
require 'rails/generators/base'

module ActiveForm
module Rails
module Generators
class InstallGenerator < Rails::Generators::Base
class FormInstallGenerator < Rails::Generators::Base

desc "Creates a forms directory into your app and test directories and includes the necessary JS file."

Expand Down
9 changes: 9 additions & 0 deletions lib/rails/generators/form/templates/form.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<% module_namespacing do -%>
class <%= class_name %>Form < ActiveForm::Base
<% if attributes.present? %>
attributes <%= attributes.map {|a| ":#{a.name}" }.join(", ") %>
<% else %>
# attributes :name, :email
<% end %>
end
<% end -%>
30 changes: 30 additions & 0 deletions test/generators/form_generator_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
require "rails/generators/test_case"
require 'rails/generators/form/form_generator'

class FormGeneratorTest < Rails::Generators::TestCase
tests Rails::Generators::FormGenerator

destination File.expand_path("../../tmp", File.dirname(__FILE__))
setup :prepare_destination

def test_help
content = run_generator ["--help"]
assert_match(/creates an active form file/, content)
end

def test_form_is_created
run_generator ["inquiry"]
assert_file "app/forms/inquiry_form.rb", /class InquiryForm < ActiveForm::Base/
end

def test_form_with_attributes
run_generator ["feedback", "name", "email", "phone"]
assert_file "app/forms/feedback_form.rb", /class FeedbackForm < ActiveForm::Base/
assert_file "app/forms/feedback_form.rb", /attributes :name, :email, :phone/
end

def test_namespaced_forms
run_generator ["admin/feedback"]
assert_file "app/forms/admin/feedback_form.rb", /class Admin::FeedbackForm < ActiveForm::Base/
end
end
7 changes: 4 additions & 3 deletions test/generators/install_generator_test.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
require "test_helper"
require "rails/generators/test_case"
require "rails/generators/form/form_install_generator"

class InstallGeneratorTest < Rails::Generators::TestCase
tests ActiveForm::Generators::InstallGenerator
class FormInstallGeneratorTest < Rails::Generators::TestCase
tests Rails::Generators::FormInstallGenerator
destination File.expand_path("../../tmp", __FILE__)
setup :prepare_destination

Expand Down
2 changes: 0 additions & 2 deletions test/test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,3 @@
ActiveSupport::TestCase.fixture_path = File.expand_path("../fixtures", __FILE__)
end

require "rails/generators/test_case"
require "generators/active_form/install_generator"