wearefine/fae

View on GitHub
app/models/concerns/fae/trackable.rb

Summary

Maintainability
A
50 mins
Test Coverage

Module has too many lines. [103/100]
Open

  module Trackable
    extend ActiveSupport::Concern

    included do
      after_create :add_create_change, if: :track_changes?

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

Assignment Branch Condition size for update_parent is too high. [23.96/15]
Open

    def update_parent
      parent = self.try(:imageable) || self.try(:fileable) || self.try(:contentable)
      if parent.present?
        latest_change = parent.try(:tracked_changes).try(:first)
        if latest_change.present? && latest_change.change_type == 'updated' && latest_change.updated_at > 2.seconds.ago

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. [16/10]
Open

    def update_parent
      parent = self.try(:imageable) || self.try(:fileable) || self.try(:contentable)
      if parent.present?
        latest_change = parent.try(:tracked_changes).try(:first)
        if latest_change.present? && latest_change.change_type == 'updated' && latest_change.updated_at > 2.seconds.ago

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. [15/10]
Open

    def add_update_change
      if has_parent?
        update_parent
      else
        return if legit_updated_attributes.blank?

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.

Perceived complexity for update_parent is too high. [8/7]
Open

    def update_parent
      parent = self.try(:imageable) || self.try(:fileable) || self.try(:contentable)
      if parent.present?
        latest_change = parent.try(:tracked_changes).try(:first)
        if latest_change.present? && latest_change.change_type == 'updated' && latest_change.updated_at > 2.seconds.ago

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_parent is too high. [7/6]
Open

    def update_parent
      parent = self.try(:imageable) || self.try(:fileable) || self.try(:contentable)
      if parent.present?
        latest_change = parent.try(:tracked_changes).try(:first)
        if latest_change.present? && latest_change.change_type == 'updated' && latest_change.updated_at > 2.seconds.ago

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.

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

    def add_update_change
      if has_parent?
        update_parent
      else
        return if legit_updated_attributes.blank?
Severity: Minor
Found in app/models/concerns/fae/trackable.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 update_parent has a Cognitive Complexity of 6 (exceeds 5 allowed). Consider refactoring.
Open

    def update_parent
      parent = self.try(:imageable) || self.try(:fileable) || self.try(:contentable)
      if parent.present?
        latest_change = parent.try(:tracked_changes).try(:first)
        if latest_change.present? && latest_change.change_type == 'updated' && latest_change.updated_at > 2.seconds.ago
Severity: Minor
Found in app/models/concerns/fae/trackable.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

Line is too long. [83/80]
Open

      attrs.merge!(tracker_parent_attrs(attrs[:change_type])) if fae_tracker_parent

Line is too long. [85/80]
Open

          updated_updated_attributes = latest_change.updated_attributes << asset_name

Redundant self detected.
Open

      parent = self.try(:imageable) || self.try(:fileable) || self.try(:contentable)

This cop checks for redundant uses of self.

The usage of self is only needed when:

  • Sending a message to same object with zero arguments in presence of a method name clash with an argument or a local variable.

  • Calling an attribute writer to prevent an local variable assignment.

Note, with using explicit self you can only send messages with public or protected scope, you cannot send private messages this way.

Note we allow uses of self with operators because it would be awkward otherwise.

Example:

# bad
def foo(bar)
  self.baz
end

# good
def foo(bar)
  self.bar  # Resolves name clash with the argument.
end

def foo
  bar = 1
  self.bar  # Resolves name clash with the local variable.
end

def foo
  %w[x y z].select do |bar|
    self.bar == bar  # Resolves name clash with argument of the block.
  end
end

Align the parameters of a method call if they span more than one line.
Open

        as: :changeable,
        class_name: 'Fae::Change'

Here we check if the parameters on a multi-line method call or definition are aligned.

Example: EnforcedStyle: withfirstparameter (default)

# good

foo :bar,
    :baz

# bad

foo :bar,
  :baz

Example: EnforcedStyle: withfixedindentation

# good

foo :bar,
  :baz

# bad

foo :bar,
    :baz

Use 2 spaces for indentation in a hash, relative to the first position after the preceding left parenthesis.
Open

            changeable_id: parent.id,

This cops checks the indentation of the first key in a hash literal where the opening brace and the first key are on separate lines. The other keys' indentations are handled by the AlignHash cop.

By default, Hash literals that are arguments in a method call with parentheses, and where the opening curly brace of the hash is on the same line as the opening parenthesis of the method call, shall have their first key indented one step (two spaces) more than the position inside the opening parenthesis.

Other hash literals shall have their first key indented one step more than the start of the line where the opening curly brace is.

This default style is called 'specialinsideparentheses'. Alternative styles are 'consistent' and 'align_braces'. Here are examples:

Example: EnforcedStyle: specialinsideparentheses (default)

