Sha256: 628d7b326743ae96bcee6a5a3550e237e35edb9f8265937c85f372d45fd56bb8

Contents?: true

Size: 1.71 KB

Versions: 39

Compression:

Stored size: 1.71 KB

Contents

require File.join(File.dirname(__FILE__) + '/../../spec_helper')

describe RailsBestPractices::Checks::UseObserverCheck do
  before(:each) do
    @runner = RailsBestPractices::Core::Runner.new(RailsBestPractices::Checks::UseObserverCheck.new)
  end

  it "should use observer" do
    content =<<-EOF
    class Project < ActiveRecord::Base
      after_create :send_create_notification

      private

      def send_create_notification
        self.members.each do |member|
          ProjectMailer.deliver_notification(self, member)
        end
      end
    end
    EOF
    @runner.check('app/models/project.rb', content)
    errors = @runner.errors
    errors.should_not be_empty
    errors[0].to_s.should == "app/models/project.rb:6 - use observer"
  end

  it "should not use observer without callback" do
    content =<<-EOF
    class Project < ActiveRecord::Base
      private

      def send_create_notification
        self.members.each do |member|
          ProjectMailer.deliver_notification(self, member)
        end
      end
    end
    EOF
    @runner.check('app/models/project.rb', content)
    errors = @runner.errors
    errors.should be_empty
  end

  it "should use observer with two after_create" do
    content =<<-EOF
    class Project < ActiveRecord::Base
      after_create :send_create_notification, :update_author

      private

      def send_create_notification
        self.members.each do |member|
          ProjectMailer.deliver_notification(self, member)
        end
      end

      def update_author
      end
    end
    EOF
    @runner.check('app/models/project.rb', content)
    errors = @runner.errors
    errors.should_not be_empty
    errors[0].to_s.should == "app/models/project.rb:6 - use observer"
  end
end

Version data entries

39 entries across 39 versions & 1 rubygems

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