https://github.com/tylerrick/paper_trail-active_record
Various ActiveRecord extensions to make your life easier when working with PaperTrail versions
https://github.com/tylerrick/paper_trail-active_record
Last synced: 8 months ago
JSON representation
Various ActiveRecord extensions to make your life easier when working with PaperTrail versions
- Host: GitHub
- URL: https://github.com/tylerrick/paper_trail-active_record
- Owner: TylerRick
- License: mit
- Created: 2020-04-23T20:39:12.000Z (about 6 years ago)
- Default Branch: master
- Last Pushed: 2022-06-07T00:25:21.000Z (about 4 years ago)
- Last Synced: 2024-12-21T00:02:58.092Z (over 1 year ago)
- Language: Ruby
- Size: 27.3 KB
- Stars: 0
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: Readme.md
- Changelog: Changelog.md
- License: License
Awesome Lists containing this project
README
# PaperTrail::ActiveRecordExt
[![Gem Version][1]][2]
[](https://rdoc.info/github/TylerRick/paper_trail-active_record/master)
Various ActiveRecord extensions to make your life easier when working with
[PaperTrail](https://github.com/paper-trail-gem/paper_trail) versions and versioned model records.
## Methods added to models with `has_paper_trail`
- `.versions`
- `.find_deleted_version`
- `.find_deleted`
- `.has_many_versions`
- `.has_related_versions`
- `.has_versions_with_all_related`
- `created_version`
- `paper_trail_update_column_using_value_from_changes`
- `paper_trail_update_columns_using_value_from_changes`
## Methods added to `PaperTrail::Version` (`VersionConcern`)
- `.preceding_inclusive`
- `.between_inclusive`
- `scope :where_object_changed`
- `scope :where_object_changed_any`
- `#action`
- `#item_class`
## `{association}_or_deleted`
### `def define_assoc_or_deleted(assoc_name, suffix: nil)`
Defines a `{association}_or_deleted` method for the given association. This method will call
the usual association method to try to find the associated record but if that returns nil,
will fall back to looking for a deleted record from the `versions` history (using
`klass.find_deleted`).
You can replace the `or_deleted` part with a different suffix using `suffix:` option.
You can even give it the same name as the existing association method if you want to override
the existing method with one that always falls back to looking for a deleted record.
```ruby
class Post
belongs_to :author
# overrides author method with a version that finds deleted if not found
define_assoc_or_deleted :author, suffix: nil
```
### Automatically add for all assocations
If you include `PaperTrail::ActiveRecordExt::OrDeleted` into a model, it will automatically add a `{association}_or_deleted`
method for every `belongs_to` or `has_one` association that is defined.
Because it reflects on all associations on that model as soon as it is included, make sure to
include it *after* all of your associations are defined. You can also call
`define_assoc_or_deleted_on_all_associations` at the end of your model class (that is the same
method that including the module triggers).
If you want it to automatically be added for all assocations on *all* application models, you can
use [gem 'active_record_include'](https://github.com/TylerRick/active_record_include) like this:
```ruby
class ApplicationRecord < ActiveRecord::Base
include_when_connected PaperTrail::ActiveRecordExt::OrDeleted
```
## Installation
Add this line to your application's Gemfile:
```ruby
gem 'paper_trail-active_record'
```
And then execute:
$ bundle
## Development
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
## Contributing
Bug reports and pull requests are welcome on GitHub at
https://github.com/TylerRick/paper_trail-active_record.
[1]: https://badge.fury.io/rb/paper_trail-active_record.svg
[2]: https://rubygems.org/gems/paper_trail-active_record