# The `special_inside_parentheses` style enforces that the first key
# in a hash literal where the opening brace and the first key are on
# separate lines is indented one step (two spaces) more than the
# position inside the opening parentheses.

# bad
hash = {
  key: :value
}
and_in_a_method_call({
  no: :difference
                     })

# good
special_inside_parentheses
hash = {
  key: :value
}
but_in_a_method_call({
                       its_like: :this
                     })

Example: EnforcedStyle: consistent

# The `consistent` style enforces that the first key in a hash
# literal where the opening brace and the first key are on
# seprate lines is indented the same as a hash literal which is not
# defined inside a method call.

# bad
hash = {
  key: :value
}
but_in_a_method_call({
                       its_like: :this
                      })

# good
hash = {
  key: :value
}
and_in_a_method_call({
  no: :difference
})

Example: EnforcedStyle: align_braces

# The `align_brackets` style enforces that the opening and closing
# braces are indented to the same position.

# bad
and_now_for_something = {
                          completely: :different
}

# good
and_now_for_something = {
                          completely: :different
                        }

%w-literals should be delimited by [ and ].
Open

      %w(Fae::Image Fae::File Fae::TextField Fae::TextArea).include? self.class.name

This cop enforces the consistent usage of %-literal delimiters.

Specify the 'default' key to set all preferred delimiters at once. You can continue to specify individual preferred delimiters to override the default.

Example:

# Style/PercentLiteralDelimiters:
#   PreferredDelimiters:
#     default: '[]'
#     '%i':    '()'

# good
%w[alpha beta] + %i(gamma delta)

# bad
%W(alpha #{beta})

# bad
%I(alpha beta)

Redundant self detected.
Open

      parent = self.try(:imageable) || self.try(:fileable) || self.try(:contentable)

This cop checks for redundant uses of self.

The usage of self is only needed when:

  • Sending a message to same object with zero arguments in presence of a method name clash with an argument or a local variable.

  • Calling an attribute writer to prevent an local variable assignment.

Note, with using explicit self you can only send messages with public or protected scope, you cannot send private messages this way.

Note we allow uses of self with operators because it would be awkward otherwise.

Example:

# bad
def foo(bar)
  self.baz
end

# good
def foo(bar)
  self.bar  # Resolves name clash with the argument.
end

def foo
  bar = 1
  self.bar  # Resolves name clash with the local variable.
end

def foo
  %w[x y z].select do |bar|
    self.bar == bar  # Resolves name clash with argument of the block.
  end
end

Indent the right brace the same as the first position after the preceding left parenthesis.
Open

          })

This cops checks the indentation of the first key in a hash literal where the opening brace and the first key are on separate lines. The other keys' indentations are handled by the AlignHash cop.

By default, Hash literals that are arguments in a method call with parentheses, and where the opening curly brace of the hash is on the same line as the opening parenthesis of the method call, shall have their first key indented one step (two spaces) more than the position inside the opening parenthesis.

Other hash literals shall have their first key indented one step more than the start of the line where the opening curly brace is.

This default style is called 'specialinsideparentheses'. Alternative styles are 'consistent' and 'align_braces'. Here are examples:

Example: EnforcedStyle: specialinsideparentheses (default)

# The `special_inside_parentheses` style enforces that the first key
# in a hash literal where the opening brace and the first key are on
# separate lines is indented one step (two spaces) more than the
# position inside the opening parentheses.

# bad
hash = {
  key: :value
}
and_in_a_method_call({
  no: :difference
                     })

# good
special_inside_parentheses
hash = {
  key: :value
}
but_in_a_method_call({
                       its_like: :this
                     })

Example: EnforcedStyle: consistent

# The `consistent` style enforces that the first key in a hash
# literal where the opening brace and the first key are on
# seprate lines is indented the same as a hash literal which is not
# defined inside a method call.

# bad
hash = {
  key: :value
}
but_in_a_method_call({
                       its_like: :this
                      })

# good
hash = {
  key: :value
}
and_in_a_method_call({
  no: :difference
})

Example: EnforcedStyle: align_braces

# The `align_brackets` style enforces that the opening and closing
# braces are indented to the same position.

# bad
and_now_for_something = {
                          completely: :different
}

# good
and_now_for_something = {
                          completely: :different
                        }

Line is too long. [85/80]
Open

        attrs.merge!(tracker_parent_attrs(attrs[:change_type])) if fae_tracker_parent

Line is too long. [89/80]
Open

          latest_change.update_attribute(:updated_attributes, updated_updated_attributes)

Redundant curly braces around a hash parameter.
Open

          Fae::Change.create({
            changeable_id: parent.id,
            changeable_type: parent.class.name,
            user_id: Fae::Change.current_user,
            change_type: 'updated',

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})

Line is too long. [83/80]
Open

      attrs.merge!(tracker_parent_attrs(attrs[:change_type])) if fae_tracker_parent

