Class: Effective::Membership

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
app/models/effective/membership.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#current_actionObject

Returns the value of attribute current_action.



6
7
8
# File 'app/models/effective/membership.rb', line 6

def current_action
  @current_action
end

Class Method Details

.acts_as_archived_owner_klassesObject



146
147
148
# File 'app/models/effective/membership.rb', line 146

def self.acts_as_archived_owner_klasses
  owner_klasses.select { |klass| klass.try(:acts_as_archived?) }
end

.effective_membership?Boolean

Returns:

  • (Boolean)


137
138
139
# File 'app/models/effective/membership.rb', line 137

def self.effective_membership?
  true
end

.max_numberObject



133
134
135
# File 'app/models/effective/membership.rb', line 133

def self.max_number
  maximum('number_as_integer') || 0
end

.owner_klassesObject



141
142
143
144
# File 'app/models/effective/membership.rb', line 141

def self.owner_klasses
  klasses = Effective::Membership.distinct(:owner_type).pluck(:owner_type)
  klasses.select { |klass| klass.safe_constantize }.map { |klass| klass.constantize }
end

Instance Method Details

#build_membership_category(category:) ⇒ Object

find or build



214
215
216
# File 'app/models/effective/membership.rb', line 214

def build_membership_category(category:)
  membership_category(category: category) || membership_categories.build(category: category)
end

#build_membership_status(status:) ⇒ Object

find or build



265
266
267
# File 'app/models/effective/membership.rb', line 265

def build_membership_status(status:)
  membership_status(status: status) || membership_statuses.build(status: status)
end

#categoriesObject

We can’t use the polymorphic has_many. So this is a helper.



170
171
172
# File 'app/models/effective/membership.rb', line 170

def categories
  membership_categories.reject(&:marked_for_destruction?).map(&:category)
end

#categories=(categories) ⇒ Object



178
179
180
181
182
183
184
185
186
187
188
189
190
191
# File 'app/models/effective/membership.rb', line 178

def categories=(categories)
  categories = Array(categories)
  raise('expecting a membership category') if categories.any? { |cat| !cat.class.respond_to?(:effective_memberships_category?) }

  # Delete any removed categories
  membership_categories.each do |membership_category|
    membership_category.mark_for_destruction unless categories.include?(membership_category.category)
  end

  # Build any additional categories
  categories.each { |category| build_membership_category(category: category) }

  nil
end

#categories_sentenceObject



204
205
206
# File 'app/models/effective/membership.rb', line 204

def categories_sentence
  categories.map(&:to_s).to_sentence.presence || 'None'
end

#categoryObject

We might want to use singular memberships.



194
195
196
197
# File 'app/models/effective/membership.rb', line 194

def category
  raise('expected singular usage but there are more than one membership category') if categories.length > 1
  categories.first
end

#category_idObject



199
200
201
202
# File 'app/models/effective/membership.rb', line 199

def category_id
  raise('expected singular usage but there are more than one membership category') if categories.length > 1
  categories.first.id
end

#category_idsObject



174
175
176
# File 'app/models/effective/membership.rb', line 174

def category_ids
  membership_categories.reject(&:marked_for_destruction?).map(&:category_id)
end

#change_fees_paid_periodObject



302
303
304
# File 'app/models/effective/membership.rb', line 302

def change_fees_paid_period
  fees_paid_period
end

#change_fees_paid_period=(date) ⇒ Object



306
307
308
309
310
311
312
313
314
315
316
317
# File 'app/models/effective/membership.rb', line 306

def change_fees_paid_period=(date)
  if date.blank?
    return assign_attributes(fees_paid_period: nil, fees_paid_through_period: nil)
  end

  date = (date.respond_to?(:strftime) ? date : Date.parse(date))

  period = EffectiveMemberships.Registrar.period(date: date)
  period_end_on = EffectiveMemberships.Registrar.period_end_on(date: date)

  assign_attributes(fees_paid_period: period, fees_paid_through_period: period_end_on)
end

#fees_paid?Boolean

Returns:

  • (Boolean)


281
282
283
# File 'app/models/effective/membership.rb', line 281

def fees_paid?
  paid_fees_through?(EffectiveMemberships.Registrar.current_period)
end

#in_good_standing?Boolean

Returns:

  • (Boolean)


277
278
279
# File 'app/models/effective/membership.rb', line 277

def in_good_standing?
  membership_statuses.none? { |ms| ms.status.not_in_good_standing? }
end

#membership_category(category:) ⇒ Object



208
209
210
211
# File 'app/models/effective/membership.rb', line 208

def membership_category(category:)
  raise('expected a category') unless category.class.respond_to?(:effective_memberships_category?)
  membership_categories.find { |mc| mc.category_id == category.id && mc.category_type == category.class.name }
