title: Rails 5 - How to create custom scaffold generator?
body: <p>The goal is for command...</p>
  
  <p><code>bin/rails generate custom_scaffold Thing</code></p>
  
  <p>... to generate the following 6 files:</p>
  
  <pre><code>db/migrate/201812031331_create_things.rb
  app/models/thing.rb
  app/controllers/things_controller.rb
  app/serializers/thing_serializer.rb
  test/fixtures/things.yml
  test/integration/requests/things_request_test.rb
  </code></pre>
  
  <p>... using Rails 5.</p>
  
  <p>My current setup does generate <code>app/models/thing.rb</code> but does not populate it with <code>Thing</code>.</p>
  
  <p>Expected:</p>
  
  <pre class="lang-rb prettyprint-override"><code>class Thing &lt; ApplicationRecord
  
  end
  </code></pre>
  
  <p>Currently:</p>
  
  <pre class="lang-rb prettyprint-override"><code>class &lt;%= class_name %&gt; &lt; ApplicationRecord
  
  end
  </code></pre>
  
  <p>I have read through these <a href="https://guides.rubyonrails.org/generators.html" rel="nofollow noreferrer">Rails guides</a> but to little avail.</p>
  
  <p>Does anyone have a working example?</p>
  
  <hr>
  
  <p>My setup:</p>
  
  <pre class="lang-rb prettyprint-override"><code># lib/generators/custom_scaffold/custom_scaffold_generator.rb
  
  class CustomScaffoldGenerator &lt; Rails::Generators::NamedBase
    source_root File.expand_path('templates', __dir__)
  
    def create_files
      copy_file 'migration.rb', "db/migrate/#{timestamp}_create_#{plural_name}.rb"
      copy_file 'model.rb', "app/models/#{file_name}.rb"
      copy_file 'controller.rb', "app/controllers/#{plural_name}_controller.rb"
      copy_file 'serializer.rb', "app/serializers/#{file_name}_serializer.rb"
      copy_file 'fixture.yml', "test/fixtures/#{plural_name}.yml"
      copy_file 'request_test.rb', "test/integration/requests/#{plural_name}_request_test.rb"
    end
  
    private
  
    def timestamp
      Time.now.utc.strftime('%Y%m%d%H%M%S')
    end
  end
  </code></pre>
  
  <pre class="lang-rb prettyprint-override"><code># lib/generators/custom_scaffold/templates/model.rb
  
  class &lt;%= class_name %&gt; &lt; ApplicationRecord
  
  end
  </code></pre>
  
  <pre class="lang-rb prettyprint-override"><code># lib/generators/custom_scaffold/templates/controller.rb
  
  module V1
    module Public
      class &lt;%= class_name.pluralize %&gt;Controller &lt; ApplicationController
  
      end
    end
  end
  </code></pre>
  
  <pre class="lang-rb prettyprint-override"><code># lib/generators/custom_scaffold/templates/migration.rb
  # Ignore for now
  </code></pre>
  
  <pre class="lang-rb prettyprint-override"><code># lib/generators/custom_scaffold/templates/serializer.rb
  # Ignore for now
  </code></pre>
  
  <pre class="lang-rb prettyprint-override"><code># lib/generators/custom_scaffold/templates/fixture.yml
  # Ignore for now
  </code></pre>
  
  <pre class="lang-rb prettyprint-override"><code># lib/generators/custom_scaffold/templates/request_test.rb
  # Ignore for now
  </code></pre>
  
  <pre class="lang-rb prettyprint-override"><code># Gemfile
  
  source 'https://rubygems.org'
  
  ruby '2.4.1'
  gem 'rails', '~&gt; 5.1.6'
  gem 'puma', '~&gt; 3.7'
  gem 'pg'
  gem 'rack-cors', require: 'rack/cors'
  gem 'olive_branch'
  gem 'fast_jsonapi'
  gem 'awesome_print'
  gem 'byebug', '~&gt; 10.0', groups: %i[development test]
  gem 'yaml_db'
  
  group :development do
    gem 'listen', '&gt;= 3.0.5', '&lt; 3.2'
    gem 'mina', '~&gt; 1.2', require: false
    gem 'mina-puma', require: false
    gem 'rubocop', require: false
    gem 'annotate', require: false
  end
  </code></pre>