Line is too long. [119/80]
Open

        if latest_change.present? && latest_change.change_type == 'updated' && latest_change.updated_at > 2.seconds.ago

Line is too long. [86/80]
Open

        change_type: "#{self.class.name.titleize} #{fae_display_field} #{change_type}"

Line is too long. [84/80]
Open

      %w(Fae::Image Fae::File Fae::TextField Fae::TextArea).include? self.class.name

Rename has_parent? to parent?.
Open

    def has_parent?

This cop makes sure that predicates are named properly.

Example:

# bad
def is_even?(value)
end

# good
def even?(value)
end

# bad
def has_value?
end

# good
def value?
end

Use a guard clause instead of wrapping the code inside a conditional expression.
Open

      if parent.present?

Use a guard clause instead of wrapping the code inside a conditional expression

Example:

# bad
def test
  if something
    work
  end
end

# good
def test
  return unless something
  work
end

# also good
def test
  work if something
end

# bad
if something
  raise 'exception'
else
  ok
end

# good
raise 'exception' if something
ok

Line is too long. [91/80]
Open

      self.class.superclass.name == 'Fae::StaticPage' ? 'Fae::StaticPage' : self.class.name

Use %w or %W for an array of words.
Open

      ['id', 'updated_at', 'created_at']

This cop can check for array literals made up of word-like strings, that are not using the %w() syntax.

Alternatively, it can check for uses of the %w() syntax, in projects which do not want to include that syntax.

Configuration option: MinSize If set, arrays with fewer elements than this value will not trigger the cop. For example, a MinSize of 3 will not enforce a style on an array of 2 or fewer elements.

Example: EnforcedStyle: percent (default)

# good
%w[foo bar baz]

# bad
['foo', 'bar', 'baz']

Example: EnforcedStyle: brackets

# good
['foo', 'bar', 'baz']

# bad
%w[foo bar baz]

Line is too long. [84/80]
Open

      parent = self.try(:imageable) || self.try(:fileable) || self.try(:contentable)

Missing top-level module documentation comment.
Open

  module Trackable

This cop checks for missing top-level documentation of classes and modules. Classes with no body are exempt from the check and so are namespace modules - modules that have nothing in their bodies except classes, other modules, or constant definitions.

The documentation requirement is annulled if the class or module has a "#:nodoc:" comment next to it. Likewise, "#:nodoc: all" does the same for all its children.

Example:

# bad
class Person
  # ...
end

# good
# Description/Explanation of Person class
class Person
  # ...
end

Redundant self detected.
Open

      parent = self.try(:imageable) || self.try(:fileable) || self.try(:contentable)

This cop checks for redundant uses of self.

The usage of self is only needed when:

  • Sending a message to same object with zero arguments in presence of a method name clash with an argument or a local variable.

  • Calling an attribute writer to prevent an local variable assignment.

Note, with using explicit self you can only send messages with public or protected scope, you cannot send private messages this way.

Note we allow uses of self with operators because it would be awkward otherwise.

Example:

# bad
def foo(bar)
  self.baz
end

# good
def foo(bar)
  self.bar  # Resolves name clash with the argument.
end

def foo
  bar = 1
  self.bar  # Resolves name clash with the local variable.
end

def foo
  %w[x y z].select do |bar|
    self.bar == bar  # Resolves name clash with argument of the block.
  end
end

Extra empty line detected at module body end.
Open


  end

This cops checks if empty lines around the bodies of modules match the configuration.

Example: EnforcedStyle: empty_lines

# good

module Foo

  def bar
    # ...
  end

end

Example: EnforcedStyle: emptylinesexcept_namespace

# good

module Foo
  module Bar

    # ...

  end
end

Example: EnforcedStyle: emptylinesspecial

# good
module Foo

  def bar; end

end

Example: EnforcedStyle: noemptylines (default)

# good

module Foo
  def bar
    # ...
  end
end

Space missing after comma.
Open

      attached_as || self.class.name.gsub('Fae::','').underscore

Checks for comma (,) not followed by some kind of space.

Example:

# bad
[1,2]
{ foo:bar,}

# good
[1, 2]
{ foo:bar, }

Prefer Object#is_a? over Object#kind_of?.
Open

      if fae_tracker_blacklist.kind_of?(Array) && fae_tracker_blacklist.present?

This cop enforces consistent use of Object#is_a? or Object#kind_of?.

Example: EnforcedStyle: is_a? (default)

# bad
var.kind_of?(Date)
var.kind_of?(Integer)

# good
var.is_a?(Date)
var.is_a?(Integer)

Example: EnforcedStyle: kind_of?

# bad
var.is_a?(Time)
var.is_a?(String)

# good
var.kind_of?(Time)
var.kind_of?(String)

There are no issues that match your filters.

Category
Status