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



70
71
72
# File 'app/models/effective/membership.rb', line 70

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

Instance Method Details

#build_membership_category(category:) ⇒ Object

find or build



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

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

#build_membership_status(status:) ⇒ Object

find or build



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

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.



95
96
97
# File 'app/models/effective/membership.rb', line 95

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

#categoryObject

We might want to use singular memberships.



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

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

#category_idObject



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

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



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

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

#change_fees_paid_periodObject



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

def change_fees_paid_period
  fees_paid_period
end

#change_fees_paid_period=(date) ⇒ Object



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

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)


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

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

#good_standing?Boolean

Returns:

  • (Boolean)


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

def good_standing?
  !bad_standing?
end

#membership_category(category:) ⇒ Object



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

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



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

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)


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

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

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

#revise!Object

Admin updating membership info



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

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.



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

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

#status_idObject



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

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



131
132
133
# File 'app/models/effective/membership.rb', line 131

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.



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

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

#to_sObject



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'app/models/effective/membership.rb', line 74

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}",
    "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)


171
172
173
174
175
176
177
178
179
# File 'app/models/effective/membership.rb', line 171

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