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.



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

def current_action
  @current_action
end

Class Method Details

.max_numberObject



75
76
77
# File 'app/models/effective/membership.rb', line 75

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

Instance Method Details

#build_membership_category(category:) ⇒ Object

find or build



129
130
131
# File 'app/models/effective/membership.rb', line 129

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

#build_membership_status(status:) ⇒ Object

find or build



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

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.



100
101
102
# File 'app/models/effective/membership.rb', line 100

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

#categories_sentenceObject



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

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

#categoryObject

We might want to use singular memberships.



109
110
111
112
# File 'app/models/effective/membership.rb', line 109

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

#category_idObject



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

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



104
105
106
# File 'app/models/effective/membership.rb', line 104

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

#change_fees_paid_periodObject



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

def change_fees_paid_period
  fees_paid_period
end

#change_fees_paid_period=(date) ⇒ Object



202
203
204
205
206
207
208
209
210
211
212
213
# File 'app/models/effective/membership.rb', line 202

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)


177
178
179
# File 'app/models/effective/membership.rb', line 177

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

#good_standing?Boolean

Returns:

  • (Boolean)


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

def good_standing?
  !bad_standing?
end

#membership_category(category:) ⇒ Object



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

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



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

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

Returns:

  • (Boolean)


181
182
183
184
185
186
# File 'app/models/effective/membership.rb', line 181

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)


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

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

#revise!Object

Admin updating membership info



216
217
218
219
220
221
222
223
224
225
226
227
228
# File 'app/models/effective/membership.rb', line 216

def revise!
  save!

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

  # Otherwise build fees right now
  EffectiveMemberships.Registrar.create_renewal_fees!(self, period: period)
  EffectiveMemberships.Registrar.create_late_fees!(self, period: period)
  EffectiveMemberships.Registrar.update_membership_status!(self, period: period)

  true
end

#statusObject

We might want to use singular memberships.



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

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

#status_idObject



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

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



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

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.



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

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

#statuses_sentenceObject



144
145
146
# File 'app/models/effective/membership.rb', line 144

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

#to_sObject



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'app/models/effective/membership.rb', line 79

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 registered #{registration_on.strftime('%F')}" if registration_on > joined_on),
    (". Membership is Not In Good Standing because #{bad_standing_reason}" if bad_standing?)
  ].compact.join(' ')

  (summary + '.').html_safe
end

#unpaid_fees_through?(period = nil) ⇒ Boolean

Returns:

  • (Boolean)


188
189
190
191
192
193
194
195
196
# File 'app/models/effective/membership.rb', line 188

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