Skip to content

Commit

Permalink
fix models designs
Browse files Browse the repository at this point in the history
  • Loading branch information
citin committed Jul 6, 2019
1 parent ce92f6c commit a953d8e
Show file tree
Hide file tree
Showing 11 changed files with 79 additions and 5 deletions.
3 changes: 3 additions & 0 deletions app/models/movie.rb
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
20 changes: 19 additions & 1 deletion app/models/purchase.rb
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
2 changes: 2 additions & 0 deletions app/models/purchase_option.rb
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
6 changes: 5 additions & 1 deletion app/models/season.rb
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
24 changes: 24 additions & 0 deletions app/models/user.rb
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
1 change: 0 additions & 1 deletion db/migrate/20190703103723_create_purchases.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ class CreatePurchases < ActiveRecord::Migration[5.2]
def change
create_table :purchases do |t|
t.references :user, foreign_key: true
t.references :content, polymorphic: true

t.timestamps
end
Expand Down
1 change: 0 additions & 1 deletion db/migrate/20190703104503_create_seasons.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ def change
create_table :seasons do |t|
t.string :title
t.text :plot
t.references :episodes, foreign_key: true

t.timestamps
end
Expand Down
1 change: 1 addition & 0 deletions db/migrate/20190703105304_create_purchase_options.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ def change
create_table :purchase_options do |t|
t.float :price
t.string :video_quality
t.references :content, polymorphic: true

t.timestamps
end
Expand Down
5 changes: 5 additions & 0 deletions db/migrate/20190705120624_add_content_to_purchase_option.rb
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
5 changes: 5 additions & 0 deletions db/migrate/20190706162714_add_purchase_option_to_purchase.rb
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
16 changes: 15 additions & 1 deletion db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema.define(version: 2019_07_03_104503) do
ActiveRecord::Schema.define(version: 2019_07_06_162714) do

create_table "episodes", force: :cascade do |t|
t.string "title"
Expand All @@ -28,13 +28,27 @@
t.datetime "updated_at", null: false
end

create_table "purchase_options", force: :cascade do |t|
t.float "price"
t.string "video_quality"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.string "content_type"
t.integer "content_id"
t.index ["content_type", "content_id"], name: "index_purchase_options_on_content_type_and_content_id"
end

create_table "purchases", force: :cascade do |t|
t.integer "user_id"
t.string "content_type"
t.integer "content_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.integer "purchase_id"
t.integer "purchase_option_id"
t.index ["content_type", "content_id"], name: "index_purchases_on_content_type_and_content_id"
t.index ["purchase_id"], name: "index_purchases_on_purchase_id"
t.index ["purchase_option_id"], name: "index_purchases_on_purchase_option_id"
t.index ["user_id"], name: "index_purchases_on_user_id"
end

Expand Down

0 comments on commit a953d8e

Please sign in to comment.