Permalink
Browse files

Connected Users to Companies and People

  • Loading branch information...
jcasimir committed Apr 24, 2011
1 parent 46ebc3e commit 5b7569c401347d34d764036e1b3004c622080d5d
View
@@ -14,4 +14,5 @@ group :development, :test do
gem "mocha"
gem "capybara"
gem "autotest"
+ gem "fabrication"
end
View
@@ -51,6 +51,7 @@ GEM
diff-lcs (1.1.2)
erubis (2.6.6)
abstract (>= 1.0.0)
+ fabrication (0.9.5)
faraday (0.6.1)
addressable (~> 2.2.4)
multipart-post (~> 1.1.0)
@@ -185,6 +186,7 @@ PLATFORMS
DEPENDENCIES
autotest
capybara
+ fabrication
haml
heroku
jquery-rails
View
@@ -1,6 +1,7 @@
class Company < ActiveRecord::Base
attr_accessible :name
- validates_presence_of :name
+ validates_presence_of :name
+ belongs_to :user
include Contact
def to_s
View
@@ -1,6 +1,7 @@
class Person < ActiveRecord::Base
attr_accessible :first_name, :last_name
validates_presence_of :first_name, :last_name
+ belongs_to :user
include Contact
def to_s
View
@@ -1,4 +1,6 @@
class User < ActiveRecord::Base
+ has_many :people
+ has_many :companies
def self.find_or_create_by_auth(auth_data)
user = self.find_or_create_by_provider_and_uid(auth_data["provider"], auth_data["uid"])
View
@@ -40,5 +40,10 @@ class Application < Rails::Application
config.filter_parameters += [:password]
config.action_view.javascript_expansions[:defaults] = %w(jquery rails)
+
+ config.generators do |g|
+ g.test_framework :rspec, :fixture => true
+ g.fixture_replacement :fabrication
+ end
end
end
@@ -0,0 +1,9 @@
+class AddUserIdToPeople < ActiveRecord::Migration
+ def self.up
+ add_column :people, :user_id, :integer
+ end
+
+ def self.down
+ remove_column :people, :user_id
+ end
+end
@@ -0,0 +1,9 @@
+class AddUserIdToCompanies < ActiveRecord::Migration
+ def self.up
+ add_column :companies, :user_id, :integer
+ end
+
+ def self.down
+ remove_column :companies, :user_id
+ end
+end
View
@@ -10,12 +10,13 @@
#
# It's strongly recommended to check this file into your version control system.
-ActiveRecord::Schema.define(:version => 20110424163240) do
+ActiveRecord::Schema.define(:version => 20110424200833) do
create_table "companies", :force => true do |t|
t.string "name"
t.datetime "created_at"
t.datetime "updated_at"
+ t.integer "user_id"
end
create_table "email_addresses", :force => true do |t|
@@ -31,6 +32,7 @@
t.string "last_name"
t.datetime "created_at"
t.datetime "updated_at"
+ t.integer "user_id"
end
create_table "phone_numbers", :force => true do |t|
@@ -0,0 +1,4 @@
+Fabricator(:company) do
+ name "Sample Company"
+ user!
+end
@@ -0,0 +1,5 @@
+Fabricator(:person) do
+ first_name "John"
+ last_name "Doe"
+ user!
+end
@@ -0,0 +1,5 @@
+Fabricator(:user) do
+ name "Sample User"
+ provider "twitter"
+ uid "123"
+end
@@ -2,7 +2,7 @@
describe Company do
before(:each) do
- @company = Company.create(:name => "Sample Corp")
+ @company = Fabricate(:company)
end
it "should be valid" do
@@ -2,14 +2,17 @@
describe Person do
before(:each) do
- @person = Person.new(:first_name => "John",
- :last_name => "Doe")
+ @person = Fabricate(:person)
end
it "should be valid" do
@person.should be_valid
end
+ it "should be the child of a User" do
+ @person.user.should be_instance_of(User)
+ end
+
it "should convert to a string with last name, first name" do
@person.to_s.should == "Doe, John"
end
View
@@ -1,5 +1,31 @@
require 'spec_helper'
describe User do
-
+ before(:each) do
+ @user = Fabricate(:user)
+ end
+
+ it "should have associated people" do
+ @user.people.should be_instance_of(Array)
+ end
+
+ it "should build associated people" do
+ person_1 = Fabricate(:person)
+ person_2 = Fabricate(:person)
+ [person_1, person_2].each do |person|
+ @user.people.should_not include(person)
+ @user.people << person
+ @user.people.should include(person)
+ end
+ end
+
+ it "should build associated companies" do
+ company_1 = Fabricate(:company)
+ company_2 = Fabricate(:company)
+ [company_1, company_2].each do |company|
+ @user.companies.should_not include(company)
+ @user.companies << company
+ @user.companies.should include(company)
+ end
+ end
end

0 comments on commit 5b7569c

Please sign in to comment.