Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions app/controllers/builds_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,13 @@ def new

render json: {build: @build, build_type_components: build_type.components}
end

private
def collection
@builds ||= params[:user_id] ? get_user(params[:user_id]).builds : Build.all
end

def get_user(id)
User.find(id)
end
end
3 changes: 2 additions & 1 deletion app/models/build.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
class Build < ActiveRecord::Base

# Relations
belongs_to :type, class_name: 'BuildType', foreign_key: 'build_type_id'
belongs_to :user
belongs_to :build_type
has_and_belongs_to_many :components

end
2 changes: 1 addition & 1 deletion app/models/component.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ class Component < ActiveRecord::Base

# Relations
belongs_to :manufacturer
belongs_to :type, class_name: 'ComponentType', foreign_key: 'component_type_id'
belongs_to :component_type
has_and_belongs_to_many :builds

end
3 changes: 3 additions & 0 deletions app/models/user.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
class User < ActiveRecord::Base

# Relationships
has_many :builds, class_name: 'Build'

def self.from_omniauth(auth)
where(auth.slice(:provider, :uid)).first_or_initialize.tap do |user|
user.provider = auth.provider
Expand Down
2 changes: 1 addition & 1 deletion app/views/builds/index.json.jbuilder
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ json.array! collection do |build|
json.name build.name
json.image_url "http://placehold.it/250x200"
json.type do
json.name build.type.name
json.name build.build_type.name
end
json.comments build.components, partial: 'components/components', as: :component
end
5 changes: 5 additions & 0 deletions db/migrate/20131220172932_add_user_references_to_builds.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class AddUserReferencesToBuilds < ActiveRecord::Migration
def change
add_reference :builds, :user, index: true
end
end
8 changes: 8 additions & 0 deletions db/migrate/20131220173814_create_users.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
class CreateUsers < ActiveRecord::Migration
def change
create_table :users do |t|
t.string :name
t.string :email
end
end
end
9 changes: 8 additions & 1 deletion db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema.define(version: 20131219172529) do
ActiveRecord::Schema.define(version: 20131220173814) do

# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"
Expand All @@ -34,9 +34,11 @@
t.datetime "created_at"
t.datetime "updated_at"
t.integer "build_type_id"
t.integer "user_id"
end

add_index "builds", ["build_type_id"], name: "index_builds_on_build_type_id", using: :btree
add_index "builds", ["user_id"], name: "index_builds_on_user_id", using: :btree

create_table "builds_components", id: false, force: true do |t|
t.integer "build_id", null: false
Expand Down Expand Up @@ -71,4 +73,9 @@
t.datetime "updated_at"
end

create_table "users", force: true do |t|
t.string "name"
t.string "email"
end

end
34 changes: 32 additions & 2 deletions spec/controllers/builds_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,39 @@
end


describe '#index' do
describe 'GET index' do

it 'assigns existing builds' do
context "requests all builds" do
let!(:build) { FactoryGirl.build(:build) }

before do
Build.stub(:all) { [build] }
build.stub(:build_type) { FactoryGirl.build(:build_type) }
get :index, format: :json
@body = response.body
end

it 'assigns existing builds' do
expect(assigns[:builds].first).to be_a Build
end

it { expect(@body).to include "\"name\":\"MyString\"" }
it { expect(@body).to include "\"image_url\":\"http://placehold.it/250x200\"" }
it { expect(@body).to include "\"type\":{\"name\":\"road\"" }
it { expect(@body).to include "\"comments\":[]" }
end

context "requests builds of user" do
let!(:build) { FactoryGirl.build(:build, :with_user) }
let!(:user) { build.user }

it 'assigns existing builds' do
controller.stub(:get_user).with(build.user_id).and_return(user)
user.should_receive(:builds) { [build] }

get :index, user_id: build.user_id, format: :json
expect(assigns[:builds].first).to be_a(Build)
end
end

end
Expand Down
5 changes: 5 additions & 0 deletions spec/factories/build_types.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
FactoryGirl.define do
factory :build_type do
name "road"
end
end
7 changes: 7 additions & 0 deletions spec/factories/builds.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,12 @@

FactoryGirl.define do
factory :build do
name "MyString"

build_type

trait :with_user do
user
end
end
end
13 changes: 9 additions & 4 deletions spec/factories/users.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,15 @@

FactoryGirl.define do
factory :user do
provider "MyString"
uid "MyString"

name "MyString"
oauth_token "MyString"
oauth_expires_at "2013-10-07 20:40:28"
email "mystring@example.com"

trait :provider do
provider "MyString"
uid "MyString"
oauth_token "MyString"
oauth_expires_at "2013-10-07 20:40:28"
end
end
end
3 changes: 2 additions & 1 deletion spec/models/build_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@

describe Build do
it { should respond_to :name }
it { should belong_to(:type).class_name('BuildType').with_foreign_key('build_type_id') }
it { should belong_to(:user) }
it { should belong_to(:build_type).class_name('BuildType') }
end
2 changes: 1 addition & 1 deletion spec/models/component_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@
it { should have_db_column(:weight).of_type(:integer) }

it { should belong_to(:manufacturer).class_name('Manufacturer') }
it { should belong_to(:type).class_name('ComponentType').with_foreign_key('component_type_id') }
it { should belong_to(:component_type).class_name('ComponentType') }
it { should have_and_belong_to_many(:builds).class_name('Build') }
end
3 changes: 2 additions & 1 deletion spec/models/user_spec.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
require 'spec_helper'

describe User do
pending "add some examples to (or delete) #{__FILE__}"
it { have_db_column :name }
it { should have_many(:builds).class_name('Build') }
end