app/controllers/topics_controller.rb

Summary

Maintainability
B
6 hrs
Test Coverage

Unprotected mass assignment
Open

      @topic.attributes = params[:topic]
Severity: Critical
Found in app/controllers/topics_controller.rb by brakeman

Mass assignment is a feature of Rails which allows an application to create a record from the values of a hash.

Example:

User.new(params[:user])

Unfortunately, if there is a user field called admin which controls administrator access, now any user can make themselves an administrator.

attr_accessible and attr_protected can be used to limit mass assignment. However, Brakeman will warn unless attr_accessible is used, or mass assignment is completely disabled.

There are two different mass assignment warnings which can arise. The first is when mass assignment actually occurs, such as the example above. This results in a warning like

Unprotected mass assignment near line 61: User.new(params[:user])

The other warning is raised whenever a model is found which does not use attr_accessible. This produces generic warnings like

Mass assignment is not restricted using attr_accessible

with a list of affected models.

In Rails 3.1 and newer, mass assignment can easily be disabled:

config.active_record.whitelist_attributes = true

Unfortunately, it can also easily be bypassed:

User.new(params[:user], :without_protection => true)

Brakeman will warn on uses of without_protection.

Unsafe reflection method constantize called with parameter value
Open

      @relate_to_item = params[:relate_to_type].constantize.find(params[:relate_to_item])
Severity: Critical
Found in app/controllers/topics_controller.rb by brakeman

Brakeman reports on several cases of remote code execution, in which a user is able to control and execute code in ways unintended by application authors.

The obvious form of this is the use of eval with user input.

However, Brakeman also reports on dangerous uses of send, constantize, and other methods which allow creation of arbitrary objects or calling of arbitrary methods.

Unprotected mass assignment
Open

      @topic.attributes = params[:topic]
Severity: Critical
Found in app/controllers/topics_controller.rb by brakeman

Mass assignment is a feature of Rails which allows an application to create a record from the values of a hash.

Example:

User.new(params[:user])

Unfortunately, if there is a user field called admin which controls administrator access, now any user can make themselves an administrator.

attr_accessible and attr_protected can be used to limit mass assignment. However, Brakeman will warn unless attr_accessible is used, or mass assignment is completely disabled.

There are two different mass assignment warnings which can arise. The first is when mass assignment actually occurs, such as the example above. This results in a warning like

Unprotected mass assignment near line 61: User.new(params[:user])

The other warning is raised whenever a model is found which does not use attr_accessible. This produces generic warnings like

Mass assignment is not restricted using attr_accessible

with a list of affected models.

In Rails 3.1 and newer, mass assignment can easily be disabled:

config.active_record.whitelist_attributes = true

Unfortunately, it can also easily be bypassed:

User.new(params[:user], :without_protection => true)

Brakeman will warn on uses of without_protection.

Class has too many lines. [139/100]
Open

class TopicsController < ApplicationController
  include ExtendedContentController

  include ImageSlideshow

This cop checks if the length a class exceeds some maximum value. Comment lines can optionally be ignored. The maximum allowed length is configurable.

Assignment Branch Condition size for update is too high. [66.47/15]
Open

  def update
    @topic = Topic.find(params[:id])
    public_or_private_version_of(@topic)

    # if they have changed the topic type, make the edit fail so they can review

This cop checks that the ABC size of methods is not higher than the configured maximum. The ABC size is based on assignments, branches (method calls), and conditions. See http://c2.com/cgi/wiki?AbcMetric

Assignment Branch Condition size for create is too high. [56.82/15]
Open

  def create
    begin
      # ultimately I would like url's for peole to do look like the following:
      # topics/people/mcginnis/john
      # topics/people/mcginnis/john_marshall

This cop checks that the ABC size of methods is not higher than the configured maximum. The ABC size is based on assignments, branches (method calls), and conditions. See http://c2.com/cgi/wiki?AbcMetric

Method has too many lines. [39/10]
Open

  def create
    begin
      # ultimately I would like url's for peole to do look like the following:
      # topics/people/mcginnis/john
      # topics/people/mcginnis/john_marshall

This cop checks if the length of a method exceeds some maximum value. Comment lines can optionally be ignored. The maximum allowed length is configurable.

Method has too many lines. [35/10]
Open

  def update
    @topic = Topic.find(params[:id])
    public_or_private_version_of(@topic)

    # if they have changed the topic type, make the edit fail so they can review

This cop checks if the length of a method exceeds some maximum value. Comment lines can optionally be ignored. The maximum allowed length is configurable.

