2014-01-11 2 views
1

나는 CodeClimate.com에 내 코드를 분석 그리고 난 같은 라인에서 항상 비슷한 코드에 문제가 :CodeClimate와 비슷한 코드를 강력한 매개 변수에서 제거하려면 어떻게해야합니까?

params.permit(some parameters here) 
CodeClimate 코드의 질량,하지에 오직 기반 유사한 코드를 감지하는

내용 때문에 내 품질이 향상되지 않습니다.

매개 변수 해시 인 한이 코드가 반복되거나 유사하지 않다는 것을 CodeClimate에 알리는 방법이 있습니까?

+0

다음과 같이 보이지 않습니다. https://twitter.com/codeclimate/status/422124396988624896 – phoet

답변

1

왜 그런 문제를 리팩토링하지 않습니까? 일부 컨트롤러는 PARAMS 권한의 서로 다른 규칙이있는 경우, 물론,

class ApplicationController < ActionControllerBase 
    # ... 

    def resource_params 
    params.require(resource_name).permit(*permitted_resource_params) 
    end 
end 

class PostsController < ApplicationController 
    # ... 

    def resource_name 
    :post 
    end 

    def permitted_resource_params 
    [:title, :body] 
    end 
end 

class CommentsController < ApplicationController 
    # ... 

    def resource_name 
    :comment 
    end 

    def permitted_resource_params 
    [:name, :email, :body] 
    end 
end 

: 그리고 당신은 슈퍼 클래스에 코드를 반복 이동할 수 있습니다

class PostsController < ApplicationController 
    # ... 

    def resource_params 
    params.require(:post).permit(:title, :body) 
    end 
end 

class CommentsController < ApplicationController 
    # ... 

    def resource_params 
    params.require(:comment).permit(:name, :email, :body) 
    end 
end 

, 서브 클래스에서 떠나에만 차이 : 말, 당신은 코드가 - resource_params 메서드를 다시 정의 할 수 있습니다.

+3

등급 도구에서 문제를 피하기 위해 레일스 규칙을 변경하는 것이 해결책 일 수 있다고 생각하지 않습니다. 그러나 나는 이런 종류의 유사한 질량 문제에 대한이 해결책을 정말로 좋아합니다. 감사. – tebayoso

0

메서드의 이름을 구분하지 않는 한 이러한 종류의 코딩 문제는 피할 수 없습니다.

1

MikiDiet이 제안했듯이 리팩터링을하는 것이 가장 좋습니다. 그러나이 제안으로는 문제가 완전히 해결되지는 않습니다. 내 경험에서 다음 CodeClimate 코드가 냄새보고합니다

def resource_name 
:post 
end 

def permitted_resource_params 
    [:title, :body] 
end  

을 컨트롤러에서 코드 중복으로.

관련 문제