Work with entity history
journals are a list of comments and changes of an entity. They are stored in the journals table with journal_details records of changed attributes.
We use journals to display history of changes usually in the detail of an entity. The number of displayed journals is limited by EasySetting.value("easy_extensions_journal_history_limit") or a default of 10. To load all journals, a "Show all" button is displayed at the bottom.
Model
First of all you need to add acts_as_easy_journalized concern into you model. It add functionality to store and work with journals.
For more details see ActsAsEasyJournalized#acts_as_easy_journalized
| Example of acts_as_easy_journalized usage | |
|---|---|
To journalize changes of attributes you need to call init_journal method first - BEFORE attributes are set. Then, calling save automatically creates JournalDetail objects.
Controller
We usually call init_journal in update action. To show history / journals we use standalone action which renders journals using AJAX.
| Example of update action | |
|---|---|
Include the EasyControllersConcerns::EasyJournals concern in your controller and define the journals method as below.
| Example of journals action for render history | |
|---|---|
Users permissions
Every action in the controller should have proper permissions. Do not forget to extend the view_ permission with your new action "journals".
| Example of permissions | |
|---|---|
Routes
You need to define a route for the journals action. For example:
Views
There are two approaches to render journals:
Async loading using tabs (recommended)
Render partial with tabs.
<div id="entity-detail-bottom">
<%= render partial: "common/entity_tabs", locals: { tabs: my_model_tabs(@my_model), tabs_container: "entity-detail-bottom" } %>
</div>
Define a helper method that holds an array of tabs, specifying a trigger with AJAX loading.
| Example of helper method for tabs | |
|---|---|
Direct rendering
TODO: add example
User reactions
Keep in mind that users can react to comments with emojis. To fully support this feature, you need to ensure that reaction components and initialized after loading of journals past the basic limit (clicking the "Show all" button). This is done using GraphQL. All you need is to define a GraphQL Type and Query field for your entity.
For the Type just use the has_journals. You can also override the default methods it adds as needed.
| Example of journals usage in GraphQL | |
|---|---|
Then, extend Query with a new field for fetching journals.
| Example of extending EasyGraphql::Types::Query | |
|---|---|