diff --git a/app/controllers/api/v1/time_entries_controller.rb b/app/controllers/api/v1/time_entries_controller.rb index 1b4853d..dc07b03 100644 --- a/app/controllers/api/v1/time_entries_controller.rb +++ b/app/controllers/api/v1/time_entries_controller.rb @@ -1,25 +1,16 @@ class Api::V1::TimeEntriesController < Api::V1::BaseController + EXPECTED_TIME_ENTRY = "Expected time entry in submitted data" + def create if params[:time_entries] && time_entry = params[:time_entries].first - puts time_entry.inspect - time_entry_defaults = { - loggable_type: "User", - loggable_id: current_user.id, - log_action_type: "ActsAsLoggable::UserAction"} - time_entry.merge!(time_entry_defaults) + time_entry.merge!({ loggable_id: current_user.id, + logger_id: current_user.id }) if time_entry[:bike_id] >= 0 - copy_defaults = { - copy_log: true, - copy_type: 'Bike', - copy_id: time_entry[:bike_id], - copy_action_type: 'ActsAsLoggable::BikeAction', - copy_action_id: 4 - } - time_entry.merge!( copy_defaults ) + time_entry.copy_to_bike_history(time_entry[:bike_id]) end - @time_entry = ::ActsAsLoggable::Log.new(time_entry.except(:bike_id)) + @time_entry = TimeEntry.new(time_entry.except(:bike_id)) if !@time_entry.save render json: { errors: @time_entry.errors }, status: 422 and return end diff --git a/app/models/time_entry.rb b/app/models/time_entry.rb new file mode 100644 index 0000000..1fbbc7a --- /dev/null +++ b/app/models/time_entry.rb @@ -0,0 +1,15 @@ +class TimeEntry < ActsAsLoggable::Log + default_scope where( loggable_type: "User", + logger_type: "User", + log_action_type: "ActsAsLoggable::UserAction") + + def copy_to_bike_history(bike_id) + self.assign_attributes({ + copy_log: true, + copy_type: 'Bike', + copy_id: bike_id, + copy_action_type: 'ActsAsLoggable::BikeAction', + copy_action_id: 4 + }) + end +end diff --git a/spec/controllers/api/time_entries_controller_spec.rb b/spec/controllers/api/time_entries_controller_spec.rb new file mode 100644 index 0000000..47fa8c0 --- /dev/null +++ b/spec/controllers/api/time_entries_controller_spec.rb @@ -0,0 +1,29 @@ +require 'spec_helper' + +describe Api::V1::TimeEntriesController do + + describe "#create" do + + context "as a user" do + let!(:user){ FactoryGirl.create(:user) } + + before(:each) do + sign_in user + end + + context "with no time entry in json data" do + it "returns error status" do + post :create + json = JSON.parse(@response.body) + expect(@response.code.to_i).to eql 422 + end + + it "returns an error message" do + post :create + json = JSON.parse(@response.body) + expect(json["errors"].first).to eql Api::V1::TimeEntriesController::EXPECTED_TIME_ENTRY + end + end + end + end +end