1# rake data:generate |
|
2# Petition state PET_STATE=rejected default open |
|
3# Petition count PET_COUNT=20 default 100 |
|
4# Signature count SIG_COUNT=25 default 100 |
|
5# Random response RANDOM_RESP=true default false |
|
6# If true will create 10K sigs for every 5th petition and add response |
|
8namespace :data do |
|
9 desc "Generate random petitions with signatures. PET_STATE=open, PET_COUNT=100, SIG_COUNT=100" |
|
10 task :generate => :environment do |
|
11 require 'faker' |
|
13 POSTCODES = ['SE58BB', 'IG110UD', 'IG110FX', 'W1F7HS', 'RM9 8PD']; |
|
14 REJECTION_CODES = ["no-action", "irrelevant", "honours", "no-action", "duplicate"] |
|
15 HIDDEN_CODES = ["libellous", "offensive"] |
|
16 VALID_STATES = ['open', 'closed', 'rejected', 'hidden'] |
|
18 PETITION_STATE = ENV.fetch('PET_STATE', 'open') |
|
19 PETITION_COUNT = ENV.fetch('PET_COUNT', '100') |
|
20 SIGNATURE_COUNT = ENV.fetch('SIG_COUNT', '100') |
|
21 RANDOM_RESPONSE = ENV.fetch('RANDOM_RESP', 'false') |
|
24 if VALID_STATES.exclude?(PETITION_STATE) |
|
25 raise "** #{PETITION_STATE} is not a valid state within #{VALID_STATES.inspect} **" |
|
26 end |
|
28 ActiveRecord::Base.transaction do |
|
29 PETITION_COUNT.to_i.times do |idx| |
|
30 @signature_count = SIGNATURE_COUNT |
|
32 petition = Petition.create!({ |
|
33 action: Faker::Lorem.sentence(rand(3..10)).first(80), |
|
34 background: Faker::Lorem.sentence(rand(7..22)).first(200), |
|
35 additional_details: Faker::Lorem.paragraph(rand(2..20)).first(500), |
|
36 creator: Signature.new({ |
|
37 uk_citizenship: '1', |
|
38 name: Faker::Name.name, |
|
39 email: Faker::Internet.safe_email, |
|
40 location_code: 'GB', |
|
41 state: 'validated', |
|
42 postcode: POSTCODES.sample, |
|
43 creator: true |
|
44 })
|
|
45 })
|
|
48 # Create the sponsor signatures |
|
49 5.times do |
|
50 petition.sponsors.create!(
|
|
51 uk_citizenship: '1', |
|
52 name: Faker::Name.name, |
|
53 email: Faker::Internet.safe_email("#{Faker::Lorem.characters(rand(10..40))}-#{rand(1..999999)}"), |
|
54 location_code: 'GB', |
|
55 state: 'validated', |
|
56 postcode: POSTCODES.sample |
|
57 )
|
|
58 end |
|
60 # Update to specified state requested |
|
61 case PETITION_STATE |
|
62 when 'open' |
|
63 petition.update_attributes(state: 'open', open_at: Time.now) |
|
64 when 'closed' |
|
65 petition.update_attributes(state: 'closed', open_at: Time.now - 1.year, closed_at: Time.now - 1.day) |
|
66 when 'rejected' |
|
67 petition.update_attributes(
|
|
68 state: 'rejected', |
|
69 open_at: Time.now - 6.month, |
|
70 closed_at: Time.now + 6.month, |
|
71 rejection_text: Faker::Lorem.paragraph(rand(10..30)), |
|
72 rejection_code: REJECTION_CODES.sample |
|
73 )
|
|
74 when 'hidden' |
|
75 petition.update_attributes(
|
|
76 state: 'hidden', |
|
77 open_at: Time.now - 6.month, |
|
78 closed_at: Time.now + 6.month, |
|
79 rejection_text: Faker::Lorem.paragraph(rand(10..30)), |
|
80 rejection_code: HIDDEN_CODES.sample |
|
81 )
|
|
82 end |
|
85 # Should we create a petition with response and 10K signatures |
|
86 @should_create_response = (((idx+1) % 5) == 0 && RANDOM_RESPONSE == 'true') |
|
87 @signature_count = Site.threshold_for_response + 1 if @should_create_response |
|
89 @signature_count.to_i.times do |
|
90 signature = petition.signatures.create!(
|
|
91 uk_citizenship: '1', |
|
92 name: Faker::Name.name, |
|
93 email: Faker::Internet.safe_email("#{Faker::Lorem.characters(rand(10..40))}-#{rand(1..999999)}"), |
|
94 location_code: 'GB', |
|
95 postcode: POSTCODES.sample |
|
96 )
|
|
97 signature.validate!
|
|
98 end |
|
100 # Add responses on random petitions when 10,000 signatures |
|
101 petition.update_attributes(
|
|
102 response: Faker::Lorem.paragraph(rand(10..30)) |
|
103 ) if @should_create_response |
|
104 end |
|
105 end |
|
106 end |
|
107end |