Skip to content

Commit

Permalink
allow users to destroy their strikes
Browse files Browse the repository at this point in the history
  • Loading branch information
grosser committed Mar 5, 2011
1 parent 37057b9 commit bddcde9
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 3 deletions.
2 changes: 1 addition & 1 deletion app/controllers/home_controller.rb
@@ -1,7 +1,7 @@
class HomeController < ApplicationController
def index
@dates = (0..30).to_a.map{|i| Date.today + i }
@strikes = Strike.where(:start_on => @dates.first..@dates.last).to_a
@strikes = Strike.in_next_days(31).to_a
@strikes = @strikes.group_by(&:organisation).sort_by{|_,strikes| strikes.map(&:start_on).min }
end
end
8 changes: 7 additions & 1 deletion app/controllers/strikes_controller.rb
@@ -1,5 +1,5 @@
class StrikesController < ApplicationController
before_filter :login_required, :only => [:new, :create]
before_filter :login_required, :only => [:new, :create, :destroy]

def show
@strike = Strike.find(params[:id])
Expand All @@ -19,4 +19,10 @@ def create
render 'new'
end
end

def destroy
strike = Strike.find(params[:id])
strike.destroy if current_user.is_owner?(strike)
redirect_to root_path
end
end
1 change: 1 addition & 0 deletions app/models/strike.rb
@@ -1,6 +1,7 @@
class Strike < ActiveRecord::Base
validates_presence_of :start_on, :end_on, :organisation, :creator_id
belongs_to :creator, :class_name => 'User'
scope :in_next_days, lambda{|days| where("start_on <= ? and ? <= end_on", Date.today+days, Date.today) }

def include?(date)
start_on <= date and date <= end_on
Expand Down
4 changes: 4 additions & 0 deletions app/models/user.rb
Expand Up @@ -16,4 +16,8 @@ def gender=(value)
def name
"#{first_name} #{last_name}"
end

def is_owner?(object)
object.creator_id == id
end
end
4 changes: 3 additions & 1 deletion app/views/strikes/show.erb
@@ -1,4 +1,6 @@
<h1><%= @strike.organisation %> Streik vom <%= d @strike.start_on %> bis <%= d @strike.start_on %></h1>
<h1><%= @strike.organisation %> Streik vom <%= d @strike.start_on %> bis <%= d @strike.end_on %></h1>
<%= @strike.comment %>
<br/><br/><br/>
eingetragen von <%= link_to @strike.creator.name, "http://www.facebook.com/#!/profile.php?id=#{@strike.creator.fb_id}&v=wall" %>
<br/><br/>
<%= link_to 'Löschen', strike_path(@strike), :method => :delete, :confirm => 'Löschen ??' if current_user.is_owner?(@strike) %>
18 changes: 18 additions & 0 deletions spec/controllers/strikes_controller_spec.rb
Expand Up @@ -33,4 +33,22 @@
response.should render_template('show')
end
end

describe :destroy do
it "deleted a strike" do
strike = Factory(:strike)
login_as strike.creator
delete :destroy, :id => strike.id
lambda{
strike.reload
}.should raise_error
end

it "does not delete if im not the owner" do
strike = Factory(:strike)
login_as Factory(:user)
delete :destroy, :id => strike.id
strike.reload
end
end
end

0 comments on commit bddcde9

Please sign in to comment.