|
1class CountryPetitionJournal < ActiveRecord::Base
|
|
|
|
3 belongs_to :location, foreign_key: :location_code, primary_key: :code
|
|
|
|
5 validates :petition, presence: true
|
|
6 validates :location, presence: true
|
|
7 validates :signature_count, presence: true
|
|
|
|
9 delegate :name, :code, to: :location
|
|
|
|
|
- UncommunicativeVariableName - has the variable name 'e' » reek
|
12 def for(petition, location_code)
|
|
|
|
14 find_or_create_by(petition: petition, location_code: location_code)
|
|
15 rescue ActiveRecord::RecordNotUnique => e
|
|
|
|
|
|
|
|
|
|
|
|
21 where(last_signed_at.lt(time).or(last_signed_at.eq(nil)))
|
|
|
|
|
|
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)
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
|
37 petition.country_petition_journals.older_than(petition.last_signed_at).delete_all
|
|
|
|
|
|
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)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
49 arel_table[:last_signed_at]
|
|
|
|
|
- NilCheck - performs a nil-check » reek
|
52 def unrecordable?(signature)
|
|
53 signature.nil? || signature.petition.nil? || signature.location_code.blank? || !signature.validated_at?
|
|
|
|
|
|
56 def signature_counts(petition, since = nil)
|
|
57 petition.signatures.validated_count_by_location_code(since, petition.last_signed_at)
|
|
|
|
|
|
|
- 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])
|
|
|
|
|
- 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])
|
|
|
|
|
|
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])
|
|
|
|
|
|
|
|
|
|
78 def update_all(updates)
|
|
79 self.class.unscoped.where(id: id).update_all(updates)
|
|
|
|
|