Skip to content
This repository was archived by the owner on Oct 19, 2018. It is now read-only.

3 Book Operation examples

Barrie Hadfield edited this page Jan 6, 2017 · 3 revisions

@barriehadfield

# Use case: we have a list of books and want to add one to the users basket

module Components
  module Books
    class List < React::Component::Base

      render(DIV) do
        Book.all.each do |book|
          li { "Add #{book.name}" }.on(:click) do
            AddBookToBasket(book: book) do |outcome|
              alert "Failed to add the book" unless outcome.success?
            end
          end
        end
      end
    end
  end
end

module Operations
  module Books
    class AddBookToBasket < HyperLoop::Operation

      param :book, type: Book

      def execute
        acting_user.basket << book 
        EmailUserAboutBook(book: params.book, user: acting_user)
        end
      end
    end

    class EmailUserAboutBook < HyperLoop::ServerOperation
      # client code knows nothing about UserMailer, so this has to be a Server Operation

      allow_operation { acting_user }
     
      param :book, type: Book
      param :user, type: User

      execute do
        UserMailer.book_email(params.user, params.book)
      end

    end

  end
end
Clone this wiki locally