Commit e040914

mo khan <mo@mokhan.ca>
2014-11-10 01:24:57
persist videos and add some test gems.
1 parent 5e80037
app/assets/javascripts/models/video.js.coffee
@@ -1,3 +1,4 @@
 App.Video = DS.Model.extend
   title: DS.attr('string')
   description: DS.attr('string')
+  uri: DS.attr('string')
app/controllers/videos_controller.rb
@@ -1,12 +1,15 @@
 class VideosController < ApplicationController
   def index
-    @videos = [
-      OpenStruct.new(id: 1, title: 'getting jiggy with it', description: 'supa fly funky dancing'),
-      OpenStruct.new(id: 2, title: 'getting jiggy with it', description: 'supa fly funky dancing'),
-    ]
+    @videos = Video.all
   end
 
   def create
-    raise "heck"
+    @video = current_user.videos.create!(video_params)
+  end
+
+  private
+
+  def video_params
+    params.require(:video).permit(:title, :description, :uri)
   end
 end
app/models/user.rb
@@ -1,3 +1,5 @@
 class User < ActiveRecord::Base
+  has_many :sessions
+  has_many :videos
   has_secure_password
 end
app/models/video.rb
@@ -0,0 +1,3 @@
+class Video < ActiveRecord::Base
+  belongs_to :user
+end
app/views/videos/_video.json.jbuilder
@@ -0,0 +1,4 @@
+json.id @video.id
+json.title @video.title
+json.description @video.description
+json.uri @video.uri
app/views/videos/create.json.jbuilder
@@ -0,0 +1,1 @@
+json.partial! @video
app/views/videos/index.jbuilder
@@ -1,7 +1,5 @@
 json.videos do
   json.array! @videos do |video|
-    json.id video.id
-    json.title video.title
-    json.description video.description
+    json.partial! video
   end
 end
db/migrate/20141110011156_create_videos.rb
@@ -0,0 +1,10 @@
+class CreateVideos < ActiveRecord::Migration
+  def change
+    create_table :videos do |t|
+      t.string :title
+      t.text :description
+      t.string :uri
+      t.references :user
+    end
+  end
+end
db/schema.rb
@@ -11,7 +11,7 @@
 #
 # It's strongly recommended that you check this file into your version control system.
 
-ActiveRecord::Schema.define(version: 20140410032614) do
+ActiveRecord::Schema.define(version: 20141110011156) do
 
   # These are extensions that must be enabled in order to support this database
   enable_extension "plpgsql"
@@ -28,4 +28,11 @@ ActiveRecord::Schema.define(version: 20140410032614) do
     t.string "password_digest"
   end
 
+  create_table "videos", force: true do |t|
+    t.string  "title"
+    t.text    "description"
+    t.string  "uri"
+    t.integer "user_id"
+  end
+
 end
spec/controllers/videos_controller_spec.rb
@@ -1,17 +1,33 @@
 require 'rails_helper'
 
 describe VideosController do
-  context "#create" do
-    let(:user) { User.create }
-    let(:user_session) { Session.create!(user_id: user.id) }
+  let(:user_session) { create(:session) }
+
+  before :each do
+    session[:user_session_id] = user_session.id
+  end
+
+  describe "#index" do
+    let!(:video) { create(:video) }
 
-    before :each do
-      session[:user_session_id] = user_session.id
+    it 'returns all the videos' do
+      xhr :get, :index
+      expect(assigns(:videos)).to include(video)
     end
+  end
+
+
+  context "#create" do
+    render_views
 
     it "creates a new video" do
-      post :create, video: { title: 'hello', url: '' }
-      response.should be_success
+      xhr :post, :create, video: { title: 'hello', uri: 'http://youtu.be/jghvdDB-t30?list=PLYuXlc3r66uFJErV5rYpZRcD8oDGtQlQc' }
+      expect(response).to be_success
+      json = JSON.parse(response.body)
+      expect(json['id']).to_not be_nil
+      expect(json['title']).to eql('hello')
+      expect(json['description']).to be_nil
+      expect(json['uri']).to eql("http://youtu.be/jghvdDB-t30?list=PLYuXlc3r66uFJErV5rYpZRcD8oDGtQlQc")
     end
   end
 end
spec/factories.rb
@@ -0,0 +1,18 @@
+FactoryGirl.define do
+  factory :video do
+    title Faker::Lorem.word
+    description Faker::Lorem.words(50).join(' ')
+    uri Faker::Internet.uri('https')
+    user
+  end
+
+  factory :user do
+    email Faker::Internet.email
+    password "password"
+    password_confirmation "password"
+  end
+
+  factory :session do
+    user
+  end
+end
spec/rails_helper.rb
@@ -25,8 +25,26 @@ require 'rspec/rails'
 ActiveRecord::Migration.maintain_test_schema!
 
 RSpec.configure do |config|
+  config.include FactoryGirl::Syntax::Methods
+
+  config.before(:suite) do
+    DatabaseCleaner.clean_with(:truncation)
+  end
+  config.before(:each) do
+    DatabaseCleaner.strategy = :transaction
+  end
+  config.before(:each, js: true) do
+    DatabaseCleaner.strategy = :truncation
+  end
+  config.before(:each) do
+    DatabaseCleaner.start
+  end
+  config.after(:each) do
+    DatabaseCleaner.clean
+  end
+
   # Remove this line if you're not using ActiveRecord or ActiveRecord fixtures
-  config.fixture_path = "#{::Rails.root}/spec/fixtures"
+  #config.fixture_path = "#{::Rails.root}/spec/fixtures"
 
   # If you're not using ActiveRecord, or you'd prefer not to run each of your
   # examples within a transaction, remove the following line or assign false
Gemfile
@@ -48,4 +48,7 @@ group :development, :test do
   gem 'pry'
   gem 'byebug'
   gem 'spring'
+  gem 'factory_girl_rails'
+  gem 'database_cleaner'
+  gem 'ffaker'
 end
Gemfile.lock
@@ -65,6 +65,7 @@ GEM
       execjs
     coffee-script-source (1.8.0)
     columnize (0.8.9)
+    database_cleaner (1.3.0)
     debugger-linecache (1.2.0)
     diff-lcs (1.2.5)
     ember-data-source (1.0.0.beta.11)
@@ -73,6 +74,12 @@ GEM
       handlebars-source (~> 1.0)
     erubis (2.7.0)
     execjs (2.2.2)
+    factory_girl (4.5.0)
+      activesupport (>= 3.0.0)
+    factory_girl_rails (4.5.0)
+      factory_girl (~> 4.5.0)
+      railties (>= 3.0.0)
+    ffaker (1.25.0)
     handlebars-source (1.3.0)
     hike (1.2.3)
     i18n (0.6.11)
@@ -171,8 +178,11 @@ DEPENDENCIES
   bootstrap-sass
   byebug
   coffee-rails
+  database_cleaner
   ember-rails!
   ember-source
+  factory_girl_rails
+  ffaker
   jbuilder
   jquery-rails
   pg