Updated

app/models / debate_outcome.rb

F
73 lines of codes
5 methods
11.1 complexity/method
11 churn
55.35 complexity
136 duplications
class DebateOutcome < ActiveRecord::Base
  1. DebateOutcome has no descriptive comment
# By default we want the user to upload a '2x' style image, and we can then # resize it down with Imagemagick COMMONS_IMAGE_SIZE = { w: 1260.0, h: 710.0 } belongs_to :petition, touch: true validates :petition, presence: true validates :debated_on, presence: true, if: :debated? validates :transcript_url, :video_url, :debate_pack_url, length: { maximum: 500 } has_attached_file :commons_image,
  1. Identical code found in 2 nodes Locations: 0 1
# default_url needs to be a lambda - this way the generated image url will # include any asset-digest default_url: ->(_) { ActionController::Base.helpers.image_url("graphics/graphic_house-of-commons.jpg") }, styles: { "1x": "#{(COMMONS_IMAGE_SIZE[:w]/2).to_i}x#{(COMMONS_IMAGE_SIZE[:h]/2).to_i}", "2x": "#{COMMONS_IMAGE_SIZE[:w]}x#{COMMONS_IMAGE_SIZE[:h]}" } validates_attachment_content_type :commons_image, content_type: /\Aimage\/.*\Z/ validate :validate_commons_image_dimensions, unless: :no_commons_image_queued after_create do petition.touch(:debate_outcome_at) unless petition.debate_outcome_at? end after_save do petition.update_columns(debate_state: debate_state) end def date debated_on end private def debate_state debated? ? 'debated' : 'not_debated' end def image_ratio(width, height)
  1. DebateOutcome#image_ratio doesn't depend on instance state (maybe move it to another class?)
(width.to_f / height.to_f).round(2) end def no_commons_image_queued commons_image.blank? || !commons_image.queued_for_write[:original] end def validate_commons_image_dimensions
  1. Identical code found in 2 nodes Locations: 0 1
  2. DebateOutcome#validate_commons_image_dimensions has a flog score of 40
  3. DebateOutcome#validate_commons_image_dimensions has approx 9 statements
# This should be tuned if the images start looking badly scaled max_ratio_delta = 0.1 dimensions = Paperclip::Geometry.from_file(commons_image.queued_for_write[:original].path) if dimensions.width < COMMONS_IMAGE_SIZE[:w]
  1. DebateOutcome#validate_commons_image_dimensions calls 'COMMONS_IMAGE_SIZE[:w]' 3 times Locations: 0 1 2
  2. DebateOutcome#validate_commons_image_dimensions calls 'dimensions.width' 3 times Locations: 0 1 2
errors.add(:commons_image, :too_narrow, width: dimensions.width, min_width: COMMONS_IMAGE_SIZE[:w])
  1. DebateOutcome#validate_commons_image_dimensions calls 'COMMONS_IMAGE_SIZE[:w]' 3 times Locations: 0 1 2
  2. DebateOutcome#validate_commons_image_dimensions calls 'dimensions.width' 3 times Locations: 0 1 2
end if dimensions.height < COMMONS_IMAGE_SIZE[:h]
  1. DebateOutcome#validate_commons_image_dimensions calls 'COMMONS_IMAGE_SIZE[:h]' 3 times Locations: 0 1 2
  2. DebateOutcome#validate_commons_image_dimensions calls 'dimensions.height' 3 times Locations: 0 1 2
errors.add(:commons_image, :too_short, height: dimensions.height, min_height: COMMONS_IMAGE_SIZE[:h])
  1. DebateOutcome#validate_commons_image_dimensions calls 'COMMONS_IMAGE_SIZE[:h]' 3 times Locations: 0 1 2
  2. DebateOutcome#validate_commons_image_dimensions calls 'dimensions.height' 3 times Locations: 0 1 2
end expected_ratio = image_ratio(COMMONS_IMAGE_SIZE[:w], COMMONS_IMAGE_SIZE[:h])
  1. DebateOutcome#validate_commons_image_dimensions calls 'COMMONS_IMAGE_SIZE[:h]' 3 times Locations: 0 1 2
  2. DebateOutcome#validate_commons_image_dimensions calls 'COMMONS_IMAGE_SIZE[:w]' 3 times Locations: 0 1 2
actual_ratio = image_ratio(dimensions.width, dimensions.height)
  1. DebateOutcome#validate_commons_image_dimensions calls 'dimensions.height' 3 times Locations: 0 1 2
  2. DebateOutcome#validate_commons_image_dimensions calls 'dimensions.width' 3 times Locations: 0 1 2
min_ratio = (expected_ratio - max_ratio_delta).round(2) max_ratio = (expected_ratio + max_ratio_delta).round(2) unless (min_ratio..max_ratio).include? actual_ratio errors.add(:commons_image, :incorrect_ratio, ratio: actual_ratio, min_ratio: min_ratio, max_ratio: max_ratio) end end end