Skip to content

Commit

Permalink
🥗🥔✨Agreements: Can be destroyed! (#1376)
Browse files Browse the repository at this point in the history
- #1364



### After
<img width="392" alt="Screenshot 2023-04-16 at 8 46 52 PM"
src="https://user-images.githubusercontent.com/50284/232373365-641d5a1b-1e95-4efb-b0c9-f42df829187b.png">
<img width="1728" alt="Screenshot 2023-04-16 at 8 46 41 PM"
src="https://user-images.githubusercontent.com/50284/232373369-ba03d821-05c8-4832-ba4e-93fcf0aef454.png">
  • Loading branch information
zspencer authored Apr 17, 2023
2 parents 6993c66 + c5590e0 commit daa9730
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 5 deletions.
5 changes: 5 additions & 0 deletions app/controllers/space/agreements_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@ def create
end
end

def destroy
agreement.destroy
redirect_to space.location(:edit), notice: t(".success", name: agreement.name)
end

helper_method def agreement
@agreement ||= if params[:id]
policy_scope(space.agreements).friendly.find(params[:id])
Expand Down
2 changes: 1 addition & 1 deletion app/lib/space_routes.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module SpaceRoutes
def self.append_routes(router)
router.resources :agreements, only: %i[show new create], controller: "space/agreements"
router.resources :agreements, only: %i[show new create destroy], controller: "space/agreements"
router.resource :authenticated_session, only: %i[new create update destroy show]
router.resources :invitations, only: %i[create destroy index] do
router.resource :rsvp, only: %i[show update]
Expand Down
2 changes: 2 additions & 0 deletions app/policies/space/agreement_policy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ def create?
person&.operator? || person&.member_of?(agreement.space)
end

alias_method :destroy?, :create?

def permitted_attributes(_)
%i[name body]
end
Expand Down
17 changes: 13 additions & 4 deletions app/views/spaces/edit.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -76,19 +76,28 @@
<p><%= link_to t('utilities.new.link_to'), space.location(:new, child: :utility) %></p>
</fieldset>

<%= render CardComponent.new do %>
<%= render CardComponent.new(classes: "mt-3 gap-y-3") do %>
<header>
<h3><%= t('space.agreements.index.link_to') %></h3>
<p class="text-sm italic"><%= t('space.agreements.help_text') %></p>
</header>
<div>
<div class="flex flex-wrap gap-y-6">
<%- space.agreements.each do |agreement| %>
<span class="flex justify-between w-full rounded border p-2 my-2 border-t-0 border-l-0">
<span class="flex justify-between w-full">
<span class="grow"><%= agreement.name %></span>
<span>
<%- if policy(agreement).destroy? %>
<%= render ButtonComponent.new(href: agreement.location,
title: t('space.agreements.destroy.link_to', name: agreement.name),
label: t('icons.destroy'),
method: :delete,
scheme: :secondary) %>
<%- end %>
</span>
</span>
<%- end %>
<div>
<footer class="mt-4 text-center">
<footer>
<%- new_agreement = space.agreements.build %>
<%- if policy(new_agreement).create? %>
<%= render ButtonComponent.new(
Expand Down
3 changes: 3 additions & 0 deletions config/locales/agreement/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,6 @@ en:
link_to: "Add Agreement"
create:
success: "Added Agreement '%{name}'"
destroy:
link_to: "Remove Agreement '%{name}'"
success: "Removed Agreement '%{name}'"
22 changes: 22 additions & 0 deletions spec/requests/space/agreements_controller_request_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -68,4 +68,26 @@
specify { expect { perform_request }.not_to change { space.agreements.reload.count } }
end
end

describe "#destroy" do
subject(:perform_request) do
delete polymorphic_path(agreement.location)
response
end

let(:agreement) { create(:space_agreement, space: space) }

it { is_expected.to be_not_found }

context "when signed in as a member" do
before { sign_in(space, member) }

specify do
perform_request
expect(Space::Agreement).not_to exist(id: agreement.id)
end

it { is_expected.to redirect_to(space.location(:edit)) }
end
end
end

0 comments on commit daa9730

Please sign in to comment.