1class ConstituencyPetitionJournal < ActiveRecord::Base
 
2  belongs_to :petition
 
3  belongs_to :constituency, primary_key: :external_id
 
4
 
5  validates :petition, presence: true
 
6  validates :constituency_id, presence: true, length: { maximum: 255 }
 
7  validates :signature_count, presence: true
 
8
 
 9  delegate :name, :ons_code, :mp_name, to: :constituency
 
 
11  class << self
  • UncommunicativeVariableName - has the variable name 'e' » reek
12    def for(petition, constituency_id)
 
13      begin
 
14        find_or_create_by(petition: petition, constituency_id: constituency_id)
 
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 ordered
 
25      order(signature_count: :desc)
 
26    end
 
 
28    def increment_signature_counts_for(petition, since)
 
29      signature_counts(petition, since).each do |constituency_id, count|
 
30        next if constituency_id.blank?
 
31        self.for(petition, constituency_id).increment_signature_count(count, petition)
 
32      end
 
33    end
 
 
35    def reset_signature_counts_for(petition)
 
36      signature_counts(petition).each do |constituency_id, count|
 
37        next if constituency_id.blank?
 
38        self.for(petition, constituency_id).reset_signature_count(count, petition)
 
39      end
 
 
41      petition.constituency_petition_journals.older_than(petition.last_signed_at).delete_all
 
42    end
 
 
44    def invalidate_signature_for(signature, now = Time.current)
 
45      unless unrecordable?(signature)
 
46        self.for(signature.petition, signature.constituency_id).decrement_signature_count(now)
 
47      end
 
48    end
 
 
50    def with_signatures_for(constituency_id)
 
51      where(arel_table[:signature_count].gt(0)).where(arel_table[:constituency_id].eq(constituency_id))
 
52    end
 
 
54    private
 
 
56    def last_signed_at
 
57      arel_table[:last_signed_at]
 
58    end
 
  • NilCheck - performs a nil-check » reek
60    def unrecordable?(signature)
 
61      signature.nil? || signature.petition.nil? || signature.constituency_id.blank? || !signature.validated_at?
 
62    end
 
 
64    def signature_counts(petition, since = nil)
 
65      petition.signatures.validated_count_by_constituency_id(since, petition.last_signed_at)
 
66    end
 
67  end
 
  • FeatureEnvy - refers to 'petition' more than self (maybe move it to another class?) » reek
69  def increment_signature_count(count, petition)
 
70    sql = "signature_count = signature_count + ?, last_signed_at = ?, updated_at = ?"
 
71    update_all([sql, count, petition.last_signed_at, petition.updated_at])
 
72  end
 
  • FeatureEnvy - refers to 'petition' more than self (maybe move it to another class?) » reek
74  def reset_signature_count(count, petition)
 
75    sql = "signature_count = ?, last_signed_at = ?, updated_at = ?"
 
76    update_all([sql, count, petition.last_signed_at, petition.updated_at])
 
77  end
 
 
79  def decrement_signature_count(now = Time.current, count = 1)
 
80    sql = "signature_count = greatest(signature_count - ?, 0), updated_at = ?"
 
81    update_all([sql, count, now])
 
82  end
 
 
84  private
 
 
86  def update_all(updates)
 
87    self.class.unscoped.where(id: id).update_all(updates)
 
88  end
 
89end