Add enterprise roles for a user and wire up interface.

This commit is contained in:
Andrew Spinks
2013-08-01 10:16:20 +10:00
parent 4f5679aac3
commit 65617e0e77
4 changed files with 97 additions and 0 deletions

View File

@@ -1,4 +1,16 @@
Spree.user_class.class_eval do
has_many :enterprise_roles, :dependent => :destroy
has_many :enterprises, through: :enterprise_roles
accepts_nested_attributes_for :enterprise_roles, :allow_destroy => true
attr_accessible :enterprise_ids, :enterprise_roles_attributes
def build_enterprise_roles
Enterprise.all.each do |enterprise|
unless self.enterprise_roles.find_by_enterprise_id enterprise.id
self.enterprise_roles.build(:enterprise => enterprise)
end
end
end
end

View File

@@ -0,0 +1,6 @@
Deface::Override.new(:virtual_path => "spree/admin/users/_form",
:insert_after => "[data-hook='admin_user_form_fields']",
:partial => "spree/admin/users/enterprises_form",
:name => "add_enterprises_to_user"
)

View File

@@ -0,0 +1,14 @@
%fieldset
%legend 'Manage Enterprises'
= f.field_container :enterprise_roles do
- f.object.build_enterprise_roles
%table
= f.fields_for :enterprise_roles do |enterprise_form|
%tr
%td
= hidden_field_tag "#{enterprise_form.object_name}[_destroy]", 1, :id => nil
= check_box_tag "#{enterprise_form.object_name}[_destroy]", 0, !enterprise_form.object.new_record?
%td
= label_tag "#{enterprise_form.object_name}[_destroy]", enterprise_form.object.enterprise.name
= enterprise_form.hidden_field :enterprise_id

View File

@@ -0,0 +1,65 @@
require "spec_helper"
feature %q{
As a Super User
I want to setup users to manage an enterprise
} do
include AuthenticationWorkflow
include WebHelper
background do
@new_user = create(:user, :email => 'enterprise@hub.com')
@enterprise1 = create(:enterprise, name: 'Enterprise 1')
@enterprise2 = create(:enterprise, name: 'Enterprise 2')
@enterprise3 = create(:enterprise, name: 'Enterprise 3')
@enterprise4 = create(:enterprise, name: 'Enterprise 4')
end
context "creating an Enterprise User" do
context 'with no enterprises' do
scenario "assigning a user to an Enterprise" do
login_to_admin_section
click_link 'Users'
click_link @new_user.email
click_link 'Edit'
check @enterprise2.name
click_button 'Update'
@new_user.enterprises.count.should == 1
@new_user.enterprises.first.name.should == @enterprise2.name
end
end
context 'with existing enterprises' do
before(:each) do
@new_user.enterprise_roles.build(enterprise: @enterprise1).save
@new_user.enterprise_roles.build(enterprise: @enterprise3).save
end
scenario "removing and add enterprises for a user" do
login_to_admin_section
click_link 'Users'
click_link @new_user.email
click_link 'Edit'
uncheck @enterprise3.name # remove
check @enterprise4.name # add
click_button 'Update'
@new_user.enterprises.count.should == 2
@new_user.enterprises.should include(@enterprise1)
@new_user.enterprises.should include(@enterprise4)
end
end
end
end