Commit 87e0c32

mo khan <mo@mokhan.ca>
2014-11-11 00:22:38
try to load environments for each service.
1 parent f8cb5ae
app/assets/javascripts/controllers/service_environments.js.coffee
@@ -0,0 +1,2 @@
+App.ServiceEnvironmentsController = Ember.ArrayController.extend
+  sortProperties: ['name']
app/assets/javascripts/models/environment.js.coffee
@@ -0,0 +1,3 @@
+App.Environment = DS.Model.extend
+  name: DS.attr('string')
+  service: DS.belongsTo('service')
app/assets/javascripts/models/service.js.coffee
@@ -1,5 +1,6 @@
 App.Service = DS.Model.extend
   name: DS.attr('string')
+  environments: DS.hasMany('environment')
 
 App.Service.reopenClass
   valid: (fields) ->
app/assets/javascripts/routes/service_environments.js.coffee
@@ -0,0 +1,3 @@
+App.ServiceEnvironmentsRoute = Ember.Route.extend
+  model: (params) ->
+    @modelFor('service').get('environments')
app/assets/javascripts/templates/service/environments.hbs
@@ -0,0 +1,7 @@
+<h1>Environments</h1>
+
+<ul>
+  {{#each environment in controller}}
+  <li>{{environment.name}}</li>
+  {{/each}}
+</ul>
app/assets/javascripts/templates/service.hbs
@@ -1,3 +1,4 @@
 <h3>{{name}}</h3>
 
+{{#link-to 'service.environments' this}}environments{{/link-to}}
 {{outlet}}
app/assets/javascripts/router.js.coffee
@@ -10,4 +10,5 @@ App.Router.map ()->
       @route 'edit'
   @resource 'services', ->
     @route 'new'
-    @resource 'service', { path: ':service_id' }
+    @resource 'service', { path: ':service_id' }, ->
+      @route 'environments'
app/controllers/environments_controller.rb
@@ -0,0 +1,9 @@
+class EnvironmentsController < ApplicationController
+  def index
+    @environments = Environment.where(id: params[:ids])
+  end
+
+  def show
+    @environment = Environment.find(params[:id])
+  end
+end
app/models/environment.rb
@@ -0,0 +1,10 @@
+class Environment < ActiveRecord::Base
+  belongs_to :service
+  before_create :create_api_key
+
+  private
+
+  def create_api_key
+    self.api_key = SecureRandom.uuid
+  end
+end
app/models/service.rb
@@ -1,2 +1,3 @@
 class Service < ActiveRecord::Base
+  has_many :environments
 end
app/views/environments/index.json.jbuilder
@@ -0,0 +1,5 @@
+json.environments @environments do |environment|
+  json.id environment.id
+  json.name environment.name
+  json.service environment.service.id
+end
app/views/environments/show.json.jbuilder
@@ -0,0 +1,5 @@
+json.environment do
+  json.id @environment.id
+  json.name @environment.name
+  json.service @environment.service.id
+end
app/views/services/index.json.jbuilder
@@ -1,4 +1,5 @@
 json.services @services do |service|
   json.id service.id
   json.name service.name
+  json.environments service.environments.pluck(:id)
 end
config/routes.rb
@@ -2,6 +2,8 @@ Erkell::Application.routes.draw do
   resources :sessions, only: [:new, :create, :destroy]
   resources :videos, only: [:index, :create, :update, :destroy]
   resources :services, only: [:index, :create]
+  resources :environments, only: [:index, :show]
+
   get 'dashboard', to: 'dashboard#index'
   root 'dashboard#index'
   # The priority is based upon order of creation: first created -> highest priority.
db/migrate/20141110181704_create_environments.rb
@@ -0,0 +1,9 @@
+class CreateEnvironments < ActiveRecord::Migration
+  def change
+    create_table :environments do |t|
+      t.string :name
+      t.references :service, index: true
+      t.string :api_key
+    end
+  end
+end
db/schema.rb
@@ -11,11 +11,19 @@
 #
 # It's strongly recommended that you check this file into your version control system.
 
-ActiveRecord::Schema.define(version: 20141110173131) do
+ActiveRecord::Schema.define(version: 20141110181704) do
 
   # These are extensions that must be enabled in order to support this database
   enable_extension "plpgsql"
 
+  create_table "environments", force: true do |t|
+    t.string  "name"
+    t.integer "service_id"
+    t.string  "api_key"
+  end
+
+  add_index "environments", ["service_id"], name: "index_environments_on_service_id", using: :btree
+
   create_table "services", force: true do |t|
     t.string   "name"
     t.datetime "created_at"
spec/controllers/environments_controller_spec.rb
@@ -0,0 +1,29 @@
+require 'rails_helper'
+
+describe EnvironmentsController do
+  let(:user) { user_session.user }
+  let(:user_session) { create(:session) }
+
+  before :each do
+    session[:user_session_id] = user_session.id
+  end
+
+  describe '#index' do
+    let(:first_env) { create(:environment) }
+    let(:second_env) { create(:environment) }
+
+    it 'returns environments' do
+      xhr :get, :index, ids: [first_env.id, second_env.id]
+      expect(assigns(:environments)).to match_array([first_env, second_env])
+    end
+  end
+
+  describe "#show" do
+    let(:environment) { create(:environment) }
+
+    it 'returns info on the environment' do
+      xhr :get, :show, id: environment.id
+      expect(assigns(:environment)).to eql(environment)
+    end
+  end
+end
spec/models/environment_spec.rb
@@ -0,0 +1,10 @@
+require 'rails_helper'
+
+describe Environment do
+  describe ".create" do
+    it 'generates an API key' do
+      environment = Environment.create(name: 'development')
+      expect(environment.api_key).to_not be_nil
+    end
+  end
+end
spec/factories.rb
@@ -19,4 +19,8 @@ FactoryGirl.define do
   factory :service do
     name Faker::Company.name
   end
+
+  factory :environment do
+    name Faker::Lorem.word
+  end
 end