Sha256: 5b64cefad34c274074adeb75c07c44dc1a2266b047f7100455f26cf18d950a9e

Contents?: true

Size: 1.25 KB

Versions: 33

Compression:

Stored size: 1.25 KB

Contents

require 'rails_best_practices/checks/check'

module RailsBestPractices
  module Checks
    # Check a model file to make sure mail deliver method is in observer not callback.
    #
    # Implementation: 
    # Record :after_create callback
    # Check method define, if it is a callback and call deliver_xxx message in method body, then it should use observer.
    class UseObserverCheck < Check

      def interesting_nodes
        [:defn, :call]
      end

      def interesting_files
        MODLE_FILES
      end

      def initialize
        super
        @callbacks = []
      end

      def evaluate_start(node)
        if :after_create == node.message
          remember_callbacks(node)
        elsif :defn == node.node_type and @callbacks.include?(node.message_name.to_s)
          add_error "use observer" if use_observer?(node)
        end
      end

      private

      def remember_callbacks(node)
        node.arguments[1..-1].each do |argument|
          @callbacks << argument.to_ruby_string
        end
      end

      def use_observer?(node)
        node.recursive_children do |child|
          return true if :call == child.node_type and :const == child.subject.node_type and child.message.to_s =~ /^deliver_/
        end
        false
      end
    end
  end
end

Version data entries

33 entries across 33 versions & 1 rubygems

Version Path
rails_best_practices-0.4.1 lib/rails_best_practices/checks/use_observer_check.rb
rails_best_practices-0.4.0 lib/rails_best_practices/checks/use_observer_check.rb
rails_best_practices-0.3.27 lib/rails_best_practices/checks/use_observer_check.rb
rails_best_practices-0.3.26 lib/rails_best_practices/checks/use_observer_check.rb
rails_best_practices-0.3.25 lib/rails_best_practices/checks/use_observer_check.rb
rails_best_practices-0.3.24 lib/rails_best_practices/checks/use_observer_check.rb
rails_best_practices-0.3.23 lib/rails_best_practices/checks/use_observer_check.rb
rails_best_practices-0.3.22 lib/rails_best_practices/checks/use_observer_check.rb
rails_best_practices-0.3.21 lib/rails_best_practices/checks/use_observer_check.rb
rails_best_practices-0.3.20 lib/rails_best_practices/checks/use_observer_check.rb
rails_best_practices-0.3.19 lib/rails_best_practices/checks/use_observer_check.rb
rails_best_practices-0.3.18 lib/rails_best_practices/checks/use_observer_check.rb
rails_best_practices-0.3.17 lib/rails_best_practices/checks/use_observer_check.rb
rails_best_practices-0.3.16 lib/rails_best_practices/checks/use_observer_check.rb
rails_best_practices-0.3.15 lib/rails_best_practices/checks/use_observer_check.rb
rails_best_practices-0.3.14 lib/rails_best_practices/checks/use_observer_check.rb
rails_best_practices-0.3.13 lib/rails_best_practices/checks/use_observer_check.rb
rails_best_practices-0.3.12 lib/rails_best_practices/checks/use_observer_check.rb
rails_best_practices-0.3.11 lib/rails_best_practices/checks/use_observer_check.rb
rails_best_practices-0.3.10 lib/rails_best_practices/checks/use_observer_check.rb