Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions homework/podcasting/.rspec
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
--require spec_helper
3 changes: 3 additions & 0 deletions homework/podcasting/Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@ group :development, :test do

# Omakase Ruby styling [https://github.com/rails/rubocop-rails-omakase/]
gem "rubocop-rails-omakase", require: false

gem "rspec-rails"
gem "factory_bot", "~> 6.5"
end

group :development do
Expand Down
22 changes: 22 additions & 0 deletions homework/podcasting/Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -103,12 +103,15 @@ GEM
debug (1.10.0)
irb (~> 1.10)
reline (>= 0.3.8)
diff-lcs (1.6.0)
dotenv (3.1.7)
drb (2.2.1)
ed25519 (1.3.0)
erubi (1.13.1)
et-orbi (1.2.11)
tzinfo
factory_bot (6.5.1)
activesupport (>= 6.1.0)
fugit (1.11.1)
et-orbi (~> 1, >= 1.2.11)
raabro (~> 1.4)
Expand Down Expand Up @@ -253,6 +256,23 @@ GEM
reline (0.6.0)
io-console (~> 0.5)
rexml (3.4.1)
rspec-core (3.13.3)
rspec-support (~> 3.13.0)
rspec-expectations (3.13.3)
diff-lcs (>= 1.2.0, < 2.0)
rspec-support (~> 3.13.0)
rspec-mocks (3.13.2)
diff-lcs (>= 1.2.0, < 2.0)
rspec-support (~> 3.13.0)
rspec-rails (7.1.1)
actionpack (>= 7.0)
activesupport (>= 7.0)
railties (>= 7.0)
rspec-core (~> 3.13)
rspec-expectations (~> 3.13)
rspec-mocks (~> 3.13)
rspec-support (~> 3.13)
rspec-support (3.13.2)
rubocop (1.73.1)
json (~> 2.3)
language_server-protocol (~> 3.17.0.2)
Expand Down Expand Up @@ -371,12 +391,14 @@ DEPENDENCIES
brakeman
capybara
debug
factory_bot (~> 6.5)
importmap-rails
jbuilder
kamal
propshaft
puma (>= 5.0)
rails (~> 8.0.1)
rspec-rails
rubocop-rails-omakase
selenium-webdriver
solid_cable
Expand Down
4 changes: 4 additions & 0 deletions homework/podcasting/app/models/comment.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
class Comment < ApplicationRecord
belongs_to :episode
belongs_to :user
end
27 changes: 27 additions & 0 deletions homework/podcasting/app/models/episode.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,28 @@ class Episode < ApplicationRecord

belongs_to :podcast
has_many :likes
has_many :comments
has_many :play_stats

scope :published, -> { where(status: :published) }
scope :popular, -> { joins(:likes) }
scope :played, -> { joins(:play_stats).where(play_stats: { is_finished: true }) }

def like(user)
Like.create(user: user, episode: self)
end

def unlike(user)
Like.delete_by(user: user, episode: self)
end

def add_comment(user, text)
Comment.create(user: user, text: text, episode: self)
end

def del_comment(user)
Comment.delete_by(user: user, episode: self)
end

def publish
self.status = "published"
Expand All @@ -24,6 +43,14 @@ def draft?
self.status == "draft"
end

def unpublish
self.status = "unpublished"
end

def unpublished?
self.status == "unpublished"
end

def podcast_not_archived
return if podcast.nil?

Expand Down
1 change: 1 addition & 0 deletions homework/podcasting/app/models/like.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
class Like < ApplicationRecord
belongs_to :episode
belongs_to :user
end
4 changes: 4 additions & 0 deletions homework/podcasting/app/models/play_stat.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
class PlayStat < ApplicationRecord
belongs_to :episode
belongs_to :user
end
11 changes: 11 additions & 0 deletions homework/podcasting/app/models/podcast.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
class Podcast < ApplicationRecord

validates :title, presence: true
validate :author_not_blocked

Expand Down Expand Up @@ -30,6 +31,16 @@ def add_draft(episode)
episode.draft
end

def unpublish(episode)
return unless episodes.include?(episode)
episode.unpublish
end

def destroy_episode(episode)
return unless episodes.include?(episode)
episodes.destroy(episode)
end

def archived?
status == "archived"
end
Expand Down
2 changes: 2 additions & 0 deletions homework/podcasting/app/models/user.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
class User < ApplicationRecord
has_one :account, autosave: true
has_many :likes
has_many :comments
has_many :play_stats

def block
self.blocked = true
Expand Down
11 changes: 11 additions & 0 deletions homework/podcasting/db/migrate/20250303121224_create_comments.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
class CreateComments < ActiveRecord::Migration[8.0]
def change
create_table :comments do |t|
t.references :episode, null: false, foreign_key: true
t.references :user, null: false, foreign_key: true
t.text :text

t.timestamps
end
end
end
12 changes: 12 additions & 0 deletions homework/podcasting/db/migrate/20250303123454_create_play_stats.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
class CreatePlayStats < ActiveRecord::Migration[8.0]
def change
create_table :play_stats do |t|
t.references :episode, null: false, foreign_key: true
t.references :user, null: false, foreign_key: true
t.integer :position
t.boolean :is_finished, default: false

t.timestamps
end
end
end
28 changes: 27 additions & 1 deletion homework/podcasting/db/schema.rb

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions homework/podcasting/spec/factories/accounts.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
FactoryBot.define do
factory :account do
end
end
27 changes: 27 additions & 0 deletions homework/podcasting/spec/factories/episodes.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
FactoryBot.define do
factory :episode do
title { 'Episode One' }

trait :with_podcast do
association :podcast, factory: :podcast
end

trait :with_archived_podcast do
association :podcast, factory: :podcast_archived
end

trait :with_status_published do
status { 'published' }
end

trait :with_status_draft do
status { 'draft' }
end

trait :with_status_unpublished do
status { 'unpublished' }
end
end

factory :episode_with_podcast, parent: :episode, traits: [:with_podcast]
end
8 changes: 8 additions & 0 deletions homework/podcasting/spec/factories/play_stats.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
FactoryBot.define do
factory :play_stat do
association :episode, factory: :episode_with_podcast
association :user, factory: :user
position { 0 }
is_finished { false }
end
end
33 changes: 33 additions & 0 deletions homework/podcasting/spec/factories/podcasts.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
FactoryBot.define do
factory :podcast do
status { 'new' }
title { 'New title' }
association :author, factory: :author
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

супер, что нашел эту конструкцию


trait :with_archived_status do
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

с одной стороны, ты аккуратно сделал через trait, но лучше использовать этот механизм для более сложных переиспользуемых случаев. Один атрибут лучше выставлять прямо в тесте передавая такие атрибуты в фабрику FactoryBot.build(:podcast, title: nil)

status { 'archived' }
end

trait :with_nil_title do
title { nil }
end

trait :with_empty_title do
title { '' }
end

trait :without_author do
author { nil }
end

trait :with_author_without_account do
association :author, factory: :author_without_account
end

trait :with_blocked_author do
association :author, factory: :author_blocked
end
end

factory :podcast_archived, parent: :podcast, traits: [:with_archived_status]
end
17 changes: 17 additions & 0 deletions homework/podcasting/spec/factories/users.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
FactoryBot.define do
factory :user, aliases: [:author, :subscriber, :no_subscriber] do
name { "John Marston" }
association :account, factory: :account

trait :blocked do
blocked { true }
end

trait :without_account do
account { nil }
end
end

factory :author_blocked, parent: :user, traits: [:blocked]
factory :author_without_account, parent: :user, traits: [:without_account]
end
Loading