Sha256: 4b4020e2d75cabfcf6c1787e917e700c8e33b33585e210b4ad87f252731f0066

Contents?: true

Size: 1.88 KB

Versions: 15

Compression:

Stored size: 1.88 KB

Contents

# encoding: utf-8

module RuboCop
  module Cop
    module Rails
      # This cop enforces the consistent use of action filters methods.
      #
      # The cop is configurable and the enforce the use of older
      # something_filter methods or the newer something_action methods.
      class ActionFilter < Cop
        include ConfigurableEnforcedStyle

        MSG = 'Prefer `%s` over `%s`.'

        FILTER_METHODS = [:before_filter, :skip_before_filter,
                          :after_filter, :around_filter]

        ACTION_METHODS = [:before_action, :skip_before_action,
                          :after_action, :around_action]

        def on_block(node)
          method, _args, _body = *node

          check_method_node(method)
        end

        def on_send(node)
          receiver, _method_name, *_args = *node

          check_method_node(node) if receiver.nil?
        end

        def autocorrect(node)
          @corrections << lambda do |corrector|
            corrector.replace(node.loc.selector,
                              preferred_method(node.loc.selector.source).to_s)
          end
        end

        private

        def check_method_node(node)
          _receiver, method_name, *_args = *node
          return unless offending_method?(method_name)

          add_offense(node, :selector,
                      format(MSG,
                             preferred_method(method_name),
                             method_name)
                     )
        end

        def offending_method?(method_name)
          bad_methods.include?(method_name)
        end

        def bad_methods
          style == :action ? FILTER_METHODS : ACTION_METHODS
        end

        def good_methods
          style == :action ? ACTION_METHODS : FILTER_METHODS
        end

        def preferred_method(method)
          good_methods[bad_methods.index(method.to_sym)]
        end
      end
    end
  end
end

Version data entries

15 entries across 15 versions & 2 rubygems

Version Path
rubyjobbuilderdsl-0.0.2 vendor/bundle/ruby/2.1.0/gems/rubocop-0.26.0/lib/rubocop/cop/rails/action_filter.rb
rubyjobbuilderdsl-0.0.1 vendor/bundle/ruby/2.1.0/gems/rubocop-0.26.0/lib/rubocop/cop/rails/action_filter.rb
rubocop-0.30.1 lib/rubocop/cop/rails/action_filter.rb
rubocop-0.30.0 lib/rubocop/cop/rails/action_filter.rb
rubocop-0.29.1 lib/rubocop/cop/rails/action_filter.rb
rubocop-0.29.0 lib/rubocop/cop/rails/action_filter.rb
rubocop-0.28.0 lib/rubocop/cop/rails/action_filter.rb
rubocop-0.27.1 lib/rubocop/cop/rails/action_filter.rb
rubocop-0.27.0 lib/rubocop/cop/rails/action_filter.rb
rubocop-0.26.1 lib/rubocop/cop/rails/action_filter.rb
rubocop-0.26.0 lib/rubocop/cop/rails/action_filter.rb
rubocop-0.25.0 lib/rubocop/cop/rails/action_filter.rb
rubocop-0.24.1 lib/rubocop/cop/rails/action_filter.rb
rubocop-0.24.0 lib/rubocop/cop/rails/action_filter.rb
rubocop-0.23.0 lib/rubocop/cop/rails/action_filter.rb