Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
erik coding challenge
From 43bbfa63f3efa6b10abcd7454d2953cb37b9efc9 Mon Sep 17 00:00:00 2001
From: Raghu <raghuvarma.bhr@gmail.com>
Date: Mon, 15 Oct 2018 15:34:18 -0700
Subject: [PATCH 1/2] Code challenge
---
vue-spa/src/App.vue | 14 ++++++++++++--
vue-spa/src/components/TrackableEvent.vue | 17 +++++++++++++++++
2 files changed, 29 insertions(+), 2 deletions(-)
create mode 100644 vue-spa/src/components/TrackableEvent.vue
diff --git a/vue-spa/src/App.vue b/vue-spa/src/App.vue
index 37c5e6e..738b044 100644
--- a/vue-spa/src/App.vue
+++ b/vue-spa/src/App.vue
@@ -9,6 +9,9 @@
<h4>Customers:</h4>
<CustomerList :collection="shipper.customers"></CustomerList>
+
+ <h4>Tackable Events</h4>
+ <TrackableEvent v-for='event in trackable_events" :key='event.event_date'></TrackableEvent>
</div>
</template>
@@ -16,6 +19,7 @@
import axios from 'axios'
import moment from 'moment'
import CustomerList from '@/components/CustomerList'
+import TrackableEvent from '@/components/TrackableEvent'
const http = axios.create({
baseURL: 'http://localhost:3000/',
@@ -27,12 +31,14 @@ const http = axios.create({
export default {
name: 'app',
components: {
- 'CustomerList': CustomerList
+ 'CustomerList': CustomerList,
+ TrackableEvent
},
data () {
return {
provider: {},
- shipper: {}
+ shipper: {},
+ trackable_events: []
}
},
mounted () {
@@ -43,6 +49,10 @@ export default {
http.get('shipper').then((response) => {
this.shipper = response.data
})
+
+ http.get('trackable_events').then((response) => {
+ this.trackable_events = response.data
+ })
},
filters: {
formatDate (date) {
diff --git a/vue-spa/src/components/TrackableEvent.vue b/vue-spa/src/components/TrackableEvent.vue
new file mode 100644
index 0000000..0a63aac
--- /dev/null
+++ b/vue-spa/src/components/TrackableEvent.vue
@@ -0,0 +1,17 @@
+<template>
+ <div>
+ {{ event.event_type }}
+ {{ even.event_date }}
+ </div>
+</template>
+
+<script>
+export default {
+ props: {
+ event: {
+ type: Object,
+ required: true
+ }
+ }
+}
+</script>
--
2.14.3 (Apple Git-98)
From 642d2b13e603ecc59f47f2608702ecaeefb6136f Mon Sep 17 00:00:00 2001
From: Raghu <raghuvarma.bhr@gmail.com>
Date: Mon, 15 Oct 2018 16:24:05 -0700
Subject: [PATCH 2/2] erik code challenge rails code
---
app/controllers/tracking_events_controller.rb | 53 ++++++++++++++++++++++
app/models/provider.rb | 1 +
app/models/shipper.rb | 2 +
app/models/tracking_event.rb | 5 ++
.../tracking_events/_tracking_event.json.jbuilder | 2 +
app/views/tracking_events/index.json.jbuilder | 1 +
app/views/tracking_events/show.json.jbuilder | 1 +
config/routes.rb | 3 +-
.../20181015214956_create_tracking_events.rb | 11 +++++
db/schema.rb | 12 ++++-
.../controllers/tracking_events_controller_test.rb | 38 ++++++++++++++++
test/fixtures/tracking_events.yml | 21 +++++++++
test/models/tracking_event_test.rb | 7 +++
13 files changed, 155 insertions(+), 2 deletions(-)
create mode 100644 app/controllers/tracking_events_controller.rb
create mode 100644 app/models/tracking_event.rb
create mode 100644 app/views/tracking_events/_tracking_event.json.jbuilder
create mode 100644 app/views/tracking_events/index.json.jbuilder
create mode 100644 app/views/tracking_events/show.json.jbuilder
create mode 100644 db/migrate/20181015214956_create_tracking_events.rb
create mode 100644 test/controllers/tracking_events_controller_test.rb
create mode 100644 test/fixtures/tracking_events.yml
create mode 100644 test/models/tracking_event_test.rb
diff --git a/app/controllers/tracking_events_controller.rb b/app/controllers/tracking_events_controller.rb
new file mode 100644
index 0000000..d941fda
--- /dev/null
+++ b/app/controllers/tracking_events_controller.rb
@@ -0,0 +1,53 @@
+class TrackingEventsController < ApplicationController
+ before_action :set_tracking_event, only: [:show, :update, :destroy]
+
+ # GET /tracking_events
+ # GET /tracking_events.json
+ def index
+ @tracking_events = TrackingEvent.all
+ end
+
+ # GET /tracking_events/1
+ # GET /tracking_events/1.json
+ def show
+ end
+
+ # POST /tracking_events
+ # POST /tracking_events.json
+ def create
+ @tracking_event = TrackingEvent.new(tracking_event_params)
+
+ if @tracking_event.save
+ render :show, status: :created, location: @tracking_event
+ else
+ render json: @tracking_event.errors, status: :unprocessable_entity
+ end
+ end
+
+ # PATCH/PUT /tracking_events/1
+ # PATCH/PUT /tracking_events/1.json
+ def update
+ if @tracking_event.update(tracking_event_params)
+ render :show, status: :ok, location: @tracking_event
+ else
+ render json: @tracking_event.errors, status: :unprocessable_entity
+ end
+ end
+
+ # DELETE /tracking_events/1
+ # DELETE /tracking_events/1.json
+ def destroy
+ @tracking_event.destroy
+ end
+
+ private
+ # Use callbacks to share common setup or constraints between actions.
+ def set_tracking_event
+ @tracking_event = TrackingEvent.find(params[:id])
+ end
+
+ # Never trust parameters from the scary internet, only allow the white list through.
+ def tracking_event_params
+ params.require(:tracking_event).permit(:trackable, :event_date, :event_type)
+ end
+end
diff --git a/app/models/provider.rb b/app/models/provider.rb
index c468731..84ed509 100644
--- a/app/models/provider.rb
+++ b/app/models/provider.rb
@@ -1,3 +1,4 @@
class Provider < ApplicationRecord
has_many :customers, as: :company
+ has_many :tracking_events, :as => :trackable
end
diff --git a/app/models/shipper.rb b/app/models/shipper.rb
index b2bb5df..536826b 100644
--- a/app/models/shipper.rb
+++ b/app/models/shipper.rb
@@ -1,3 +1,5 @@
class Shipper < ApplicationRecord
has_many :customers, as: :company
+ has_many :tracking_events, :as => :trackables
+
end
diff --git a/app/models/tracking_event.rb b/app/models/tracking_event.rb
new file mode 100644
index 0000000..58704a8
--- /dev/null
+++ b/app/models/tracking_event.rb
@@ -0,0 +1,5 @@
+class TrackingEvent < ApplicationRecord
+ belongs_to :trackable, :polymorphic => true
+
+ enum :event_type => [ :status, :pickup, :exception ]
+end
diff --git a/app/views/tracking_events/_tracking_event.json.jbuilder b/app/views/tracking_events/_tracking_event.json.jbuilder
new file mode 100644
index 0000000..8a0f489
--- /dev/null
+++ b/app/views/tracking_events/_tracking_event.json.jbuilder
@@ -0,0 +1,2 @@
+json.extract! tracking_event, :id, :trackable, :event_date, :event_type, :created_at, :updated_at
+json.url tracking_event_url(tracking_event, format: :json)
diff --git a/app/views/tracking_events/index.json.jbuilder b/app/views/tracking_events/index.json.jbuilder
new file mode 100644
index 0000000..40cd1c7
--- /dev/null
+++ b/app/views/tracking_events/index.json.jbuilder
@@ -0,0 +1 @@
+json.array! @tracking_events, partial: 'tracking_events/tracking_event', as: :tracking_event
diff --git a/app/views/tracking_events/show.json.jbuilder b/app/views/tracking_events/show.json.jbuilder
new file mode 100644
index 0000000..4d7c696
--- /dev/null
+++ b/app/views/tracking_events/show.json.jbuilder
@@ -0,0 +1 @@
+json.partial! "tracking_events/tracking_event", tracking_event: @tracking_event
diff --git a/config/routes.rb b/config/routes.rb
index 6363414..4f874bb 100644
--- a/config/routes.rb
+++ b/config/routes.rb
@@ -1,4 +1,5 @@
Rails.application.routes.draw do
- resource :provider, only: :show
+ resources :tracking_events
+ resource :provider, only: :shows
resource :shipper, only: :show
end
diff --git a/db/migrate/20181015214956_create_tracking_events.rb b/db/migrate/20181015214956_create_tracking_events.rb
new file mode 100644
index 0000000..205c81e
--- /dev/null
+++ b/db/migrate/20181015214956_create_tracking_events.rb
@@ -0,0 +1,11 @@
+class CreateTrackingEvents < ActiveRecord::Migration[5.2]
+ def change
+ create_table :tracking_events do |t|
+ t.references :trackable, null: false, polymorphic: true
+ t.datetime :event_date
+ t.integer :event_type
+
+ t.timestamps
+ end
+ end
+end
diff --git a/db/schema.rb b/db/schema.rb
index 59dacd4..47a9bcd 100644
--- a/db/schema.rb
+++ b/db/schema.rb
@@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.
-ActiveRecord::Schema.define(version: 2018_10_02_224410) do
+ActiveRecord::Schema.define(version: 2018_10_15_214956) do
# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"
@@ -37,4 +37,14 @@ ActiveRecord::Schema.define(version: 2018_10_02_224410) do
t.datetime "updated_at", null: false
end
+ create_table "tracking_events", force: :cascade do |t|
+ t.string "trackable_type", null: false
+ t.bigint "trackable_id", null: false
+ t.datetime "event_date"
+ t.integer "event_type"
+ t.datetime "created_at", null: false
+ t.datetime "updated_at", null: false
+ t.index ["trackable_type", "trackable_id"], name: "index_tracking_events_on_trackable_type_and_trackable_id"
+ end
+
end
diff --git a/test/controllers/tracking_events_controller_test.rb b/test/controllers/tracking_events_controller_test.rb
new file mode 100644
index 0000000..0205e9d
--- /dev/null
+++ b/test/controllers/tracking_events_controller_test.rb
@@ -0,0 +1,38 @@
+require 'test_helper'
+
+class TrackingEventsControllerTest < ActionDispatch::IntegrationTest
+ setup do
+ @tracking_event = tracking_events(:one)
+ end
+
+ test "should get index" do
+ get tracking_events_url, as: :json
+ assert_response :success
+ end
+
+ test "should create tracking_event" do
+ assert_difference('TrackingEvent.count') do
+ post tracking_events_url, params: { tracking_event: { event_date: @tracking_event.event_date, event_type: @tracking_event.event_type, trackable: @tracking_event.trackable } }, as: :json
+ end
+
+ assert_response 201
+ end
+
+ test "should show tracking_event" do
+ get tracking_event_url(@tracking_event), as: :json
+ assert_response :success
+ end
+
+ test "should update tracking_event" do
+ patch tracking_event_url(@tracking_event), params: { tracking_event: { event_date: @tracking_event.event_date, event_type: @tracking_event.event_type, trackable: @tracking_event.trackable } }, as: :json
+ assert_response 200
+ end
+
+ test "should destroy tracking_event" do
+ assert_difference('TrackingEvent.count', -1) do
+ delete tracking_event_url(@tracking_event), as: :json
+ end
+
+ assert_response 204
+ end
+end
diff --git a/test/fixtures/tracking_events.yml b/test/fixtures/tracking_events.yml
new file mode 100644
index 0000000..49d057f
--- /dev/null
+++ b/test/fixtures/tracking_events.yml
@@ -0,0 +1,21 @@
+# Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html
+
+one:
+ trackable: maersk (Provider)
+ event_date: 2018-10-15 14:49:56
+ event_type: 1
+
+two:
+ trackable: maersk (Provider)
+ event_date: 2018-10-15 14:49:56
+ event_type: 2
+
+three:
+ trackable: kratos (Shipper)
+ event_date: 2018-10-15 14:49:56
+ event_type: 0
+
+four:
+ trackable: kratos (Shipper)
+ event_date: 2018-10-15 14:49:56
+ event_type: 1
diff --git a/test/models/tracking_event_test.rb b/test/models/tracking_event_test.rb
new file mode 100644
index 0000000..5cdf597
--- /dev/null
+++ b/test/models/tracking_event_test.rb
@@ -0,0 +1,7 @@
+require 'test_helper'
+
+class TrackingEventTest < ActiveSupport::TestCase
+ # test "the truth" do
+ # assert true
+ # end
+end
--
2.14.3 (Apple Git-98)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment