Skip to content

Commit

Permalink
Episodes all tests passed
Browse files Browse the repository at this point in the history
  • Loading branch information
kalashnikovisme committed Jul 23, 2023
1 parent a189678 commit 6a7c0fc
Show file tree
Hide file tree
Showing 15 changed files with 338 additions and 0 deletions.
46 changes: 46 additions & 0 deletions app/controllers/episodes_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
# frozen_string_literal: true

class EpisodesController < ApplicationController
before_action :set_episode, only: %i[show edit update destroy]
before_action :authenticate_user!

def index
@episodes = tramway_decorate Episode.all
end

def show; end

def new
@episode = Episode.new
end

def edit; end

def create
@episode = Episode.create!(**episode_params)

render :show
end

def update
@episode.update!(**episode_params)

render :show
end

def destroy
@episode.destroy

redirect_to episodes_path
end

private

def set_episode
@episode = Episode.find(params[:id])
end

def episode_params
params.require(:episode).permit(:title, :podcast_id)
end
end
13 changes: 13 additions & 0 deletions app/decorators/episode_decorator.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# frozen_string_literal: true

class EpisodeDecorator < Tramway::BaseDecorator
delegate_attributes :title

def dates
table = %i[created_at updated_at].map do |date|
{ name: date, date: object.public_send(date) }
end

render(TableComponent.new(table))
end
end
12 changes: 12 additions & 0 deletions app/views/episodes/_episode.html.haml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
-# FIXME replace window.location.href with something
%tr.cursor-pointer.hover:bg-gray-100
%td.py-2.px-4.border-b.border-r
= link_to episode_path(episode) do
= episode.title
%td.py-2.px-4.border-b.border-r
= episode.dates
%td.py-2.px-4.border-b.border-r.text-right
%a.btn-edit.bg-blue-500.hover:bg-blue-700.text-white.font-bold.py-2.px-4.mr-2.rounded{ href: edit_episode_path(episode) }
Edit
%button.btn-delete.bg-orange-500.hover:bg-orange-700.text-white.font-bold.py-2.px-4.rounded{ href: episode, method: :delete, data: { confirm: 'Are sure?' } }
Delete
14 changes: 14 additions & 0 deletions app/views/episodes/_form.html.haml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
= form_for @episode do |f|
- if @episode.errors.any?
#error_explanation
%h2= "#{pluralize(@episode.errors.count, "error")} prohibited this episode from being saved:"
%ul
- @episode.errors.full_messages.each do |message|
%li= message

= text_input f, :title, placeholder: :title
= text_input f, :podcast_id
.flex.justify-end
= submit_button f, "Save"
%button.btn.btn-delete.bg-orange-500.hover:bg-orange-700.text-white.font-bold.py-2.px-4.ml-2.rounded{ onclick: 'document.referrer' }
Back
6 changes: 6 additions & 0 deletions app/views/episodes/edit.html.haml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
= turbo_frame_tag "episode" do
.flex.items-left.justify-center
.bg-white.p-8.rounded.max-w-md.w-full
%h1.text-2xl.font-bold.mb-4
Editing episode
= render 'form'
13 changes: 13 additions & 0 deletions app/views/episodes/index.html.haml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
.flex.justify-between.mb-4.mt-4
%h1{ class: "text-4xl font-bold text-left text-black-700 mb-8 mt-4" }
Episodes
.text-right.mt-4
= link_to 'New Episode', new_episode_path, class: " bg-red-500 hover:bg-red-700 text-white font-bold py-2 px-4 rounded focus:outline-none focus:shadow-outline"
= turbo_stream_from "episodes"
%table.w-full.bg-white.border.border-gray-200
%thead
%tr
%th.py-2.px-4.border-b.border-r Episode Title
%th.py-2.px-4.border-b.border-r Actions
%tbody#episodes
= render @episodes
6 changes: 6 additions & 0 deletions app/views/episodes/new.html.haml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
= turbo_frame_tag "episode" do
.flex.items-left.justify-center
.bg-white.p-8.rounded.max-w-md.w-full
%h1.text-2xl.font-bold.mb-4
New episode
= render 'form'
16 changes: 16 additions & 0 deletions app/views/episodes/show.html.haml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
= turbo_frame_tag "episode" do
.flex.justify-between.mb-4.mt-4
%h1.text-2xl.font-bold
= @episode.title
.text-right.mt-4
= link_to 'Edit', edit_episode_path(@episode), class: "btn btn-delete bg-blue-500 hover:bg-blue-700 text-white font-bold py-3 px-4 rounded"
= link_to 'Destroy', @episode, class: "btn btn-delete bg-orange-500 hover:bg-orange-700 text-white font-bold py-3 px-4 rounded", method: :delete, data: { confirm: 'Are sure?' }
= link_to 'Back', episodes_path, class: "btn btn-delete bg-gray-500 hover:bg-gray-700 text-white font-bold py-3 px-4 rounded"
%table.min-w-full.bg-white.border.border-gray-200
%tbody
- @episode.attributes.each do |(key, value)|
%tr
%td.px-4.py-2.border-b
= Episode.human_attribute_name(key)
%td.px-4.py-2.border-b
= value
8 changes: 8 additions & 0 deletions spec/factories/episodes.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# frozen_string_literal: true

