|
1class ConstituencyPetitionJournal < ActiveRecord::Base
|
|
|
|
3 belongs_to :constituency, primary_key: :external_id
|
|
|
|
5 validates :petition, presence: true
|
|
6 validates :constituency_id, presence: true, length: { maximum: 255 }
|
|
7 validates :signature_count, presence: true
|
|
|
|
9 delegate :name, :ons_code, :mp_name, to: :constituency
|
|
|
|
|
- UncommunicativeVariableName - has the variable name 'e' » reek
|
12 def for(petition, constituency_id)
|
|
|
|
14 find_or_create_by(petition: petition, constituency_id: constituency_id)
|
|
15 rescue ActiveRecord::RecordNotUnique => e
|
|
|
|
|
|
|
|
|
|
|
|
21 where(last_signed_at.lt(time).or(last_signed_at.eq(nil)))
|
|
|
|
|
|
|
|
25 order(signature_count: :desc)
|
|
|
|
|
|
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)
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
|
41 petition.constituency_petition_journals.older_than(petition.last_signed_at).delete_all
|
|
|
|
|
|
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)
|
|
|
|
|
|
|
|
50 def with_signatures_for(constituency_id)
|
|
51 where(arel_table[:signature_count].gt(0)).where(arel_table[:constituency_id].eq(constituency_id))
|
|
|
|
|
|
|
|
|
|
|
|
57 arel_table[:last_signed_at]
|
|
|
|
|
- NilCheck - performs a nil-check » reek
|
60 def unrecordable?(signature)
|
|
61 signature.nil? || signature.petition.nil? || signature.constituency_id.blank? || !signature.validated_at?
|
|
|
|
|
|
64 def signature_counts(petition, since = nil)
|
|
65 petition.signatures.validated_count_by_constituency_id(since, petition.last_signed_at)
|
|
|
|
|
|
|
- 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])
|
|
|
|
|
- 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])
|
|
|
|
|
|
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])
|
|
|
|
|
|
|
|
|
|
86 def update_all(updates)
|
|
87 self.class.unscoped.where(id: id).update_all(updates)
|
|
|
|
|