pattern_generator icon indicating copy to clipboard operation
pattern_generator copied to clipboard

The missing Rails generator for patterns

Pattern Generator Build Status

Pattern Generator lets you generate various Rails patterns and tests with a single command.

Install

In your Gemfile, add the gem in the development group.

gem 'pattern_generator', group: :development

Run bundle install to install the gem.

Now you are ready to rock the pattern generator!

Usage

rails generate [PATTERN_TYPE] [YOUR_FILE_NAME (in snake_case)]

Currently, PATTERN_TYPE can be:

  • Service
  • Policy
  • PORO (Plain Old Ruby Object)
  • Form

Destroy command is also supported.

Examples

Here are usage examples with commands and generated files.

Service

rails generate service find_match

generates:

app/services/find_match_service.rb

class FindMatchService
  def initialize

  end
end

spec/services/find_match_service_spec.rb

require 'rails_helper'

RSpec.describe FindMatchService, type: :service do
  describe '#call' do
    pending "Add some tests to #{__FILE__}"
  end
end

Policy

rails generate policy voting

generates:

app/policies/voting_policy.rb

class VotingPolicy
  def initialize

  end
end

spec/policies/voting_policy_spec.rb

require 'rails_helper'

RSpec.describe VotingPolicy, type: :policy do
  describe '#policy_method' do
    pending "Add some tests to #{__FILE__}"
  end
end

PORO (Plain Old Ruby Object)

a minimalistic non-ActiveRecord model that can be customized to fit your needs

rails generate poro payment_gateway

generates:

app/models/payment_gateway.rb

class PaymentGateway
  def initialize

  end
end

spec/policies/payment_gateway_spec.rb

require 'rails_helper'

RSpec.describe PaymentGateway do
  describe 'something' do
    pending "Add some tests to #{__FILE__}"
  end
end

Options

  • --minitest

    • Will create your test files inside test folder.
  • DEFAULT: --rspec

    • Will create your test files inside spec folder.
  • Usage: rails generate service subscribe_user --minitest

Where can I learn about more patterns?

Feel free to add good blog articles or tutorials here.

Contributing

Know your patterns? Open a pull request!

This project respects the contributor code of conduct.

License

This project rocks and uses MIT-LICENSE.