Browse Source

Added api/time_entry/delete method

denney-fix-saving-dates
Jason Denney 11 years ago
parent
commit
d94c436ba5
  1. 10
      app/controllers/api/v1/time_entries_controller.rb
  2. 1
      config/routes.rb
  3. 37
      spec/controllers/api/time_entries_controller_spec.rb
  4. 10
      spec/factories/time_entries.rb

10
app/controllers/api/v1/time_entries_controller.rb

@ -1,5 +1,6 @@
class Api::V1::TimeEntriesController < Api::V1::BaseController
EXPECTED_TIME_ENTRY = "Expected time entry in submitted data"
NOT_FOUND = "Time entry not found"
def create
if params[:time_entries] && time_entry = params[:time_entries].first
@ -19,4 +20,13 @@ class Api::V1::TimeEntriesController < Api::V1::BaseController
render json: { errors: [EXPECTED_TIME_ENTRY]}, status: 422 and return
end
end
def delete
if time_entry = TimeEntry.find_by_id(params[:id])
time_entry.delete
render nothing: true, status: 204 and return
else
render json: { errors: [NOT_FOUND]}, status: 404 and return
end
end
end

1
config/routes.rb

@ -31,6 +31,7 @@ Velocipede::Application.routes.draw do
put 'tasks/update' => "tasks#update", as: "api_update_task"
post 'time_entries/create' => "time_entries#create", as: "api_create_time_entry"
delete 'time_entries/:id' => "time_entries#delete", as: "api_delete_time_entry"
end
end

37
spec/controllers/api/time_entries_controller_spec.rb

@ -77,4 +77,41 @@ describe Api::V1::TimeEntriesController do
end
end
end
describe "#delete" do
context "as a user" do
let!(:user){ FactoryGirl.create(:user) }
before(:each) do
sign_in user
end
context "entry does not exist" do
it "returns 404" do
delete :delete, id: 9000
expect(@response.code.to_i).to eql 404
end
it "returns not found" do
delete :delete, id: 9000
json = JSON.parse(@response.body)
expect(json).to have_key("errors")
expect(json.to_s).to include("not found")
end
end
context "entry exists" do
let!(:entry){ FactoryGirl.create(:time_entry) }
it "deletes the time entry" do
expect{delete :delete, id: entry.id}.to change{TimeEntry.count}
end
it "returns 204" do
delete :delete, id: entry.id
expect(@response.code.to_i).to eql 204
end
end
end
end
end

10
spec/factories/time_entries.rb

@ -0,0 +1,10 @@
FactoryGirl.define do
factory :time_entry do
loggable_type User.to_s
logger_type User.to_s
start_date Time.now
end_date Time.now
log_action_id 1
log_action_type ActsAsLoggable::UserAction.to_s
end
end
Loading…
Cancel
Save