ikuseiGmbH/smart-village-app-cms

View on GitHub
app/controllers/offers_controller.rb

Summary

Maintainability
D
1 day
Test Coverage

Parameters should be whitelisted for mass assignment
Open

      params.require(:offer).permit!

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. [201/100]
Open

class OffersController < ApplicationController
  before_action :verify_current_user
  before_action { verify_current_user_role("role_offer") }
  before_action :init_graphql_client

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

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

  def edit
    results = @smart_village.query <<~GRAPHQL
      query {
        genericItem(
          id: #{params[:id]}

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 convert_params_for_graphql has a Cognitive Complexity of 44 (exceeds 5 allowed). Consider refactoring.
Open

    def convert_params_for_graphql
      # Convert has_many content_blocks(which has_many media_contents)
      content_block_params = @offer_params["content_blocks"]
      return unless content_block_params.present?

Severity: Minor
Found in app/controllers/offers_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

Assignment Branch Condition size for convert_params_for_graphql is too high. [50.26/15]
Open

    def convert_params_for_graphql
      # Convert has_many content_blocks(which has_many media_contents)
      content_block_params = @offer_params["content_blocks"]
      return unless content_block_params.present?

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 and https://en.wikipedia.org/wiki/ABC_Software_Metric.

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

    def convert_params_for_graphql
      # Convert has_many content_blocks(which has_many media_contents)
      content_block_params = @offer_params["content_blocks"]
      return unless content_block_params.present?

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.

Complex method OffersController#convert_params_for_graphql (71.3)
Open

    def convert_params_for_graphql
      # Convert has_many content_blocks(which has_many media_contents)
      content_block_params = @offer_params["content_blocks"]
      return unless content_block_params.present?

Severity: Minor
Found in app/controllers/offers_controller.rb by flog

Flog calculates the ABC score for methods. The ABC score is based on assignments, branches (method calls), and conditions.

You can read more about ABC metrics or the flog tool

Cyclomatic complexity for convert_params_for_graphql is too high. [15/6]
Open

    def convert_params_for_graphql
      # Convert has_many content_blocks(which has_many media_contents)
      content_block_params = @offer_params["content_blocks"]
      return unless content_block_params.present?

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 has too many lines. [18/10]
Open

  def destroy
    results = @smart_village.query <<~GRAPHQL
      mutation {
        destroyRecord(
          id: #{params["id"]},

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

    def convert_params_for_graphql
      # Convert has_many content_blocks(which has_many media_contents)
      content_block_params = @offer_params["content_blocks"]
      return unless content_block_params.present?

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

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

  def index
    results = @smart_village.query <<~GRAPHQL
      query {
        genericItems(genericType: "Offer") {
          id

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 edit has 60 lines of code (exceeds 25 allowed). Consider refactoring.
Open

  def edit
    results = @smart_village.query <<~GRAPHQL
      query {
        genericItem(
          id: #{params[:id]}
Severity: Major
Found in app/controllers/offers_controller.rb - About 2 hrs to fix

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

      def create
        query = create_or_update_mutation
        begin
          results = @smart_village.query query
        rescue Graphlient::Errors::GraphQLError => e

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

        def new_generic_item
          OpenStruct.new(
            generic_type: "Offer",
            content_blocks: [OpenStruct.new],
            media_contents: [OpenStruct.new(source_url: OpenStruct.new)],

    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.

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

      def create
        query = create_or_update_mutation
        begin
          results = @smart_village.query query
        rescue Graphlient::Errors::GraphQLError => e

    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 and https://en.wikipedia.org/wiki/ABC_Software_Metric.

    Method convert_params_for_graphql has 42 lines of code (exceeds 25 allowed). Consider refactoring.
    Open

        def convert_params_for_graphql
          # Convert has_many content_blocks(which has_many media_contents)
          content_block_params = @offer_params["content_blocks"]
          return unless content_block_params.present?
    
    
    Severity: Minor
    Found in app/controllers/offers_controller.rb - About 1 hr to fix

      OffersController#convert_params_for_graphql contains iterators nested 2 deep
      Open

                content_block[:media_contents].each do |_key, media_content|
      Severity: Minor
      Found in app/controllers/offers_controller.rb by reek

      A Nested Iterator occurs when a block contains another block.

      Example

      Given

      class Duck
        class << self
          def duck_names
            %i!tick trick track!.each do |surname|
              %i!duck!.each do |last_name|
                puts "full name is #{surname} #{last_name}"
              end
            end
          end
        end
      end

      Reek would report the following warning:

      test.rb -- 1 warning:
        [5]:Duck#duck_names contains iterators nested 2 deep (NestedIterators)

      OffersController#create_or_update_mutation has boolean parameter 'update'
      Open

          def create_or_update_mutation(update = false)
      Severity: Minor
      Found in app/controllers/offers_controller.rb by reek

      Boolean Parameter is a special case of Control Couple, where a method parameter is defaulted to true or false. A Boolean Parameter effectively permits a method's caller to decide which execution path to take. This is a case of bad cohesion. You're creating a dependency between methods that is not really necessary, thus increasing coupling.

      Example

      Given

      class Dummy
        def hit_the_switch(switch = true)
          if switch
            puts 'Hitting the switch'
            # do other things...
          else
            puts 'Not hitting the switch'
            # do other things...
          end
        end
      end

      Reek would emit the following warning:

      test.rb -- 3 warnings:
        [1]:Dummy#hit_the_switch has boolean parameter 'switch' (BooleanParameter)
        [2]:Dummy#hit_the_switch is controlled by argument switch (ControlParameter)

      Note that both smells are reported, Boolean Parameter and Control Parameter.

      Getting rid of the smell

      This is highly dependent on your exact architecture, but looking at the example above what you could do is:

      • Move everything in the if branch into a separate method
      • Move everything in the else branch into a separate method
      • Get rid of the hit_the_switch method alltogether
      • Make the decision what method to call in the initial caller of hit_the_switch

      OffersController#create has approx 10 statements
      Open

        def create
      Severity: Minor
      Found in app/controllers/offers_controller.rb by reek

      A method with Too Many Statements is any method that has a large number of lines.

      Too Many Statements warns about any method that has more than 5 statements. Reek's smell detector for Too Many Statements counts +1 for every simple statement in a method and +1 for every statement within a control structure (if, else, case, when, for, while, until, begin, rescue) but it doesn't count the control structure itself.

      So the following method would score +6 in Reek's statement-counting algorithm:

      def parse(arg, argv, &error)
        if !(val = arg) and (argv.empty? or /\A-/ =~ (val = argv[0]))
          return nil, block, nil                                         # +1
        end
        opt = (val = parse_arg(val, &error))[1]                          # +2
        val = conv_arg(*val)                                             # +3
        if opt and !arg
          argv.shift                                                     # +4
        else
          val[0] = nil                                                   # +5
        end
        val                                                              # +6
      end

      (You might argue that the two assigments within the first @if@ should count as statements, and that perhaps the nested assignment should count as +2.)

      OffersController#convert_params_for_graphql has approx 29 statements
      Open

          def convert_params_for_graphql
      Severity: Minor
      Found in app/controllers/offers_controller.rb by reek

      A method with Too Many Statements is any method that has a large number of lines.

      Too Many Statements warns about any method that has more than 5 statements. Reek's smell detector for Too Many Statements counts +1 for every simple statement in a method and +1 for every statement within a control structure (if, else, case, when, for, while, until, begin, rescue) but it doesn't count the control structure itself.

      So the following method would score +6 in Reek's statement-counting algorithm:

      def parse(arg, argv, &error)
        if !(val = arg) and (argv.empty? or /\A-/ =~ (val = argv[0]))
          return nil, block, nil                                         # +1
        end
        opt = (val = parse_arg(val, &error))[1]                          # +2
        val = conv_arg(*val)                                             # +3
        if opt and !arg
          argv.shift                                                     # +4
        else
          val[0] = nil                                                   # +5
        end
        val                                                              # +6
      end

      (You might argue that the two assigments within the first @if@ should count as statements, and that perhaps the nested assignment should count as +2.)

      OffersController#update has approx 6 statements
      Open

        def update
      Severity: Minor
      Found in app/controllers/offers_controller.rb by reek

      A method with Too Many Statements is any method that has a large number of lines.

      Too Many Statements warns about any method that has more than 5 statements. Reek's smell detector for Too Many Statements counts +1 for every simple statement in a method and +1 for every statement within a control structure (if, else, case, when, for, while, until, begin, rescue) but it doesn't count the control structure itself.

      So the following method would score +6 in Reek's statement-counting algorithm:

      def parse(arg, argv, &error)
        if !(val = arg) and (argv.empty? or /\A-/ =~ (val = argv[0]))
          return nil, block, nil                                         # +1
        end
        opt = (val = parse_arg(val, &error))[1]                          # +2
        val = conv_arg(*val)                                             # +3
        if opt and !arg
          argv.shift                                                     # +4
        else
          val[0] = nil                                                   # +5
        end
        val                                                              # +6
      end

      (You might argue that the two assigments within the first @if@ should count as statements, and that perhaps the nested assignment should count as +2.)

      OffersController#convert_params_for_graphql calls 'media_contents << media_content' 2 times
      Open

                  media_contents << media_content
                end
                content_block[:media_contents] = media_contents
              end
      
      
      Severity: Minor
      Found in app/controllers/offers_controller.rb by reek

      Duplication occurs when two fragments of code look nearly identical, or when two fragments of code have nearly identical effects at some conceptual level.

      Reek implements a check for Duplicate Method Call.

      Example

      Here's a very much simplified and contrived example. The following method will report a warning:

      def double_thing()
        @other.thing + @other.thing
      end

      One quick approach to silence Reek would be to refactor the code thus:

      def double_thing()
        thing = @other.thing
        thing + thing
      end

      A slightly different approach would be to replace all calls of double_thing by calls to @other.double_thing:

      class Other
        def double_thing()
          thing + thing
        end
      end

      The approach you take will depend on balancing other factors in your code.

      OffersController#convert_params_for_graphql calls 'nested_values?(media_content.except(:content_type).to_h)' 2 times
      Open

                  next unless nested_values?(media_content.except(:content_type).to_h).include?(true)
      
                  media_contents << media_content
                end
                content_block[:media_contents] = media_contents
      Severity: Minor
      Found in app/controllers/offers_controller.rb by reek

      Duplication occurs when two fragments of code look nearly identical, or when two fragments of code have nearly identical effects at some conceptual level.

      Reek implements a check for Duplicate Method Call.

      Example

      Here's a very much simplified and contrived example. The following method will report a warning:

      def double_thing()
        @other.thing + @other.thing
      end

      One quick approach to silence Reek would be to refactor the code thus:

      def double_thing()
        thing = @other.thing
        thing + thing
      end

      A slightly different approach would be to replace all calls of double_thing by calls to @other.double_thing:

      class Other
        def double_thing()
          thing + thing
        end
      end

      The approach you take will depend on balancing other factors in your code.

      OffersController assumes too much for instance variable '@offer_params'
      Open

      class OffersController < ApplicationController
      Severity: Minor
      Found in app/controllers/offers_controller.rb by reek

      Classes should not assume that instance variables are set or present outside of the current class definition.

      Good:

      class Foo
        def initialize
          @bar = :foo
        end
      
        def foo?
          @bar == :foo
        end
      end

      Good as well:

      class Foo
        def foo?
          bar == :foo
        end
      
        def bar
          @bar ||= :foo
        end
      end

      Bad:

      class Foo
        def go_foo!
          @bar = :foo
        end
      
        def foo?
          @bar == :foo
        end
      end

      Example

      Running Reek on:

      class Dummy
        def test
          @ivar
        end
      end

      would report:

      [1]:InstanceVariableAssumption: Dummy assumes too much for instance variable @ivar

      Note that this example would trigger this smell warning as well:

      class Parent
        def initialize(omg)
          @omg = omg
        end
      end
      
      class Child < Parent
        def foo
          @omg
        end
      end

      The way to address the smell warning is that you should create an attr_reader to use @omg in the subclass and not access @omg directly like this:

      class Parent
        attr_reader :omg
      
        def initialize(omg)
          @omg = omg
        end
      end
      
      class Child < Parent
        def foo
          omg
        end
      end

      Directly accessing instance variables is considered a smell because it breaks encapsulation and makes it harder to reason about code.

      If you don't want to expose those methods as public API just make them private like this:

      class Parent
        def initialize(omg)
          @omg = omg
        end
      
        private
        attr_reader :omg
      end
      
      class Child < Parent
        def foo
          omg
        end
      end

      Current Support in Reek

      An instance variable must:

      • be set in the constructor
      • or be accessed through a method with lazy initialization / memoization.

      If not, Instance Variable Assumption will be reported.

      OffersController has no descriptive comment
      Open

      class OffersController < ApplicationController
      Severity: Minor
      Found in app/controllers/offers_controller.rb by reek

      Classes and modules are the units of reuse and release. It is therefore considered good practice to annotate every class and module with a brief comment outlining its responsibilities.

      Example

      Given

      class Dummy
        # Do things...
      end

      Reek would emit the following warning:

      test.rb -- 1 warning:
        [1]:Dummy has no descriptive comment (IrresponsibleModule)

      Fixing this is simple - just an explaining comment:

      # The Dummy class is responsible for ...
      class Dummy
        # Do things...
      end

      OffersController#convert_params_for_graphql calls '@offer_params["operating_company"]' 3 times
      Open

            if @offer_params["operating_company"].present?
              if nested_values?(@offer_params["operating_company"].to_h).include?(true)
                @offer_params["companies"] = [@offer_params["operating_company"]]
      Severity: Minor
      Found in app/controllers/offers_controller.rb by reek

      Duplication occurs when two fragments of code look nearly identical, or when two fragments of code have nearly identical effects at some conceptual level.

      Reek implements a check for Duplicate Method Call.

      Example

      Here's a very much simplified and contrived example. The following method will report a warning:

      def double_thing()
        @other.thing + @other.thing
      end

      One quick approach to silence Reek would be to refactor the code thus:

      def double_thing()
        thing = @other.thing
        thing + thing
      end

      A slightly different approach would be to replace all calls of double_thing by calls to @other.double_thing:

      class Other
        def double_thing()
          thing + thing
        end
      end

      The approach you take will depend on balancing other factors in your code.

      OffersController#convert_params_for_graphql calls 'media_content.except(:content_type).to_h' 2 times
      Open

                  next unless nested_values?(media_content.except(:content_type).to_h).include?(true)
      
                  media_contents << media_content
                end
                content_block[:media_contents] = media_contents
      Severity: Minor
      Found in app/controllers/offers_controller.rb by reek

      Duplication occurs when two fragments of code look nearly identical, or when two fragments of code have nearly identical effects at some conceptual level.

      Reek implements a check for Duplicate Method Call.

      Example

      Here's a very much simplified and contrived example. The following method will report a warning:

      def double_thing()
        @other.thing + @other.thing
      end

      One quick approach to silence Reek would be to refactor the code thus:

      def double_thing()
        thing = @other.thing
        thing + thing
      end

      A slightly different approach would be to replace all calls of double_thing by calls to @other.double_thing:

      class Other
        def double_thing()
          thing + thing
        end
      end

      The approach you take will depend on balancing other factors in your code.

      OffersController#convert_params_for_graphql calls '@offer_params["dates"]' 2 times
      Open

            if @offer_params["dates"].present?
              dates = []
              @offer_params["dates"].each do |_key, date|
      Severity: Minor
      Found in app/controllers/offers_controller.rb by reek

      Duplication occurs when two fragments of code look nearly identical, or when two fragments of code have nearly identical effects at some conceptual level.

      Reek implements a check for Duplicate Method Call.

      Example

      Here's a very much simplified and contrived example. The following method will report a warning:

      def double_thing()
        @other.thing + @other.thing
      end

      One quick approach to silence Reek would be to refactor the code thus:

      def double_thing()
        thing = @other.thing
        thing + thing
      end

      A slightly different approach would be to replace all calls of double_thing by calls to @other.double_thing:

      class Other
        def double_thing()
          thing + thing
        end
      end

      The approach you take will depend on balancing other factors in your code.

      OffersController#convert_params_for_graphql calls '@offer_params["media_contents"]' 2 times
      Open

            if @offer_params["media_contents"].present?
              media_contents = []
              @offer_params["media_contents"].each do |_key, media_content|
      Severity: Minor
      Found in app/controllers/offers_controller.rb by reek

      Duplication occurs when two fragments of code look nearly identical, or when two fragments of code have nearly identical effects at some conceptual level.

      Reek implements a check for Duplicate Method Call.

      Example

      Here's a very much simplified and contrived example. The following method will report a warning:

      def double_thing()
        @other.thing + @other.thing
      end

      One quick approach to silence Reek would be to refactor the code thus:

      def double_thing()
        thing = @other.thing
        thing + thing
      end

      A slightly different approach would be to replace all calls of double_thing by calls to @other.double_thing:

      class Other
        def double_thing()
          thing + thing
        end
      end

      The approach you take will depend on balancing other factors in your code.

      OffersController#convert_params_for_graphql calls 'content_block[:media_contents]' 2 times
      Open

              if content_block[:media_contents].present?
                media_contents = []
                content_block[:media_contents].each do |_key, media_content|
      Severity: Minor
      Found in app/controllers/offers_controller.rb by reek

      Duplication occurs when two fragments of code look nearly identical, or when two fragments of code have nearly identical effects at some conceptual level.

      Reek implements a check for Duplicate Method Call.

      Example

      Here's a very much simplified and contrived example. The following method will report a warning:

      def double_thing()
        @other.thing + @other.thing
      end

      One quick approach to silence Reek would be to refactor the code thus:

      def double_thing()
        thing = @other.thing
        thing + thing
      end

      A slightly different approach would be to replace all calls of double_thing by calls to @other.double_thing:

      class Other
        def double_thing()
          thing + thing
        end
      end

      The approach you take will depend on balancing other factors in your code.

      OffersController assumes too much for instance variable '@smart_village'
      Open

      class OffersController < ApplicationController
      Severity: Minor
      Found in app/controllers/offers_controller.rb by reek

      Classes should not assume that instance variables are set or present outside of the current class definition.

      Good:

      class Foo
        def initialize
          @bar = :foo
        end
      
        def foo?
          @bar == :foo
        end
      end

      Good as well:

      class Foo
        def foo?
          bar == :foo
        end
      
        def bar
          @bar ||= :foo
        end
      end

      Bad:

      class Foo
        def go_foo!
          @bar = :foo
        end
      
        def foo?
          @bar == :foo
        end
      end

      Example

      Running Reek on:

      class Dummy
        def test
          @ivar
        end
      end

      would report:

      [1]:InstanceVariableAssumption: Dummy assumes too much for instance variable @ivar

      Note that this example would trigger this smell warning as well:

      class Parent
        def initialize(omg)
          @omg = omg
        end
      end
      
      class Child < Parent
        def foo
          @omg
        end
      end

      The way to address the smell warning is that you should create an attr_reader to use @omg in the subclass and not access @omg directly like this:

      class Parent
        attr_reader :omg
      
        def initialize(omg)
          @omg = omg
        end
      end
      
      class Child < Parent
        def foo
          omg
        end
      end

      Directly accessing instance variables is considered a smell because it breaks encapsulation and makes it harder to reason about code.

      If you don't want to expose those methods as public API just make them private like this:

      class Parent
        def initialize(omg)
          @omg = omg
        end
      
        private
        attr_reader :omg
      end
      
      class Child < Parent
        def foo
          omg
        end
      end

      Current Support in Reek

      An instance variable must:

      • be set in the constructor
      • or be accessed through a method with lazy initialization / memoization.

      If not, Instance Variable Assumption will be reported.

      OffersController#convert_params_for_graphql calls 'nested_values?(media_content.except(:content_type).to_h).include?(true)' 2 times
      Open

                  next unless nested_values?(media_content.except(:content_type).to_h).include?(true)
      
                  media_contents << media_content
                end
                content_block[:media_contents] = media_contents
      Severity: Minor
      Found in app/controllers/offers_controller.rb by reek

      Duplication occurs when two fragments of code look nearly identical, or when two fragments of code have nearly identical effects at some conceptual level.

      Reek implements a check for Duplicate Method Call.

      Example

      Here's a very much simplified and contrived example. The following method will report a warning:

      def double_thing()
        @other.thing + @other.thing
      end

      One quick approach to silence Reek would be to refactor the code thus:

      def double_thing()
        thing = @other.thing
        thing + thing
      end

      A slightly different approach would be to replace all calls of double_thing by calls to @other.double_thing:

      class Other
        def double_thing()
          thing + thing
        end
      end

      The approach you take will depend on balancing other factors in your code.

      OffersController#convert_params_for_graphql calls 'media_content.except(:content_type)' 2 times
      Open

                  next unless nested_values?(media_content.except(:content_type).to_h).include?(true)
      
                  media_contents << media_content
                end
                content_block[:media_contents] = media_contents
      Severity: Minor
      Found in app/controllers/offers_controller.rb by reek

      Duplication occurs when two fragments of code look nearly identical, or when two fragments of code have nearly identical effects at some conceptual level.

      Reek implements a check for Duplicate Method Call.

      Example

      Here's a very much simplified and contrived example. The following method will report a warning:

      def double_thing()
        @other.thing + @other.thing
      end

      One quick approach to silence Reek would be to refactor the code thus:

      def double_thing()
        thing = @other.thing
        thing + thing
      end

      A slightly different approach would be to replace all calls of double_thing by calls to @other.double_thing:

      class Other
        def double_thing()
          thing + thing
        end
      end

      The approach you take will depend on balancing other factors in your code.

      OffersController#new_generic_item doesn't depend on instance state (maybe move it to another class?)
      Open

          def new_generic_item
      Severity: Minor
      Found in app/controllers/offers_controller.rb by reek

      A Utility Function is any instance method that has no dependency on the state of the instance.

      OffersController#create has the variable name 'e'
      Open

          rescue Graphlient::Errors::GraphQLError => e
      Severity: Minor
      Found in app/controllers/offers_controller.rb by reek

      An Uncommunicative Variable Name is a variable name that doesn't communicate its intent well enough.

      Poor names make it hard for the reader to build a mental picture of what's going on in the code. They can also be mis-interpreted; and they hurt the flow of reading, because the reader must slow down to interpret the names.

      OffersController#update has the variable name 'e'
      Open

          rescue Graphlient::Errors::GraphQLError => e
      Severity: Minor
      Found in app/controllers/offers_controller.rb by reek

      An Uncommunicative Variable Name is a variable name that doesn't communicate its intent well enough.

      Poor names make it hard for the reader to build a mental picture of what's going on in the code. They can also be mis-interpreted; and they hurt the flow of reading, because the reader must slow down to interpret the names.

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

            content_block_params.each do |_key, content_block|
              next if content_block.blank?
      
              if content_block[:media_contents].present?
                media_contents = []
      Severity: Major
      Found in app/controllers/offers_controller.rb and 2 other locations - About 1 hr to fix
      app/controllers/jobs_controller.rb on lines 214..231
      app/controllers/news_items_controller.rb on lines 235..252

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

      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

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

            if @offer_params["media_contents"].present?
              media_contents = []
              @offer_params["media_contents"].each do |_key, media_content|
                next if media_content.blank?
                # content_type is always something (default: `image`), so we need to check all values
      Severity: Major
      Found in app/controllers/offers_controller.rb and 4 other locations - About 1 hr to fix
      app/controllers/constructions_controller.rb on lines 189..200
      app/controllers/deadlines_controller.rb on lines 223..234
      app/controllers/events_controller.rb on lines 371..382
      app/controllers/jobs_controller.rb on lines 236..247

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

      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

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

        def create
          query = create_or_update_mutation
          begin
            results = @smart_village.query query
          rescue Graphlient::Errors::GraphQLError => e
      Severity: Major
      Found in app/controllers/offers_controller.rb and 6 other locations - About 50 mins to fix
      app/controllers/constructions_controller.rb on lines 90..102
      app/controllers/events_controller.rb on lines 208..220
      app/controllers/jobs_controller.rb on lines 117..129
      app/controllers/point_of_interests_controller.rb on lines 172..184
      app/controllers/surveys_controller.rb on lines 48..60
      app/controllers/tours_controller.rb on lines 149..161

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

      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

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

        def destroy
          results = @smart_village.query <<~GRAPHQL
            mutation {
              destroyRecord(
                id: #{params["id"]},
      Severity: Major
      Found in app/controllers/offers_controller.rb and 5 other locations - About 35 mins to fix
      app/controllers/constructions_controller.rb on lines 120..139
      app/controllers/events_controller.rb on lines 239..258
      app/controllers/jobs_controller.rb on lines 147..166
      app/controllers/news_items_controller.rb on lines 175..194
      app/controllers/point_of_interests_controller.rb on lines 202..221

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

      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

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

            if @offer_params["dates"].present?
              dates = []
              @offer_params["dates"].each do |_key, date|
                next if date.blank?
                next unless nested_values?(date.to_h).include?(true)
      Severity: Major
      Found in app/controllers/offers_controller.rb and 10 other locations - About 30 mins to fix
      app/controllers/constructions_controller.rb on lines 231..239
      app/controllers/deadlines_controller.rb on lines 238..246
      app/controllers/events_controller.rb on lines 320..328
      app/controllers/events_controller.rb on lines 332..340
      app/controllers/events_controller.rb on lines 359..367
      app/controllers/events_controller.rb on lines 386..394
      app/controllers/jobs_controller.rb on lines 198..206
      app/controllers/jobs_controller.rb on lines 251..259
      app/controllers/news_items_controller.rb on lines 219..227
      app/controllers/surveys_controller.rb on lines 191..199

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

      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

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

        def update
          offer_id = params[:id]
      
          query = create_or_update_mutation(true)
          # logger.warn(query)
      Severity: Major
      Found in app/controllers/offers_controller.rb and 4 other locations - About 30 mins to fix
      app/controllers/constructions_controller.rb on lines 105..117
      app/controllers/jobs_controller.rb on lines 132..144
      app/controllers/point_of_interests_controller.rb on lines 187..199
      app/controllers/tours_controller.rb on lines 164..176

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

      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

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

        def edit
          results = @smart_village.query <<~GRAPHQL
            query {
              genericItem(
                id: #{params[:id]}
      Severity: Major
      Found in app/controllers/offers_controller.rb and 6 other locations - About 20 mins to fix
      app/controllers/constructions_controller.rb on lines 40..87
      app/controllers/deadlines_controller.rb on lines 42..100
      app/controllers/events_controller.rb on lines 45..205
      app/controllers/jobs_controller.rb on lines 40..114
      app/controllers/point_of_interests_controller.rb on lines 40..169
      app/controllers/tours_controller.rb on lines 40..146

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

      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

      Use the return of the conditional for variable assignment and comparison.
      Open

          if results.try(:data).try(:destroy_record).try(:status_code) == 200
            flash["notice"] = "Eintrag wurde gelöscht"
          else
            flash["notice"] = "Fehler: #{results.errors.inspect}"
          end

      Line is too long. [123/100]
      Open

                media_content["source_url"] = media_content.dig("source_url", "url").present? ? media_content["source_url"] : nil

      Missing top-level class documentation comment.
      Open

      class OffersController < ApplicationController

      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

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

            if @offer_params["operating_company"].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

      There are no issues that match your filters.

      Category
      Status