ArtOfCode-/qpixel

View on GitHub
app/controllers/posts_controller.rb

Summary

Maintainability
F
4 days
Test Coverage

File posts_controller.rb has 445 lines of code (exceeds 250 allowed). Consider refactoring.
Open

class PostsController < ApplicationController
  before_action :authenticate_user!, except: [:document, :help_center, :show]
  before_action :set_post, only: [:toggle_comments, :feature, :lock, :unlock]
  before_action :set_scoped_post, only: [:change_category, :show, :edit, :update, :close, :reopen, :delete, :restore]
  before_action :verify_moderator, only: [:toggle_comments]
Severity: Minor
Found in app/controllers/posts_controller.rb - About 6 hrs to fix

    Method update has a Cognitive Complexity of 43 (exceeds 5 allowed). Consider refactoring.
    Open

      def update
        before = { body: @post.body_markdown, title: @post.title, tags: @post.tags }
        after_tags = if @post_type.has_category?
                       Tag.where(tag_set_id: @post.category.tag_set_id, name: params[:post][:tags_cache])
                     end
    Severity: Minor
    Found in app/controllers/posts_controller.rb - About 6 hrs to fix

    Cognitive Complexity

    Cognitive Complexity is a measure of how difficult a unit of code is to intuitively understand. Unlike Cyclomatic Complexity, which determines how difficult your code will be to test, Cognitive Complexity tells you how difficult your code will be to read and comprehend.

    A method's cognitive complexity is based on a few simple rules:

    • Code is not considered more complex when it uses shorthand that the language provides for collapsing multiple statements into one
    • Code is considered more complex for each "break in the linear flow of the code"
    • Code is considered more complex when "flow breaking structures are nested"

    Further reading

    Class PostsController has 27 methods (exceeds 20 allowed). Consider refactoring.
    Open

    class PostsController < ApplicationController
      before_action :authenticate_user!, except: [:document, :help_center, :show]
      before_action :set_post, only: [:toggle_comments, :feature, :lock, :unlock]
      before_action :set_scoped_post, only: [:change_category, :show, :edit, :update, :close, :reopen, :delete, :restore]
      before_action :verify_moderator, only: [:toggle_comments]
    Severity: Minor
    Found in app/controllers/posts_controller.rb - About 3 hrs to fix

      Method create has a Cognitive Complexity of 21 (exceeds 5 allowed). Consider refactoring.
      Open

        def create
          @post_type = PostType.find(params[:post][:post_type_id])
          @parent = Post.where(id: params[:parent]).first
          @category = if @post_type.has_category
                        if params[:post][:category_id].present?
      Severity: Minor
      Found in app/controllers/posts_controller.rb - About 2 hrs to fix

      Cognitive Complexity

      Cognitive Complexity is a measure of how difficult a unit of code is to intuitively understand. Unlike Cyclomatic Complexity, which determines how difficult your code will be to test, Cognitive Complexity tells you how difficult your code will be to read and comprehend.

      A method's cognitive complexity is based on a few simple rules:

      • Code is not considered more complex when it uses shorthand that the language provides for collapsing multiple statements into one
      • Code is considered more complex for each "break in the linear flow of the code"
      • Code is considered more complex when "flow breaking structures are nested"

      Further reading

      Method update has 57 lines of code (exceeds 25 allowed). Consider refactoring.
      Open

        def update
          before = { body: @post.body_markdown, title: @post.title, tags: @post.tags }
          after_tags = if @post_type.has_category?
                         Tag.where(tag_set_id: @post.category.tag_set_id, name: params[:post][:tags_cache])
                       end
      Severity: Major
      Found in app/controllers/posts_controller.rb - About 2 hrs to fix

        Method create has 53 lines of code (exceeds 25 allowed). Consider refactoring.
        Open

          def create
            @post_type = PostType.find(params[:post][:post_type_id])
            @parent = Post.where(id: params[:parent]).first
            @category = if @post_type.has_category
                          if params[:post][:category_id].present?
        Severity: Major
        Found in app/controllers/posts_controller.rb - About 2 hrs to fix

          Method close has 32 lines of code (exceeds 25 allowed). Consider refactoring.
          Open

            def close
              unless check_your_privilege('flag_close', nil, false)
                render json: { status: 'failed', message: helpers.ability_err_msg(:flag_close, 'close this post') },
                       status: :forbidden
                return
          Severity: Minor
          Found in app/controllers/posts_controller.rb - About 1 hr to fix

            Method close has a Cognitive Complexity of 11 (exceeds 5 allowed). Consider refactoring.
            Open

              def close
                unless check_your_privilege('flag_close', nil, false)
                  render json: { status: 'failed', message: helpers.ability_err_msg(:flag_close, 'close this post') },
                         status: :forbidden
                  return
            Severity: Minor
            Found in app/controllers/posts_controller.rb - About 1 hr to fix

            Cognitive Complexity

            Cognitive Complexity is a measure of how difficult a unit of code is to intuitively understand. Unlike Cyclomatic Complexity, which determines how difficult your code will be to test, Cognitive Complexity tells you how difficult your code will be to read and comprehend.

            A method's cognitive complexity is based on a few simple rules:

            • Code is not considered more complex when it uses shorthand that the language provides for collapsing multiple statements into one
            • Code is considered more complex for each "break in the linear flow of the code"
            • Code is considered more complex when "flow breaking structures are nested"

            Further reading

            Method lock has a Cognitive Complexity of 11 (exceeds 5 allowed). Consider refactoring.
            Open

              def lock
                return not_found unless current_user.privilege? 'flag_curate'
                return not_found if @post.locked?
            
                length = params[:length].present? ? params[:length].to_i : nil
            Severity: Minor
            Found in app/controllers/posts_controller.rb - About 1 hr to fix

            Cognitive Complexity

            Cognitive Complexity is a measure of how difficult a unit of code is to intuitively understand. Unlike Cyclomatic Complexity, which determines how difficult your code will be to test, Cognitive Complexity tells you how difficult your code will be to read and comprehend.

            A method's cognitive complexity is based on a few simple rules:

            • Code is not considered more complex when it uses shorthand that the language provides for collapsing multiple statements into one
            • Code is considered more complex for each "break in the linear flow of the code"
            • Code is considered more complex when "flow breaking structures are nested"

            Further reading

            Method restore has 31 lines of code (exceeds 25 allowed). Consider refactoring.
            Open

              def restore
                unless check_your_privilege('flag_curate', @post, false)
                  flash[:danger] = helpers.ability_err_msg(:flag_curate, 'restore this post')
                  redirect_to post_path(@post)
                  return
            Severity: Minor
            Found in app/controllers/posts_controller.rb - About 1 hr to fix

              Method delete has 31 lines of code (exceeds 25 allowed). Consider refactoring.
              Open

                def delete
                  unless check_your_privilege('flag_curate', @post, false)
                    flash[:danger] = helpers.ability_err_msg(:flag_curate, 'delete this post')
                    redirect_to post_path(@post)
                    return
              Severity: Minor
              Found in app/controllers/posts_controller.rb - About 1 hr to fix

                Method delete has a Cognitive Complexity of 8 (exceeds 5 allowed). Consider refactoring.
                Open

                  def delete
                    unless check_your_privilege('flag_curate', @post, false)
                      flash[:danger] = helpers.ability_err_msg(:flag_curate, 'delete this post')
                      redirect_to post_path(@post)
                      return
                Severity: Minor
                Found in app/controllers/posts_controller.rb - About 45 mins to fix

                Cognitive Complexity

                Cognitive Complexity is a measure of how difficult a unit of code is to intuitively understand. Unlike Cyclomatic Complexity, which determines how difficult your code will be to test, Cognitive Complexity tells you how difficult your code will be to read and comprehend.

                A method's cognitive complexity is based on a few simple rules:

                • Code is not considered more complex when it uses shorthand that the language provides for collapsing multiple statements into one
                • Code is considered more complex for each "break in the linear flow of the code"
                • Code is considered more complex when "flow breaking structures are nested"

                Further reading

                Method restore has a Cognitive Complexity of 8 (exceeds 5 allowed). Consider refactoring.
                Open

                  def restore
                    unless check_your_privilege('flag_curate', @post, false)
                      flash[:danger] = helpers.ability_err_msg(:flag_curate, 'restore this post')
                      redirect_to post_path(@post)
                      return
                Severity: Minor
                Found in app/controllers/posts_controller.rb - About 45 mins to fix

                Cognitive Complexity

                Cognitive Complexity is a measure of how difficult a unit of code is to intuitively understand. Unlike Cyclomatic Complexity, which determines how difficult your code will be to test, Cognitive Complexity tells you how difficult your code will be to read and comprehend.

                A method's cognitive complexity is based on a few simple rules:

                • Code is not considered more complex when it uses shorthand that the language provides for collapsing multiple statements into one
                • Code is considered more complex for each "break in the linear flow of the code"
                • Code is considered more complex when "flow breaking structures are nested"

                Further reading

                Method new has a Cognitive Complexity of 7 (exceeds 5 allowed). Consider refactoring.
                Open

                  def new
                    @post_type = PostType.find(params[:post_type])
                    @category = params[:category].present? ? Category.find(params[:category]) : nil
                    @parent = Post.where(id: params[:parent]).first
                    @post = Post.new(category: @category, post_type: @post_type, parent: @parent)
                Severity: Minor
                Found in app/controllers/posts_controller.rb - About 35 mins to fix

                Cognitive Complexity

                Cognitive Complexity is a measure of how difficult a unit of code is to intuitively understand. Unlike Cyclomatic Complexity, which determines how difficult your code will be to test, Cognitive Complexity tells you how difficult your code will be to read and comprehend.

                A method's cognitive complexity is based on a few simple rules:

                • Code is not considered more complex when it uses shorthand that the language provides for collapsing multiple statements into one
                • Code is considered more complex for each "break in the linear flow of the code"
                • Code is considered more complex when "flow breaking structures are nested"

                Further reading

                Method change_category has a Cognitive Complexity of 7 (exceeds 5 allowed). Consider refactoring.
                Open

                  def change_category
                    @target = Category.find params[:target_id]
                    unless helpers.can_change_category(current_user, @target)
                      render json: { success: false, errors: ["You don't have permission to make that change."] }, status: :forbidden
                      return
                Severity: Minor
                Found in app/controllers/posts_controller.rb - About 35 mins to fix

                Cognitive Complexity

                Cognitive Complexity is a measure of how difficult a unit of code is to intuitively understand. Unlike Cyclomatic Complexity, which determines how difficult your code will be to test, Cognitive Complexity tells you how difficult your code will be to read and comprehend.

                A method's cognitive complexity is based on a few simple rules:

                • Code is not considered more complex when it uses shorthand that the language provides for collapsing multiple statements into one
                • Code is considered more complex for each "break in the linear flow of the code"
                • Code is considered more complex when "flow breaking structures are nested"

                Further reading

                Avoid too many return statements within this method.
                Open

                      return
                Severity: Major
                Found in app/controllers/posts_controller.rb - About 30 mins to fix

                  Method reopen has a Cognitive Complexity of 6 (exceeds 5 allowed). Consider refactoring.
                  Open

                    def reopen
                      unless check_your_privilege('flag_close', nil, false)
                        flash[:danger] = helpers.ability_err_msg(:flag_close, 'reopen this post')
                        redirect_to post_path(@post)
                        return
                  Severity: Minor
                  Found in app/controllers/posts_controller.rb - About 25 mins to fix

                  Cognitive Complexity

                  Cognitive Complexity is a measure of how difficult a unit of code is to intuitively understand. Unlike Cyclomatic Complexity, which determines how difficult your code will be to test, Cognitive Complexity tells you how difficult your code will be to read and comprehend.

                  A method's cognitive complexity is based on a few simple rules:

                  • Code is not considered more complex when it uses shorthand that the language provides for collapsing multiple statements into one
                  • Code is considered more complex for each "break in the linear flow of the code"
                  • Code is considered more complex when "flow breaking structures are nested"

                  Further reading

                  Method unlock has a Cognitive Complexity of 6 (exceeds 5 allowed). Consider refactoring.
                  Open

                    def unlock
                      return not_found(errors: ['no_privilege']) unless current_user.privilege? 'flag_curate'
                      return not_found(errors: ['not_locked']) unless @post.locked?
                      if @post.locked_by.is_moderator && !current_user.is_moderator
                        return not_found(errors: ['locked_by_mod'])
                  Severity: Minor
                  Found in app/controllers/posts_controller.rb - About 25 mins to fix

                  Cognitive Complexity

                  Cognitive Complexity is a measure of how difficult a unit of code is to intuitively understand. Unlike Cyclomatic Complexity, which determines how difficult your code will be to test, Cognitive Complexity tells you how difficult your code will be to read and comprehend.

                  A method's cognitive complexity is based on a few simple rules:

                  • Code is not considered more complex when it uses shorthand that the language provides for collapsing multiple statements into one
                  • Code is considered more complex for each "break in the linear flow of the code"
                  • Code is considered more complex when "flow breaking structures are nested"

                  Further reading

                  Identical blocks of code found in 2 locations. Consider refactoring.
                  Open

                      if @post_type.has_category? && @category.nil? && @parent.nil?
                        flash[:danger] = helpers.i18ns('posts.type_requires_category', type: @post_type.name)
                        redirect_back fallback_location: root_path
                        return
                      end
                  Severity: Minor
                  Found in app/controllers/posts_controller.rb and 1 other location - About 15 mins to fix
                  app/controllers/posts_controller.rb on lines 53..57

                  Duplicated Code

                  Duplicated code can lead to software that is hard to understand and difficult to change. The Don't Repeat Yourself (DRY) principle states:

                  Every piece of knowledge must have a single, unambiguous, authoritative representation within a system.

                  When you violate DRY, bugs and maintenance problems are sure to follow. Duplicated code has a tendency to both continue to replicate and also to diverge (leaving bugs as two similar implementations differ in subtle ways).

                  Tuning

                  This issue has a mass of 25.

                  We set useful threshold defaults for the languages we support but you may want to adjust these settings based on your project guidelines.

                  The threshold configuration represents the minimum mass a code block must have to be analyzed for duplication. The lower the threshold, the more fine-grained the comparison.

                  If the engine is too easily reporting duplication, try raising the threshold. If you suspect that the engine isn't catching enough duplication, try lowering the threshold. The best setting tends to differ from language to language.

                  See codeclimate-duplication's documentation for more information about tuning the mass threshold in your .codeclimate.yml.

                  Refactorings

                  Further Reading

                  Identical blocks of code found in 2 locations. Consider refactoring.
                  Open

                      if @post_type.has_category? && @category.nil? && @parent.nil?
                        flash[:danger] = helpers.i18ns('posts.type_requires_category', type: @post_type.name)
                        redirect_back fallback_location: root_path
                        return
                      end
                  Severity: Minor
                  Found in app/controllers/posts_controller.rb and 1 other location - About 15 mins to fix
                  app/controllers/posts_controller.rb on lines 22..26

                  Duplicated Code

                  Duplicated code can lead to software that is hard to understand and difficult to change. The Don't Repeat Yourself (DRY) principle states:

                  Every piece of knowledge must have a single, unambiguous, authoritative representation within a system.

                  When you violate DRY, bugs and maintenance problems are sure to follow. Duplicated code has a tendency to both continue to replicate and also to diverge (leaving bugs as two similar implementations differ in subtle ways).

                  Tuning

                  This issue has a mass of 25.

                  We set useful threshold defaults for the languages we support but you may want to adjust these settings based on your project guidelines.

                  The threshold configuration represents the minimum mass a code block must have to be analyzed for duplication. The lower the threshold, the more fine-grained the comparison.

                  If the engine is too easily reporting duplication, try raising the threshold. If you suspect that the engine isn't catching enough duplication, try lowering the threshold. The best setting tends to differ from language to language.

                  See codeclimate-duplication's documentation for more information about tuning the mass threshold in your .codeclimate.yml.

                  Refactorings

                  Further Reading

                  There are no issues that match your filters.

                  Category
                  Status