Module: EffectiveMembershipsRegistrar

Extended by:
ActiveSupport::Concern
Included in:
Effective::Registrar
Defined in:
app/models/concerns/effective_memberships_registrar.rb

Overview

EffectiveMembershipsRegistrar

This is different cause its not an ActiveRecord one

Mark your registrar with include EffectiveMembershipsRegistrar

Mark your category model with effective_memberships_category to get all the includes

Defined Under Namespace

Modules: ClassMethods

Instance Method Summary collapse

Instance Method Details

#advance_period(period:, number:) ⇒ Object

These two could be overridden if we do non 1-year periods



82
83
84
# File 'app/models/concerns/effective_memberships_registrar.rb', line 82

def advance_period(period:, number:)
  period.advance(years: number).beginning_of_year
end

#assign!(owner, categories:, statuses: nil, date: nil, number: nil) ⇒ Object

Category is required, statuses are optional



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
# File 'app/models/concerns/effective_memberships_registrar.rb', line 137

def assign!(owner, categories:, statuses: nil, date: nil, number: nil)
  categories = Array(categories)
  statuses = Array(statuses)

  raise('expecting a memberships owner') unless owner.class.respond_to?(:effective_memberships_owner?)
  raise('expecting a membership category') if categories.blank? || categories.any? { |cat| !cat.class.respond_to?(:effective_memberships_category?) }
  raise('expecting an membership status (optional)') if statuses.present? && statuses.any? { |status| !status.class.respond_to?(:effective_memberships_status?) }

  # Default Date and next number
  date ||= Time.zone.now
  period = period(date: date)
  period_end_on = period_end_on(date: date)

  # Find or build a membership
  membership = owner.membership || owner.build_membership

  # Assign Dates
  membership.joined_on ||= date  # Only if not already present

  # Assign Number (this could be nil)
  number = next_membership_number(owner, to: categories.first) if number.blank?
  membership.number ||= number
  membership.number_as_integer ||= (Integer(number) rescue nil)

  # Builds the membership_categories and membership_statuses. Marks for destruction ones that don't exist.
  membership.assign_attributes(statuses: statuses, categories: categories)

  changed = membership.membership_categories.any? { |mc| mc.new_record? || mc.marked_for_destruction? }
  changed ||= membership.membership_statuses.any? { |ms| ms.new_record? || ms.marked_for_destruction? }

  if changed
    membership.registration_on = date # Always new registration_on

    owner.build_membership_history(start_on: date)
    owner.save!
  end

  # Assign member role
  add_member_role(owner)

  owner.update_membership_status!
end

#build_reclassify_fees(owner:, membership:, to:, from:, period:, date:) ⇒ Object



281
282
283
284
285
286
287
288
289
290
291
292
# File 'app/models/concerns/effective_memberships_registrar.rb', line 281

def build_reclassify_fees(owner:, membership:, to:, from:, period:, date:)
  existing = owner.membership_period_fee(category: from, period: period)

  if existing
    [
      owner.build_prorated_fee(date: date),
      owner.build_discount_fee(date: date, from: from)
    ]
  else
    owner.build_renewal_fee(category: to, period: period)
  end
end

#build_register_fees(owner:, membership:, date:) ⇒ Object



232
233
234
# File 'app/models/concerns/effective_memberships_registrar.rb', line 232

def build_register_fees(owner:, membership:, date:)
  owner.build_prorated_fee(date: date)
end

#build_reinstate_fees(owner:, membership:, date:) ⇒ Object



356
357
358
# File 'app/models/concerns/effective_memberships_registrar.rb', line 356

def build_reinstate_fees(owner:, membership:, date:)
  owner.build_prorated_fee(date: date)
end

#create_all_fees!(period: nil, force: false) ⇒ Object

This is intended to be run once per day in a rake task rake effective_memberships:create_fees Create Renewal and Late fees Assigns NIGS



516
517
518
519
520
521
522
523
524
525
# File 'app/models/concerns/effective_memberships_registrar.rb', line 516

def create_all_fees!(period: nil, force: false)
  period ||= current_period
  memberships = Effective::Membership.deep.with_unpaid_fees_through(period)

  memberships.find_each do |membership|
    create_fees!(membership, period: period, force: force); GC.start
  end

  true
end

#create_early_fees!(membership, period:) ⇒ Object



577
578
579
580
581
582
583
584
585
586
# File 'app/models/concerns/effective_memberships_registrar.rb', line 577

