Skip to content

Instantly share code, notes, and snippets.

@amitpatelx
Created November 24, 2021 14:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save amitpatelx/f9d650750543f046a2f421637b6a7cc5 to your computer and use it in GitHub Desktop.
Save amitpatelx/f9d650750543f046a2f421637b6a7cc5 to your computer and use it in GitHub Desktop.
Working Book Example App - Rent Action with custom precondition
# frozen_string_literal: true
class Ba::Book::Availability < Granite::Action::Precondition
description 'Must not be available to rent'
def call(**)
decline_with(:unavailable) unless book_available?
end
private
def book_available?
book.available?
end
end
# frozen_string_literal: true
class Ba::Book::Rent < BaseAction
allow_if { performer.is_a?(User) }
projector :inline
subject :book
precondition Availability
private
def execute_perform!(*)
subject.available = false
subject.save!
::Rental.create!(book: subject, user: performer)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment