Updated

app/models / constituency_petition_journal.rb

D
89 lines of codes
14 methods
7.5 complexity/method
16 churn
105.57 complexity
93 duplications
class ConstituencyPetitionJournal < ActiveRecord::Base
  1. ConstituencyPetitionJournal has no descriptive comment
belongs_to :petition belongs_to :constituency, primary_key: :external_id validates :petition, presence: true validates :constituency_id, presence: true, length: { maximum: 255 } validates :signature_count, presence: true delegate :name, :ons_code, :mp_name, to: :constituency class << self def for(petition, constituency_id)
  1. Similar code found in 2 nodes Locations: 0 1
begin find_or_create_by(petition: petition, constituency_id: constituency_id) rescue ActiveRecord::RecordNotUnique => e
  1. ConstituencyPetitionJournal#for has the variable name 'e'
retry end end def older_than(time) where(last_signed_at.lt(time).or(last_signed_at.eq(nil))) end def ordered order(signature_count: :desc) end def increment_signature_counts_for(petition, since)
  1. Similar code found in 2 nodes Locations: 0 1
signature_counts(petition, since).each do |constituency_id, count| next if constituency_id.blank? self.for(petition, constituency_id).increment_signature_count(count, petition) end end def reset_signature_counts_for(petition)
  1. Similar code found in 2 nodes Locations: 0 1
signature_counts(petition).each do |constituency_id, count| next if constituency_id.blank? self.for(petition, constituency_id).reset_signature_count(count, petition) end petition.constituency_petition_journals.older_than(petition.last_signed_at).delete_all end def invalidate_signature_for(signature, now = Time.current)
  1. Similar code found in 2 nodes Locations: 0 1
unless unrecordable?(signature) self.for(signature.petition, signature.constituency_id).decrement_signature_count(now) end end def with_signatures_for(constituency_id) where(arel_table[:signature_count].gt(0)).where(arel_table[:constituency_id].eq(constituency_id)) end private def last_signed_at arel_table[:last_signed_at] end def unrecordable?(signature)
  1. Similar code found in 2 nodes Locations: 0 1
signature.nil? || signature.petition.nil? || signature.constituency_id.blank? || !signature.validated_at?
  1. ConstituencyPetitionJournal#unrecordable? performs a nil-check
end def signature_counts(petition, since = nil) petition.signatures.validated_count_by_constituency_id(since, petition.last_signed_at) end end def increment_signature_count(count, petition) sql = "signature_count = signature_count + ?, last_signed_at = ?, updated_at = ?" update_all([sql, count, petition.last_signed_at, petition.updated_at])
  1. ConstituencyPetitionJournal#increment_signature_count refers to 'petition' more than self (maybe move it to another class?)
end def reset_signature_count(count, petition) sql = "signature_count = ?, last_signed_at = ?, updated_at = ?" update_all([sql, count, petition.last_signed_at, petition.updated_at])
  1. ConstituencyPetitionJournal#reset_signature_count refers to 'petition' more than self (maybe move it to another class?)
end def decrement_signature_count(now = Time.current, count = 1) sql = "signature_count = greatest(signature_count - ?, 0), updated_at = ?" update_all([sql, count, now]) end private def update_all(updates) self.class.unscoped.where(id: id).update_all(updates) end end