def create_early_fees!(membership, period:)
  membership.categories.select(&:create_early_fees?).map do |category|
    fee = membership.owner.build_early_fee(category: category, period: period)
    next if fee.blank? || fee.persisted?

    puts("Created early fee for #{membership.owner}") if debug?

    fee.save!
  end
end

#create_fees!(resource, period: nil, force: false) ⇒ Object



527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
# File 'app/models/concerns/effective_memberships_registrar.rb', line 527

def create_fees!(resource, period: nil, force: false)
  today = Time.zone.now.beginning_of_day
  period ||= current_period

  membership = (resource.class.respond_to?(:effective_memberships_owner?) ? resource.membership : resource)
  raise('expected a membership') unless membership.present? && membership.kind_of?(Effective::Membership)

  # Create Renewal Fees
  if force || today == renewal_fee_date(date: today)
    create_renewal_fees!(membership, period: period)
  end

  # Create Early Fees
  if force || today == early_fee_date(period: period)
    create_early_fees!(membership, period: period)
  end

  # Destroy Early Fees
  if force || today == early_fee_end_date(period: period)
    destroy_early_fees!(membership, period: period)
  end

  # Create Late Fees
  if force || today == late_fee_date(period: period)
    create_late_fees!(membership, period: period)
  end

  # Update Membership Status - Assign Not In Good Standing
  if force || today == not_in_good_standing_date(period: period)
    create_not_in_good_standing!(membership, period: period)
  end

  true
end

#create_late_fees!(membership, period:) ⇒ Object



599
600
601
602
603
604
605
606
607
608
# File 'app/models/concerns/effective_memberships_registrar.rb', line 599

def create_late_fees!(membership, period:)
  membership.categories.select(&:create_late_fees?).map do |category|
    fee = membership.owner.build_late_fee(category: category, period: period)
    next if fee.blank? || fee.persisted?

    puts("Created late fee for #{membership.owner}") if debug?

    fee.save!
  end
end

#create_not_in_good_standing!(membership, period:) ⇒ Object



610
611
612
613
614
# File 'app/models/concerns/effective_memberships_registrar.rb', line 610

def create_not_in_good_standing!(membership, period:)
  return unless membership.categories.any?(&:create_not_in_good_standing?)

  membership.owner.update_membership_status!
end

#create_renewal_fees!(membership, period:) ⇒ Object



562
563
564
565
566
567
568
569
570
571
572
573
574
575
# File 'app/models/concerns/effective_memberships_registrar.rb', line 562

def create_renewal_fees!(membership, period:)
  membership.categories.select(&:create_renewal_fees?).map do |category|
    existing = membership.owner.membership_period_fee(category: category, period: period, except: 'Renewal')
    next if existing.present? # This might be an existing Prorated fee

    fee = membership.owner.build_renewal_fee(category: category, period: period)
    raise("expected build_renewal_fee to return a fee for period #{period}") unless fee.kind_of?(Effective::Fee)
    next if fee.blank? || fee.persisted?

    puts("Created renewal fee for #{membership.owner}") if debug?

    fee.save!
  end
end

#current_periodObject



90
91
92
# File 'app/models/concerns/effective_memberships_registrar.rb', line 90

def current_period
  period(date: Time.zone.now)
end

#delete_fees!(resource) ⇒ Object

Called by applicant.approve!



498
499
500
501
502
503
504
505
506
507
508
509
510
# File 'app/models/concerns/effective_memberships_registrar.rb', line 498

def delete_fees!(resource)
  owner = (resource.class.respond_to?(:effective_memberships_owner?) ? resource : resource.try(:owner))
  raise('expecting a memberships owner') unless owner.class.respond_to?(:effective_memberships_owner?)

  # Delete unpurchased fees and orders
  owner.outstanding_fee_payment_fees.each { |fee| fee.mark_for_destruction }
  owner.outstanding_fee_payment_orders.each { |order| order.mark_for_destruction }
  owner.save!

  owner.outstanding_fee_payments.each { |fee_payment| fee_payment.destroy! }

  true
end

#destroy_early_fees!(membership, period:) ⇒ Object



588
589
590
591
592
593
594
595
596
597
# File 'app/models/concerns/effective_memberships_registrar.rb', line 588

def destroy_early_fees!(membership, period:)
  membership.categories.select(&:create_early_fees?).map do |category|
    fee = membership.owner.build_early_fee(category: category, period: period)
    next if fee.blank? || fee.early? || fee.purchased? || fee.deferred?

    puts("Destroyed early fee for #{membership.owner}") if debug?

    fee.destroy!
  end
end

#early_fee_date(period:) ⇒ Object

Assumed to be the same as renewal_fee_date



41
42
43
44
# File 'app/models/concerns/effective_memberships_registrar.rb', line 41

def early_fee_date(period:)
  Time.zone.local(period.year - 1, 12, 1) # Fees roll over every December 1st
  raise('to be implemented by app registrar')
end

#early_fee_date_range(period:) ⇒ Object



132
133
134
# File 'app/models/concerns/effective_memberships_registrar.rb', line 132

def early_fee_date_range(period:)
  (early_fee_date(period: period).beginning_of_day)..(early_fee_end_date(period: period).beginning_of_day)
end

#early_fee_end_date(period:) ⇒ Object

Assumed to be just before the period actually starts



47
48
49
50
# File 'app/models/concerns/effective_memberships_registrar.rb', line 47

def early_fee_end_date(period:)
  Time.zone.local(period.year - 1, 12, 31) # Early fees are destroyed on December 31st
  raise('to be implemented by app registrar')
end

#fee_payment_purchased!(owner) ⇒ Object

Called in the after_purchase of fee payment



617
618
619
620
# File 'app/models/concerns/effective_memberships_registrar.rb', line 617

def fee_payment_purchased!(owner)
  raise('expecting a memberships owner') unless owner.class.respond_to?(:effective_memberships_owner?)
  owner.update_membership_status! if owner.membership.present?
end

#fees_paid!(owner, date: nil, order_attributes: nil) ⇒ Object



460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
# File 'app/models/concerns/effective_memberships_registrar.rb', line 460

def fees_paid!(owner, date: nil, order_attributes: nil)
  raise('expecting a memberships owner') unless owner.class.respond_to?(:effective_memberships_owner?)
  raise('expected a member') unless owner.membership.present?
  raise('expected a Hash of attributes') if order_attributes.present? && !order_attributes.kind_of?(Hash)

  # Date
  date ||= Time.zone.now

  period = period(date: date)
  period_end_on = period_end_on(date: date)

  if owner.outstanding_fee_payment_fees.present?
    order = Effective::Order.new(items: owner.outstanding_fee_payment_fees, user: owner)
    order.assign_attributes(order_attributes) if order_attributes.present?
    order.mark_as_purchased!
  end

  owner.update_membership_status!
  owner.membership.update!(fees_paid_period: period, fees_paid_through_period: period_end_on)
end

#in_good_standing!(owner, date: nil, notes: nil) ⇒ Object



452
453
454
455
456
457
458
# File 'app/models/concerns/effective_memberships_registrar.rb', line 452

def in_good_standing!(owner, date: nil, notes: nil)
  raise('expecting a memberships owner') unless owner.class.respond_to?(:effective_memberships_owner?)
  raise('expected a member') unless owner.membership.present?
  raise('expected owner to be not in good standing') unless owner.membership.not_in_good_standing?

  status_remove!(owner, status: not_in_good_standing_status!, date: date, notes: notes)
end

#last_periodObject



94
95
96
# File 'app/models/concerns/effective_memberships_registrar.rb', line 94

def last_period
  advance_period(period: current_period, number: -1)
end

#late_fee_date(period:) ⇒ Object



26
27
28
29
# File 'app/models/concerns/effective_memberships_registrar.rb', line 26

def late_fee_date(period:)
  Time.zone.local(period.year, 2, 1) # Fees are late after February 1st.
  raise('to be implemented by app registrar')
end

#min_late_durationObject

A renewal fee must be at least this old to be considered late? Must be less than (late_fee_date - renewal_fee_date) but this is not enforced



54
55
56
# File 'app/models/concerns/effective_memberships_registrar.rb', line 54

def min_late_duration
  60.days
end

#min_not_in_good_standing_durationObject

A renewal fee must be at least this old to be considered nigs? Must be less than (nigs_date - renewal_fee_date) but this is not enforced



60
61
62
# File 'app/models/concerns/effective_memberships_registrar.rb', line 60

def min_not_in_good_standing_duration
  60.days
end

#next_membership_number(owner, to:) ⇒ Object



481
482
483
484
485
486
487
488
489
490
491
492
493
494
# File 'app/models/concerns/effective_memberships_registrar.rb', line 481

