Class: MembershipChange

Inherits:
Object
  • Object
show all
Includes:
ActiveModel::Validations
Defined in:
app/models/membership_change.rb

Defined Under Namespace

Classes: Cart, Checkout, Error, Order

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(params = nil) ⇒ MembershipChange

Returns a new instance of MembershipChange.



29
30
31
32
# File 'app/models/membership_change.rb', line 29

def initialize(params = nil)
  @price = 0
  assign_params(params) if params
end

Instance Attribute Details

#cartObject (readonly)

Returns the value of attribute cart.



4
5
6
# File 'app/models/membership_change.rb', line 4

def cart
  @cart
end

#changing_membershipsObject (readonly)

Returns the value of attribute changing_memberships.



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

def changing_memberships
  @changing_memberships
end

#checkoutObject (readonly)

Returns the value of attribute checkout.



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

def checkout
  @checkout
end

#credit_card_infoObject

Returns the value of attribute credit_card_info.



10
11
12
# File 'app/models/membership_change.rb', line 10

def credit_card_info
  @credit_card_info
end

#membership_idsObject

Returns the value of attribute membership_ids.



11
12
13
# File 'app/models/membership_change.rb', line 11

def membership_ids
  @membership_ids
end

#membership_type_idObject

Returns the value of attribute membership_type_id.



12
13
14
# File 'app/models/membership_change.rb', line 12

def membership_type_id
  @membership_type_id
end

#new_membershipsObject (readonly)

Returns the value of attribute new_memberships.



7
8
9
# File 'app/models/membership_change.rb', line 7

def new_memberships
  @new_memberships
end

#paymentObject (readonly)

Returns the value of attribute payment.



8
9
10
# File 'app/models/membership_change.rb', line 8

def payment
  @payment
end

#payment_methodObject

Returns the value of attribute payment_method.



13
14
15
# File 'app/models/membership_change.rb', line 13

def payment_method
  @payment_method
end

#person_idObject

Returns the value of attribute person_id.



14
15
16
# File 'app/models/membership_change.rb', line 14

def person_id
  @person_id
end

#priceObject

Returns the value of attribute price.



15
16
17
# File 'app/models/membership_change.rb', line 15

def price
  @price
end

Class Method Details

.reflect_on_association(a) ⇒ Object

This is purely to assuage shoulda and it’s validation matchers



25
26
27
# File 'app/models/membership_change.rb', line 25

def self.reflect_on_association(a)
  return false
end

Instance Method Details

#assign_params(new_params) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
# File 'app/models/membership_change.rb', line 34

def assign_params(new_params)
  return if new_params.blank?

  params = new_params.stringify_keys
  params.each do |k,v|
    if respond_to?("#{k}=")
      send("#{k}=", v)
    else
      raise(ArgumentError, "unknown parameter: #{k}")
    end
  end
end

#comp?Boolean

Returns:

  • (Boolean)


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

def comp?
  'comp' == payment_method
end

#credit?Boolean

Returns:

  • (Boolean)


71
72
73
# File 'app/models/membership_change.rb', line 71

def credit?
  !!(payment_method =~ /credit|cc/)
end

#membership_typeObject



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

def membership_type
  @membership_type ||= MembershipType.find(membership_type_id)
end

#personObject



121
122
123
# File 'app/models/membership_change.rb', line 121

def person
  @person ||= Person.find(person_id)
end

#saveObject



136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
# File 'app/models/membership_change.rb', line 136

def save
  return false unless valid?

  Order.transaction do
    expiration = 1.second.ago

    # Complete checkout
    success = checkout.finish

    # Failure?
    raise MembershipChange::Error unless success

    # Expire old memberships
    changing_memberships.each do |old|
      old.adjust_expiration_to(expiration)
    end
  end
  true
rescue MembershipChange::Error => e
  payment.errors[:base].each do |msg|
    errors.add(:base, msg)
  end
  false
end

#valid?Boolean

Returns:

  • (Boolean)


161
162
163
# File 'app/models/membership_change.rb', line 161

def valid?
  super && checkout.valid?
end