Skip to content

Commit 007fad6

Browse files
authored
Inactivating vendors (#4959)
* initial hability to deactivate the vendor * adds migration to set all existing vendors active to true * adds reactivation to vendors index * remove inactive vendors from new purchase form * adds filter to vendors * apply redirect suggestion * refactor specs * revert schema changes * revert schema changes * rubocop
1 parent 131c9ed commit 007fad6

File tree

13 files changed

+181
-14
lines changed

13 files changed

+181
-14
lines changed

app/controllers/purchases_controller.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ def destroy
101101
def load_form_collections
102102
@storage_locations = current_organization.storage_locations.active.alphabetized
103103
@items = current_organization.items.active.alphabetized
104-
@vendors = current_organization.vendors.alphabetized
104+
@vendors = current_organization.vendors.active.alphabetized
105105
end
106106

107107
def purchase_params

app/controllers/vendors_controller.rb

Lines changed: 40 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,14 @@ class VendorsController < ApplicationController
33
include Importable
44

55
def index
6-
@vendors = current_organization.vendors.with_volumes.alphabetized
6+
@vendors = current_organization
7+
.vendors
8+
.with_volumes
9+
.alphabetized
10+
.class_filter(filter_params)
11+
12+
@vendors = @vendors.active unless params[:include_inactive_vendors]
13+
@include_inactive_vendors = params[:include_inactive_vendors]
714

815
respond_to do |format|
916
format.html
@@ -53,6 +60,34 @@ def update
5360
end
5461
end
5562

63+
def deactivate
64+
vendor = current_organization.vendors.find(params[:id])
65+
66+
begin
67+
vendor.deactivate!
68+
rescue => e
69+
flash[:error] = e.message
70+
redirect_back(fallback_location: vendors_path)
71+
return
72+
end
73+
74+
redirect_to vendors_path, notice: "#{vendor.business_name} has been deactivated."
75+
end
76+
77+
def reactivate
78+
vendor = current_organization.vendors.find(params[:id])
79+
80+
begin
81+
vendor.reactivate!
82+
rescue => e
83+
flash[:error] = e.message
84+
redirect_back(fallback_location: vendors_path)
85+
return
86+
end
87+
88+
redirect_to vendors_path, notice: "#{vendor.business_name} has been reactivated."
89+
end
90+
5691
private
5792

5893
def vendor_params
@@ -61,7 +96,9 @@ def vendor_params
6196
end
6297

6398
helper_method \
64-
def filter_params
65-
{}
99+
def filter_params(_parameters = nil)
100+
return {} unless params.key?(:filters)
101+
102+
params.require(:filters).permit(:include_inactive_vendors)
66103
end
67104
end

app/models/vendor.rb

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
# Table name: vendors
44
#
55
# id :bigint not null, primary key
6+
# active :boolean default(TRUE)
67
# address :string
78
# business_name :string
89
# comment :string
@@ -20,16 +21,32 @@ class Vendor < ApplicationRecord
2021
has_paper_trail
2122
include Provideable
2223
include Geocodable
24+
include Filterable
2325

2426
has_many :purchases, inverse_of: :vendor, dependent: :destroy
2527

2628
validates :business_name, presence: true
2729

2830
scope :alphabetized, -> { order(:business_name) }
29-
31+
scope :active, -> { where(active: true) }
3032
scope :with_volumes, -> {
3133
left_joins(purchases: :line_items)
3234
.select("vendors.*, SUM(COALESCE(line_items.quantity, 0)) AS volume")
3335
.group(:id)
3436
}
37+
38+
def volume
39+
LineItem.where(
40+
itemizable_type: "Purchase",
41+
itemizable_id: purchase_ids
42+
).sum(:quantity)
43+
end
44+
45+
def deactivate!
46+
update!(active: false)
47+
end
48+
49+
def reactivate!
50+
update!(active: true)
51+
end
3552
end

app/views/vendors/_vendor_row.html.erb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,14 @@
77
<td class="text-center">
88
<%= view_button_to vendor_row %>
99
<%= edit_button_to edit_vendor_path(vendor_row) %>
10+
<% if vendor_row.active? %>
11+
<%= deactivate_button_to deactivate_vendor_path(vendor_row),
12+
text: 'Deactivate',
13+
confirm: confirm_deactivate_msg(vendor_row.business_name) %>
14+
<% else %>
15+
<%= reactivate_button_to reactivate_vendor_path(vendor_row),
16+
text: 'Reactivate',
17+
confirm: confirm_reactivate_msg(vendor_row.business_name) %>
18+
<% end %>
1019
</td>
1120
</tr>

app/views/vendors/index.html.erb

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,20 +29,38 @@
2929
<div class="col-md-12">
3030
<!-- jquery validation -->
3131
<div class="card card-primary">
32-
<div class="card-footer">
33-
<div class="pull-right">
34-
<%= modal_button_to("#csvImportModal", { icon: "upload", text: "Import Vendors", size: "md"}) if @vendors.empty? %>
35-
<%= download_button_to(vendors_path(format: :csv, filters: filter_params.merge(date_range: date_range_params)), {text: "Export Vendors", size: "md"}) if @vendors.any? %>
36-
<%= new_button_to new_vendor_path, text: "New Vendor" %> </div>
32+
<div class="card-header">
33+
<h3 class="card-title">Vendor Filter</h3>
3734
</div>
38-
</div>
35+
<!-- /.card-header -->
36+
<!-- form start -->
37+
<div class="card-body">
38+
<%= form_tag(vendors_path, method: :get) do |f| %>
39+
<div class="row">
40+
<div class="form-group col-lg-3 col-md-4 col-sm-6 col-xs-12">
41+
<%= filter_checkbox(label: "Also include inactive vendors", scope: "include_inactive_vendors", selected: @include_inactive_vendors) %>
42+
</div>
43+
</div>
44+
</div>
45+
<div class="card-footer">
46+
<%= filter_button %>
47+
<%= clear_filter_button %>
48+
<span class="float-right">
49+
<%= download_button_to(vendors_path(format: :csv, filters: filter_params.merge(date_range: date_range_params, include_inactive_vendors: @include_inactive_vendors)), {text: "Export Vendors", size: "md"}) if @vendors.any? %>
50+
<%= modal_button_to("#csvImportModal", { icon: "upload", text: "Import Vendors", size: "md"}) if @vendors.empty? %>
51+
<%= new_button_to new_vendor_path, text: "New Vendor" %>
52+
</span>
53+
</div>
54+
<% end # form %>
55+
</div>
3956
<!-- /.card -->
4057
</div>
41-
<!--/.col (left) -->
4258
</div>
4359
<!-- /.row -->
4460
</div><!-- /.container-fluid -->
61+
</section>
4562
63+
<section class="content">
4664
<div class="container-fluid">
4765
<div class="row">
4866
<div class="col-12">

config/routes.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,10 @@ def set_up_flipper
173173
collection do
174174
post :import_csv
175175
end
176+
member do
177+
put :deactivate
178+
put :reactivate
179+
end
176180
end
177181

178182
resources :kits do
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# Add active column to vendors to make it possible to delete vendors without actually deleting them
2+
class AddActiveToVendors < ActiveRecord::Migration[7.2]
3+
def up
4+
add_column :vendors, :active, :boolean
5+
change_column_default :vendors, :active, true
6+
end
7+
8+
def down
9+
remove_column :vendors, :active
10+
end
11+
end
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# Add active column to vendors to make it possible to delete vendors without actually deleting them
2+
class SetExistingVendorsActive < ActiveRecord::Migration[7.2]
3+
def change
4+
Vendor.update_all(active: true)
5+
end
6+
end

db/schema.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,7 @@
1010
#
1111
# It's strongly recommended that you check this file into your version control system.
1212

13-
14-
ActiveRecord::Schema[7.2].define(version: 2025_01_29_015253) do
13+
ActiveRecord::Schema[7.2].define(version: 2025_02_01_151720) do
1514
# These are extensions that must be enabled in order to support this database
1615
enable_extension "plpgsql"
1716

@@ -856,6 +855,7 @@
856855
t.float "longitude"
857856
t.datetime "created_at", precision: nil, null: false
858857
t.datetime "updated_at", precision: nil, null: false
858+
t.boolean "active", default: true
859859
t.index ["latitude", "longitude"], name: "index_vendors_on_latitude_and_longitude"
860860
end
861861

spec/models/vendor_spec.rb

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
# Table name: vendors
44
#
55
# id :bigint not null, primary key
6+
# active :boolean default(TRUE)
67
# address :string
78
# business_name :string
89
# comment :string
@@ -37,6 +38,22 @@
3738
expect(subject.first.volume).to eq(10)
3839
end
3940
end
41+
42+
describe "deactivate!" do
43+
it "deactivates the vendor" do
44+
vendor = create(:vendor)
45+
vendor.deactivate!
46+
expect(vendor.active).to be(false)
47+
end
48+
end
49+
50+
describe "reactivate!" do
51+
it "reactivates the vendor" do
52+
vendor = create(:vendor, active: false)
53+
vendor.reactivate!
54+
expect(vendor.active).to be(true)
55+
end
56+
end
4057
end
4158

4259
describe "versioning" do

0 commit comments

Comments
 (0)