Skip to content

Commit

Permalink
Merge pull request #91 from rescuedcode/add_company_switcher
Browse files Browse the repository at this point in the history
Add company switcher
  • Loading branch information
fermion committed Mar 19, 2013
2 parents 53fcf25 + 0fb96a4 commit 5c2102a
Show file tree
Hide file tree
Showing 8 changed files with 81 additions and 1 deletion.
1 change: 1 addition & 0 deletions app/assets/javascripts/models/user.js.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ class window.StaffPlan.Models.User extends StaffPlan.Model
first_name: @get("first_name")
last_name: @get("last_name")
email: @get("email")
current_company_id: @get("current_company_id")

dateChanged: (event) ->
event.preventDefault()
Expand Down
1 change: 1 addition & 0 deletions app/assets/javascripts/staffplan-app.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ window.StaffPlan =
initialize: (data) ->
@users = new StaffPlan.Collections.Users data.users
@projects = new StaffPlan.Collections.Projects data.projects
@userCompanies = data.userCompanies
@clients = new StaffPlan.Collections.Clients data.clients
@assignments = new StaffPlan.Collections.Assignments data.assignments
@currentCompany = data.currentCompany
Expand Down
18 changes: 18 additions & 0 deletions app/assets/javascripts/templates/shared.templates.js.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,24 @@ _templates =
</ul>
</div>
'''
companySwitcher: '''
<div class="btn-group btn-mini">
<a class="btn btn-mini dropdown-toggle" data-toggle="dropdown" data-bypass href="#">
{{currentCompanyName}}
<span class="caret"></span>
</a>
<ul class="dropdown-menu">
{{#each companies}}
<li>
<a href="#" data-company-id="{{id}}" class="switcher" data-bypass>
{{name}}
</a>
</li>
{{/each}}
</ul>
</div>
'''

StaffPlan.Templates.Shared =
yearFilter: Handlebars.compile _templates.yearFilter
companySwitcher: Handlebars.compile _templates.companySwitcher
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,9 @@ class StaffPlan.View extends Support.CompositeView

@$el.html StaffPlan.Templates.Layouts.application
currentUserId: StaffPlan.currentUser.id

if StaffPlan.userCompanies.length > 1
companySwitcher = new StaffPlan.Views.Shared.CompanySwitcher
@$el.find('header .inner ul:first').append companySwitcher.render().el

$( document.body ).trigger( 'view:rendered' );
$( document.body ).trigger( 'view:rendered' );
25 changes: 25 additions & 0 deletions app/assets/javascripts/views/_shared/company_switcher.js.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
class StaffPlan.Views.Shared.CompanySwitcher extends Backbone.View
tagName: "li"
className: "company-switcher"

initialize: ->

events:
"click a.switcher": "changeCompany"

changeCompany: (event) ->
event.preventDefault()
event.stopPropagation()
selectedCompanyId = $(event.target).data('company-id')
user = StaffPlan.users.get StaffPlan.currentUser.id
user.save {current_company_id: selectedCompanyId},
success: (model, response, options) ->
window.location.href = "/staffplans/#{user.id}"
, error: (model, xhr, options) ->
alert "An error occurred while switching companies. Please try again."
render: ->
@$el.html StaffPlan.Templates.Shared.companySwitcher
userId: StaffPlan.currentUser.id
companies: _.select(StaffPlan.userCompanies, (obj) -> (obj.id isnt StaffPlan.currentCompany.id))
currentCompanyName: StaffPlan.currentCompany.name
@
22 changes: 22 additions & 0 deletions app/assets/stylesheets/screen.css.scss
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,28 @@ body {
line-height: 24px;
}

&.company-switcher {

li {
margin-left: 5px;
}

a {
line-height: 26px;
}
span.caret {
margin-top: 11px;
}

ul {
z-index: 9999;

li {
display: block;
}
}
}

&.year-filter {

li {
Expand Down
8 changes: 8 additions & 0 deletions app/decorators/user_decorator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,14 @@ def project_json(project)
end
end
end

def companies_as_json
Jbuilder.encode do |json|
json.array! user.companies do |json, company|
json.(company, :id, :name)
end
end
end

def current_company_selector
init_haml_helpers
Expand Down
1 change: 1 addition & 0 deletions app/views/shared/_backbone_bootstrap.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
:erb
{
"users": <%= current_user.current_company.users_json %>,
"userCompanies": <%= current_user.decorate.companies_as_json %>,
"clients": <%= current_user.current_company.clients_as_json %>,
"assignments": <%= current_user.current_company.assignments_as_json %>,
"currentCompany": <%= current_user.current_company.self_as_json %>,
Expand Down

0 comments on commit 5c2102a

Please sign in to comment.