Skip to content

Commit

Permalink
Merge pull request #4 from NikitaNaumenko/add_generators
Browse files Browse the repository at this point in the history
add generators
  • Loading branch information
mokevnin committed Nov 8, 2020
2 parents 5e338f7 + b1c1b1d commit 6998d0e
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 0 deletions.
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 -%>

0 comments on commit 6998d0e

Please sign in to comment.