Skip to content

Commit cbd6159

Browse files
committed
Form generator
# rails generate form feedback email phone body
1 parent 576c57c commit cbd6159

File tree

7 files changed

+71
-7
lines changed

7 files changed

+71
-7
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,4 @@ test/dummy/db/*.sqlite3-journal
66
test/dummy/log/*.log
77
test/dummy/tmp/
88
test/dummy/.sass-cache
9+
tmp
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
require 'rails/generators'
2+
require 'rails/generators/named_base'
3+
4+
module Rails
5+
module Generators # :nodoc:
6+
class FormGenerator < Rails::Generators::NamedBase # :nodoc:
7+
desc 'This generator creates an active form file at app/forms'
8+
9+
check_class_collision suffix: 'Form'
10+
11+
hook_for :test_framework
12+
13+
argument :attributes, type: :array, default: [], banner: "field1 field2 field3"
14+
15+
def self.default_generator_root
16+
File.dirname(__FILE__)
17+
end
18+
19+
def create_job_file
20+
template 'form.rb', File.join('app/forms', class_path, "#{file_name}_form.rb")
21+
end
22+
23+
end
24+
end
25+
end

lib/generators/active_form/install_generator.rb renamed to lib/rails/generators/form/form_install_generator.rb

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
require 'rails/generators'
22
require 'rails/generators/base'
33

4-
module ActiveForm
4+
module Rails
55
module Generators
6-
class InstallGenerator < Rails::Generators::Base
6+
class FormInstallGenerator < Rails::Generators::Base
77

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

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<% module_namespacing do -%>
2+
class <%= class_name %>Form < ActiveForm::Base
3+
<% if attributes.present? %>
4+
attributes <%= attributes.map {|a| ":#{a.name}" }.join(", ") %>
5+
<% else %>
6+
# attributes :name, :email
7+
<% end %>
8+
end
9+
<% end -%>
+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
require "rails/generators/test_case"
2+
require 'rails/generators/form/form_generator'
3+
4+
class FormGeneratorTest < Rails::Generators::TestCase
5+
tests Rails::Generators::FormGenerator
6+
7+
destination File.expand_path("../../tmp", File.dirname(__FILE__))
8+
setup :prepare_destination
9+
10+
def test_help
11+
content = run_generator ["--help"]
12+
assert_match(/creates an active form file/, content)
13+
end
14+
15+
def test_form_is_created
16+
run_generator ["inquiry"]
17+
assert_file "app/forms/inquiry_form.rb", /class InquiryForm < ActiveForm::Base/
18+
end
19+
20+
def test_form_with_attributes
21+
run_generator ["feedback", "name", "email", "phone"]
22+
assert_file "app/forms/feedback_form.rb", /class FeedbackForm < ActiveForm::Base/
23+
assert_file "app/forms/feedback_form.rb", /attributes :name, :email, :phone/
24+
end
25+
26+
def test_namespaced_forms
27+
run_generator ["admin/feedback"]
28+
assert_file "app/forms/admin/feedback_form.rb", /class Admin::FeedbackForm < ActiveForm::Base/
29+
end
30+
end

test/generators/install_generator_test.rb

+4-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1-
require "test_helper"
1+
require "rails/generators/test_case"
2+
require "rails/generators/form/form_install_generator"
23

3-
class InstallGeneratorTest < Rails::Generators::TestCase
4-
tests ActiveForm::Generators::InstallGenerator
4+
class FormInstallGeneratorTest < Rails::Generators::TestCase
5+
tests Rails::Generators::FormInstallGenerator
56
destination File.expand_path("../../tmp", __FILE__)
67
setup :prepare_destination
78

test/test_helper.rb

-2
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,3 @@
1414
ActiveSupport::TestCase.fixture_path = File.expand_path("../fixtures", __FILE__)
1515
end
1616

17-
require "rails/generators/test_case"
18-
require "generators/active_form/install_generator"

0 commit comments

Comments
 (0)