Sha256: 8934dc23efdfc775f755ac7e2ecf1f7e723050e98fc4aefcebb4dff0946db29d

Contents?: true

Size: 1.92 KB

Versions: 14

Compression:

Stored size: 1.92 KB

Contents

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

describe RailsBestPractices::Checks::UseBeforeFilterCheck do
  before(:each) do
    @runner = RailsBestPractices::Core::Runner.new(RailsBestPractices::Checks::UseBeforeFilterCheck.new)
  end
  
  it "should use before_filter" do
    content = <<-EOF
    class PostsController < ApplicationController

      def show
        @post = current_user.posts.find(params[:id])
      end

      def edit
        @post = current_user.posts.find(params[:id])
      end

      def update
        @post = current_user.posts.find(params[:id])
        @post.update_attributes(params[:post])
      end

      def destroy
        @post = current_user.posts.find(params[:id])
        @post.destroy
      end

    end
    EOF
    @runner.check('app/controllers/posts_controller.rb', content)
    errors = @runner.errors
    errors.should_not be_empty
    errors[0].to_s.should == "app/controllers/posts_controller.rb:3,7,11,16 - use before_filter for show,edit,update,destroy"
  end
  
  it "should not use before_filter" do
    content = <<-EOF
    class PostsController < ApplicationController
      before_filter :find_post, :only => [:show, :edit, :update, :destroy]

      def update
        @post.update_attributes(params[:post])
      end

      def destroy
        @post.destroy
      end

      protected

      def find_post
        @post = current_user.posts.find(params[:id])
      end
    end
    EOF
    @runner.check('app/controllers/posts_controller.rb', content)
    errors = @runner.errors
    errors.should be_empty
  end

  it "should not use before_filter by nil" do
    content = <<-EOF
    class PostsController < ApplicationController

      def show
      end

      def edit
      end

      def update
      end

      def destroy
      end

    end
    EOF
    @runner.check('app/controllers/posts_controller.rb', content)
    errors = @runner.errors
    errors.should be_empty
  end
end

Version data entries

14 entries across 14 versions & 1 rubygems

Version Path
rails_best_practices-0.4.0 spec/rails_best_practices/checks/use_before_filter_check_spec.rb
rails_best_practices-0.3.27 spec/rails_best_practices/checks/use_before_filter_check_spec.rb
rails_best_practices-0.3.26 spec/rails_best_practices/checks/use_before_filter_check_spec.rb
rails_best_practices-0.3.25 spec/rails_best_practices/checks/use_before_filter_check_spec.rb
rails_best_practices-0.3.24 spec/rails_best_practices/checks/use_before_filter_check_spec.rb
rails_best_practices-0.3.23 spec/rails_best_practices/checks/use_before_filter_check_spec.rb
rails_best_practices-0.3.22 spec/rails_best_practices/checks/use_before_filter_check_spec.rb
rails_best_practices-0.3.21 spec/rails_best_practices/checks/use_before_filter_check_spec.rb
rails_best_practices-0.3.20 spec/rails_best_practices/checks/use_before_filter_check_spec.rb
rails_best_practices-0.3.19 spec/rails_best_practices/checks/use_before_filter_check_spec.rb
rails_best_practices-0.3.18 spec/rails_best_practices/checks/use_before_filter_check_spec.rb
rails_best_practices-0.3.17 spec/rails_best_practices/checks/use_before_filter_check_spec.rb
rails_best_practices-0.3.16 spec/rails_best_practices/checks/use_before_filter_check_spec.rb
rails_best_practices-0.3.15 spec/rails_best_practices/checks/use_before_filter_check_spec.rb