def next_membership_number(owner, to:)
  raise('expecting a memberships owner') unless owner.class.respond_to?(:effective_memberships_owner?)

  categories = Array(to)
  raise('expecting a membership category') unless categories.all? { |cat| cat.class.respond_to?(:effective_memberships_category?) }

  return nil if categories.none?(&:membership_number_required?)

  # Just a simple number right now
  number = (Effective::Membership.all.max_number || 0) + 1

  # Returns a string
  number.to_s
end

#not_in_good_standing!(owner, date: nil, notes: nil) ⇒ Object



444
445
446
447
448
449
450
# File 'app/models/concerns/effective_memberships_registrar.rb', line 444

def not_in_good_standing!(owner, date: nil, notes: nil)
  raise('expecting a memberships owner') unless owner.class.respond_to?(:effective_memberships_owner?)
  raise('expected a member') unless owner.membership.present?
  raise('expected owner to be in good standing') unless owner.membership.in_good_standing?

  status_add!(owner, status: not_in_good_standing_status!, date: date, notes: notes)
end

#not_in_good_standing_date(period:) ⇒ Object



31
32
33
34
# File 'app/models/concerns/effective_memberships_registrar.rb', line 31

def not_in_good_standing_date(period:)
  Time.zone.local(period.year, 3, 1) # Membership in bad standing after March 1st
  raise('to be implemented by app registrar')
end

#not_in_good_standing_statusObject

scope



65
66
67
# File 'app/models/concerns/effective_memberships_registrar.rb', line 65

def not_in_good_standing_status
  EffectiveMemberships.Status.where(title: 'Not In Good Standing')
end

#not_in_good_standing_status!Object



69
70
71
# File 'app/models/concerns/effective_memberships_registrar.rb', line 69

def not_in_good_standing_status!
  not_in_good_standing_status.first!
end

#period(date:) ⇒ Object

Returns a date of Jan 1, Year



99
100
101
102
103
# File 'app/models/concerns/effective_memberships_registrar.rb', line 99

def period(date:)
  cutoff = renewal_fee_date(date: date) # period_end_on
  period = (date < cutoff) ? advance_period(period: date, number: 0) : advance_period(period: date, number: 1)
  period.to_date
end

#period_end_on(date:) ⇒ Object



86
87
88
# File 'app/models/concerns/effective_memberships_registrar.rb', line 86

def period_end_on(date:)
  period(date: date).end_of_year
end

#periods(from:, to: nil) ⇒ Object

This is only used for a form collection on admin memberships And now for ASPB applicant reinstatement fees



107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'app/models/concerns/effective_memberships_registrar.rb', line 107

def periods(from:, to: nil)
  to ||= Time.zone.now

  raise('expected to date') unless to.respond_to?(:strftime)
  raise('expected from date') unless from.respond_to?(:strftime)

  from = period(date: from)
  to = period(date: to)

  retval = []

  loop do
    retval << from
    from = advance_period(period: from, number: 1)
    break if from > to
  end

  retval
end

#periods_collection(from: nil, to: nil) ⇒ Object



127
128
129
130
# File 'app/models/concerns/effective_memberships_registrar.rb', line 127

def periods_collection(from: nil, to: nil)
  from ||= Time.zone.now - 10.years
  periods(from: from, to: to).reverse.map { |period| [period_end_on(date: period), period] }
end

#reclassify!(owner, to:, status: nil, date: nil, number: nil, skip_fees: false) ⇒ Object



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
# File 'app/models/concerns/effective_memberships_registrar.rb', line 236

def reclassify!(owner, to:, status: nil, date: nil, number: nil, skip_fees: false)
  raise('expecting a memberships owner') unless owner.class.respond_to?(:effective_memberships_owner?)
  raise('owner must have an existing membership. use register! instead') if owner.membership.blank?

  from = owner.membership.category

  raise('expecting a to memberships category') unless to.class.respond_to?(:effective_memberships_category?)
  raise('expecting a from memberships category') unless from.class.respond_to?(:effective_memberships_category?)
  raise('expecting a memberships status') unless status.nil? || status.class.respond_to?(:effective_memberships_status?)
  raise('expected to and from to be different') if from == to

  date ||= Time.zone.now
  period = period(date: date)

  # Existing Membership
  membership = owner.membership

  # Last Changed Date
  membership.registration_on = date

  # Assign Number (this could be nil)
  number = next_membership_number(owner, to: to) if number.nil?

  if number.present?
    membership.number = number
    membership.number_as_integer = (Integer(number) rescue nil)
  end

  # Assign Category
  membership.build_membership_category(category: to)
  membership.membership_category(category: from).mark_for_destruction

  # Assign Status
  membership.build_membership_status(status: status) if status.present?

  unless skip_fees
    fees = build_reclassify_fees(owner: owner, membership: membership, to: to, from: from, period: period, date: date)
    raise('unexpected purchased fee') if Array(fees).compact.any?(&:purchased?)
  end

  # Save owner
  owner.build_membership_history(start_on: date)
  owner.save!
