Skip to content

Commit

Permalink
🥗✨ Agreement: Test and implement the #create endpoint
Browse files Browse the repository at this point in the history
- #1364

This is only for the classic HTML Request <=> Response Cycle. I realized
I probably want to do the `#index` Routes before `#create`, or at least
get started on the bits to expose adding an `Agreement` in the
`Spaces#edit` page.
  • Loading branch information
zspencer committed Apr 16, 2023
1 parent 173ee3f commit d168898
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 5 deletions.
21 changes: 19 additions & 2 deletions app/controllers/space/agreements_controller.rb
Original file line number Diff line number Diff line change
@@ -1,11 +1,28 @@
class Space
class AgreementsController < ApplicationController
def show
authorize(agreement)
end

def create
agreement.save

if agreement.errors.present?
render :new
else
redirect_to space.location(child: :agreements)
end
end

helper_method def agreement
@agreement ||= policy_scope(space.agreements).friendly.find(params[:id])
@agreement ||= if params[:id]
policy_scope(space.agreements).friendly.find(params[:id])
else
space.agreements.new(agreement_params)
end.tap { |agreement| authorize(agreement) }
end

def agreement_params
policy(Agreement).permit(params.require(:agreement))
end

def space
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], controller: "space/agreements"
router.resources :agreements, only: %i[show create], 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
10 changes: 10 additions & 0 deletions app/policies/space/agreement_policy.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,19 @@
class Space
class AgreementPolicy < ApplicationPolicy
alias_method :agreement, :object

def show?
true
end

def create?
person&.operator? || person&.member_of?(agreement.space)
end

def permitted_attributes(_)
%i[name body]
end

class Scope < ::ApplicationScope
def resolve
scope.all
Expand Down
Empty file.
40 changes: 38 additions & 2 deletions spec/requests/space/agreements_controller_request_spec.rb
Original file line number Diff line number Diff line change
@@ -1,15 +1,51 @@
require "rails_helper"

RSpec.describe Space::AgreementsController do
let(:space) { create(:space) }

describe "#show" do
subject(:perform_request) do
get polymorphic_path([space, agreement])
response
end

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

it { is_expected.to render_template(:show) }
end

describe "#create" do
subject(:perform_request) do
post polymorphic_path(space.location(child: :agreements)), params: {agreement: agreement_params}
response
end

let(:agreement_params) { attributes_for(:space_agreement) }

it { is_expected.to be_not_found }

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

let(:person) { create(:membership, space: space).member }

specify do
perform_request
expect(space.reload.agreements).to exist(name: agreement_params[:name], body: agreement_params[:body])
end

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

context "when the agreement is invalid" do
before { sign_in(space, person) }

let(:person) { create(:membership, space: space).member }

let(:agreement_params) { attributes_for(:space_agreement, name: nil) }

it { is_expected.to render_template(:new) }
specify { expect { perform_request }.not_to change { space.agreements.reload.count } }
end
end
end

0 comments on commit d168898

Please sign in to comment.