Skip to content

Commit

Permalink
receber ID do usuário e responder com um JSON dos seus builds
Browse files Browse the repository at this point in the history
  • Loading branch information
digoonrails committed Dec 21, 2013
1 parent df5ec11 commit 899cc0a
Show file tree
Hide file tree
Showing 15 changed files with 95 additions and 13 deletions.
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

This comment has been minimized.

Copy link
@digoonrails

digoonrails Dec 21, 2013

Author Contributor

@rbrancher e @lucas aqui voltei para o padrão do Rails porque estava dando conflito ao instanciar um novo objeto. sei q o type era um capo reservado para o tipo do objeto em caso de herança, não sei se mudaram.

irb(main):010:0> bt = BuildType.first
  BuildType Load (1.1ms)  SELECT "build_types".* FROM "build_types" ORDER BY "build_types"."id" ASC LIMIT 1
=> #<BuildType id: 1, name: "road", created_at: "2013-12-19 20:06:55", updated_at: "2013-12-19 20:06:55">
irb(main):011:0> Build.new(name: 'b', type: bt)
NoMethodError: undefined method `safe_constantize' for #<BuildType:0x007fedc51f60c0>
    from /Users/rodrigop/.rbenv/versions/2.0.0-p247/lib/ruby/gems/2.0.0/gems/activemodel-4.0.0/lib/active_model/attribute_methods.rb:436:in `method_missing'
    from /Users/rodrigop/.rbenv/versions/2.0.0-p247/lib/ruby/gems/2.0.0/gems/activerecord-4.0.0/lib/active_record/attribute_methods.rb:131:in `method_missing'
    from /Users/rodrigop/.rbenv/versions/2.0.0-p247/lib/ruby/gems/2.0.0/gems/activerecord-4.0.0/lib/active_record/inheritance.rb:175:in `subclass_from_attrs'
    from /Users/rodrigop/.rbenv/versions/2.0.0-p247/lib/ruby/gems/2.0.0/gems/activerecord-4.0.0/lib/active_record/inheritance.rb:22:in `new'
    from (irb):11
    from /Users/rodrigop/.rbenv/versions/2.0.0-p247/lib/ruby/gems/2.0.0/gems/railties-4.0.0/lib/rails/commands/console.rb:90:in `start'
    from /Users/rodrigop/.rbenv/versions/2.0.0-p247/lib/ruby/gems/2.0.0/gems/railties-4.0.0/lib/rails/commands/console.rb:9:in `start'
    from /Users/rodrigop/.rbenv/versions/2.0.0-p247/lib/ruby/gems/2.0.0/gems/railties-4.0.0/lib/rails/commands.rb:64:in `<top (required)>'
    from bin/rails:4:in `require'
    from bin/rails:4:in `<main>'
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 "[email protected]"

trait :provider do
provider "MyString"
uid "MyString"
oauth_token "MyString"
oauth_expires_at "2013-10-07 20:40:28"
end
end

This comment has been minimized.

Copy link
@digoonrails

digoonrails Dec 21, 2013

Author Contributor

@lucasrenan deixei separado só para a gente não esquecer dos campos.

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

0 comments on commit 899cc0a

Please sign in to comment.