end

#register!(owner, to:, status: nil, date: nil, number: nil, skip_fees: false) ⇒ Object



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
# File 'app/models/concerns/effective_memberships_registrar.rb', line 180

def register!(owner, to:, status: nil, date: nil, number: nil, skip_fees: false)
  raise('expecting a memberships owner') unless owner.class.respond_to?(:effective_memberships_owner?)
  raise('expecting a memberships category') unless to.class.respond_to?(:effective_memberships_category?)
  raise('expecting a memberships status') unless status.nil? || status.class.respond_to?(:effective_memberships_status?)
  raise('owner has existing membership. use reclassify! instead.') if owner.membership.present?

  # Default Date and next number
  date ||= Time.zone.now
  period = period(date: date)
  period_end_on = period_end_on(date: date)

  # Build a membership
  membership = owner.build_membership

  # Assign Dates
  membership.joined_on ||= date  # Only if not already present
  membership.registration_on = date  # Always new registration_on

  # Assign Number (this could be nil)
  number = next_membership_number(owner, to: to) if number.nil?

  if number.present?
    membership.number = number
    membership.number_as_integer = (Integer(number) rescue nil)
  end

  # Assign Category
  membership.build_membership_category(category: to)

  # Assign Status
  membership.build_membership_status(status: status) if status.present?

  # Assign fees paid through period
  if skip_fees
    membership.fees_paid_period = period
    membership.fees_paid_through_period = period_end_on
  end

  # Or, Build Fees
  unless skip_fees
    fees = build_register_fees(owner: owner, membership: membership, date: date)
    raise('unexpected purchased fee') if Array(fees).compact.any?(&:purchased?)
  end

  # Assign member role
  add_member_role(owner)

  # Save owner
  owner.build_membership_history(start_on: date)
  owner.save!
end

#reinstate!(owner, to: nil, date: nil, number: nil, skip_fees: false) ⇒ Object



294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
# File 'app/models/concerns/effective_memberships_registrar.rb', line 294

def reinstate!(owner, to: nil, date: nil, number: nil, skip_fees: false)
  raise('expecting a memberships owner') unless owner.class.respond_to?(:effective_memberships_owner?)

  history = owner.reinstatement_membership_history || owner.membership_histories.last
  raise('owner must have a removed membership history or some kind of history.') if history.blank?

  to ||= owner.reinstatement_membership_category
  raise('expecting a to memberships category') unless to.class.respond_to?(:effective_memberships_category?)

  statuses = Array(owner.reinstatement_membership_statuses)

  # Default Date
  date ||= Time.zone.now
  period = period(date: date)
  period_end_on = period_end_on(date: date)

  # Build a membership
  membership = owner.build_membership

  # Assign Dates
  membership.joined_on = date
  membership.registration_on = date

  # Assign Old Number (this could be nil)
  number = history.number if history.number.present?

  # Assign Number (this could be nil)
  number = next_membership_number(owner, to: to) if number.nil?

  if number.present?
    membership.number = number
    membership.number_as_integer = (Integer(number) rescue nil)
  end

  # Assign Category
  membership.build_membership_category(category: to)

  # Assign Statuses
  statuses.each do |status|
    membership.build_membership_status(status: status)
  end

  # Assign fees paid through period
  if skip_fees
    membership.fees_paid_period = period
    membership.fees_paid_through_period = period_end_on
  end

  # Or, Build Fees
  unless skip_fees
    build_reinstate_fees(owner: owner, membership: membership, date: date)
    # This might already be present and purchased if joined and reinstated in the same period
  end

  # Assign member role
  add_member_role(owner)

  # Save owner
  owner.build_membership_history(start_on: date)
  owner.save!
end

#remove!(owner, statuses: nil, date: nil, notes: nil) ⇒ Object

When you remove, you can set a status(es) which may affect reinstatement applications



