forked from ruby-no-kai/sponsor-app
-
Notifications
You must be signed in to change notification settings - Fork 4
/
sponsorship.rb
287 lines (228 loc) · 8.4 KB
/
sponsorship.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
require 'securerandom'
class Sponsorship < ApplicationRecord
include EditingHistoryTarget
belongs_to :conference
belongs_to :organization
belongs_to :plan, optional: true
has_many :contacts, dependent: :destroy
has_one :contact, -> { where(kind: :primary) }, class_name: 'Contact'
has_one :alternate_billing_contact, -> { where(kind: :billing) }, class_name: 'Contact'
has_many :requests, dependent: :destroy, class_name: 'SponsorshipRequest'
has_one :billing_request, -> { where(kind: :billing) }, class_name: 'SponsorshipRequest'
has_one :customization_request, -> { where(kind: :customization) }, class_name: 'SponsorshipRequest'
has_one :note, -> { where(kind: :note) }, class_name: 'SponsorshipRequest'
has_one :asset_file, class_name: 'SponsorshipAssetFile', dependent: :destroy
def asset_file_id; self.asset_file&.id; end
def asset_file_id=(other)
self.asset_file = SponsorshipAssetFile.find_by(id: other.to_i)
end
# XXX: this can only hold a value, because authorization is mandatory in a controller
def asset_file_id_to_copy; @asset_file_id_to_copy; end
def asset_file_id_to_copy=(id)
raise "asset_file_id_to_copy= cannot be used for a persisted record" if id && self.persisted?
@asset_file_id_to_copy = id
end
has_many :staff_notes, class_name: 'SponsorshipStaffNote', dependent: :destroy
has_one :exhibition
has_many :broadcast_deliveries, dependent: :nullify
has_many :tickets, dependent: :destroy
has_many :tito_discount_codes, dependent: :destroy
def tito_attendee_discount_code
@tito_attendee_discount_code ||= tito_discount_codes.where(kind: 'attendee').first
end
def tito_booth_staff_discount_code
@tito_booth_staff_discount_code ||= tito_discount_codes.where(kind: 'booth_staff').first
end
scope :active, -> { where(withdrawn_at: nil).where.not(accepted_at: nil) }
scope :exhibitor, -> { where(booth_assigned: true) }
scope :plan_determined, -> { where.not(plan_id: nil) }
scope :withdrawn, -> { where.not(withdrawn_at: nil) }
scope :not_withdrawn, -> { where(withdrawn_at: nil) }
scope :accepted, -> { where.not(accepted_at: nil) }
scope :have_presence, -> { where(suspended: false).merge(Sponsorship.active).merge(Sponsorship.plan_determined) }
scope :includes_contacts, -> { includes(:contact, :alternate_billing_contact) }
scope :includes_requests, -> { includes(:billing_request, :customization_request, :note) }
validates :organization, presence: true, uniqueness: {scope: :conference_id}
validates :contact, presence: true
validates :name, presence: true
validates :url, presence: true
validates :profile, presence: true
validates :asset_file, presence: true
validates :ticket_key, presence: true
validates_numericality_of :number_of_additional_attendees, allow_nil: true, greater_than_or_equal_to: 0, only_integer: true
validate :validate_correct_plan
validate :validate_plan_change, on: :update_by_user
validate :validate_plan_availability, on: :update_by_user
validate :validate_booth_eligibility, on: :update_by_user
validate :validate_word_count, on: :update_by_user
validate :validate_no_plan_allowance, on: :update_by_user
validate :policy_agreement
accepts_nested_attributes_for :contact, allow_destroy: true,reject_if: -> (attrs) { attrs['kind'].present? }
accepts_nested_attributes_for :alternate_billing_contact, allow_destroy: true, reject_if: -> (attrs) { attrs['kind'].present? }
accepts_nested_attributes_for :billing_request, reject_if: -> (attrs) { attrs['kind'].present? }
accepts_nested_attributes_for :customization_request, reject_if: -> (attrs) { attrs['kind'].present? }
accepts_nested_attributes_for :note, reject_if: -> (attrs) { attrs['kind'].present? }
before_validation :generate_ticket_key
def build_nested_attributes_associations
self.build_contact unless self.contact
self.build_alternate_billing_contact unless self.alternate_billing_contact
self.build_billing_request unless self.billing_request
self.build_customization_request unless self.customization_request
self.build_note unless self.note
end
def active?
accepted? && !withdrawn?
end
def accept
self.accepted_at = Time.zone.now
end
def accepted?
!!accepted_at
end
def accepted
accepted?
end
def accepted=(x)
case
when !x || x == '0'
self.accepted_at = nil
when x && !accepted?
self.accepted_at = Time.zone.now
else
accepted?
end
end
def withdrawn?
!!withdrawn_at
end
def customized?
customization && customization_name.present?
end
def customization_planned?
!customization && customization_name.present?
end
def plan_name
customized? ? (customization_name || plan&.name) : plan&.name
end
def slug
self.organization&.slug
end
def word_count
profile&.scan(/\w+/)&.size || 0
end
def policy_agreement
return @policy_agreement if defined? @policy_agreement
@policy_agreement = self.persisted?
end
def policy_agreement=(other)
@policy_agreement = !!other
end
def billing_contact
alternate_billing_contact || contact
end
def assume_organization
self.organization = Organization.create_with(name: self.name).find_or_initialize_by(domain: self.contact&.email&.split(?@, 2).last)
end
def to_h_for_history
{
"conference_id" => conference&.id,
"contact" => contact&.as_json&.slice("id", "name", "email", "email_cc", "organization", "unit", "address"),
"alternate_billing_contact" => alternate_billing_contact&.as_json&.slice("id", "name", "email", "email_cc", "organization", "unit", "address"),
"billing_request" => billing_request&.body,
"plan_id" => plan&.id,
"plan_name" => plan&.name,
"plan_display_name" => plan_name,
"customization_name" => customization_name,
"customized?" => customized?,
"suspended?" => suspended?,
"customization_planned?" => customization_planned?,
"customization_request" => customization_request&.body,
"booth_requested" => booth_requested,
"booth_assigned" => booth_assigned,
"name" => name,
"url" => url,
"profile" => profile,
"organization_id" => organization&.id,
"organization_name" => organization&.name,
"locale" => locale,
"asset_file_id" => asset_file&.id,
"note" => note&.body,
"number_of_additional_attendees" => number_of_additional_attendees,
"accepted_at" => accepted_at,
}.tap do |h|
h["withdrawn_at"] = withdrawn_at if withdrawn_at
end
end
def total_number_of_attendees
if active?
(plan&.number_of_guests || 0) + (number_of_additional_attendees || 0)
else
0
end
end
def total_number_of_booth_staff
(active? && booth_assigned?) ? 4 : 0 # FIXME:
end
def number_of_registered_attendees
tickets.where.not(kind: :attendee, checked_in_at: nil).count
end
def booth_size
plan&.booth_size
end
def assigned_booth_size
booth_assigned? ? (plan&.booth_size || 0) : 0
end
def exhibitor?
booth_assigned?
end
def withdraw
self.withdrawn_at = Time.zone.now
self.booth_assigned = false
self.plan = nil
return self
end
private
def validate_correct_plan
if plan && plan.conference_id != self.conference_id
errors.add :plan, "can't have a plan for an another conference"
end
end
def validate_plan_availability
if plan && plan_id_changed? && !plan.available?
errors.add :plan, :unavailable
end
end
def validate_plan_change
if plan_id_changed? && plan_id_was && accepted?
errors.add :plan, :unchangeable_after_finalization
end
end
def validate_policy_agreement
if !policy_agreement
errors.add :policy_agreement, "must agree with the policy"
end
end
def validate_booth_eligibility
if booth_requested && !(plan&.booth_eligible?)
errors.add :booth_requested, :not_eligible
end
end
def validate_word_count
limit = plan&.words_limit_hard
if limit && word_count > limit
errors.add :profile, :too_long, maximum: (plan.words_limit || 0)
end
end
def validate_no_plan_allowance
if !plan_id && conference && !conference.no_plan_allowed
errors.add :plan, :no_plan_not_allowed
end
end
def generate_ticket_key
if self.ticket_key.blank?
begin
self.ticket_key = SecureRandom.urlsafe_base64(64)
end while self.class.where(conference: conference, ticket_key: self.ticket_key).exists?
end
end
end