Sha256: 996564d097f0c75f6c60f2e08a26df4dfa8568ccdf51e1ca5733c3aabfb8cdd6

Contents?: true

Size: 1.91 KB

Versions: 1

Compression:

Stored size: 1.91 KB

Contents

require 'spec_helper'

module RailsBestPractices
  module Reviews
    describe MoveModelLogicIntoModelReview do
      let(:runner) { Core::Runner.new(:reviews => MoveModelLogicIntoModelReview.new) }

      it "should move model logic into model" do
        content = <<-EOF
        class PostsController < ApplicationController
          def publish
            @post = Post.find(params[:id])
            @post.update_attributes(:is_published, true)
            @post.approved_by = current_user
            if @post.created_at > Time.now - 7.days
              @post.popular = 100
            else
              @post.popular = 0
            end

            redirect_to post_url(@post)
          end
        end
        EOF
        runner.review('app/controllers/posts_controller.rb', content)
        runner.should have(1).errors
        runner.errors[0].to_s.should == "app/controllers/posts_controller.rb:3 - move model logic into model (@post use_count > 4)"
      end

      it "should not move model logic into model with simple model calling" do
        content = <<-EOF
        class PostsController < ApplicationController
          def publish
            @post = Post.find(params[:id])
            @post.update_attributes(:is_published, true)
            @post.approved_by = current_user

            redirect_to post_url(@post)
          end
        end
        EOF
        runner.review('app/controllers/posts_controller.rb', content)
        runner.should have(0).errors
      end

      it "should not move model logic into model with self calling" do
        content = <<-EOF
        class PostsController < ApplicationController
          def publish
            self.step1
            self.step2
            self.step3
            self.step4
            self.step5
          end
        end
        EOF
        runner.review('app/controllers/posts_controller.rb', content)
        runner.should have(0).errors
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
rails_best_practices-1.9.1 spec/rails_best_practices/reviews/move_model_logic_into_model_review_spec.rb