Skip to content

Commit c8bd9c3

Browse files
authored
Merge pull request #869 from EFForg/fix_signatures_task
Fix signature cleanup task; add validations to prevent duplicates
2 parents 7c571e5 + 21ab743 commit c8bd9c3

File tree

7 files changed

+18
-23
lines changed

7 files changed

+18
-23
lines changed

app/controllers/concerns/tooling.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ def create_partner_subscription
77
return unless @action_page
88
@action_page.partners.each do |partner|
99
if params["#{partner.code}_subscribe"] == "1"
10-
Subscription.create!(partner_signup_params.merge(partner: partner))
10+
Subscription.create(partner_signup_params.merge(partner: partner))
1111
end
1212
end
1313
end

app/models/signature.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ class Signature < ActiveRecord::Base
1313
validates_presence_of :country_code, if: :location_required?
1414

1515
validates :email, email: true
16+
validates :email, uniqueness: { scope: :petition_id,
17+
message: "You've already signed this petition!" }
1618
validates :zipcode, length: { maximum: 12 }
1719
validate :country_code, :arbitrary_opinion_of_country_string_validity
1820

app/models/subscription.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
class Subscription < ActiveRecord::Base
22
belongs_to :partner, counter_cache: true
33
validates :email, presence: true, email: true
4+
validates :email, uniqueness: { scope: :partner_id }
45
end

docker/crontab

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
0 * * * * root su -s/bin/bash www-data -c '. /var/www/.profile && cd /opt/actioncenter && bundle exec rake signatures:deduplicate' >>/proc/1/fd/1 2>&1
21
0 0 * * * root su -s/bin/bash www-data -c '. /var/www/.profile && cd /opt/actioncenter && bundle exec rake congress:update' >>/proc/1/fd/1 2>&1
32
0 0 * * * root su -s/bin/bash www-data -c '. /var/www/.profile && cd /opt/actioncenter && bundle exec rake db:sessions:trim' >>/proc/1/fd/1 2>&1
43

lib/tasks/signatures.rake

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,16 +19,20 @@ namespace :signatures do
1919

2020
desc "Deduplicate signatures from petitions and subscriptions from partners"
2121
task deduplicate: :environment do
22-
sig_dups = Signature.group(:petition_id, :email).having("count(*) > 1")
23-
sig_dups.pluck(:petition_id, :email).each do |petition_id, email|
24-
ids = Signature.where(petition_id: petition_id, email: email).order(:created_at).ids
25-
Signature.where(id: ids[1..-1]).delete_all
22+
sig_dups = Signature.group(:email, :petition_id).count.map do |data, count|
23+
data if count > 1
24+
end.compact
25+
sig_dups.each do |email, petition_id|
26+
Signature.where(petition_id: petition_id, email: email)
27+
.order(:created_at).drop(1).map(&:delete)
2628
end
2729

28-
sub_dups = Subscription.group(:partner_id, :email).having("count(*) > 1")
29-
sub_dups.pluck(:partner_id, :email).each do |partner_id, email|
30+
sub_dups = Subscription.group(:email, :partner_id).count.map do |data, count|
31+
data if count > 1
32+
end.compact
33+
sub_dups.each do |email, partner_id|
3034
Subscription.where(partner_id: partner_id, email: email)
31-
.order(:created_at)[1..-1].each(&:delete)
35+
.order(:created_at).drop(1).map(&:delete)
3236
end
3337
end
3438
end

spec/controllers/admin/petitions_controller_spec.rb

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -34,19 +34,7 @@
3434
describe "DELETE #destroy_signatures" do
3535
let(:petition) { FactoryGirl.create(:petition) }
3636
let(:signatures) do
37-
30.times.map do
38-
petition.signatures.create(
39-
first_name: "Save Kittens",
40-
last_name: "Save kittens in great detail",
41-
42-
country_code: "US",
43-
zipcode: "94109",
44-
street_address: "815 Eddy Street",
45-
city: "San Francisco",
46-
state: "CA",
47-
anonymous: false
48-
)
49-
end
37+
30.times.map { FactoryGirl.create(:signature, petition: petition) }
5038
end
5139

5240
it "should delete signatures from the signature_ids param" do

spec/tasks/signatures_spec.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,8 @@
4141
FactoryGirl.create(:subscription, partner: partner)
4242
end
4343
let!(:dup_subscription) do
44-
FactoryGirl.create(:subscription, email: email, partner: partner)
44+
FactoryGirl.create(:subscription)
45+
.update_columns(email: email, partner_id: partner.id)
4546
end
4647

4748
it "removes the newer duplicates" do

0 commit comments

Comments
 (0)