Skip to content

Commit

Permalink
Merge pull request #61 from rubyforgood/new-distributions
Browse files Browse the repository at this point in the history
New distributions
  • Loading branch information
armahillo committed May 20, 2017
2 parents 05135f8 + eab97c6 commit 91fed95
Show file tree
Hide file tree
Showing 7 changed files with 122 additions and 18 deletions.
23 changes: 23 additions & 0 deletions app/assets/javascripts/distributions.coffee
Original file line number Diff line number Diff line change
@@ -1,3 +1,26 @@
# Place all the behaviors and hooks related to the matching controller here.
# All this logic will automatically be available in application.js.
# You can use CoffeeScript in this file: http://coffeescript.org/

$ ->
$(document).on "change", "select#distribution_storage_location_id", ->
control = $("select#distribution_storage_location_id")
$.ajax
url: control.data("storage-location-inventory-path").replace(":id", control.val())
dataType: "json"
success: (data) ->
options = ""
$.each data, (index) ->
options += "<option value=\"" + data[index].item_id + "\">" + data[index].item_name + "</option>\n"
$("#transfer_line_items select").find('option').remove().end().append(options)

$(document).on "cocoon:after-insert", "form#new_distribution", (e, insertedItem) ->
control = $("select#distribution_storage_location_id")
$.ajax
url: control.data("storage-location-inventory-path").replace(":id", control.val())
dataType: "json"
success: (data) ->
options = ""
$.each data, (index) ->
options += "<option value=\"" + data[index].item_id + "\">" + data[index].item_name + "</option>\n"
$("select", insertedItem).find('option').remove().end().append(options)
7 changes: 5 additions & 2 deletions app/controllers/distributions_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ def index
def create
@distribution = Distribution.new(distribution_params.merge(organization: current_organization))
if @distribution.save
redirect_to distribution_path(@distribution, organization_id: current_organization.short_name)
redirect_to distributions_path
else
flash[:notice] = "An error occurred, try again?"
render :new
Expand All @@ -24,6 +24,9 @@ def create

def new
@distribution = Distribution.new
@distribution.line_items.build
@items = Item.alphabetized
@storage_locations = StorageLocation.all
end

def show
Expand All @@ -33,6 +36,6 @@ def show
private

def distribution_params
params.require(:distribution).permit(:comment, :partner_id, :storage_location_id)
params.require(:distribution).permit(:comment, :partner_id, :storage_location_id, line_items_attributes: [:item_id, :quantity, :_destroy])
end
end
12 changes: 12 additions & 0 deletions app/views/distributions/_line_item_fields.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<div class="nested-fields">
<div class="row">
<div class="small-3 small-offset-3 columns">
<%= f.input_field :item_id, collection: @items, prompt: "Choose an item" %>
</div>
<div class="small-2 columns">
<%= f.input_field :quantity, placeholder: "Quantity" %>
</div>

<%= link_to_remove_association "X", f, class: "button tiny alert float-left" %>
</div>
</div>
6 changes: 5 additions & 1 deletion app/views/distributions/index.html.erb
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
<div class="header-action">
<%= link_to "New Distribution", new_distribution_path(organization_id: current_organization), class: "button float-right" %>
</div>

<h1>Distributions</h1>

<table>
<table id="distributions">
<thead>
<tr>
<th>Partner</th>
Expand Down
48 changes: 46 additions & 2 deletions app/views/distributions/new.html.erb
Original file line number Diff line number Diff line change
@@ -1,2 +1,46 @@
<h1>Distributions#new</h1>
<p>Find me in app/views/distributions/new.html.erb</p>
<h1>Create a new distribution</h1>

<%= simple_form_for @distribution, wrapper: :horizontal_form do |f| %>
<%= f.association :partner,
label: "Partner",
error: "Which partner is this distribution going to?"
%>
<%= f.association :storage_location,
collection: @storage_locations,
label: "From storage location",
error: "Which location are you moving inventory from?",
input_html: {
data: {
storage_location_inventory_path: inventory_storage_location_path(
organization_id: current_organization,
id: ":id",
format: :json
)
}
}
%>
<%= f.input :comment,
label: "Comment" %>
<%= f.simple_fields_for :line_items do |item| %>
<div id="transfer_line_items">
<%= render 'line_item_fields', f: item %>
</div>
<div class="row links">
<div class="small-offset-3 small-9 columns">
<%= link_to_add_association "add line item", f, :line_items,
data: {
association_insertion_node: "#transfer_line_items",
association_insertion_method: "append"
}
%>
</div>
</div>
<% end %>
<%= f.button :submit %>
<% end %>
22 changes: 11 additions & 11 deletions spec/controllers/distributions_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,57 +7,57 @@
before do
sign_in(@user)
end

describe "GET #print" do
subject { get :print, params: default_params.merge({ id: create(:distribution).id }) }
it "returns http success" do
expect(subject).to be_successful
end
end

describe "GET #reclaim" do
subject { get :reclaim, params: default_params.merge({ organization_id: @organization, id: create(:distribution).id }) }
it "returns http success" do
expect(subject).to be_successful
end
end

describe "GET #index" do
subject { get :index, params: default_params }
it "returns http success" do
expect(subject).to be_successful
end
end

describe "POST #create" do
it "redirects to #show on success" do
it "redirects to #index on success" do
i = create(:storage_location)
p = create(:partner)

expect(i).to be_valid
expect(p).to be_valid

post :create, params: default_params.merge(distribution: { storage_location_id: i.id, partner_id: p.id })
expect(response).to have_http_status(:redirect)

d = Distribution.last
expect(response).to redirect_to(distribution_path(d.id, organization_id: @organization.short_name))
expect(response).to redirect_to(distributions_path)
end

it "renders #new again on failure, with notice" do
post :create, params:default_params.merge(distribution: { comment: nil, partner_id: nil, storage_location_id: nil })
expect(response).to be_successful
expect(flash[:notice]).to match(/error/i)
end
end

describe "GET #new" do
subject { get :new, params: default_params }
it "returns http success" do
expect(subject).to be_successful
end
end

describe "GET #show" do
subject { get :show, params: default_params.merge(id: create(:distribution).id) }
it "returns http success" do
Expand Down
22 changes: 20 additions & 2 deletions spec/views/distributions/new.html.erb_spec.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,23 @@
RSpec.describe "distributions/new.html.erb", type: :view do
before(:each) do
@organization = create(:organization)
@storage_location = create(:storage_location, organization_id: @organization.id)
@partner = create(:partner, organization_id: @organization.id)

assign(:distribution, Distribution.new)
assign(:storage_locations, [@storage_location])
assign(:partner, @partner)
assign(:organization_id, @organization.id)

RSpec.describe "distributions/new.html.erb", type: :view do
pending "add some examples to (or delete) #{__FILE__}"
render
end

it "Asks for storage location and partner", :focus do
expect(rendered).to have_xpath("//form/div/select[@name='distribution[storage_location_id]']")
expect(rendered).to have_xpath("//form/div/select[@name='distribution[partner_id]']")
end

xit "shows fields for the user to add items to this distribution" do
# TODO: How should this work?
end
end

0 comments on commit 91fed95

Please sign in to comment.