Skip to content

Instantly share code, notes, and snippets.

@raghuvarmabh
Created October 18, 2018 23:31
Show Gist options
  • Save raghuvarmabh/a45c3bb9d1b96caae06ec233a2ae60f8 to your computer and use it in GitHub Desktop.
Save raghuvarmabh/a45c3bb9d1b96caae06ec233a2ae60f8 to your computer and use it in GitHub Desktop.
alex coding challenge
diff --git a/app/models/provider.rb b/app/models/provider.rb
index c468731..d4afb3a 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
end
diff --git a/app/models/shipper.rb b/app/models/shipper.rb
index b2bb5df..82afdcd 100644
--- a/app/models/shipper.rb
+++ b/app/models/shipper.rb
@@ -1,3 +1,4 @@
class Shipper < ApplicationRecord
has_many :customers, as: :company
+ has_many :tracking_events
end
diff --git a/app/models/tracking_event.rb b/app/models/tracking_event.rb
new file mode 100644
index 0000000..7363391
--- /dev/null
+++ b/app/models/tracking_event.rb
@@ -0,0 +1,5 @@
+class TrackingEvent < ApplicationRecord
+ enum event_type: [ :status, :pickup, :exception ]
+ belongs_to :provider
+ belongs_to :shipper
+end
diff --git a/app/views/providers/show.json.jbuilder b/app/views/providers/show.json.jbuilder
index a7d3dfe..3308b22 100644
--- a/app/views/providers/show.json.jbuilder
+++ b/app/views/providers/show.json.jbuilder
@@ -7,3 +7,10 @@ json.customers do
json.createdAt customer.created_at
end
end
+
+json.tracking_events do
+ json.array! @provider.tracking_events do |tracking_event|
+ json.extract! tracking_event, :id, :description, :event_date, :event_type
+ json.createdAt tracking_event.created_at
+ end
+end
diff --git a/app/views/shippers/show.json.jbuilder b/app/views/shippers/show.json.jbuilder
index 251878b..d3f6c86 100644
--- a/app/views/shippers/show.json.jbuilder
+++ b/app/views/shippers/show.json.jbuilder
@@ -7,3 +7,10 @@ json.customers do
json.createdAt customer.created_at
end
end
+
+json.tracking_events do
+ json.array! @shipper.tracking_events do |tracking_event|
+ json.extract! tracking_event, :id, :description, :event_date, :event_type
+ json.createdAt tracking_event.created_at
+ end
+end
\ No newline at end of file
diff --git a/db/migrate/20181018210754_create_tracking_events.rb b/db/migrate/20181018210754_create_tracking_events.rb
new file mode 100644
index 0000000..1c27a63
--- /dev/null
+++ b/db/migrate/20181018210754_create_tracking_events.rb
@@ -0,0 +1,13 @@
+class CreateTrackingEvents < ActiveRecord::Migration[5.2]
+ def change
+ create_table :tracking_events do |t|
+ t.references :provider
+ t.references :shipper
+ t.string :description
+ t.datetime :event_date
+ t.integer :event_type, default: 0
+
+ t.timestamps
+ end
+ end
+end
diff --git a/db/schema.rb b/db/schema.rb
index 59dacd4..a92e3a1 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_18_210754) do
# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"
@@ -37,4 +37,16 @@ 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.bigint "provider_id"
+ t.bigint "shipper_id"
+ t.string "description"
+ t.datetime "event_date"
+ t.integer "event_type", default: 0
+ t.datetime "created_at", null: false
+ t.datetime "updated_at", null: false
+ t.index ["provider_id"], name: "index_tracking_events_on_provider_id"
+ t.index ["shipper_id"], name: "index_tracking_events_on_shipper_id"
+ end
+
end
diff --git a/test/fixtures/tracking_events.yml b/test/fixtures/tracking_events.yml
new file mode 100644
index 0000000..6759869
--- /dev/null
+++ b/test/fixtures/tracking_events.yml
@@ -0,0 +1,19 @@
+# Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html
+
+# This model initially had no columns defined. If you add columns to the
+# model remove the '{}' from the fixture names and add the columns immediately
+# below each fixture, per the syntax in the comments below
+#
+one:
+ provider: maersk
+ shipper: kratos
+ description: Package received for shipment
+ event_date: <%= DateTime.now - 2.days %>
+ event_type: 0
+
+two:
+ provider: maersk
+ shipper: kratos
+ description: Order picked up by customer
+ event_date: <%= DateTime.now %>
+ event_type: 1
\ No newline at end of file
diff --git a/test/models/tracking_event_test.rb b/test/models/tracking_event_test.rb
new file mode 100644
index 0000000..2db444c
--- /dev/null
+++ b/test/models/tracking_event_test.rb
@@ -0,0 +1,14 @@
+require 'test_helper'
+
+class TrackingEventTest < ActiveSupport::TestCase
+ test "it creates tracking_events with valid event_types" do
+ tracking_event = TrackingEvent.create(event_type: :status)
+ assert_not_nil tracking_event
+ end
+
+ test "it fails to create tracking_events with invalid event_types" do
+ assert_raises(Exception) {
+ tracking_event = TrackingEvent.create(event_type: :test)
+ }
+ end
+end
diff --git a/vue-spa/src/App.vue b/vue-spa/src/App.vue
index 37c5e6e..96bebbf 100644
--- a/vue-spa/src/App.vue
+++ b/vue-spa/src/App.vue
@@ -4,11 +4,15 @@
<h4>Customers:</h4>
<CustomerList :collection="provider.customers"></CustomerList>
+ <h4>Tracking Events:</h4>
+ <TrackingEventList :collection="provider.tracking_events"></TrackingEventList>
<h2>{{ shipper.name }} - Shipper</h2>
<h4>Customers:</h4>
<CustomerList :collection="shipper.customers"></CustomerList>
+ <h4>Tracking Events:</h4>
+ <TrackingEventList :collection="shipper.tracking_events"></TrackingEventList>
</div>
</template>
@@ -16,6 +20,7 @@
import axios from 'axios'
import moment from 'moment'
import CustomerList from '@/components/CustomerList'
+import TrackingEventList from '@/components/TrackingEventList'
const http = axios.create({
baseURL: 'http://localhost:3000/',
@@ -27,7 +32,8 @@ const http = axios.create({
export default {
name: 'app',
components: {
- 'CustomerList': CustomerList
+ 'CustomerList': CustomerList,
+ 'TrackingEventList': TrackingEventList
},
data () {
return {
diff --git a/vue-spa/src/components/TrackingEventList.vue b/vue-spa/src/components/TrackingEventList.vue
new file mode 100644
index 0000000..7046180
--- /dev/null
+++ b/vue-spa/src/components/TrackingEventList.vue
@@ -0,0 +1,32 @@
+<template>
+ <div>
+ <div v-for="event in collection" :key="event.id">
+ <h5>{{ event.description }}</h5>
+ <dl>
+ <dt>Type</dt>
+ <dd>{{ event.event_type }}</dd>
+
+ <dt>Date</dt>
+ <dd>{{ event.event_date | formatDate }}</dd>
+ </dl>
+ </div>
+ </div>
+</template>
+
+<script>
+import moment from 'moment'
+
+export default {
+ name: 'TrackingEventList',
+ props: {
+ collection: {
+ required: true
+ }
+ },
+ filters: {
+ formatDate (date) {
+ return moment(date).format('LLL')
+ }
+ }
+}
+</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment