Skip to content

Commit

Permalink
Merge pull request #2 from prestoncabe/delete-songs
Browse files Browse the repository at this point in the history
Delete songs
  • Loading branch information
prestoncabe committed Mar 17, 2014
2 parents 8eb79e7 + b776307 commit 75f5970
Show file tree
Hide file tree
Showing 10 changed files with 86 additions and 9 deletions.
6 changes: 6 additions & 0 deletions app/controllers/songs_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,12 @@ def show
@song = Song.find(params[:id])
end

def destroy
@song = Song.find(params[:id])
@song.destroy
redirect_to songs_path
end

private

def song_params
Expand Down
7 changes: 7 additions & 0 deletions app/helpers/songs_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module SongsHelper
def delete_link song
if current_user && current_user.can_delete_songs?
link_to 'delete', song_path(song), method: :delete, data: {confirm: "Are you sure you want to delete \"#{song.title}\"?"}
end
end
end
4 changes: 4 additions & 0 deletions app/models/user.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
class User < ActiveRecord::Base
include Clearance::User

def can_delete_songs?
true if self.admin?
end
end
5 changes: 4 additions & 1 deletion app/views/songs/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ Songs

<ul>
<% @songs.each do |song| %>
<li class='song'><%= link_to song.title, song_path(song) %></li>
<li class='song'>
<%= link_to song.title, song_path(song) %>
<%= delete_link(song) %>
</li>
<% end %>
</ul>
2 changes: 1 addition & 1 deletion config/routes.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
CircleMusic::Application.routes.draw do
root to: 'homes#show'

resources :songs, only: [:new, :index, :create, :show]
resources :songs, only: [:new, :index, :create, :show, :destroy]
end
5 changes: 5 additions & 0 deletions db/migrate/20140312181602_add_admin_to_users.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class AddAdminToUsers < ActiveRecord::Migration
def change
add_column :users, :admin, :boolean, default: false, null: false
end
end
13 changes: 7 additions & 6 deletions db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema.define(version: 20140307200647) do
ActiveRecord::Schema.define(version: 20140312181602) do

create_table "songs", force: true do |t|
t.string "title", null: false
Expand All @@ -20,12 +20,13 @@
end

create_table "users", force: true do |t|
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.string "email", null: false
t.string "encrypted_password", limit: 128, null: false
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.string "email", null: false
t.string "encrypted_password", limit: 128, null: false
t.string "confirmation_token", limit: 128
t.string "remember_token", limit: 128, null: false
t.string "remember_token", limit: 128, null: false
t.boolean "admin", default: false, null: false
end

add_index "users", ["email"], name: "index_users_on_email"
Expand Down
26 changes: 26 additions & 0 deletions spec/features/delete_song_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
require 'spec_helper'

feature 'Delete a song' do
scenario 'as an admin user on songs index page' do
create(:song, title: 'Come Thou Fount')
create(:song, title: 'Lord I Lift')
admin_user = create(:user, admin: true)

visit songs_path as: admin_user
within "li.song:contains('Lord I Lift')" do
click_link 'delete'
end

expect(page).not_to have_content 'Lord I Lift'
end

scenario '(non-admin user does not see delete link on songs index page)' do
create(:song, title: 'Come Thou Fount')
create(:song, title: 'Lord I Lift')
non_admin_user = create(:user)

visit songs_path as: non_admin_user

expect(page).not_to have_content 'delete'
end
end
3 changes: 2 additions & 1 deletion spec/features/view_songs_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,17 @@
end

context 'as a user' do

background do
@user = create(:user)
end

scenario 'on songs index page' do
visit songs_path as: @user

user_sees_list_of_songs
end


scenario 'on song show page' do
visit songs_path as: @user

Expand Down
24 changes: 24 additions & 0 deletions spec/models/user_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
require 'spec_helper'

describe User, '#can_delete_songs?' do
it 'returns true if user is an admin' do
admin_user = create(:user, admin: true)

expect(admin_user.can_delete_songs?).to be_true
end

it 'returns false if user is not an admin' do
non_admin_user = create :user

expect(non_admin_user.can_delete_songs?).to be_false
end
end

describe User, '#admin?' do
it 'is explicitly false by default' do
non_admin_user = create :user

expect(non_admin_user.admin?).to be_false
expect(non_admin_user.admin).not_to be_nil
end
end

0 comments on commit 75f5970

Please sign in to comment.