-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
79 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,5 @@ | ||
class Movie < ApplicationRecord | ||
has_many :users, through: :purchases | ||
has_many :purchases, as: :content | ||
has_many :purchase_options, as: :content | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,22 @@ | ||
class Purchase < ApplicationRecord | ||
belongs_to :user | ||
belongs_to :content, polymorphic: true | ||
belongs_to :purchase_option | ||
|
||
delegate :content, to: :purchase_option | ||
|
||
def alive? | ||
remaining_time > 0 | ||
end | ||
|
||
private | ||
|
||
# in minutes | ||
# if is expired return 0 | ||
def remaining_time | ||
[(expire_date - Time.now).to_i / 1.minute, 0].max | ||
end | ||
|
||
def expire_date | ||
(self.created_at + 2.days) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,4 @@ | ||
class PurchaseOption < ApplicationRecord | ||
has_many :purchases | ||
belongs_to :content, polymorphic: true | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,7 @@ | ||
class Season < ApplicationRecord | ||
belongs_to :episodes | ||
has_many :users, through: :purchases | ||
has_many :purchases, as: :content | ||
has_many :purchase_options, as: :content | ||
|
||
has_many :episodes | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,26 @@ | ||
class User < ApplicationRecord | ||
|
||
class AlreadyPurchasedError < StandardError; end | ||
|
||
has_many :purchases | ||
|
||
def library | ||
self.purchases.select {|p| p.alive? } | ||
end | ||
|
||
def purchase(purchase_option) | ||
# todo: remove content assoc from Purchase | ||
# check if content is already purchased | ||
if is_in_library?(purchase_option.content) | ||
raise AlreadyPurchasedError | ||
else | ||
self.purchases << Purchase.create(purchase_option: purchase_option) | ||
end | ||
end | ||
|
||
private | ||
|
||
def is_in_library?(content) | ||
library.any? {|purchase| purchase.content.title == content.title } | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
class AddContentToPurchaseOption < ActiveRecord::Migration[5.2] | ||
def change | ||
add_reference :purchase_options, :content, polymorphic: true | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
class AddPurchaseOptionToPurchase < ActiveRecord::Migration[5.2] | ||
def change | ||
add_reference :purchases, :purchase_option, foreign_key: true | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters