{"id":13835660,"url":"https://github.com/falm/attribute-depends-calculator","last_synced_at":"2025-10-31T14:30:38.384Z","repository":{"id":56842637,"uuid":"63218538","full_name":"falm/attribute-depends-calculator","owner":"falm","description":"Automatically calculate a collection of depends attribute of ActiveRecord","archived":false,"fork":false,"pushed_at":"2017-12-20T02:43:24.000Z","size":32,"stargazers_count":41,"open_issues_count":0,"forks_count":1,"subscribers_count":4,"default_branch":"master","last_synced_at":"2025-01-29T09:11:25.036Z","etag":null,"topics":["activerecord","activerecord-callbacks","calculator","rails"],"latest_commit_sha":null,"homepage":"","language":"Ruby","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/falm.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2016-07-13T05:56:32.000Z","updated_at":"2020-12-31T02:40:24.000Z","dependencies_parsed_at":"2022-08-29T12:40:25.623Z","dependency_job_id":null,"html_url":"https://github.com/falm/attribute-depends-calculator","commit_stats":null,"previous_names":[],"tags_count":2,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/falm%2Fattribute-depends-calculator","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/falm%2Fattribute-depends-calculator/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/falm%2Fattribute-depends-calculator/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/falm%2Fattribute-depends-calculator/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/falm","download_url":"https://codeload.github.com/falm/attribute-depends-calculator/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":238828564,"owners_count":19537704,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["activerecord","activerecord-callbacks","calculator","rails"],"created_at":"2024-08-04T14:01:08.003Z","updated_at":"2025-10-31T14:30:38.346Z","avatar_url":"https://github.com/falm.png","language":"Ruby","funding_links":[],"categories":["Ruby"],"sub_categories":[],"readme":"# Attribute Depends Calculator\n[![Build Status](https://travis-ci.org/falm/attribute-depends-calculator.svg?branch=master)](https://travis-ci.org/falm/attribute-depends-calculator) [![Coverage Status](https://coveralls.io/repos/github/falm/attribute-depends-calculator/badge.svg?branch=master)](https://coveralls.io/github/falm/attribute-depends-calculator?branch=master) [![Code Climate](https://codeclimate.com/github/falm/attribute-depends-calculator/badges/gpa.svg)](https://codeclimate.com/github/falm/attribute-depends-calculator) [![Dependency Status](https://gemnasium.com/badges/github.com/falm/attribute-depends-calculator.svg)](https://gemnasium.com/github.com/falm/attribute-depends-calculator) [![Gem Version](https://badge.fury.io/rb/attribute-depends-calculator.svg)](https://badge.fury.io/rb/attribute-depends-calculator)\n\nThe 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\n\n## Requirements\n1. Ruby **2.0+**\n2. Rails **4.0+**\n\n## Installation\n\nAdd this line to your application's Gemfile:\n\n```ruby\ngem 'attribute-depends-calculator'\n```\n\nAnd then execute:\n\n    $ bundle\n\n## Usage\nAssume you have model order and order-item\n```ruby\nclass Order \u003c ActiveRecord::Base\n  has_many :order_items\n  depend total_price: {order_items: :price}\nend\n\nclass OrderItem \u003c ActiveRecord::Base\n  belongs_to :order\nend\n```\nAnd you can\n```ruby\norder = Order.first\norder.total_price\n#=\u003e 100.0\norder.order_items.pluck(:price)\n#=\u003e [50.0, 50.0]\norder_item = order.order_items.first\norder_item.update(price: 100)\norder.reload.total_price\n#=\u003e 150.0\n```\nAs above show the price of order automatically update when whatever order_items changes\n\n## Advanced\n\nThe options **operator** had two cateogries of value, the default value of the gem is expression **sum**\n\n#### Operation\n\n```ruby\nclass Order \u003c ActiveRecord::Base\n  has_many :order_items\n  depend total_price: {order_items: :price, operator: :+} # or :*\nend\n```\n\n#### Expression\n\nThe following expression can be use to calculate the collection of depends attributes\n\n**sum**\n\n```ruby\nclass Order \u003c ActiveRecord::Base\n  ...\n  depend total_price: {order_items: :price, operator: :sum} # default\nend\n```\n\n**average**\n\n```ruby\nclass Order \u003c ActiveRecord::Base\n  ...\n  depend avg_price: {order_items: :price, operator: :average}\nend\n```\n\n**count**\n\n```ruby\nclass Order \u003c ActiveRecord::Base\n  ...\n  depend order_items_count: {order_items: :price, operator: :count}\nend\n```\n\n**minimum**\n\n```ruby\nclass Order \u003c ActiveRecord::Base\n  ...\n  depend min_price: {order_items: :price, operator: :minimum}\nend\n```\n\n**maximum**\n\n```ruby\nclass Order \u003c ActiveRecord::Base\n  ...\n  depend max_price: {order_items: :price, operator: :maximum}\nend\n```\n\n\n**Proc**\n\nProc can be passing in operator option, and the only one params is the active reload of the depended relate Model\n\n```ruby\nclass Order \u003c ActiveRecord::Base\n  ...\n  depend discount_price: {order_items: :price, operator: -\u003e (items) { items.sum(:price) * discount } }\nend\n```\n\n\n\n\n\n## Contributing\nBug reports and pull requests are welcome [on GitHub](https://github.com/falm/attribute-depends-calculator)\n\n## License\nMIT © [Falm](https://github.com/falm)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffalm%2Fattribute-depends-calculator","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ffalm%2Fattribute-depends-calculator","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffalm%2Fattribute-depends-calculator/lists"}