FactoryBot.define do
factory :episode do
podcast
title { Faker::Lorem.sentence }
end
end
34 changes: 34 additions & 0 deletions spec/features/admin/episodes/create_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# frozen_string_literal: true

require 'rails_helper'

feature 'Creating a new Episode', type: :turbo do
let(:user) { create :user }
let(:podcast) { create :podcast }
let(:episode) { create 'episode' }
let(:attributes) { attributes_for 'episode' }

it 'creates new Episode' do
visit root_path

sign_in user

click_link 'Episodes'
count = Episode.count

sleep 1
click_on 'New Episode'

fill_in 'Title', with: attributes[:title]
fill_in 'Podcast', with: podcast.id

click_button 'Save'

expect(page).to have_content attributes[:email]
expect(Episode.count).to eq(count + 1)

new_episode = Episode.last

episodes_expectations new_episode, attributes
end
end
25 changes: 25 additions & 0 deletions spec/features/admin/episodes/destroy_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# frozen_string_literal: true

require 'rails_helper'

feature 'Destroy episode', type: :feature do
let(:user) { create :user }
let(:episode) { create 'episode' }
let!(:another_episode) { create 'episode' }

scenario 'displays a episode page' do
sign_in user

count = Episode.count

click_on 'Episodes'

click_on another_episode.title

click_on 'Destroy'

expect(page.status_code).to eq(200)
expect(count).to eq(Episode.count + 1)
expect(page).to have_current_path(episodes_path)
end
end
21 changes: 21 additions & 0 deletions spec/features/admin/episodes/index_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# frozen_string_literal: true

require 'rails_helper'

feature 'Episode Index Page', type: :feature do
let(:user) { create :user }
let!(:episodes) { create_list 'episode', 5 }

scenario 'displays a list of episodes' do
sign_in user

click_on 'Episodes'

expect(page.status_code).to eq(200)
expect(page).to have_current_path(episodes_path)

episodes.each do |episode|
expect(page).to have_content(episode.title)
end
end
end
21 changes: 21 additions & 0 deletions spec/features/admin/episodes/show_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# frozen_string_literal: true

require 'rails_helper'

feature 'Episode Show Page', type: :feature do
let(:user) { create :user }
let!(:episode) { create 'episode' }

scenario 'displays a episode page' do
sign_in user

click_on 'Episodes'

click_on episode.title

expect(page.status_code).to eq(200)
expect(page).to have_current_path(episode_path(episode))

expect(page).to have_content(episode.title)
end
end
84 changes: 84 additions & 0 deletions spec/features/admin/episodes/update_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
# frozen_string_literal: true

require 'rails_helper'

feature 'Episode Update Page', type: :feature do
let(:user) { create :user }
let!(:episode) { create 'episode' }

context 'check edit page' do
before do
sign_in user

click_on 'Episodes'

click_on episode.title
click_on 'Edit'
end

scenario 'opens page' do
expect(page.status_code).to eq(200)
expect(page).to have_current_path(edit_episode_path(episode))
end

scenario 'check all inputs' do
expect(find_field('episode[title]').value).to eq(episode.title)
end

scenario 'returns back' do
click_on 'Back'

expect(page).to have_current_path(episode_path(episode))
end
end

context 'check updates all attributes' do
let(:attributes) { attributes_for 'episode' }

before do
sign_in user

click_on 'Episodes'

click_on episode.title
click_on 'Edit'

fill_in 'Title', with: attributes[:title]

click_on 'Save'
end

scenario 'updates a episode' do
episode.reload

episodes_expectations episode, attributes
end

scenario 'renders show page' do
expect(page.status_code).to eq(200)

expect(page).to have_current_path(episode_path(episode))
expect(page).to have_content(attributes[:title])
end
end

context 'check updates single attribute' do
let(:attributes) { attributes_for 'episode' }

scenario 'just title' do
sign_in user

click_on 'Episodes'

click_on episode.title
click_on 'Edit'

fill_in 'Title', with: attributes[:title]

click_on 'Save'
episode.reload

episodes_expectations episode, attributes
end
end
end
19 changes: 19 additions & 0 deletions spec/support/expectations/episodes.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# frozen_string_literal: true

module Expectations
module Episodes
def episodes_expectations(object, attributes)
exceptions = []
attributes.except(*exceptions).each do |(key, value)|
real = object.public_send(key)
expected = attributes[key].to_s

expect(real).to eq(expected), "Attribute #{key} is not equal to #{value}"
end
end
end
end

RSpec.configure do |config|
config.include Expectations::Episodes
end

0 comments on commit 6a7c0fc

Please sign in to comment.