Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Implemented scoping for companies

  • Loading branch information...
commit 60349565ef44e6018cdd25f8db6b232d465de16d 1 parent f97ca0c
@jcasimir authored
View
5 app/controllers/companies_controller.rb
@@ -2,7 +2,7 @@ class CompaniesController < ApplicationController
before_filter :find_resource, :only => [:show, :destroy, :edit, :update]
def index
- @companies = Company.all
+ @companies = current_user.companies
end
def show
@@ -13,7 +13,8 @@ def new
end
def create
- @company = Company.new(params[:company])
+ #@company = Company.new(params[:company])
+ @company = current_user.companies.new(params[:company])
if @company.save
redirect_to @company, :notice => "Successfully created company."
else
View
3  app/controllers/people_controller.rb
@@ -13,7 +13,8 @@ def new
end
def create
- @person = Person.new(params[:person])
+ #@person = Person.new(params[:person])
+ @person = current_user.people.new(params[:person])
if @person.save
redirect_to @person, :notice => "Successfully created person."
else
View
2  app/views/companies/index.html.haml
@@ -5,7 +5,7 @@
.companies
- @companies.each do |company|
.company
- %h4= link_to company, company
+ %h4= link_to company, company, :id => "company_#{company.id}"
= render :partial => 'email_addresses/email_addresses', :object => company.email_addresses
= render :partial => 'phone_numbers/phone_numbers', :object => company.phone_numbers
%ul.actions
View
2  app/views/layouts/application.html.erb
@@ -16,7 +16,7 @@
<% end %>
<div id='account'>
<% if current_user %>
- <span>Welcome, <%= current_user.name %></span>
+ <span>Welcome, <%= current_user.name %> (<%= current_user.id %>)</span>
<%= link_to "logout", logout_path, :id => "login" %>
<% else %>
<%= link_to "login", login_path, :id => "logout" %>
View
5 spec/fabricators/company_fabricator.rb
@@ -1,4 +1,9 @@
Fabricator(:company) do
name "Sample Company"
user!
+end
+
+Fabricator(:company_with_details, :from => :company) do
+ email_addresses!(:count => 2){|company, i| Fabricate(:email_address, :address => "sample_#{i}@sample.com", :contact => company)}
+ phone_numbers!(:count => 2){|company, i| Fabricate(:phone_number, :number => "#{i.to_s*10}", :contact => company)}
end
View
10 spec/fabricators/user_fabricator.rb
@@ -1,7 +1,7 @@
Fabricator(:user) do
name "Sample User"
provider "twitter"
- uid "123"
+ uid {Fabricate.sequence(:uid)}
end
Fabricator(:user_with_people, :from => :user) do
@@ -10,4 +10,12 @@
Fabricator(:user_with_people_with_details, :from => :user) do
people!(:count => 3){|user, i| Fabricate(:person_with_details, :first_name => "Sample", :last_name => "Person #{i}", :user => user) }
+end
+
+Fabricator(:user_with_companies, :from => :user) do
+ companies!(:count => 3){|user, i| Fabricate(:company, :name => "Company #{i}", :user => user) }
+end
+
+Fabricator(:user_with_companies_with_details, :from => :user) do
+ companies!(:count => 3){|user, i| Fabricate(:company_with_details, :name => "Company #{i}", :user => user) }
end
View
212 spec/integration/companies_views_spec.rb
@@ -2,113 +2,131 @@
require 'capybara/rspec'
describe "the views for companies", :type => :request do
- before(:all) do
- @company = Company.create(:name => "Sample Corp")
- number_a = @company.phone_numbers.create(:number => "1234567")
- number_b = @company.phone_numbers.create(:number => "7654321")
- @company.email_addresses.create(:address => "john@doe.com")
- @company.email_addresses.create(:address => "doe@john.com")
- end
-
- describe "when looking at the list of companies" do
+ describe "when logged in as a user" do
before(:all) do
- visit companies_path
+ @user = Fabricate(:user_with_companies_with_details)
+ login_as(@user)
end
-
- it "should display email addresses" do
- @company.email_addresses.each do |email_address|
- page.should have_selector('li', :text => email_address.address)
+
+ describe "when looking at the list of companies" do
+ before(:each) do
+ visit companies_path
end
- end
-
- it "should display phone numbers" do
- @company.phone_numbers.each do |phone_number|
- page.should have_selector('li', :text => phone_number.number)
+
+ it "should display companies associated with this user" do
+ @user.companies.each do |company|
+ page.should have_link("company_#{company.id}")
+ end
end
- end
- end
-
- describe "when looking at a single company" do
- before(:each) do
- visit company_path(@company)
- end
- it "should have delete links for each email address" do
- @company.email_addresses.each do |email_address|
- page.should have_link("delete_email_address_#{email_address.id}")
+ it "should not display companies associated with another user" do
+ @user_2 = Fabricate(:user_with_companies)
+ visit(companies_path)
+ @user_2.companies.each do |company|
+ page.should_not have_link("company_#{company.id}")
+ end
end
- end
-
- it "should have an add email address link" do
- page.should have_link("new_email_address")
- end
-
- it "should go to the new email address form when the link is clicked" do
- click_link "new_email_address"
- current_path.should == new_email_address_path
- end
-
- it "should display each of the email addresses" do
- @company.email_addresses.each do |email_address|
- page.should have_selector('li', :text => email_address.address)
+
+ it "should display email addresses" do
+ @user.companies.each do |company|
+ company.email_addresses.each do |email_address|
+ page.should have_selector('li', :text => email_address.address)
+ end
+ end
end
- end
- it "should have edit links for each phone number" do
- @company.phone_numbers.each do |phone_number|
- page.should have_link("edit", :href => edit_phone_number_path(phone_number))
+ it "should display phone numbers" do
+ @user.companies.each do |company|
+ company.phone_numbers.each do |phone_number|
+ page.should have_selector('li', :text => phone_number.number)
+ end
+ end
end
end
-
- it "should have an add phone number link" do
- page.should have_link("new_phone_number")
- end
-
- it "should display each of the phone numbers" do
- @company.phone_numbers.each do |phone_number|
- page.should have_selector('li', :text => phone_number.number)
+
+ describe "when looking at a single company" do
+ before(:each) do
+ @company = @user.companies.first
+ visit company_path(@company)
end
- end
-
- it "should have delete links for each phone number" do
- @company.phone_numbers.each do |phone_number|
- page.should have_link("delete", :href => phone_number_path(phone_number))
- end
- end
-
- it "should show the company after deleting a phone number" do
- target = @company.phone_numbers.first
- page.click_link("delete_phone_number_#{target.id}")
- current_path.should == company_path(@company)
- page.should_not have_link("delete", :href => phone_number_path(target))
- end
-
- it "should show the company including the new number after creating a phone number" do
- page.click_link("new_phone_number")
- sample_number = "3334445555"
- page.fill_in("phone_number_number", :with => sample_number)
- page.click_button("phone_number_submit")
- current_path.should == company_path(@company)
- page.should have_selector('li', :text => sample_number)
- end
-
- it "should show the company including the updated number after editing a phone number" do
- target = @company.phone_numbers.first
- original_number = target.number
- new_number = original_number.reverse
- page.click_link("edit_phone_number_#{target.id}")
- current_path.should == edit_phone_number_path(target)
- page.fill_in("phone_number_number", :with => new_number)
- page.click_button("phone_number_submit")
- current_path.should == company_path(@company)
- page.should have_selector('li', :text => new_number)
- end
-
- it "should show the company after deleting an email address" do
- target = @company.email_addresses.first
- page.click_link("delete_email_address_#{target.id}")
- current_path.should == company_path(@company)
- page.should_not have_link("delete_email_address_#{target.id}")
- end
+
+ it "should have delete links for each email address" do
+ @company.email_addresses.each do |email_address|
+ page.should have_link("delete_email_address_#{email_address.id}")
+ end
+ end
+
+ it "should have an add email address link" do
+ page.should have_link("new_email_address")
+ end
+
+ it "should go to the new email address form when the link is clicked" do
+ click_link "new_email_address"
+ current_path.should == new_email_address_path
+ end
+
+ it "should display each of the email addresses" do
+ @company.email_addresses.each do |email_address|
+ page.should have_selector('li', :text => email_address.address)
+ end
+ end
+
+ it "should have edit links for each phone number" do
+ @company.phone_numbers.each do |phone_number|
+ page.should have_link("edit", :href => edit_phone_number_path(phone_number))
+ end
+ end
+
+ it "should have an add phone number link" do
+ page.should have_link("new_phone_number")
+ end
+
+ it "should display each of the phone numbers" do
+ @company.phone_numbers.each do |phone_number|
+ page.should have_selector('li', :text => phone_number.number)
+ end
+ end
+
+ it "should have delete links for each phone number" do
+ @company.phone_numbers.each do |phone_number|
+ page.should have_link("delete", :href => phone_number_path(phone_number))
+ end
+ end
+
+ it "should show the company after deleting a phone number" do
+ target = @company.phone_numbers.first
+ page.click_link("delete_phone_number_#{target.id}")
+ current_path.should == company_path(@company)
+ page.should_not have_link("delete", :href => phone_number_path(target))
+ end
+
+ it "should show the company including the new number after creating a phone number" do
+ page.click_link("new_phone_number")
+ sample_number = "3334445555"
+ page.fill_in("phone_number_number", :with => sample_number)
+ page.click_button("phone_number_submit")
+ current_path.should == company_path(@company)
+ page.should have_selector('li', :text => sample_number)
+ end
+
+ it "should show the company including the updated number after editing a phone number" do
+ target = @company.phone_numbers.first
+ original_number = target.number
+ new_number = original_number.reverse
+ page.click_link("edit_phone_number_#{target.id}")
+ current_path.should == edit_phone_number_path(target)
+ page.fill_in("phone_number_number", :with => new_number)
+ page.click_button("phone_number_submit")
+ current_path.should == company_path(@company)
+ page.should have_selector('li', :text => new_number)
+ end
+
+ it "should show the company after deleting an email address" do
+ target = @company.email_addresses.first
+ page.click_link("delete_email_address_#{target.id}")
+ current_path.should == company_path(@company)
+ page.should_not have_link("delete_email_address_#{target.id}")
+ end
+ end
end
end
View
3  spec/integration/people_views_spec.rb
@@ -5,11 +5,11 @@
describe "when logged in as a user" do
before(:all) do
@user = Fabricate(:user_with_people_with_details)
- login_as(@user)
end
describe "when looking at the list of people" do
before(:each) do
+ login_as(@user)
visit people_path
end
@@ -30,6 +30,7 @@
describe "when looking at a single person" do
before(:each) do
+ login_as(@user)
@person = @user.people.first
visit person_path(@person)
end
Please sign in to comment.
Something went wrong with that request. Please try again.