Skip to content
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

Fix spec on pr 900 #909

Merged
merged 11 commits into from
Mar 19, 2021
13 changes: 12 additions & 1 deletion app/controllers/community_resources_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,18 @@ def new
end

def create
community_resource.assign_attributes permitted_attributes(community_resource)
location_params = params['community_resource']['location']

location = Location.where(
street_address: location_params['street_address'],
city: location_params['city'],
state: location_params['state'],
zip: location_params['zip'],
location_type_id: location_params['location_type_id']
).first_or_create

community_resource.location = location
community_resource.update permitted_attributes(community_resource)

if community_resource.save
redirect_after_create
Expand Down
2 changes: 1 addition & 1 deletion app/policies/community_resource_policy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ def permitted_attributes
:youtube_identifier,
service_area_ids: [],
tag_list: [],
organization_attributes: %i[id name _destroy],
organization_attributes: %i[id name _destroy]
]
.tap do |permitted|
permitted.push :is_approved if can_admin?
Expand Down
10 changes: 9 additions & 1 deletion app/views/community_resources/_form.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,15 @@
<div class="field is-grouped">
<%= f.input :tag_list, as: :check_boxes, collection: available_tags %>
<%= f.input :service_area_ids, label: "Service area", as: :check_boxes, collection: service_areas %>
<%= f.association :location, label: "Address", label_method: "address" %>
<div class="form-vertical">
<%= f.simple_fields_for :location do |location_form| %>
<%= location_form.input :street_address, required: true %>
<%= location_form.input :city, required: true %>
<%= location_form.input :state, required: true, maxlength: 2 %>
<%= location_form.input :zip, required: true, maxlength: 5 %>
<%= location_form.collection_select :location_type_id, LocationType.order(:name), :id, :name, include_blank: false, required: true %>
<% end %>
</div>
</div>

<div class="label is-expanded">Links</div>
Expand Down
39 changes: 37 additions & 2 deletions spec/requests/community_resources_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,13 @@
description: 'Food for the rev!',
publish_from: Date.current,
organization_attributes: { name: 'Black Panther Party' },
location: {
street_address: '123 Sesame Street',
city: 'Kings Park',
state: 'NY',
zip: '11754',
location_type_id: 1
}
}}}

describe 'GET /community_resources' do
Expand Down Expand Up @@ -52,11 +59,39 @@
end

context 'as a guest' do
it 'succeeds' do
expect { post community_resources_path, params: params }.to change(CommunityResource, :count).by 1
it 'redirects to the thank you page' do
post community_resources_path, params: params

expect(response).to have_http_status :found
expect(response.location).to match thank_you_path
end

it 'creates a new community resource and location' do
expect { post community_resources_path, params: params }.to(
change(CommunityResource, :count).by(1).and(
change(Location, :count).by(1)
))
end

context 'when a matching location already exists' do
let(:location_attrs) {{
street_address: '42 existing location',
location_type_id: 1,
}}

before do
Location.create! location_attrs
end

it 'creates a new community resource using the existing location' do
params[:community_resource][:location] = location_attrs

expect { post community_resources_path, params: params }.to(
change(CommunityResource, :count).by(1).and(
change(Location, :count).by(0)
))
end
end
end
end

Expand Down