Cyclomatic complexity for create is too high. [13/6]
Open

  def create
    begin
      # ultimately I would like url's for peole to do look like the following:
      # topics/people/mcginnis/john
      # topics/people/mcginnis/john_marshall

This cop checks that the cyclomatic complexity of methods is not higher than the configured maximum. The cyclomatic complexity is the number of linearly independent paths through a method. The algorithm counts decision points and adds one.

An if statement (or unless or ?:) increases the complexity by one. An else branch does not, since it doesn't add a decision point. The && operator (or keyword and) can be converted to a nested if statement, and ||/or is shorthand for a sequence of ifs, so they also add one. Loops can be said to have an exit condition, so they add one.

Perceived complexity for create is too high. [13/7]
Open

  def create
    begin
      # ultimately I would like url's for peole to do look like the following:
      # topics/people/mcginnis/john
      # topics/people/mcginnis/john_marshall

This cop tries to produce a complexity score that's a measure of the complexity the reader experiences when looking at a method. For that reason it considers when nodes as something that doesn't add as much complexity as an if or a &&. Except if it's one of those special case/when constructs where there's no expression after case. Then the cop treats it as an if/elsif/elsif... and lets all the when nodes count. In contrast to the CyclomaticComplexity cop, this cop considers else nodes as adding complexity.

Example:

def my_method                   # 1
  if cond                       # 1
    case var                    # 2 (0.8 + 4 * 0.2, rounded)
    when 1 then func_one
    when 2 then func_two
    when 3 then func_three
    when 4..10 then func_other
    end
  else                          # 1
    do_something until a && b   # 2
  end                           # ===
end                             # 7 complexity points

Perceived complexity for update is too high. [10/7]
Open

  def update
    @topic = Topic.find(params[:id])
    public_or_private_version_of(@topic)

    # if they have changed the topic type, make the edit fail so they can review

This cop tries to produce a complexity score that's a measure of the complexity the reader experiences when looking at a method. For that reason it considers when nodes as something that doesn't add as much complexity as an if or a &&. Except if it's one of those special case/when constructs where there's no expression after case. Then the cop treats it as an if/elsif/elsif... and lets all the when nodes count. In contrast to the CyclomaticComplexity cop, this cop considers else nodes as adding complexity.

Example:

def my_method                   # 1
  if cond                       # 1
    case var                    # 2 (0.8 + 4 * 0.2, rounded)
    when 1 then func_one
    when 2 then func_two
    when 3 then func_three
    when 4..10 then func_other
    end
  else                          # 1
    do_something until a && b   # 2
  end                           # ===
end                             # 7 complexity points

Cyclomatic complexity for update is too high. [8/6]
Open

  def update
    @topic = Topic.find(params[:id])
    public_or_private_version_of(@topic)

    # if they have changed the topic type, make the edit fail so they can review

This cop checks that the cyclomatic complexity of methods is not higher than the configured maximum. The cyclomatic complexity is the number of linearly independent paths through a method. The algorithm counts decision points and adds one.

An if statement (or unless or ?:) increases the complexity by one. An else branch does not, since it doesn't add a decision point. The && operator (or keyword and) can be converted to a nested if statement, and ||/or is shorthand for a sequence of ifs, so they also add one. Loops can be said to have an exit condition, so they add one.

Assignment Branch Condition size for list is too high. [15.65/15]
Open

  def list
    respond_to do |format|
      format.html { redirect_to basket_topics_path }
      format.rss do
        date = DateTime.parse(params[:updated_since]) if params[:updated_since]

This cop checks that the ABC size of methods is not higher than the configured maximum. The ABC size is based on assignments, branches (method calls), and conditions. See http://c2.com/cgi/wiki?AbcMetric

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

  def create
    begin
      # ultimately I would like url's for peole to do look like the following:
      # topics/people/mcginnis/john
      # topics/people/mcginnis/john_marshall
