Coder Social home page Coder Social logo

attribute-depends-calculator's Introduction

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

attribute-depends-calculator's People

Contributors

falm avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

Forkers

xiajian

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.