-
-
Notifications
You must be signed in to change notification settings - Fork 572
Fixes #473 Refactor inventory changing methods mkd/amh #718
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
fc67e16
c065f1f
dda8bfa
ad162e5
7e9a1d9
4ed08fa
82e9662
28919ab
74147b2
a276234
53c7540
8e0b0e2
dc4733f
6cf161c
7c9a714
cca4a34
c3fe20b
c4d8369
127b441
e3c3ecd
26c9c86
e20f1a6
64f2a9b
82b6efc
3ac6151
9f331b0
b5179d1
61972e6
0477136
3dc86a2
e817b74
81778ea
1286a02
7d8f5a7
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,7 +1,5 @@ | ||
| # Provides limited CRUD for Adjustments, which are the way that Diaper Banks fix incorrect inventory totals at Storage Locations | ||
| class AdjustmentsController < ApplicationController | ||
| before_action :set_adjustment, only: %i(show destroy) | ||
|
|
||
| # GET /adjustments | ||
| # GET /adjustments.json | ||
| def index | ||
|
|
@@ -13,44 +11,45 @@ def index | |
|
|
||
| # GET /adjustments/1 | ||
| # GET /adjustments/1.json | ||
| def show; end | ||
| def show | ||
| @adjustment = current_organization.adjustments.find(params[:id]) | ||
| end | ||
|
|
||
| # GET /adjustments/new | ||
| def new | ||
| @adjustment = current_organization.adjustments.new | ||
| @adjustment.line_items.build | ||
| @storage_locations = current_organization.storage_locations | ||
| @items = current_organization.items.alphabetized | ||
| load_form_collections | ||
| end | ||
|
|
||
| # POST /adjustments | ||
| def create | ||
| @adjustment = current_organization.adjustments.new(adjustment_params) | ||
| @storage_locations = current_organization.storage_locations | ||
| @items = current_organization.items.alphabetized | ||
|
|
||
| if @adjustment.valid? | ||
| @adjustment.storage_location.adjust!(@adjustment) | ||
|
|
||
| if @adjustment.save | ||
| redirect_to adjustment_path(@adjustment), notice: "Adjustment was successfully created." | ||
| else | ||
| flash[:error] = @adjustment.errors.collect { |model, message| "#{model}: " + message }.join("<br />".html_safe) | ||
| render :new | ||
| if @adjustment.valid? && @adjustment.save | ||
| increasing_adjustment, decreasing_adjustment = @adjustment.split_difference | ||
| ActiveRecord::Base.transaction do | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I love this! I hadn't used ActiveRecord::Base.transaction and I looked it up. Super nice! |
||
| @adjustment.storage_location.increase_inventory increasing_adjustment | ||
| @adjustment.storage_location.decrease_inventory decreasing_adjustment | ||
| end | ||
|
|
||
| redirect_to adjustment_path(@adjustment), notice: "Adjustment was successful." | ||
| else | ||
| flash[:error] = @adjustment.errors.collect { |model, message| "#{model}: " + message }.join("<br />".html_safe) | ||
| load_form_collections | ||
| render :new | ||
| end | ||
| rescue Errors::InsufficientAllotment => ex | ||
| flash[:error] = ex.message | ||
| load_form_collections | ||
| render :new | ||
| end | ||
|
|
||
| private | ||
|
|
||
| def set_adjustment | ||
| @adjustment = current_organization.adjustments.find(params[:id]) | ||
| def load_form_collections | ||
| @storage_locations = current_organization.storage_locations | ||
| @items = current_organization.items.alphabetized | ||
| end | ||
|
|
||
| def adjustment_params | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -19,16 +19,14 @@ def print | |
| end | ||
| end | ||
|
|
||
| # TODO: This should be renamed :destroy, because that's what it does. I think the name is vestigial from a time when it didn't actually destroy the object | ||
| def reclaim | ||
| def destroy | ||
| ActiveRecord::Base.transaction do | ||
| @distribution_id = params[:id] | ||
| distribution = Distribution.find(params[:id]) | ||
| distribution.storage_location.reclaim!(distribution) | ||
| distribution = current_organization.distributions.find(params[:id]) | ||
| distribution.storage_location.increase_inventory(distribution) | ||
| distribution.destroy! | ||
| end | ||
|
|
||
| flash[:notice] = "Distribution #{@distribution_id} has been reclaimed!" | ||
| flash[:notice] = "Distribution #{params[:id]} has been reclaimed!" | ||
| redirect_to distributions_path | ||
| end | ||
|
|
||
|
|
@@ -44,31 +42,25 @@ def index | |
|
|
||
| def create | ||
| @distribution = Distribution.new(distribution_params.merge(organization: current_organization)) | ||
| @storage_locations = current_organization.storage_locations | ||
|
armahillo marked this conversation as resolved.
|
||
|
|
||
| if @distribution.valid? | ||
| if params[:commit] == "Preview Distribution" | ||
| @distribution.line_items.combine! | ||
| @line_items = @distribution.line_items | ||
| render :show | ||
| @distribution.storage_location.decrease_inventory @distribution | ||
|
|
||
| if @distribution.save | ||
| update_request(params[:distribution][:request_attributes], @distribution.id) | ||
|
|
||
| send_notification(current_organization, @distribution) | ||
| flash[:notice] = "Distribution created!" | ||
| session[:created_distribution_id] = @distribution.id | ||
| redirect_to distributions_path | ||
| else | ||
| @distribution.storage_location.distribute!(@distribution) | ||
|
|
||
| if @distribution.save | ||
| update_request(params[:distribution][:request_attributes], @distribution.id) | ||
|
|
||
| send_notification(current_organization.id, @distribution.id) | ||
| flash[:notice] = "Distribution created!" | ||
| session[:created_distribution_id] = @distribution.id | ||
| redirect_to distributions_path | ||
| else | ||
| flash[:error] = "There was an error, try again?" | ||
| render :new | ||
| end | ||
| flash[:error] = "There was an error, try again?" | ||
| render :new | ||
| end | ||
| else | ||
| @storage_locations = current_organization.storage_locations | ||
| flash[:error] = "An error occurred, try again?" | ||
| logger.error "failed to save distribution: #{@distribution.errors.full_messages}" | ||
| logger.error "[!] DistributionsController#create failed to save distribution: #{@distribution.errors.full_messages}" | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I know we use the rails logger in a couple places. I wonder if it would make sense to have a custom log file since (I think) the logger just writes to production.log which is probably easy to miss stuff in.
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That's actually a really good idea - like
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Would it be possible to put a helper method somewhere that outputs this kind of log entry to both the current and a new special log? It would be helpful for the error to be in the current log as well, right?
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We should make an issue for that. I think it's not in-scope for this change (a heavy refactor on storage location I/O), but I definitely agree with a lot of what you're saying and we should improve our logging methods
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| render :new | ||
| end | ||
| rescue Errors::InsufficientAllotment => ex | ||
|
|
@@ -109,7 +101,7 @@ def update | |
| # see examples: https://stackoverflow.com/questions/13605598/how-to-get-a-date-from-date-select-or-select-date-in-rails | ||
| old_issued_at = distribution.issued_at | ||
|
|
||
| if distribution.storage_location.update_distribution!(distribution, distribution_params) | ||
| if distribution.replace_distribution!(distribution_params) | ||
|
armahillo marked this conversation as resolved.
|
||
| @distribution = Distribution.includes(:line_items).includes(:storage_location).find(params[:id]) | ||
| @line_items = @distribution.line_items | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -41,20 +41,20 @@ def scale_intake | |
| line_items_attributes: { "0" => { "item_id" => params["diaper_type"], | ||
| "quantity" => params["number_of_diapers"], | ||
| "_destroy" => "false" } }) | ||
| @donation.storage_location.intake! @donation | ||
| @donation.storage_location.increase_inventory @donation | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. much better name |
||
| render status: :ok, json: @donation.to_json | ||
| end | ||
|
|
||
| def create | ||
| @donation = Donation.new(donation_params.merge(organization: current_organization)) | ||
| @donation = current_organization.donations.new(donation_params) | ||
| if @donation.save | ||
| @donation.storage_location.intake! @donation | ||
| @donation.storage_location.increase_inventory @donation | ||
| redirect_to donations_path | ||
| else | ||
| load_form_collections | ||
| @donation.line_items.build if @donation.line_items.count.zero? | ||
| flash[:error] = "There was an error starting this donation, try again?" | ||
| Rails.logger.error "ERROR: #{@donation.errors}" | ||
| Rails.logger.error "[!] DonationsController#create Error: #{@donation.errors}" | ||
| render action: :new | ||
| end | ||
| end | ||
|
|
@@ -78,18 +78,21 @@ def show | |
|
|
||
| def update | ||
| @donation = Donation.find(params[:id]) | ||
| previous_quantities = @donation.line_items_quantities | ||
| if @donation.update(donation_params) | ||
| @donation.storage_location.adjust_from_past!(@donation, previous_quantities) | ||
| if @donation.replace_increase!(donation_params) | ||
| redirect_to donations_path | ||
| else | ||
| render "edit" | ||
| end | ||
| end | ||
|
|
||
| def destroy | ||
| @donation = current_organization.donations.includes(:line_items, storage_location: :inventory_items).find(params[:id]) | ||
| @donation.destroy | ||
| ActiveRecord::Base.transaction do | ||
| donation = current_organization.donations.find(params[:id]) | ||
| donation.storage_location.decrease_inventory(donation) | ||
| donation.destroy! | ||
| end | ||
|
|
||
| flash[:notice] = "Donation #{params[:id]} has been removed!" | ||
| redirect_to donations_path | ||
| end | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -30,6 +30,14 @@ def self.storage_locations_adjusted_for(organization) | |
| includes(:storage_location).where(organization_id: organization.id).collect(&:storage_location) | ||
| end | ||
|
|
||
| def split_difference | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I love how simple you guys made this look!
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not gonna lie, this shit is poetry. this took some serious noodling - @mdworken had some good ideas for breaking through the block I had hit on sorting this out. |
||
| pre_adjustment = line_items.partition { |line_item| line_item.quantity.positive? } | ||
| increasing_adjustment, decreasing_adjustment = pre_adjustment.map { |adjustment| Adjustment.new(line_items: adjustment) } | ||
|
|
||
| decreasing_adjustment.line_items.each { |line_item| line_item.quantity *= -1 } | ||
| [increasing_adjustment, decreasing_adjustment] | ||
| end | ||
|
|
||
| def self.csv_export_headers | ||
| ["Created", "Organization", "Storage Location", "Comment", "Changes"] | ||
| end | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why didn't I know about this gem! :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I always forget which gems you need to add to make
pryfunction normally. This should really be part ofpry-rails