Severity: Minor
Found in app/controllers/topics_controller.rb - About 1 hr to fix

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

      def update
        @topic = Topic.find(params[:id])
        public_or_private_version_of(@topic)
    
        # if they have changed the topic type, make the edit fail so they can review
    Severity: Minor
    Found in app/controllers/topics_controller.rb - About 1 hr to fix

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

        def create
          begin
            # ultimately I would like url's for peole to do look like the following:
            # topics/people/mcginnis/john
            # topics/people/mcginnis/john_marshall
      Severity: Minor
      Found in app/controllers/topics_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 update has a Cognitive Complexity of 10 (exceeds 5 allowed). Consider refactoring.
      Open

        def update
          @topic = Topic.find(params[:id])
          public_or_private_version_of(@topic)
      
          # if they have changed the topic type, make the edit fail so they can review
      Severity: Minor
      Found in app/controllers/topics_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 list has a Cognitive Complexity of 6 (exceeds 5 allowed). Consider refactoring.
      Open

        def list
          respond_to do |format|
            format.html { redirect_to basket_topics_path }
            format.rss do
              date = DateTime.parse(params[:updated_since]) if params[:updated_since]
      Severity: Minor
      Found in app/controllers/topics_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

      TODO found
      Open

            # TODO: allow current_user whom is at least moderator to pick another user
      Severity: Minor
      Found in app/controllers/topics_controller.rb by fixme

      Similar blocks of code found in 6 locations. Consider refactoring.
      Open

        def list
          respond_to do |format|
            format.html { redirect_to basket_topics_path }
            format.rss do
              date = DateTime.parse(params[:updated_since]) if params[:updated_since]
      Severity: Major
      Found in app/controllers/topics_controller.rb and 5 other locations - About 45 mins to fix
      app/controllers/audio_controller.rb on lines 9..18
      app/controllers/documents_controller.rb on lines 9..18
      app/controllers/images_controller.rb on lines 9..18
      app/controllers/video_controller.rb on lines 9..18
      app/controllers/web_links_controller.rb on lines 11..20

      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 40.

      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

      Favor modifier if usage when having a single-line body. Another good alternative is the usage of control flow &&/||.
      Open

          if params[:index_for_basket] && @successful

      Checks for if and unless statements that would fit on one line if written as a modifier if/unless. The maximum line length is configured in the Metrics/LineLength cop.

      Example:

      # bad
      if condition
        do_stuff(bar)
      end
      
      unless qux.empty?
        Foo.do_something
      end
      
      # good
      do_stuff(bar) if condition
      Foo.do_something unless qux.empty?

      Favor modifier if usage when having a single-line body. Another good alternative is the usage of control flow &&/||.
      Open

          if !index_for_basket.nil?

      Checks for if and unless statements that would fit on one line if written as a modifier if/unless. The maximum line length is configured in the Metrics/LineLength cop.

      Example:

      # bad
      if condition
        do_stuff(bar)
      end
      
      unless qux.empty?
        Foo.do_something
      end
      
      # good
      do_stuff(bar) if condition
      Foo.do_something unless qux.empty?

      Redundant curly braces around a hash parameter.
      Open

              redirect_to_related_item(@relate_to_item, { private: (params[:related_item_private] && params[:related_item_private] == 'true' && permitted_to_view_private_items?) })

      This cop checks for braces around the last parameter in a method call if the last parameter is a hash. It supports braces, no_braces and context_dependent styles.

      Example: EnforcedStyle: braces

      # The `braces` style enforces braces around all method
      # parameters that are hashes.
      
      # bad
      some_method(x, y, a: 1, b: 2)
      
      # good
      some_method(x, y, {a: 1, b: 2})

      Example: EnforcedStyle: no_braces (default)

      # The `no_braces` style checks that the last parameter doesn't
      # have braces around it.
      
      # bad
      some_method(x, y, {a: 1, b: 2})
      
      # good
      some_method(x, y, a: 1, b: 2)

      Example: EnforcedStyle: context_dependent

      # The `context_dependent` style checks that the last parameter
      # doesn't have braces around it, but requires braces if the
      # second to last parameter is also a hash literal.
      
      # bad
      some_method(x, y, {a: 1, b: 2})
      some_method(x, y, {a: 1, b: 2}, a: 1, b: 2)
      
      # good
      some_method(x, y, a: 1, b: 2)
      some_method(x, y, {a: 1, b: 2}, {a: 1, b: 2})

      Favor unless over if for negative conditions.
      Open

          if !index_for_basket.nil?
            index_for_basket.update_index_topic('destroy')
          end

      Checks for uses of if with a negated condition. Only ifs without else are considered. There are three different styles:

      - both
      - prefix
      - postfix

      Example: EnforcedStyle: both (default)

      # enforces `unless` for `prefix` and `postfix` conditionals
      
      # bad
      
      if !foo
        bar
      end
      
      # good
      
      unless foo
        bar
      end
      
      # bad
      
      bar if !foo
      
      # good
      
      bar unless foo

      Example: EnforcedStyle: prefix

      # enforces `unless` for just `prefix` conditionals
      
      # bad
      
      if !foo
        bar
      end
      
      # good
      
      unless foo
        bar
      end
      
      # good
      
      bar if !foo

      Example: EnforcedStyle: postfix

      # enforces `unless` for just `postfix` conditionals
      
      # bad
      
      bar if !foo
      
      # good
      
      bar unless foo
      
      # good
      
      if !foo
        bar
      end

      Prefer Date or Time over DateTime.
      Open

              date = DateTime.now.beginning_of_month        if date.nil?

      This cop checks for uses of DateTime that should be replaced by Date or Time.

      Example:

      # bad - uses `DateTime` for current time
      DateTime.now
      
      # good - uses `Time` for current time
      Time.now
      
      # bad - uses `DateTime` for modern date
      DateTime.iso8601('2016-06-29')
      
      # good - uses `Date` for modern date
      Date.iso8601('2016-06-29')
      
      # good - uses `DateTime` with start argument for historical date
      DateTime.iso8601('1751-04-23', Date::ENGLAND)

      Prefer Date or Time over DateTime.
      Open

              date = DateTime.parse(params[:updated_since]) if params[:updated_since]

      This cop checks for uses of DateTime that should be replaced by Date or Time.

      Example:

      # bad - uses `DateTime` for current time
      DateTime.now
      
      # good - uses `Time` for current time
      Time.now
      
      # bad - uses `DateTime` for modern date
      DateTime.iso8601('2016-06-29')
      
      # good - uses `Date` for modern date
      Date.iso8601('2016-06-29')
      
      # good - uses `DateTime` with start argument for historical date
      DateTime.iso8601('1751-04-23', Date::ENGLAND)

      Avoid rescuing without specifying an error class.
      Open

          rescue

      This cop checks for rescuing StandardError. There are two supported styles implicit and explicit. This cop will not register an offense if any error other than StandardError is specified.

      Example: EnforcedStyle: implicit

      # `implicit` will enforce using `rescue` instead of
      # `rescue StandardError`.
      
      # bad
      begin
        foo
      rescue StandardError
        bar
      end
      
      # good
      begin
        foo
      rescue
        bar
      end
      
      # good
      begin
        foo
      rescue OtherError
        bar
      end
      
      # good
      begin
        foo
      rescue StandardError, SecurityError
        bar
      end

      Example: EnforcedStyle: explicit (default)

      # `explicit` will enforce using `rescue StandardError`
      # instead of `rescue`.
      
      # bad
      begin
        foo
      rescue
        bar
      end
      
      # good
      begin
        foo
      rescue StandardError
        bar
      end
      
      # good
      begin
        foo
      rescue OtherError
        bar
      end
      
      # good
      begin
        foo
      rescue StandardError, SecurityError
        bar
      end

      Prefer $ERROR_INFO from the stdlib 'English' module (don't forget to require it) over $!.
      Open

            flash[:error], @successful = $!.to_s, false

      Do not use parallel assignment.
      Open

            flash[:error], @successful = $!.to_s, false

      Checks for simple usages of parallel assignment. This will only complain when the number of variables being assigned matched the number of assigning variables.

      Example:

      # bad
      a, b, c = 1, 2, 3
      a, b, c = [1, 2, 3]
      
      # good
      one, two = *foo
      a, b = foo()
      a, b = b, a
      
      a = 1
      b = 2
      c = 3

      Use safe navigation (&.) instead of checking if an object exists before calling the method.
      Open

          if !index_for_basket.nil?
            index_for_basket.update_index_topic('destroy')
          end

      This cop transforms usages of a method call safeguarded by a non nil check for the variable whose method is being called to safe navigation (&.).

      Configuration option: ConvertCodeThatCanStartToReturnNil The default for this is false. When configured to true, this will check for code in the format !foo.nil? && foo.bar. As it is written, the return of this code is limited to false and whatever the return of the method is. If this is converted to safe navigation, foo&.bar can start returning nil as well as what the method returns.

      Example:

      # bad
      foo.bar if foo
      foo.bar(param1, param2) if foo
      foo.bar { |e| e.something } if foo
      foo.bar(param) { |e| e.something } if foo
      
      foo.bar if !foo.nil?
      foo.bar unless !foo
      foo.bar unless foo.nil?
      
      foo && foo.bar
      foo && foo.bar(param1, param2)
      foo && foo.bar { |e| e.something }
      foo && foo.bar(param) { |e| e.something }
      
      # good
      foo&.bar
      foo&.bar(param1, param2)
      foo&.bar { |e| e.something }
      foo&.bar(param) { |e| e.something }
      
      foo.nil? || foo.bar
      !foo || foo.bar
      
      # Methods that `nil` will `respond_to?` should not be converted to
      # use safe navigation
      foo.to_i if foo

      There are no issues that match your filters.

      Category
      Status