Created
November 24, 2021 14:59
-
-
Save amitpatelx/f9d650750543f046a2f421637b6a7cc5 to your computer and use it in GitHub Desktop.
Working Book Example App - Rent Action with custom precondition
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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