Skip to content

Commit

Permalink
🥗🥔✨ Agreements: Tea up the #update action
Browse files Browse the repository at this point in the history
  • Loading branch information
zspencer committed Apr 17, 2023
1 parent 570737d commit 2f2a6cd
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 1 deletion.
8 changes: 8 additions & 0 deletions app/controllers/space/agreements_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,14 @@ def create
end
end

def update
if agreement.update(agreement_params)
redirect_to space.location(:edit), notice: t(".success", name: agreement.name)
else
render :edit, status: :unprocessable_entity
end
end

def destroy
agreement.destroy
redirect_to space.location(:edit), notice: t(".success", name: agreement.name)
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 destroy], controller: "space/agreements"
router.resources :agreements, only: %i[show new create update 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
1 change: 1 addition & 0 deletions app/policies/space/agreement_policy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ def create?
end

alias_method :destroy?, :create?
alias_method :update?, :create?

def permitted_attributes(_)
%i[name body]
Expand Down
Empty file.
2 changes: 2 additions & 0 deletions config/locales/agreement/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ en:
link_to: "Add Agreement"
create:
success: "Added Agreement %{name}"
update:
success: "Changed Agreement '%{name}'"
destroy:
link_to: "Remove Agreement '%{name}'"
success: "Removed Agreement '%{name}'"
29 changes: 29 additions & 0 deletions spec/requests/space/agreements_controller_request_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,35 @@
end
end

describe "#update" do
subject(:perform_request) do
put polymorphic_path(agreement.location, params: {agreement: agreement_params})
agreement.reload
response
end

let(:agreement_params) { {name: "Ooh"} }
let(:agreement) { create(:space_agreement, space: space) }

it { is_expected.to be_not_found }

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

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

specify do
expect { perform_request }.to change(agreement, :name).to("Ooh")
end

context "when the params are invalid" do
let(:agreement_params) { {name: ""} }

it { is_expected.to render_template(:edit).and(be_unprocessable) }
end
end
end

describe "#destroy" do
subject(:perform_request) do
delete polymorphic_path(agreement.location)
Expand Down

0 comments on commit 2f2a6cd

Please sign in to comment.