1class CountryPetitionJournal < ActiveRecord::Base
 
2  belongs_to :petition
 
3  belongs_to :location, foreign_key: :location_code, primary_key: :code
 
4
 
5  validates :petition, presence: true
 
6  validates :location, presence: true
 
7  validates :signature_count, presence: true
 
8
 
 9  delegate :name, :code, to: :location
 
 
11  class << self
  • UncommunicativeVariableName - has the variable name 'e' » reek
12    def for(petition, location_code)
 
13      begin
 
14        find_or_create_by(petition: petition, location_code: location_code)
 
15      rescue ActiveRecord::RecordNotUnique => e
 
16        retry
 
17      end
 
18    end
 
 
20    def older_than(time)
 
21      where(last_signed_at.lt(time).or(last_signed_at.eq(nil)))
 
22    end
 
 
24    def increment_signature_counts_for(petition, since)
 
25      signature_counts(petition, since).each do |location_code, count|
 
26        next if location_code.blank?
 
27        self.for(petition, location_code).increment_signature_count(count, petition)
 
28      end
 
29    end
 
 
31    def reset_signature_counts_for(petition)
 
32      signature_counts(petition).each do |location_code, count|
 
33        next if location_code.blank?
 
34        self.for(petition, location_code).reset_signature_count(count, petition)
 
35      end
 
 
37      petition.country_petition_journals.older_than(petition.last_signed_at).delete_all
 
38    end
 
 
40    def invalidate_signature_for(signature, now = Time.current)
 
41      unless unrecordable?(signature)
 
42        self.for(signature.petition, signature.location_code).decrement_signature_count(now)
 
43      end
 
44    end
 
 
46    private
 
 
48    def last_signed_at
 
49      arel_table[:last_signed_at]
 
50    end
 
  • NilCheck - performs a nil-check » reek
52    def unrecordable?(signature)
 
53      signature.nil? || signature.petition.nil? || signature.location_code.blank? || !signature.validated_at?
 
54    end
 
 
56    def signature_counts(petition, since = nil)
 
57      petition.signatures.validated_count_by_location_code(since, petition.last_signed_at)
 
58    end
 
59  end
 
  • FeatureEnvy - refers to 'petition' more than self (maybe move it to another class?) » reek
61  def increment_signature_count(count, petition)
 
62    sql = "signature_count = signature_count + ?, last_signed_at = ?, updated_at = ?"
 
63    update_all([sql, count, petition.last_signed_at, petition.updated_at])
 
64  end
 
  • FeatureEnvy - refers to 'petition' more than self (maybe move it to another class?) » reek
66  def reset_signature_count(count, petition)
 
67    sql = "signature_count = ?, last_signed_at = ?, updated_at = ?"
 
68    update_all([sql, count, petition.last_signed_at, petition.updated_at])
 
69  end
 
 
71  def decrement_signature_count(now = Time.current, count = 1)
 
72    sql = "signature_count = greatest(signature_count - ?, 0), updated_at = ?"
 
73    update_all([sql, count, now])
 
74  end
 
 
76  private
 
 
78  def update_all(updates)
 
79    self.class.unscoped.where(id: id).update_all(updates)
 
80  end
 
81end