Skip to content

Day1 #2

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
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
51 changes: 0 additions & 51 deletions homework/episode_spec.rb

This file was deleted.

58 changes: 0 additions & 58 deletions homework/podcast_spec.rb

This file was deleted.

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
5 changes: 5 additions & 0 deletions homework/podcasting/Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,11 @@ group :development, :test do

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

gem "rspec", "~> 3.13"
gem "rspec-rails", "~> 7.1"
gem "factory_bot", "~> 6.5"
gem "factory_bot_rails", "~> 6.4"
end

group :development do
Expand Down
31 changes: 31 additions & 0 deletions homework/podcasting/Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -103,12 +103,18 @@ 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)
factory_bot_rails (6.4.4)
factory_bot (~> 6.5)
railties (>= 5.0.0)
fugit (1.11.1)
et-orbi (~> 1, >= 1.2.11)
raabro (~> 1.4)
Expand Down Expand Up @@ -253,6 +259,27 @@ GEM
reline (0.6.0)
io-console (~> 0.5)
rexml (3.4.1)
rspec (3.13.0)
rspec-core (~> 3.13.0)
rspec-expectations (~> 3.13.0)
rspec-mocks (~> 3.13.0)
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 +398,16 @@ DEPENDENCIES
brakeman
capybara
debug
factory_bot (~> 6.5)
factory_bot_rails (~> 6.4)
importmap-rails
jbuilder
kamal
propshaft
puma (>= 5.0)
rails (~> 8.0.1)
rspec (~> 3.13)
rspec-rails (~> 7.1)
rubocop-rails-omakase
selenium-webdriver
solid_cable
Expand Down
3 changes: 3 additions & 0 deletions homework/podcasting/app/models/comment.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
class Comment < ApplicationRecord
belongs_to :episode
end
23 changes: 23 additions & 0 deletions homework/podcasting/app/models/episode.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ class Episode < ApplicationRecord

belongs_to :podcast
has_many :likes
has_many :comments
has_many :stats

scope :published, -> { where(status: :published) }
scope :popular, -> { joins(:likes) }
Expand All @@ -12,6 +14,10 @@ def publish
self.status = "published"
end

def unpublish
self.status = "draft"
end

def published?
self.status == "published"
end
Expand All @@ -31,4 +37,21 @@ def podcast_not_archived
errors.add(:podcast, "is archived")
end
end

def like_by(user)
likes.create(user:)
end

def unliked_by(user)
likes.find_by(user:).destroy
end

def play_by(user)
stats.create(user:)
end

def pause_by(user, position)
stat = stats.find_by(user:)
stat.update(position:)
end
end
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
9 changes: 9 additions & 0 deletions homework/podcasting/app/models/podcast.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@ def publish(episode)
episode.publish
end

def unpublish(episode)
self.episodes.delete(episode)
episode.unpublish
end

def add_draft(episode)
self.episodes << episode
episode.draft
Expand All @@ -33,4 +38,8 @@ def add_draft(episode)
def archived?
status == "archived"
end

def delete(episode)
self.episodes.delete(episode)
end
end
4 changes: 4 additions & 0 deletions homework/podcasting/app/models/stat.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
class Stat < ApplicationRecord
belongs_to :episode
belongs_to :user
end
10 changes: 10 additions & 0 deletions homework/podcasting/db/migrate/20250303182547_create_comments.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
class CreateComments < ActiveRecord::Migration[8.0]
def change
create_table :comments do |t|
t.text :text
t.references :episode, null: false, foreign_key: true

t.timestamps
end
end
end
11 changes: 11 additions & 0 deletions homework/podcasting/db/migrate/20250303184027_create_stats.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
class CreateStats < ActiveRecord::Migration[8.0]
def change
create_table :stats do |t|
t.references :episode, null: false, foreign_key: true
t.references :user, null: false, foreign_key: true
t.integer :position

t.timestamps
end
end
end
24 changes: 23 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.

6 changes: 6 additions & 0 deletions homework/podcasting/spec/factories/comments.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
FactoryBot.define do
factory :comment do
text { "MyText" }
episode { nil }
end
end
10 changes: 10 additions & 0 deletions homework/podcasting/spec/factories/episode.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
FactoryBot.define do
factory :episode do
podcast { create(:podcast) }
title { "Episode Title" }
status { "published" }
created_at { DateTime.now }
updated_at { DateTime.now }
likes_count { 0 }
end
end
6 changes: 6 additions & 0 deletions homework/podcasting/spec/factories/like.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
FactoryBot.define do
factory :like do
user { create(:user) }
episode { create(:episode) }
end
end
9 changes: 9 additions & 0 deletions homework/podcasting/spec/factories/podcast.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
FactoryBot.define do
factory :podcast do
title { "Podcast Title" }
author { create(:user) }
status { :active }
created_at { DateTime.now }
updated_at { DateTime.now }
end
end
7 changes: 7 additions & 0 deletions homework/podcasting/spec/factories/stats.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
FactoryBot.define do
factory :stat do
episode { nil }
user { nil }
position { 1 }
end
end
6 changes: 6 additions & 0 deletions homework/podcasting/spec/factories/subscription.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
FactoryBot.define do
factory :subscription do
user { create(:user) }
podcast { create(:podcast) }
end
end
8 changes: 8 additions & 0 deletions homework/podcasting/spec/factories/user.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
FactoryBot.define do
factory :user do
name { "John Doe" }
blocked { false }
created_at { DateTime.now }
Copy link
Contributor

Choose a reason for hiding this comment

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

в фабриках лучше указывать минимальный набор атрибутов, например created/updated - проставятся и так через AR

updated_at { DateTime.now }
end
end
Loading