Sha256: 43879c1ab77ba9e053c3fdcb06f7ff9f3a902e3ae282947a525e131ea0f36913

Contents?: true

Size: 1.26 KB

Versions: 2

Compression:

Stored size: 1.26 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
        /models\/.*rb/
      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 << eval(argument.to_ruby).to_s
        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

2 entries across 2 versions & 1 rubygems

Version Path
rails_best_practices-0.2.8 lib/rails_best_practices/checks/use_observer_check.rb
rails_best_practices-0.2.6 lib/rails_best_practices/checks/use_observer_check.rb