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



127
128
129
# File 'app/models/effective/membership.rb', line 127

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

.effective_membership?Boolean

Returns:

  • (Boolean)


118
119
120
# File 'app/models/effective/membership.rb', line 118

def self.effective_membership?
  true
end

.max_numberObject



114
115
116
# File 'app/models/effective/membership.rb', line 114

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

.owner_klassesObject



122
123
124
125
# File 'app/models/effective/membership.rb', line 122

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



180
181
182
# File 'app/models/effective/membership.rb', line 180

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

#build_membership_status(status:) ⇒ Object

find or build



216
217
218
# File 'app/models/effective/membership.rb', line 216

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.



151
152
153
# File 'app/models/effective/membership.rb', line 151

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

#categories_sentenceObject



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

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

#categoryObject

We might want to use singular memberships.



160
161
162
163
# File 'app/models/effective/membership.rb', line 160

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

#category_idObject



165
166
167
168
# File 'app/models/effective/membership.rb', line 165

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



155
156
157
# File 'app/models/effective/membership.rb', line 155

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

#change_fees_paid_periodObject



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

def change_fees_paid_period
  fees_paid_period
end

#change_fees_paid_period=(date) ⇒ Object



257
258
259
260
261
262
263
264
265
266
267
268
# File 'app/models/effective/membership.rb', line 257

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)


232
233
234
# File 'app/models/effective/membership.rb', line 232

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

#in_good_standing?Boolean

Returns:

  • (Boolean)


228
229
230
# File 'app/models/effective/membership.rb', line 228

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

#membership_category(category:) ⇒ Object



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

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



210
211
212
213
# File 'app/models/effective/membership.rb', line 210

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)


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

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

Returns:

  • (Boolean)


236
237
238
239
240
241
# File 'app/models/effective/membership.rb', line 236

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

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

#reregistered?Boolean

Returns:

  • (Boolean)


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

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

#revise!Object

Admin updating membership info



271
272
273
274
275
276
277
278
279
280
281
282
# File 'app/models/effective/membership.rb', line 271

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)

  true
end

#statusObject

We might want to use singular memberships.



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

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

#status_idObject



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

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



191
192
193
# File 'app/models/effective/membership.rb', line 191

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.



187
188
189
# File 'app/models/effective/membership.rb', line 187

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

#statuses_sentenceObject



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

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

#to_sObject



131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
# File 'app/models/effective/membership.rb', line 131

def to_s
  return 'membership' 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)


243
244
245
246
247
248
249
250
251
# File 'app/models/effective/membership.rb', line 243

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