Commit afb0a4a

mo khan <mo@mokhan.ca>
2014-11-11 02:33:58
create a new environment.
1 parent 13f694d
app/assets/javascripts/controllers/service_environments_new.js.coffee
@@ -0,0 +1,13 @@
+App.ServiceEnvironmentsNewController = Ember.Controller.extend
+  actions:
+    createEnvironment: ->
+      fields = @get('fields')
+      if App.Environment.valid(fields)
+        environment = @store.createRecord('environment', @get('fields'))
+        serviceId = @get('fields')['serviceId']
+        service = @store.find('service', serviceId).then (service) =>
+          environment.set('service', service)
+          environment.save().then =>
+            @transitionTo('environment', environment)
+      else
+        @set('showError', true)
app/assets/javascripts/controllers/services_new.js.coffee
@@ -5,6 +5,6 @@ App.ServicesNewController = Ember.Controller.extend
       if App.Service.valid(fields)
         service = @store.createRecord('service', @get('fields'))
         service.save().then =>
-          @transitionTo('service', service)
+          @transitionTo('services', service)
       else
         @set('showError', true)
app/assets/javascripts/models/environment.js.coffee
@@ -2,3 +2,7 @@ App.Environment = DS.Model.extend
   name: DS.attr('string')
   apiKey: DS.attr('string')
   service: DS.belongsTo('service')
+
+App.Environment.reopenClass
+  valid: (fields) ->
+    fields.name
app/assets/javascripts/routes/service_environments_new.js.coffee
@@ -0,0 +1,5 @@
+App.ServiceEnvironmentsNewRoute = Ember.Route.extend
+  setupController: (controller) ->
+    service = @modelFor('service')
+    controller.set('fields', { serviceId: service.get('id') })
+    controller.set('showError', false)
app/assets/javascripts/templates/service/environments/new.hbs
@@ -0,0 +1,14 @@
+<h1>New Environment</h1>
+{{#if showError}}
+<p>Environment must have a name.</p>
+{{/if}}
+<form>
+<fieldset>
+  <dl>
+    <dt><label>Name:</label></dt>
+    <dd>{{view Ember.TextField value=fields.name}}</dd>
+  </dl>
+</fieldset>
+</form>
+
+<input type="submit" value="Create" {{action 'createEnvironment'}} />
app/assets/javascripts/templates/service/environments.hbs
@@ -1,4 +1,5 @@
 <h1>Environments</h1>
+{{#link-to 'service.environments.new'}}New{{/link-to}}
 
 <ul>
   {{#each environment in controller}}
app/assets/javascripts/router.js.coffee
@@ -12,4 +12,5 @@ App.Router.map ()->
     @route 'new'
     @resource 'service', { path: ':service_id' }, ->
       @route 'environments', ->
+        @route 'new'
         @resource 'environment', { path: ':environment_id' }
app/controllers/environments_controller.rb
@@ -6,4 +6,15 @@ class EnvironmentsController < ApplicationController
   def show
     @environment = Environment.find(params[:id])
   end
+
+  def create
+    service = Service.find(params[:environment][:service_id])
+    @environment = service.environments.create!(environment_params)
+  end
+
+  private
+
+  def environment_params
+    params.require(:environment).permit(:name)
+  end
 end
app/controllers/services_controller.rb
@@ -5,7 +5,6 @@ class ServicesController < ApplicationController
 
   def create
     Service.create!(application_params)
-    render nothing: true
   end
 
   private
app/views/environments/create.json.jbuilder
@@ -0,0 +1,6 @@
+json.environment do
+  json.id @environment.id
+  json.name @environment.name
+  json.apiKey @environment.api_key
+  json.service @environment.service.id
+end
app/views/services/_service.json.jbuilder
@@ -0,0 +1,3 @@
+json.id service.id
+json.name service.name
+json.environments service.environments.pluck(:id)
app/views/services/create.json.jbuilder
@@ -0,0 +1,3 @@
+json.service do
+  json.partial! service, service: @service
+end
app/views/services/index.json.jbuilder
@@ -1,5 +1,3 @@
 json.services @services do |service|
-  json.id service.id
-  json.name service.name
-  json.environments service.environments.pluck(:id)
+  json.partial! service, service: service
 end
config/routes.rb
@@ -2,7 +2,7 @@ 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]
+  resources :environments, only: [:index, :show, :create]
 
   get 'dashboard', to: 'dashboard#index'
   root 'dashboard#index'
db/migrate/20140409034211_create_users.rb
@@ -3,6 +3,7 @@ class CreateUsers < ActiveRecord::Migration
     create_table :users do |t|
       t.string :email
       t.string :password_digest
+      t.timestamps
     end
   end
 end
db/migrate/20141110181704_create_environments.rb
@@ -4,6 +4,7 @@ class CreateEnvironments < ActiveRecord::Migration
       t.string :name
       t.references :service, index: true
       t.string :api_key
+      t.timestamps
     end
   end
 end
db/schema.rb
@@ -17,9 +17,11 @@ ActiveRecord::Schema.define(version: 20141110181704) do
   enable_extension "plpgsql"
 
   create_table "environments", force: true do |t|
-    t.string  "name"
-    t.integer "service_id"
-    t.string  "api_key"
+    t.string   "name"
+    t.integer  "service_id"
+    t.string   "api_key"
+    t.datetime "created_at"
+    t.datetime "updated_at"
   end
 
   add_index "environments", ["service_id"], name: "index_environments_on_service_id", using: :btree
@@ -38,8 +40,10 @@ ActiveRecord::Schema.define(version: 20141110181704) do
   end
 
   create_table "users", force: true do |t|
-    t.string "email"
-    t.string "password_digest"
+    t.string   "email"
+    t.string   "password_digest"
+    t.datetime "created_at"
+    t.datetime "updated_at"
   end
 
   create_table "videos", force: true do |t|
lib/tasks/populate.rake
@@ -1,7 +1,8 @@
 namespace :db do
   task populate: :environment do
-    user = User.last
     Video.destroy_all
+    user = User.create!(email: Faker::Internet.email, password: 'password', password_confirmation: 'password')
+    puts user.email
 
     20.times do
       user.videos.create(
@@ -10,6 +11,5 @@ namespace :db do
         uri: Faker::Internet.uri('https')
       )
     end
-
   end
 end
spec/controllers/environments_controller_spec.rb
@@ -26,4 +26,15 @@ describe EnvironmentsController do
       expect(assigns(:environment)).to eql(environment)
     end
   end
+
+  describe "#create" do
+    let(:service) { create(:service) }
+
+    it 'creates a new environment' do
+      xhr :post, :create, environment: { name: 'development', service_id: service.id }
+
+      expect(Environment.count).to eql(1)
+      expect(Environment.last.name).to eql('development')
+    end
+  end
 end