414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
# File 'app/models/concerns/effective_memberships_registrar.rb', line 414

def remove!(owner, statuses: nil, date: nil, notes: nil)
  statuses = Array(statuses)

  raise('expecting a memberships owner') unless owner.class.respond_to?(:effective_memberships_owner?)
  raise('expected a member with a membership') unless owner.membership.present?
  raise('expecting an membership status (optional)') if statuses.present? && statuses.any? { |status| !status.class.respond_to?(:effective_memberships_status?) }

  # Date
  date ||= Time.zone.now

  # Delete unpurchased fees and orders
  owner.outstanding_fee_payment_fees.each { |fee| fee.mark_for_destruction }
  owner.outstanding_fee_payment_orders.each { |order| order.mark_for_destruction }

  # Remove member role
  remove_member_role(owner)

  # Remove Membership
  owner.membership.mark_for_destruction

  # We do the save a bit differently here because we want to set the removed statuses
  history = owner.build_membership_history(start_on: date, notes: notes)

  if statuses.present?
    history.assign_attributes(statuses: statuses.map(&:to_s), status_ids: statuses.map(&:id))
  end

  owner.save!
end

#renewal_fee_date(date:) ⇒ Object



21
22
23
24
# File 'app/models/concerns/effective_memberships_registrar.rb', line 21

def renewal_fee_date(date:)
  Time.zone.local(date.year, 12, 1) # Fees roll over every December 1st
  raise('to be implemented by app registrar')
end

#resigned_statusObject



73
74
75
# File 'app/models/concerns/effective_memberships_registrar.rb', line 73

def resigned_status
  EffectiveMemberships.Status.where(title: 'Resigned')
end

#resigned_status!Object



77
78
79
# File 'app/models/concerns/effective_memberships_registrar.rb', line 77

def resigned_status!
  resigned_status.first!
end

#status_add!(owner, status:, date: nil, notes: nil) ⇒ Object



367
368
369
370
371
372
# File 'app/models/concerns/effective_memberships_registrar.rb', line 367

def status_add!(owner, status:, date: nil, notes: nil)
  raise('expecting a memberships owner') unless owner.class.respond_to?(:effective_memberships_owner?)
  raise('owner must have an existing membership. use register! instead') if owner.membership.blank?

  status_assign!(owner, date: date, notes: notes, status: owner.membership.statuses + Array(status))
end

#status_assign!(owner, status:, date: nil, notes: nil) ⇒ Object



381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
# File 'app/models/concerns/effective_memberships_registrar.rb', line 381

def status_assign!(owner, status:, date: nil, notes: nil)
  raise('expecting a memberships owner') unless owner.class.respond_to?(:effective_memberships_owner?)

  membership = owner.membership
  raise('owner must have an existing membership. use register! instead') if membership.blank?

  statuses = Array(status)
  raise('expected a memberships status') unless statuses.blank? || statuses.all? { |status| status.class.respond_to?(:effective_memberships_status?) }

  # Add
  statuses.each do |status|
    next if membership.membership_status(status: status).present?
    membership.build_membership_status(status: status)
  end

  # Remove
  membership.statuses.each do |existing|
    next if statuses.include?(existing)
    membership.membership_status(status: existing).mark_for_destruction
  end

  changed = membership.membership_statuses.any? { |ms| ms.new_record? || ms.marked_for_destruction? }

  if changed
    # Save owner
    owner.build_membership_history(start_on: date, notes: notes)
    owner.save!
  end

  true
end

#status_clear!(owner, date: nil) ⇒ Object



360
361
362
363
364
365
# File 'app/models/concerns/effective_memberships_registrar.rb', line 360

def status_clear!(owner, date: nil)
  raise('expecting a memberships owner') unless owner.class.respond_to?(:effective_memberships_owner?)
  raise('owner must have an existing membership. use register! instead') if owner.membership.blank?

  status_assign!(owner, date: date, status: [])
end

#status_remove!(owner, status:, date: nil, notes: nil) ⇒ Object



374
375
376
377
378
379
# File 'app/models/concerns/effective_memberships_registrar.rb', line 374

def status_remove!(owner, status:, date: nil, notes: nil)
  raise('expecting a memberships owner') unless owner.class.respond_to?(:effective_memberships_owner?)
  raise('owner must have an existing membership. use register! instead') if owner.membership.blank?

  status_assign!(owner, date: date, notes: notes, status: owner.membership.statuses - Array(status))
end