Updated

app/models / country_petition_journal.rb

D
81 lines of codes
12 methods
7.4 complexity/method
17 churn
88.87 complexity
93 duplications
class CountryPetitionJournal < ActiveRecord::Base
  1. CountryPetitionJournal has no descriptive comment
belongs_to :petition belongs_to :location, foreign_key: :location_code, primary_key: :code validates :petition, presence: true validates :location, presence: true validates :signature_count, presence: true delegate :name, :code, to: :location class << self def for(petition, location_code)
  1. Similar code found in 2 nodes Locations: 0 1
begin find_or_create_by(petition: petition, location_code: location_code) rescue ActiveRecord::RecordNotUnique => e
  1. CountryPetitionJournal#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 increment_signature_counts_for(petition, since)
  1. Similar code found in 2 nodes Locations: 0 1
signature_counts(petition, since).each do |location_code, count| next if location_code.blank? self.for(petition, location_code).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 |location_code, count| next if location_code.blank? self.for(petition, location_code).reset_signature_count(count, petition) end petition.country_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.location_code).decrement_signature_count(now) end 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.location_code.blank? || !signature.validated_at?
  1. CountryPetitionJournal#unrecordable? performs a nil-check
end def signature_counts(petition, since = nil) petition.signatures.validated_count_by_location_code(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. CountryPetitionJournal#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. CountryPetitionJournal#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