Rubocop::Rails::OrderModelDeclarativeMethods
Sort declarative methods of Rails model, as an extension to RuboCop.
What's this?
Bad code
class User < ActiveRecord::Base
belongs_to :plan
validate :validate_name
after_create :after_create_1
has_many :messages
attr_readonly :email
after_create :after_create_2
belongs_to :role
before_create :set_name
endDeclarative methods are not sorted...
Run rubocop --auto-correct
class User < ActiveRecord::Base
belongs_to :plan
belongs_to :role
has_many :messages
validate :validate_name
before_create :set_name
after_create :after_create_1
after_create :after_create_2
attr_readonly :email
end- Group by
associations,callbacks, and others. - Sort by execution order if it's callback method.
Installation
Just install the rubocop-rails-order_model_declarative_methods gem.
gem install rubocop-rails-order_model_declarative_methodsor if you use bundler put this in your Gemfile.
gem 'rubocop-rails-order_model_declarative_methods'Usage
RuboCop configuration file
Put this into your .rubocop.yml.
require: rubocop-rails-order_model_declarative_methodsCommand line
rubocop --require rubocop-rails-order_model_declarative_methodsRake task
RuboCop::RakeTask.new do |task|
task.requires << 'rubocop-rails-order_model_declarative_methods'
endContributing
- Fork it
- Create your feature branch (
git checkout -b my-new-feature) - Commit your changes (
git commit -am 'Add some feature') - Push to the branch (
git push origin my-new-feature) - Create new Pull Request
Links
- Rails のモデル内のメソッドをソートするRubocop Extension を作った - pockestrap (Japanese Blog)
License
These codes are licensed under CC0.