Skip to content

Commit 1cc15c0

Browse files
Hoc Survey 2014 Prize Code Claiming
1 parent a4f6337 commit 1cc15c0

File tree

2 files changed

+39
-0
lines changed

2 files changed

+39
-0
lines changed

pegasus/forms/hoc_survey_2014.rb

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
class HocSurvey2014
2+
3+
def self.claim_prize_code(type, user_id, params={})
4+
ip_address = params[:ip_address] || request.ip
5+
6+
begin
7+
rows_updated = DB[:hoc_survey_prizes].where(claimant:nil, type:type).limit(1).update(
8+
claimant:user_id,
9+
claimed_at:DateTime.now,
10+
claimed_ip:ip_address,
11+
)
12+
raise StandardError, "Out of '#{type}' codes." if rows_updated == 0
13+
rescue Sequel::UniqueConstraintViolation
14+
# This user has already claimed a prize, the query below will return that existing prize.
15+
rescue
16+
raise
17+
end
18+
19+
DB[:hoc_survey_prizes].where(claimant:user_id).first
20+
end
21+
22+
end
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
Sequel.migration do
2+
up do
3+
create_table?(:hoc_survey_prizes, charset:'utf8') do
4+
primary_key :id
5+
String :type, null:false, index:true
6+
String :value, null:false
7+
8+
foreign_key :claimant, unique:true, index:true
9+
DateTime :claimed_at
10+
String :claimed_ip, size:39
11+
end
12+
end
13+
14+
down do
15+
drop_table(:hoc_survey_prizes)
16+
end
17+
end

0 commit comments

Comments
 (0)