Class: RuboCop::Cop::Rails::OrderModelDeclarativeMethods
- Inherits:
-
RuboCop::Cop
- Object
- RuboCop::Cop
- RuboCop::Cop::Rails::OrderModelDeclarativeMethods
- Defined in:
- lib/rubocop/cop/rails/order_model_declarative_methods.rb
Constant Summary collapse
- MSG =
"not sorted"- Associations =
%i[ belongs_to has_many has_one has_and_belongs_to_many ].freeze
- Callbacks =
%i[ after_initialize after_find after_touch before_validation validates validate after_validation before_save around_save before_create around_create before_update around_update before_destroy around_destroy after_destroy after_update after_create after_save after_commit after_rollback ].freeze
- Others =
%i[ attr_readonly serialize ]
Instance Method Summary collapse
Instance Method Details
#autocorrect(body) ⇒ Object
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 |
# File 'lib/rubocop/cop/rails/order_model_declarative_methods.rb', line 57 def autocorrect(body) targets = target_methods(body) sorteds = sort_callbacks(targets) sorteds.push(nil) lambda do |corrector| sorteds.each_cons(2).with_index do |(sorted, next_sorted), idx| target = targets[idx] expr = target.loc.expression corrector.replace( expr, sorted.loc.expression.source.to_s ) lnum = expr.last_line loop do l = processed_source.lines[lnum] break if l !~ /\A[[:space:]]*\z/ # blank range = source_range(expr.source_buffer, lnum+1, 0, l.size+1) corrector.remove(range) lnum += 1 end if next_sorted corrector.insert_after(expr, "\n") if method_type(sorted) != method_type(next_sorted) else corrector.insert_after(expr, "\n") end end end end |
#on_class(node) ⇒ Object
46 47 48 49 50 51 52 53 54 55 |
# File 'lib/rubocop/cop/rails/order_model_declarative_methods.rb', line 46 def on_class(node) _name, _superclass, body = *node return unless body return unless body.begin_type? targets = target_methods(body) return if targets == sort_callbacks(targets) add_offense(body, :expression) end |