add payment model and its associations
This commit is contained in:
parent
b639d2eec5
commit
ee0a575a6f
3 changed files with 29 additions and 0 deletions
|
|
@ -20,6 +20,7 @@ class Conference < ActiveRecord::Base
|
|||
has_one :program, dependent: :destroy
|
||||
has_one :venue, dependent: :destroy
|
||||
has_many :ticket_purchases, dependent: :destroy
|
||||
has_many :payments, dependent: :destroy
|
||||
has_many :supporters, through: :ticket_purchases, source: :user
|
||||
has_many :tickets, dependent: :destroy
|
||||
|
||||
|
|
|
|||
27
app/models/payment.rb
Normal file
27
app/models/payment.rb
Normal file
|
|
@ -0,0 +1,27 @@
|
|||
class Payment < ActiveRecord::Base
|
||||
has_many :ticket_purchases
|
||||
belongs_to :user
|
||||
belongs_to :conference
|
||||
|
||||
validates :last4, presence: true
|
||||
validates :authorization_code, presence: true
|
||||
validates :status, presence: true
|
||||
validates :amount, presence: true, numericality: { greater_than: 0 }
|
||||
validates :user_id, presence: true
|
||||
validates :conference_id, presence: true
|
||||
|
||||
enum status: {
|
||||
unpaid: 0,
|
||||
success: 1,
|
||||
failure: 2
|
||||
}
|
||||
|
||||
def self.purchase(gateway_response, user, conference)
|
||||
create(last4: gateway_response[:source][:last4],
|
||||
amount: gateway_response[:amount],
|
||||
status: (gateway_response[:paid] ? 1 : 0),
|
||||
authorization_code: gateway_response[:id],
|
||||
user_id: user.id,
|
||||
conference_id: conference.id)
|
||||
end
|
||||
end
|
||||
|
|
@ -38,6 +38,7 @@ class User < ActiveRecord::Base
|
|||
has_many :registrations, dependent: :destroy
|
||||
has_many :events_registrations, through: :registrations
|
||||
has_many :ticket_purchases, dependent: :destroy
|
||||
has_many :payments, dependent: :destroy
|
||||
has_many :tickets, through: :ticket_purchases, source: :ticket
|
||||
has_many :votes, dependent: :destroy
|
||||
has_many :voted_events, through: :votes, source: :events
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue