YaleSTC/shifts

View on GitHub
app/models/shift.rb

Summary

Maintainability
F
5 days
Test Coverage

File shift.rb has 453 lines of code (exceeds 250 allowed). Consider refactoring.
Open

class Shift < ActiveRecord::Base
  include ActionView::Helpers::DateHelper

  delegate :loc_group, to: 'location'
  belongs_to :calendar
Severity: Minor
Found in app/models/shift.rb - About 6 hrs to fix

    Class Shift has 47 methods (exceeds 20 allowed). Consider refactoring.
    Open

    class Shift < ActiveRecord::Base
      include ActionView::Helpers::DateHelper
    
      delegate :loc_group, to: 'location'
      belongs_to :calendar
    Severity: Minor
    Found in app/models/shift.rb - About 6 hrs to fix

      Method restrictions has a Cognitive Complexity of 19 (exceeds 5 allowed). Consider refactoring.
      Open

        def restrictions
          unless self.power_signed_up
            errors.add(:user, "is required") and return if self.user.nil?
            self.user.restrictions.each do |restriction|
              if restriction.max_hours
      Severity: Minor
      Found in app/models/shift.rb - About 2 hrs to fix

      Cognitive Complexity

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

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

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

      Further reading

      Method adjust_sub_requests has a Cognitive Complexity of 16 (exceeds 5 allowed). Consider refactoring.
      Open

        def adjust_sub_requests
          self.sub_requests.each do |sub|
            if sub.start > self.end || sub.end < self.start
              sub.destroy
            else
      Severity: Minor
      Found in app/models/shift.rb - About 2 hrs to fix

      Cognitive Complexity

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

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

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

      Further reading

      Method combine_with_surrounding_shifts has a Cognitive Complexity of 14 (exceeds 5 allowed). Consider refactoring.
      Open

        def combine_with_surrounding_shifts
          if (shift_later = Shift.where("start = ? AND user_id = ? AND location_id = ? AND calendars.active = ?", self.end, self.user_id, self.location_id, self.calendar.active?).includes(:calendar).first) && (!shift_later.has_sub?)
                if (self.report.nil? || self.report.departed.nil?) && (shift_later.report.nil?)
                  self.end = shift_later.end
                  shift_later.sub_requests.each { |s| s.shift = self }
      Severity: Minor
      Found in app/models/shift.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 does_not_exceed_max_concurrent_shifts_in_location has a Cognitive Complexity of 13 (exceeds 5 allowed). Consider refactoring.
      Open

        def does_not_exceed_max_concurrent_shifts_in_location
          if self.scheduled?
            max_concurrent = self.location.max_staff
            shifts = Shift.active.scheduled.in_location(self.location).overlaps(self.start, self.end)
            shifts.delete_if{|shift| shift.id = self.id} unless self.new_record?
      Severity: Minor
      Found in app/models/shift.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 delete_part_of_shift has a Cognitive Complexity of 11 (exceeds 5 allowed). Consider refactoring.
      Open

        def self.delete_part_of_shift(shift, start_of_delete, end_of_delete)
          #Used for taking sub requests
          if !(start_of_delete.between?(shift.start, shift.end) && end_of_delete.between?(shift.start, shift.end))
            raise "You can\'t delete more than the entire shift"
          elsif start_of_delete >= end_of_delete
      Severity: Minor
      Found in app/models/shift.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 make_future has 30 lines of code (exceeds 25 allowed). Consider refactoring.
      Open

        def self.make_future(event, location, wipe)
          cal = event.calendar
          if cal.active 
            shift_scope = Shift.active
          else
      Severity: Minor
      Found in app/models/shift.rb - About 1 hr to fix

        Method delete_part_of_shift has 27 lines of code (exceeds 25 allowed). Consider refactoring.
        Open

          def self.delete_part_of_shift(shift, start_of_delete, end_of_delete)
            #Used for taking sub requests
            if !(start_of_delete.between?(shift.start, shift.end) && end_of_delete.between?(shift.start, shift.end))
              raise "You can\'t delete more than the entire shift"
            elsif start_of_delete >= end_of_delete
        Severity: Minor
        Found in app/models/shift.rb - About 1 hr to fix

          Method obeys_signup_priority has a Cognitive Complexity of 9 (exceeds 5 allowed). Consider refactoring.
          Open

            def obeys_signup_priority
          
              return if (self.power_signed_up || !self.scheduled || !self.calendar.active)
          
              #check for all higher-priority locations in this loc group
          Severity: Minor
          Found in app/models/shift.rb - About 55 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 type has a Cognitive Complexity of 9 (exceeds 5 allowed). Consider refactoring.
          Open

            def type
              if self.start <= Time.now
                if self.missed
                  return "Missed"
                end
          Severity: Minor
          Found in app/models/shift.rb - About 55 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 exceeds_max_staff? has a Cognitive Complexity of 9 (exceeds 5 allowed). Consider refactoring.
          Open

            def exceeds_max_staff?
              count = 0
              shifts_in_period = []
              Shift.where(location_id: self.location_id, scheduled: true).each do |shift|
                shifts_in_period << shift if (self.start..self.end).to_a.overlaps((shift.start..shift.end).to_a) && self.end != shift.start && self.start != shift.end
          Severity: Minor
          Found in app/models/shift.rb - About 55 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 shift_is_within_time_slot has a Cognitive Complexity of 8 (exceeds 5 allowed). Consider refactoring.
          Open

            def shift_is_within_time_slot
              unless self.power_signed_up
                if (self.calendar.default || self.calendar.active)
                  c = TimeSlot.where("location_id = ? AND start <= ? AND end >= ? AND active  = ?", self.location_id, self.start, self.end, true).count
                else
          Severity: Minor
          Found in app/models/shift.rb - About 45 mins to fix

          Cognitive Complexity

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

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

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

          Further reading

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

            def css_class(current_user = nil)
              if current_user and self.user == current_user
                css_class = "user"
              else
                css_class = "shift"
          Severity: Minor
          Found in app/models/shift.rb - About 45 mins to fix

          Cognitive Complexity

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

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

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

          Further reading

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

            def self.check_for_conflicts(shifts, wipe, shift_scope)
              return "" if shifts.empty?
              table = Shift.arel_table
              shifts_with_conflict = Array.new
              shifts.each_slice(450) do |shs|
          Severity: Minor
          Found in app/models/shift.rb - About 35 mins to fix

          Cognitive Complexity

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

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

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

          Further reading

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

            def self.make_future(event, location, wipe)
              cal = event.calendar
              if cal.active 
                shift_scope = Shift.active
              else
          Severity: Minor
          Found in app/models/shift.rb - About 35 mins to fix

          Cognitive Complexity

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

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

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

          Further reading

          Avoid too many return statements within this method.
          Open

                return "Future"
          Severity: Major
          Found in app/models/shift.rb - About 30 mins to fix

            Avoid too many return statements within this method.
            Open

                  return "Completed"
            Severity: Major
            Found in app/models/shift.rb - About 30 mins to fix

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

                def user_does_not_have_concurrent_shift
                  c = Shift.for_user(self.user).in_department(self.department).overlaps(self.start, self.end)
                  if self.calendar.active
                    c = c.active
                  else
              Severity: Minor
              Found in app/models/shift.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

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

                def stats_display_left_early
                   str = "#{self.how_much_left_early} early. #{self.report.id}: #{start.to_s(:am_pm)} - #{self.end.to_s(:am_pm)}, #{user.name}, #{location.name}"
                   str << " <a href='mailto:#{user.email}?cc=#{user.default_department.department_config.stats_mailer_address}&subject=Left+Early+From+#{location.short_name}+Shift+&body=Hi%20#{user.goes_by}%2C%0A%0AYou%20left%20your%20#{location.short_name}%20shift%20early%20this%20week.%20What%20happened%3F%0A%0A%0ADeCaL'>[this week]</a>".html_safe
                   str << " <a href='mailto:#{user.email}?cc=#{user.default_department.department_config.stats_mailer_address}&subject=Left+Early+From+#{location.short_name}+Shift+&body=Hi%20#{user.goes_by}%2C%0A%0AYou%20left%20your%20#{location.short_name}%20shift%20early%20yesterday.%20What%20happened%3F%0A%0A%0ADeCaL'>[yesterday]</a>".html_safe
                   return str.html_safe
              Severity: Major
              Found in app/models/shift.rb and 1 other location - About 2 hrs to fix
              app/models/shift.rb on lines 356..360

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

              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 2 locations. Consider refactoring.
              Open

                def stats_display_late
                   str = "#{self.how_much_arrived_late} late. #{self.report.id}: #{start.to_s(:am_pm)} - #{self.end.to_s(:am_pm)}, #{user.name}, #{location.name}"
                   str << " <a href='mailto:#{user.email}?cc=#{user.default_department.department_config.stats_mailer_address}&subject=Late+#{location.short_name}+Shift+&body=Hi%20#{user.goes_by}%2C%0A%0AYou%20were%20late%20to%20your%20#{location.short_name}%20shift%20this%20week.%20What%20happened%3F%0A%0A%0ADeCaL'>[this week]</a>".html_safe
                   str << " <a href='mailto:#{user.email}?cc=#{user.default_department.department_config.stats_mailer_address}&subject=Late+#{location.short_name}+Shift+&body=Hi%20#{user.goes_by}%2C%0A%0AYou%20were%20late%20to%20your%20#{location.short_name}%20shift%20yesterday.%20What%20happened%3F%0A%0A%0ADeCaL'>[yesterday]</a>".html_safe
                   return str.html_safe
              Severity: Major
              Found in app/models/shift.rb and 1 other location - About 2 hrs to fix
              app/models/shift.rb on lines 363..367

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

              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 2 locations. Consider refactoring.
              Open

                    self.user.restrictions.each do |restriction|
                      if restriction.max_hours
                        relevant_shifts = Shift.between(restriction.starts,restriction.expires).for_user(self.user)
                        hours_sum = relevant_shifts.map{|shift| shift.end - shift.start}.flatten.sum / 3600.0
                        hours_sum += (self.end - self.start) / 3600.0
              Severity: Major
              Found in app/models/shift.rb and 1 other location - About 1 hr to fix
              app/models/shift.rb on lines 421..427

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

              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 2 locations. Consider refactoring.
              Open

                    self.location.restrictions.each do |restriction|
                      if restriction.max_hours
                        relevant_shifts = Shift.between(restriction.starts,restriction.expires).in_location(self.location)
                        hours_sum = relevant_shifts.map{|shift| shift.end - shift.start}.flatten.sum / 3600.0
                        hours_sum += (self.end - self.start) / 3600.0
              Severity: Major
              Found in app/models/shift.rb and 1 other location - About 1 hr to fix
              app/models/shift.rb on lines 411..417

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

              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 2 locations. Consider refactoring.
              Open

                  if conflict_msg.empty?
                    if shifts_all.map(&:valid?).all?
                      Shift.import shifts_all
                      return false
                    else
              Severity: Minor
              Found in app/models/shift.rb and 1 other location - About 55 mins to fix
              app/models/time_slot.rb on lines 55..64

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

              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 3 locations. Consider refactoring.
              Open

                def is_within_calendar
                  unless self.calendar.default
                    errors.add(:base, "Shift start and end dates must be within the range of the calendar.") if self.start.to_date < self.calendar.start_date.to_date || self.end.to_date > self.calendar.end_date.to_date
                  end
              Severity: Minor
              Found in app/models/shift.rb and 2 other locations - About 20 mins to fix
              app/models/repeating_event.rb on lines 185..188
              app/models/time_slot.rb on lines 143..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

              There are no issues that match your filters.

              Category
              Status