Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/midnightmonster/rails-utils
https://github.com/midnightmonster/rails-utils
Last synced: about 16 hours ago
JSON representation
- Host: GitHub
- URL: https://github.com/midnightmonster/rails-utils
- Owner: midnightmonster
- Created: 2021-08-13T01:38:58.000Z (about 3 years ago)
- Default Branch: master
- Last Pushed: 2021-09-27T14:04:49.000Z (about 3 years ago)
- Last Synced: 2024-10-22T02:13:19.659Z (17 days ago)
- Language: Ruby
- Size: 6.84 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Rails Utilities
## MultiCount
Are you sending multiple queries to get different counts or summaries of the same rows of data? Is Postgres giving you side-eye because you ask it to visit the same records several times in one controller action? `MultiCount` is here for you. `include MultiCount` in your `ApplicationModel` and replace those 2-10 queries with one smart, fast query.
```ruby
# Assuming foo and bar are Widget scopes...widgets = factory.widgets.available
foo_count = widgets.foo.count
bar_count = widgets.bar(zonk).count
spin_counts = widgets.group(:spin).count# ...becomes...
foo_count, bar_count, spin_counts =
factory.widgets.available.multi_count(
:foo,
[:bar,zonk],
Arel.sql('widgets.spin')
)# ...which runs a single query that in most cases is no slower than the
# slowest of the original three. Sometimes it's actually faster, due to
# Query Planner Mysteries.
```## CountVonCount
You're using `find_each` to avoid instantiating thousands of records at once, but are you really releasing those records or accidentally holding on to them? Use `CountVonCount` to audit exactly what Ruby's allocating and garbage collecting.
## Snowdrift
Explicitly and conveniently accumulate and report on counts, lists, and sets over multi-step processes, even with nested levels of consideration. (E.g., while processing inventory reports, track available widgets by store, region, and globally.) `Snowdrift` is not specific to Rails/ActiveRecord and could be used in any Ruby application.