Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Form generators are realised #4

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
11 changes: 10 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,16 @@ Add this line to your application's Gemfile:
gem 'reform-rails'
```

## Generators

This gem also have support of rails generators:

```
bin/rails g form Test property1 property2
```

It will generate TestForm in `app/forms` dir.

## License

The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).

20 changes: 20 additions & 0 deletions lib/rails/generators/form/form_generator.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
module Rails
module Generators
class FormGenerator < Rails::Generators::NamedBase
source_root File.expand_path('../templates', __FILE__)

check_class_collision suffix: 'Form'

argument :properties,
type: :array,
default: [],
banner: 'property property2'

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

hook_for :test_framework
end
end
end
5 changes: 5 additions & 0 deletions lib/rails/generators/form/templates/form.rb.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class <%= class_name %>Form < Reform::Form
<% properties.each do |property| -%>
property :<%= property %>
<% end -%>
end
1 change: 1 addition & 0 deletions reform-rails.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,6 @@ Gem::Specification.new do |spec|
spec.add_development_dependency "rails"
spec.add_development_dependency "bundler", "~> 1.10"
spec.add_development_dependency "rake", "~> 10.0"
spec.add_development_dependency "sqlite3"
spec.add_development_dependency "minitest"
end
27 changes: 27 additions & 0 deletions test/form_generator_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
require 'test_helper'
require 'rails/generators/form/form_generator'
describe Rails::Generators::FormGenerator do
let(:arguments) { ['test'] }
let(:content) { File.read("./tmp/generators/app/forms/#{arguments[0]}_form.rb") }

before do
Rails::Generators.invoke('form', arguments, destination_root: './tmp/generators')
end

after do
FileUtils.rm_rf './tmp/generators'
end

it 'generates form' do
content.must_match /^class TestForm < Reform::Form$/
end

describe 'when properties are present' do
let(:arguments) { ['second_test', 'username', 'password'] }

it 'generates form with properties' do
content.must_match /^ property :username$/
content.must_match /^ property :password$/
end
end
end
6 changes: 2 additions & 4 deletions test/rails-app/config/application.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
require File.expand_path('../boot', __FILE__)

require "active_model/railtie"
require "action_controller/railtie"
require "action_view/railtie"
require 'rails/all'

Bundler.require

Expand All @@ -17,4 +15,4 @@ class Application < Rails::Application
end
end

#require "reform/rails" # FIXME: this has to happen automatically in the rake test_rails run.
#require "reform/rails" # FIXME: this has to happen automatically in the rake test_rails run.
3 changes: 3 additions & 0 deletions test/rails-app/config/environments/test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@
config.action_controller.allow_forgery_protection = false
config.secret_key_base = "yo"

config.eager_load = 'false'
config.active_support.test_order = :random

# Tell Action Mailer not to deliver emails to the real world.
# The :test delivery method accumulates sent emails in the
# ActionMailer::Base.deliveries array.
Expand Down
Loading