All Projects → falm → attribute-depends-calculator

falm / attribute-depends-calculator

Licence: other
Automatically calculate a collection of depends attribute of ActiveRecord

Programming Languages

ruby
36898 projects - #4 most used programming language
shell
77523 projects

Projects that are alternatives of or similar to attribute-depends-calculator

Occams Record
The missing high-efficiency query API for ActiveRecord
Stars: ✭ 240 (+485.37%)
Mutual labels:  activerecord
sql-builder
A simple SQL builder for generate SQL for non-ActiveRecord supports databases
Stars: ✭ 34 (-17.07%)
Mutual labels:  activerecord
Kelvin
A powerful language for symbolic computation written in Swift.
Stars: ✭ 23 (-43.9%)
Mutual labels:  calculator
MHWCalculator
Monster Hunter World: Iceborne - Calculator
Stars: ✭ 39 (-4.88%)
Mutual labels:  calculator
query-objects-example
Example of rails app using Query Objects
Stars: ✭ 37 (-9.76%)
Mutual labels:  activerecord
Calcu
CALCULADORAS
Stars: ✭ 22 (-46.34%)
Mutual labels:  calculator
Octopus
Database Sharding for ActiveRecord
Stars: ✭ 2,496 (+5987.8%)
Mutual labels:  activerecord
yii2-activerecord-inheritance
ActiveRecord Inheritance is an util to provide the Class Table Inheritance Pattern the to the Yii2 framework
Stars: ✭ 18 (-56.1%)
Mutual labels:  activerecord
activerecord-migrations
A gem to simplify activerecord migrations in non-rails projects.
Stars: ✭ 15 (-63.41%)
Mutual labels:  activerecord
netcalc
Advanced network calculator and address planning helper
Stars: ✭ 20 (-51.22%)
Mutual labels:  calculator
calculator-reactjs
Simple Calculator app created with React.js
Stars: ✭ 25 (-39.02%)
Mutual labels:  calculator
n1 loader
Loader to solve N+1 issues for good. Highly recommended for GraphQL API.
Stars: ✭ 182 (+343.9%)
Mutual labels:  activerecord
WarpPI
WarpPI Calculator, Step-by-step algebra calculator for Raspberry Pi. (abandoned project)
Stars: ✭ 93 (+126.83%)
Mutual labels:  calculator
Scenic
Scenic is maintained by Derek Prior, Caleb Hearth, and you, our contributors.
Stars: ✭ 2,856 (+6865.85%)
Mutual labels:  activerecord
embedded
Rails/Activerecord plugin to make value objects embedded into active record objects
Stars: ✭ 48 (+17.07%)
Mutual labels:  activerecord
Activerecord Postgres enum
Integrate PostgreSQL's enum data type into ActiveRecord's schema and migrations.
Stars: ✭ 227 (+453.66%)
Mutual labels:  activerecord
kalker
Kalker/kalk is a calculator with math syntax that supports user-defined variables and functions, complex numbers, and estimation of derivatives and integrals
Stars: ✭ 1,237 (+2917.07%)
Mutual labels:  calculator
activerecord-debug errors
An extension of activerecord to display useful debug logs on errors
Stars: ✭ 23 (-43.9%)
Mutual labels:  activerecord
caeroc
☴🖩 Compressible Aerodynamics Calculator for Python
Stars: ✭ 25 (-39.02%)
Mutual labels:  calculator
bc
some useful math functions for linux calculator bc
Stars: ✭ 18 (-56.1%)
Mutual labels:  calculator

Attribute Depends Calculator

Build Status Coverage Status Code Climate Dependency Status Gem Version

The scenario of the gem is when you have an attribute on model that value depends of a calculation of other model's attribute which attribute's model related. AttributeDependsCalculator will help you solve the case

Requirements

  1. Ruby 2.0+
  2. Rails 4.0+

Installation

Add this line to your application's Gemfile:

gem 'attribute-depends-calculator'

And then execute:

$ bundle

Usage

Assume you have model order and order-item

class Order < ActiveRecord::Base
  has_many :order_items
  depend total_price: {order_items: :price}
end

class OrderItem < ActiveRecord::Base
  belongs_to :order
end

And you can

order = Order.first
order.total_price
#=> 100.0
order.order_items.pluck(:price)
#=> [50.0, 50.0]
order_item = order.order_items.first
order_item.update(price: 100)
order.reload.total_price
#=> 150.0

As above show the price of order automatically update when whatever order_items changes

Advanced

The options operator had two cateogries of value, the default value of the gem is expression sum

Operation

class Order < ActiveRecord::Base
  has_many :order_items
  depend total_price: {order_items: :price, operator: :+} # or :*
end

Expression

The following expression can be use to calculate the collection of depends attributes

sum

class Order < ActiveRecord::Base
  ...
  depend total_price: {order_items: :price, operator: :sum} # default
end

average

class Order < ActiveRecord::Base
  ...
  depend avg_price: {order_items: :price, operator: :average}
end

count

class Order < ActiveRecord::Base
  ...
  depend order_items_count: {order_items: :price, operator: :count}
end

minimum

class Order < ActiveRecord::Base
  ...
  depend min_price: {order_items: :price, operator: :minimum}
end

maximum

class Order < ActiveRecord::Base
  ...
  depend max_price: {order_items: :price, operator: :maximum}
end

Proc

Proc can be passing in operator option, and the only one params is the active reload of the depended relate Model

class Order < ActiveRecord::Base
  ...
  depend discount_price: {order_items: :price, operator: -> (items) { items.sum(:price) * discount } }
end

Contributing

Bug reports and pull requests are welcome on GitHub

License

MIT © Falm

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].