Enterprise user can view, create and edit payment methods

This commit is contained in:
Rohan Mitchell
2013-10-18 09:09:53 +11:00
parent 05932a69cd
commit e6041aaf2f
5 changed files with 97 additions and 14 deletions

View File

@@ -1,14 +1,21 @@
Spree::Admin::PaymentMethodsController.class_eval do
# Only show payment methods that user has access to and sort by distributor name
# ! Redundant code copied from Spree::Admin::ResourceController with two added lines
# ! Redundant code copied from Spree::Admin::ResourceController with modifications marked
def collection
return parent.send(controller_name) if parent_data.present?
if model_class.respond_to?(:accessible_by) && !current_ability.has_block?(params[:action], model_class)
model_class.accessible_by(current_ability, action).
managed_by(spree_current_user).by_distributor # this line added
else
model_class.scoped.
managed_by(spree_current_user).by_distributor # this line added
collection = if model_class.respond_to?(:accessible_by) && !current_ability.has_block?(params[:action], model_class)
model_class.accessible_by(current_ability, action).
managed_by(spree_current_user).by_distributor # this line added
else
model_class.scoped.
managed_by(spree_current_user).by_distributor # this line added
end
if params.key? :enterprise_id
distributor = Enterprise.find params[:enterprise_id]
collection = collection.for_distributor(distributor)
end
collection
end
end
end

View File

@@ -14,6 +14,11 @@ Spree::PaymentMethod.class_eval do
end
}
scope :for_distributor, lambda { |distributor|
joins(:distributors).
where('enterprises.id = ?', distributor)
}
scope :by_distributor, lambda {
joins(:distributors).
order('enterprises.name, spree_payment_methods.name').

View File

@@ -11,6 +11,14 @@
<%= form_for @enterprise_set, :url => main_app.bulk_update_admin_enterprises_path do |f| %>
<table class="index" id="listing_enterprises">
<colgroup>
<col style="width: 20%;">
<col style="width: 5%;">
<col style="width: 20%;">
<col>
<col style="width: 20%;">
</colgroup>
<thead>
<tr data-hook="enterprises_header">
<th>Name</th>
@@ -23,14 +31,19 @@
<tbody>
<%= f.fields_for :collection do |enterprise_form| %>
<% enterprise = enterprise_form.object %>
<tr>
<tr class="enterprise-<%= enterprise.id %>">
<td><%= link_to enterprise.name, main_app.admin_enterprise_path(enterprise) %></td>
<td><%= 'PP ' if enterprise.is_primary_producer %><%= 'D' if enterprise.is_distributor %></td>
<td><%= enterprise_form.text_field :next_collection_at %></td>
<td><%= enterprise.description %></td>
<td data-hook="admin_users_index_row_actions">
<%= link_to_edit enterprise, :class => 'edit' %> &nbsp;
<%= link_to_delete_enterprise enterprise %>
<%= link_to_with_icon('icon-edit', 'Edit Profile', main_app.admin_enterprise_path(enterprise), class: 'edit') %><br />
<%= link_to_delete_enterprise enterprise %><br />
<%= link_to_with_icon 'icon-chevron-right', 'Payment Methods', spree.admin_payment_methods_path(enterprise_id: enterprise.id) %> (<%= enterprise.payment_methods.count %>)<br />
<%#= link_to_with_icon 'icon-plane', 'Shipping Methods', spree.admin_shipping_methods_path %><br />
<%#= link_to_with_icon 'icon-money', 'Enterprise Fees', main_app.admin_enterprise_fees_path %>
</td>
</tr>
<% end %>

View File

@@ -11,8 +11,7 @@ feature %q{
@distributors = (1..3).map { create(:distributor_enterprise) }
end
#Create and Edit uses same partial form
context "creating a payment method", js: true do
describe "creating a payment method", js: true do
scenario "assigning a distributor to the payment method" do
login_to_admin_section
@@ -28,7 +27,52 @@ feature %q{
flash_message.should == 'Payment Method has been successfully created!'
payment_method = Spree::PaymentMethod.find_by_name('Cheque payment method')
payment_method.distributors.should include(@distributors[0])
payment_method.distributors.should == [@distributors[0]]
end
end
context "as an enterprise user" do
let(:enterprise_user) { create_enterprise_user }
let(:distributor1) { create(:distributor_enterprise, name: 'First Distributor') }
let(:distributor2) { create(:distributor_enterprise, name: 'Second Distributor') }
let(:pm1) { create(:payment_method, name: 'One', distributors: [distributor1]) }
let(:pm2) { create(:payment_method, name: 'Two', distributors: [distributor2]) }
before(:each) do
enterprise_user.enterprise_roles.build(enterprise: distributor1).save
enterprise_user.enterprise_roles.build(enterprise: distributor2).save
login_to_admin_as enterprise_user
end
it "creates payment methods" do
click_link 'Enterprises'
within(".enterprise-#{distributor1.id}") { click_link 'Payment Methods' }
click_link 'New Payment Method'
fill_in 'payment_method_name', :with => 'Cheque payment method'
select distributor1.name, :from => 'payment_method_distributor_ids'
click_button 'Create'
flash_message.should == 'Payment Method has been successfully created!'
payment_method = Spree::PaymentMethod.find_by_name('Cheque payment method')
payment_method.distributors.should == [distributor1]
end
it "shows me only payment methods for the enterprise I select" do
pm1
pm2
click_link 'Enterprises'
within(".enterprise-#{distributor1.id}") { click_link 'Payment Methods' }
page.should have_content pm1.name
page.should_not have_content pm2.name
click_link 'Enterprises'
within(".enterprise-#{distributor2.id}") { click_link 'Payment Methods' }
page.should_not have_content pm1.name
page.should have_content pm2.name
end
end
end

View File

@@ -0,0 +1,14 @@
require 'spec_helper'
module Spree
describe PaymentMethod do
it "finds payment methods for a particular distributor" do
d1 = create(:distributor_enterprise)
d2 = create(:distributor_enterprise)
pm1 = create(:payment_method, distributors: [d1, d1])
pm2 = create(:payment_method, distributors: [d2, d2])
PaymentMethod.for_distributor(d1).should == [pm1]
end
end
end