Giter Site home page Giter Site logo

serviceobjectsexercise's Introduction

Introduction

The aim of this exercise is to explain the use of Service Objects in Rails App, and help you to answer the question: 'How Service Object is supposed to make my life easier?'.

App description

This is a very simple API app, that allows only to create table reservations. There are 3 controllers doing the very same thing:

  • ReservationsController - plain Rails controller
  • SoReservationsController - Controller using Service Objects
  • MonadsReservationsController - Controller using Service Objects based ond dry-monad :result

Reservation Creation

In order to make a reservation, we need to provide the following params:

  • id of the customer
  • id of the table
  • Date of the reservation
  • number of guests

Reservation parameters' validation

Thanks to ActiveRecord, we have a validation for the presence of customers and tables id. We wan to add two more validations:

  • Date of the reservation must be at least 7 days in the future
  • Number of guests should be smaller than the capacity of the table

Exercise description

Make all the tests green :) You should change only with the following files:

  • app/controllers/reservations_controller.rb (plain Rails approach)
  • app/services/ReservationServices/create_reservation_with_monads.rb (Monad service object approach)
  • app/services/ReservationServices/create_reservation.rb (Service Object approach)

Goal

At the first glance, all three controllers look more or less the same, but Service Objects approaches require additional class (and more code) to do the same thing as simple Rails controllers. The real goal of this exercise is to show, that Service Objects approach is not only more readable, but also more flexible.

How to run

Install dependiencies

bundle install

Run tests

bundle exec rspec

serviceobjectsexercise's People

Contributors

ciejj avatar

Watchers

 avatar

Forkers

haessr

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.