end

#membership_status(status:) ⇒ Object



259
260
261
262
# File 'app/models/effective/membership.rb', line 259

def membership_status(status:)
  raise('expected a status') unless status.class.respond_to?(:effective_memberships_status?)
  membership_statuses.find { |ms| ms.status_id == status.id && ms.status_type == status.class.name }
end

#not_in_good_standing?Boolean

Returns:

  • (Boolean)


273
274
275
# File 'app/models/effective/membership.rb', line 273

def not_in_good_standing?
  membership_statuses.any? { |ms| ms.status.not_in_good_standing? }
end

Returns:

  • (Boolean)


285
286
287
288
289
290
# File 'app/models/effective/membership.rb', line 285

def paid_fees_through?(period = nil)
  period ||= EffectiveMemberships.Registrar.current_period

  return false if fees_paid_period.blank?
  fees_paid_period >= period
end

#reportable_scopesObject

effective_reports



40
41
42
43
44
45
46
47
48
49
50
51
# File 'app/models/effective/membership.rb', line 40

def reportable_scopes
  {
    with_category: :string,
    with_status: :string,
    joined_before: :date,
    joined_after: :date,
    with_paid_fees_through: :date,
    with_unpaid_fees_through: :date,
    in_good_standing: nil,
    not_in_good_standing: nil
  }
end

#reregistered?Boolean

Returns:

  • (Boolean)


269
270
271
# File 'app/models/effective/membership.rb', line 269

def reregistered?
  registration_on.present? && joined_on.present? && registration_on > joined_on
end

#revise!Object

Admin updating membership info



320
321
322
323
324
325
326
327
328
329
330
331
# File 'app/models/effective/membership.rb', line 320

def revise!
  save!

  period = EffectiveMemberships.Registrar.current_period
  return true if paid_fees_through?(period)

  # Otherwise build fees right now
  EffectiveMemberships.Registrar.delete_fees!(self)
  EffectiveMemberships.Registrar.create_fees!(self, period: period, force: true)

  true
end

#statusObject

We might want to use singular memberships.



249
250
251
252
# File 'app/models/effective/membership.rb', line 249

def status
  raise('expected singular usage but there are more than one membership status') if statuses.length > 1
  statuses.first
end

#status_idObject



254
255
256
257
# File 'app/models/effective/membership.rb', line 254

def status_id
  raise('expected singular usage but there are more than one membership status') if statuses.length > 1
  statuses.first.id
end

#status_idsObject



225
226
227
# File 'app/models/effective/membership.rb', line 225

def status_ids
  membership_statuses.reject(&:marked_for_destruction?).map(&:status_id)
end

#statusesObject

We can’t use the polymorphic has_many. So this is a helper.



221
222
223
# File 'app/models/effective/membership.rb', line 221

def statuses
  membership_statuses.reject(&:marked_for_destruction?).map(&:status)
end

#statuses=(statuses) ⇒ Object



229
230
231
232
233
234
235
236
237
238
239
240
241
242
# File 'app/models/effective/membership.rb', line 229

def statuses=(statuses)
  statuses = Array(statuses)
  raise('expecting a membership category') if statuses.any? { |status| !status.class.respond_to?(:effective_memberships_status?) }

  # Delete any removed statuses
  membership_statuses.each do |membership_status|
    membership_status.mark_for_destruction unless statuses.include?(membership_status.status)
  end

  # Build any additional statuses
  statuses.each { |status| build_membership_status(status: status) }

  nil
end

#statuses_sentenceObject



244
245
246
# File 'app/models/effective/membership.rb', line 244

def statuses_sentence
  statuses.map(&:to_s).to_sentence.presence || 'None'
end

#to_sObject



150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
# File 'app/models/effective/membership.rb', line 150

def to_s
  return model_name.human if owner.blank?

  summary = [
    owner.to_s,
    'is',
    (statuses.to_sentence if statuses.present?),
    (categories.to_sentence if categories.present?),
    'member',
    ("##{number_was}" if number_was.present?),
    "who joined #{joined_on&.strftime('%F') || '-'}",
    ("and last changed on #{registration_on.strftime('%F')}" if registration_on > joined_on)
  ].compact.join(' ')

  (summary + '.').html_safe
end

#unpaid_fees_through?(period = nil) ⇒ Boolean

Returns:

  • (Boolean)


292
293
294
295
296
297
298
299
300
# File 'app/models/effective/membership.rb', line 292

def unpaid_fees_through?(period = nil)
  period ||= EffectiveMemberships.Registrar.current_period

  return false if joined_on.blank?
  return false unless joined_on < period

  return true if fees_paid_period.blank?
  fees_paid_period < period
end