GithubHelp home page GithubHelp logo

codemanmeet / amountable Goto Github PK

View Code? Open in Web Editor NEW

This project forked from instacart/amountable

0.0 2.0 0.0 28 KB

An easy and transparent way to attach, manage and sum Money fields to your ActiveRecord models.

License: ISC License

Ruby 100.00%

amountable's Introduction

Amountable

Build Status Gem Version

This gem helps you integrate Money fields into your ActiveRecord models without having to add new columns each time.

It also helps manage and sum various components of your models to keep amount definitions consistent across your application.

Installation

Add

gem 'amountable', github: 'instacart/amountable'

to your Gemfile. Then run

bundle

and

rake amountable:install:migrations

and finally

rake db:migrate

Usage

Setup your model

class Order < ActiveRecord::Base
  include Amountable
  amount :subtotal, sets: [:total]
  amount :delivery_fee, sets: [:total, :fees]
  amount :bags_fee, sets: [:total, :fees]
  amount :sales_tax, sets: [:total, :taxes]
  amount :local_tax, sets: [:total, :taxes]
end

then create it

order = Order.create(
  subtotal: Money.new(123),
  delivery_fee: Money.new(100),
  bags_fee: Money.new(10),
  sales_tax: Money.new(56)
  )
order.subtotal
# => #<Money fractional:123 currency:USD>
order.total
# => #<Money fractional:289 currency:USD>
order.fees
# => #<Money fractional:110 currency:USD>
order.taxes
# => #<Money fractional:56 currency:USD>

Implementation

When you run the migration, an amounts table will be created with a polymorphic relationship to an amountable, which in the above example would be your orders.

When the an order is created with some amounts, the associated Amount objects are persisted.

Amount objects are persisted in bulk, so there are no N + 1 queries. If an amount is zero, no Amount model is created.

amountable's People

Contributors

neutralino1 avatar sferik avatar

Watchers

 avatar  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.