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



67
68
69
# File 'app/models/effective/membership.rb', line 67

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

Instance Method Details

#build_membership_category(category:) ⇒ Object

find or build



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

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

#categoriesObject

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



89
90
91
# File 'app/models/effective/membership.rb', line 89

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

#categoryObject

We might want to use singular memberships.



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

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

#category_idObject



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

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

#category_idsObject



93
94
95
# File 'app/models/effective/membership.rb', line 93

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

#change_fees_paid_periodObject



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

def change_fees_paid_period
  fees_paid_period
end

#change_fees_paid_period=(date) ⇒ Object



147
148
149
150
151
152
153
154
155
156
157
158
# File 'app/models/effective/membership.rb', line 147

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)


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

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

#good_standing?Boolean

Returns:

  • (Boolean)


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

def good_standing?
  !bad_standing?
end

#membership_category(category:) ⇒ Object



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

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

Returns:

  • (Boolean)


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

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



161
162
163
164
165
166
167
168
169
170
171
172
173
# File 'app/models/effective/membership.rb', line 161

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

#to_sObject



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'app/models/effective/membership.rb', line 71

def to_s
  return 'membership' if owner.blank?

  summary = [
    owner.to_s,
    'is',
    (categories.to_sentence),
    '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)


133
134
135
136
137
138
139
140
141
# File 'app/models/effective/membership.rb', line 133

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