• Stars
    star
    123
  • Rank 290,145 (Top 6 %)
  • Language
    Ruby
  • License
    ISC License
  • Created almost 9 years ago
  • Updated over 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

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

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
  act_as_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

act_as_amountable can take the storage option:

  act_as_amountable storage: :table

where storage can be either :table, the amounts table will be used to store amounts, or :jsonb, a JSONB field will be used on the amountable object to store amounts are json. If you use the JSONB format, you can specify the name of the column with the column option. The default value for the storage option is :table.

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.

If you choose the JSONB storage option, the amounts table will not be used. Instead a JSONB column on the target model will be used.

More Repositories

1

lore

Lore makes machine learning approachable for Software Engineers and maintainable for Machine Learning Researchers
Python
1,549
star
2

truetime-android

Android NTP time library. Get the true current time impervious to device clock time changes
Kotlin
1,415
star
3

Nantes

Swift TTTAttributedLabel replacement
Swift
1,109
star
4

makara

A Read-Write Proxy for Connections; Also provides an ActiveRecord adapter.
Ruby
902
star
5

TrueTime.swift

NTP library for Swift and Objective-C. Get the true time impervious to device clock changes.
Swift
589
star
6

wilson_score

Simple, dependency-free Wilson score
Ruby
154
star
7

formula

A functional reactive framework for managing state and side effects based on RxJava.
Kotlin
151
star
8

ohmycron

Run cron jobs in a standardized environment with logs and locking
Shell
138
star
9

ahab

Docker event handling with Python
Python
137
star
10

jardin-archived

A pandas.DataFrame-based ORM.
Python
84
star
11

Snacks

The Instacart Component Library
JavaScript
81
star
12

redux-rails

Redux and your server talking without fuss.
JavaScript
56
star
13

arn

A Python library for parsing AWS ARNs.
Python
44
star
14

ahoy-android

Android attribution library build on top of Ahoy for Ruby on Rails.
Java
20
star
15

cwam

CloudWatch Alarms Manager. Easy way to create default CloudWatch Alarms for AWS resources.
Python
19
star
16

gadjit

Python
16
star
17

instacart-android-pp-sandbox

Android challenge sandbox
Kotlin
9
star
18

aws-scripts-mon

[TO BE DELETED] Clone of AWS Cloudwatch Monitor Scripts + report inodes
Perl
4
star
19

redux-rails-resource

Simple interface of redux-rails resources for react components
JavaScript
3
star
20

OmniAX

UIAccessibility wrapper
Swift
2
star
21

instacart-ios-pp-starter

Starter app to give candidates starting on pair programming exercise
Swift
2
star
22

ExploreRxSwift

Swift
2
star
23

cloudflare-rails

Ruby
2
star
24

SocketLogger.swift

Lightweight, flexible logging utility compatible with any socket-based syslog service.
Swift
2
star
25

optplayground

A playground for optimization!
Python
1
star
26

instacart-android-pp-starter

Starter app to give candidates starting on pair programming exercise
Java
1
star
27

activerecord-import_with_callbacks

A library for bulk importing data using ActiveRecord
Ruby
1
star