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

add generators #4

Merged
merged 2 commits into from Nov 8, 2020
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 10 additions & 0 deletions README.md
Expand Up @@ -29,6 +29,16 @@ Or install it yourself as:
1. Add permitted params inside the class
1. Use it as a normal model (without strong_params)

## Generator

Use the supplied generator to generate forms:

$ rails g active_form_model:form sign_up --model=user

or with namespace model

$ rails g active_form_model:form admin_post --model=blog/post

### Example

```ruby
Expand Down
8 changes: 8 additions & 0 deletions lib/generators/active_form_model/USAGE
@@ -0,0 +1,8 @@
Description:
Generates a form for a model with the given name.

Example:
rails generate active_form_model:form sign_up --model=user

This will create:
app/forms/user_form.rb
14 changes: 14 additions & 0 deletions lib/generators/active_form_model/form_generator.rb
@@ -0,0 +1,14 @@
# frozen_string_literal: true

module ActiveFormModel
module Generators
class FormGenerator < ::Rails::Generators::NamedBase
class_option :model, type: :string
source_root File.expand_path('templates', __dir__)

def create_form
template 'form.rb', File.join('app/forms', class_path, "#{file_name}_form.rb")
end
end
end
end
5 changes: 5 additions & 0 deletions lib/generators/active_form_model/templates/form.rb
@@ -0,0 +1,5 @@
<% module_namespacing do -%>
class <%= class_name %>Form < <%= options['model'].classify %>
include ActiveFormModel
end
<% end -%>