Giter Club home page Giter Club logo

acts_as_holdable's Introduction

Build Status Maintainability Coverage Status

ActsAsHoldable

ActsAsHoldable allows resources to be held by users. It:

  • Is a solution based on Rails engines

Installation

Add this line to your application's Gemfile:

gem 'acts_as_holdable'

And then execute:

$ bundle

Or install it yourself as:

$ gem install acts_as_holdable

Migrations

bundle exec rake acts_as_holdable_engine:install:migrations

Holdables, Holders and Holdings

To set-up a Holdable model, use acts_as_holdable. A Holdable model is enabled to be held.

class Ticket < ApplicationRecord
  acts_as_holdable
end

To set-up a Holder model, use acts_as_holder. Only Holders can create holdings.

class User < ApplicationRecord
  acts_as_holder
end

From this time on, a User can hold a Ticket with

@user.hold! @ticket

A User can unhold a Ticket with

@holding = @user.hold! @ticket
@user.unhold! @holding

You can access holdings both from the Holdable and the Holder

@ticket.holdings # return all holdings created on this ticket
@user.holdings # return all holdings made by this user

Configuring Options

There are a number available options to make your models behave differently. They are all configurable in the Holdable model, passing a hash to acts_as_holdable

Available options (with values) are:

  • :on_hand_type: Specifies how the amount of a holding (e.g. number of tickets from an event) affects the future availability of the holdable. Allowed values are:
    • :none
    • :open

No constraints

The model accepts holdings without any constraint. This means every holder can create an infinite number of holdings on it and no capacity or time checks are performed.

Creating a holding on this model means holding it forever and without care for other existing holdings. In other words, the number of holdings do not affect the availability of this holdable. (e.g. pre-ordering a product that will be released soon)

Capacity constraints

The option on_hand_type may be used to set a constraint over the amount attribute of the holding

No capacity constraints - on_hand_type: :none

The model is holdable without capacity constraints.

class Product < ActiveRecord::Base
  # As `on_hand_type: :none` is a default and can be ommited
  acts_as_holdable on_hand_type: :none
end

Open capacity - on_hand_type: :open

WARNING - migration needed! - with this option the model must have an attribute on_hand: :integer

The model is holdable until its capacity is reached. (e.g. an event't tickets)

Configuration

class Event < ActiveRecord::Base
  acts_as_holdable on_hand_type: :open
end

Creating a new holdable

Each instance of the model must define its capacity.

@ticket = Ticket.new(...)
@ticket.on_hand = 30 # This event allows 30 tickets
@ticket.save!

Holding

# Holding a model with `on_hand_type: :open` requires `:amount`

@user1.hold! @ticket, amount: 5 # holding tickets for 5 people, OK
@user2.hold! @ticket, amount: 20 # holding tickets for other 20 people, OK
@user3.hold! @ticket, amount: 10 # overholding! raise ActsAsHoldable::AvailabilityError

Holdings Track - on_hold_track: :true

Useful if you want an easier way to keep track of how many holdings you currently have. Every time a holding is created or deleted the on_hold counter gets uptaded on your model

WARNING - migration needed! - with this option the model must have an attribute on_hold: :integer

Holding resources on a span of time

Holdings can be created to be 'living' within a span of time, if the holding doesn't gets confirmed within the time span, it will be destroyed.

@holding = @user.hold_for(@ticket, duration: 10.minutes, amount: 1)

A User can confirm a holding with

@user.confirm_holding!(@holding)

Contributing

Contribution directions go here.

License

The gem is available as open source under the terms of the MIT License.

Acknowledgements

To speed-up the initialization process of this project, the structure of this repository was strongly influenced by ActsAsBookable by Tandù srl.

acts_as_holdable's People

Contributors

aromaron avatar

Stargazers

Alejandro Gutiérrez avatar

Watchers

James Cloos avatar

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.