{"id":13484319,"url":"https://github.com/michelson/espinita","last_synced_at":"2025-04-09T12:09:40.434Z","repository":{"id":11536376,"uuid":"14021019","full_name":"michelson/espinita","owner":"michelson","description":"Audit activerecord models like a boss (and works with rails 4!)","archived":false,"fork":false,"pushed_at":"2023-10-03T14:03:59.000Z","size":186,"stargazers_count":220,"open_issues_count":18,"forks_count":29,"subscribers_count":25,"default_branch":"master","last_synced_at":"2024-05-21T01:05:49.245Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"Ruby","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/michelson.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"MIT-LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null}},"created_at":"2013-10-31T15:37:44.000Z","updated_at":"2023-10-03T14:03:38.000Z","dependencies_parsed_at":"2023-02-10T20:00:58.637Z","dependency_job_id":"685755e8-4cc2-4774-8670-884c3cf13abe","html_url":"https://github.com/michelson/espinita","commit_stats":{"total_commits":63,"total_committers":10,"mean_commits":6.3,"dds":"0.47619047619047616","last_synced_commit":"2dc027edc838ee5de0d68558f1758273ccc01636"},"previous_names":["continuum/espinita"],"tags_count":9,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/michelson%2Fespinita","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/michelson%2Fespinita/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/michelson%2Fespinita/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/michelson%2Fespinita/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/michelson","download_url":"https://codeload.github.com/michelson/espinita/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248036067,"owners_count":21037092,"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":[],"created_at":"2024-07-31T17:01:22.467Z","updated_at":"2025-04-09T12:09:40.418Z","avatar_url":"https://github.com/michelson.png","language":"Ruby","funding_links":[],"categories":["ORM/ODM Extensions","Ruby"],"sub_categories":[],"readme":"# Espinita\n\n[![Build Status](https://secure.travis-ci.org/continuum/espinita.png)](http://travis-ci.org/continuum/espinita) [![Dependency Status](https://gemnasium.com/continuum/espinita.png)](https://gemnasium.com/continuum/espinita) [![Coverage Status](https://coveralls.io/repos/continuum/espinita/badge.png?branch=master)](https://coveralls.io/r/continuum/espinita?branch=master) [![Code Climate](https://codeclimate.com/github/continuum/espinita.png)](https://codeclimate.com/github/continuum/espinita)\n=======\n\n## Audits activerecord models like a boss\n\n![Alt text](./espinita.jpg)\n\nAudit activerecord models like a boss. Tested in rails 4.0 / 4.1 and ruby 1.9.3 / 2.0.0.\n\nThis project is heavily based in audited gem.\n\n## Installation\n\nIn your gemfile\n\n```ruby\ngem \"espinita\"\n```\n\nIn console\n```ruby\n$ rake espinita:install:migrations\n$ rake db:migrate\n```\n\n## Usage\n\n```ruby\nclass Post \u003c ActiveRecord::Base\n  auditable\nend\n\n@post.create(title: \"an awesome blog post\" )\n```\n\nEspinita will create an audit by default on creation , edition and destroy:\n\n```ruby\n@post.audits.size #=\u003e 1\n```\n\nEspinita provides options to include or exclude columns to trigger the creation of audit.\n\n```ruby\nclass Post \u003c ActiveRecord::Base\n  auditable only: [:title] # except: [:some_column]\nend\n```\n\nAnd lets you declare the callbacks you want for audit creation:\n\n```ruby\nclass Post \u003c ActiveRecord::Base\n  auditable on: [:create]  # on: [:create, :update]\nend\n```\n\nYou can find the audits records easily:\n\n```ruby\n@post.audits.first #=\u003e  #\u003cEspinita::Audit id: 1, auditable_id: 1, auditable_type: \"Post\", user_id: 1, user_type: \"User\", audited_changes: {\"title\"=\u003e[nil, \"MyString\"], \"created_at\"=\u003e[nil, 2013-10-30 15:50:14 UTC], \"updated_at\"=\u003e[nil, 2013-10-30 15:50:14 UTC], \"id\"=\u003e[nil, 1]}\n```\n\nEspinita will save the model changes in a serialized column called audited_changes:\n\n```ruby\n@post.audits.first.audited_changes #=\u003e {\"title\"=\u003e[nil, \"MyString\"], \"created_at\"=\u003e[nil, 2013-10-30 15:50:14 UTC], \"updated_at\"=\u003e[nil, 2013-10-30 15:50:14 UTC], \"id\"=\u003e[nil, 1]}\n```\n\nEspinita will detect the current user when records saved from rails controllers. By default Espinita uses current_user method but you can change it:\n\n```ruby\nEspinita.current_user_method = :authenticated_user\n```\n\n#### History and Restoration\nIf you just want a summary of changes for a particular attribute or attributes of a model, you can use the `history_from_audits_for` method.\n```ruby\nmy_model.history_from_audits_for(:name)\n=\u003e [{changes: {name: \"Arglebargle\"}, changed_at: 2015-05-13 15:28:22 -0700},\n{changes: {name: \"Baz\"}, changed_at: 2014-05-13 15:28:22 -0700},\n{changes: {name: \"Foo\"}, changed_at: 2013-05-13 15:28:22 -0700}]\n\n```\n\nYou can also provide an array of attributes to get a single history for all of them.\n```ruby\nmy_model.history_from_audits_for([:name, :settings])\n=\u003e [{changes: {name: \"Arglebargle\", settings: \"Waffles\"}, changed_at: 2015-05-13 15:28:22 -0700},\n{changes: {name: \"Baz\"}, changed_at: 2014-05-13 15:28:22 -0700}]\n\n```\n\nSometimes it's useful to roll a record back to a particular point in time, such as if it was accidentally modified. For this, the `restore_attributes!` method is provided.\n\nAs with `history_from_audits_for`, this can be used with a single attribute or an array of attributes.\n```ruby\nmodel.name\n=\u003e \"Baz\"\nmodel.settings\n=\u003e \"\"\n\nmodel.history_from_audits_for([:name, :settings])\n=\u003e [{:changes=\u003e{:name=\u003e\"Baz\", :settings=\u003e\"\"}, :changed_at=\u003e2015-05-03 15:33:58 -0700},\n {:changes=\u003e{:name=\u003e\"Arglebargle\", :settings=\u003e\"IHOP\"}, :changed_at=\u003e2015-03-24 15:33:58 -0700},\n {:changes=\u003e{:name=\u003e\"Walrus\"}, :changed_at=\u003e2014-05-13 15:33:58 -0700}]\n\nmodel.restore_attributes!([:name, :settings], DateTime.now - 57.days)\n=\u003e true\n\nmodel.name\n=\u003e \"Walrus\"\nmodel.settings\n=\u003e \"MyText\"\n```\n\nThe `restore_attributes!` method returns `true` if it makes a change to the model, or `false` if there is no resulting change.\n\nNote: this uses `update_attributes()` to do the rollback, so it will *skip* validations, but will trigger any callbacks that you may have in place.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmichelson%2Fespinita","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmichelson%2Fespinita","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmichelson%2Fespinita/lists"}