Skip to content

Add admin logs codebase#2604

Merged
mrcasals merged 49 commits intomasterfrom
admin/log-actions
Feb 16, 2018
Merged

Add admin logs codebase#2604
mrcasals merged 49 commits intomasterfrom
admin/log-actions

Conversation

@mrcasals
Copy link
Copy Markdown
Contributor

@mrcasals mrcasals commented Jan 30, 2018

🎩 What? Why?

See #2603 for reference.

This PR intends to add a log of all actions performed by admins. It will only be visible by organization admins.

In a first iteration of this, I will only log actions on accountability results, since they are already versioned and it's simpler to start. This way I can test the whole feature, work on it, and later improve it when logging actions from other modules.

Note that this is not the MVP, this adds the codebase so the MVP can be added.

📌 Related Issues

📋 Subtasks

  • Add ActionLog class
  • Add a wrapper class to create ActionLogs
  • Make Decidim.traceability service log all actions
  • Log actions in accountability module => I will work on results first
  • Create presenters
  • Add sentence for updates
  • Add base log presenter
  • List the log with pagination
  • Add CHANGELOG entry

📷 Screenshots (optional)

The admin dashboard shows the last 20 logs:

The logs section shows all logs, paginated (20 per page):

@mrcasals mrcasals self-assigned this Jan 30, 2018
@ghost ghost added the in-progress label Jan 30, 2018
@mrcasals mrcasals changed the title [WIP} Log all actions for admins [WIP] Log all actions for admins Jan 30, 2018
@codecov
Copy link
Copy Markdown

codecov bot commented Jan 30, 2018

Codecov Report

Merging #2604 into master will increase coverage by 0.01%.
The diff coverage is 99.57%.

@@            Coverage Diff             @@
##           master    #2604      +/-   ##
==========================================
+ Coverage   98.84%   98.85%   +0.01%     
==========================================
  Files        1504     1531      +27     
  Lines       35293    35987     +694     
==========================================
+ Hits        34884    35575     +691     
- Misses        409      412       +3

@mrcasals mrcasals force-pushed the admin/log-actions branch 2 times, most recently from 832e2f1 to 1eab324 Compare January 30, 2018 14:40
@mrcasals mrcasals force-pushed the admin/log-actions branch 7 times, most recently from ea8bb1f to dc80397 Compare February 1, 2018 10:14
Copy link
Copy Markdown
Contributor

@oriolgual oriolgual left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks nice!

<p><%= t ".welcome" %></p>
<div class="row">
<ul>
<% Decidim::ActionLog.order(id: :desc).first(20).each do |log| %>
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't we order by date?

optional: true,
polymorphic: true

validates :organization, :user, :action, :resource, presence: true
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we also validate the the organization of all associations is the same?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is automatically done by the system

end

def participatory_space
resource.participatory_space if resource.respond_to?(:participatory_space)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do all resource delegate the participatory space to the feature?

If not, maybe you can try to get it from the feature too

@mrcasals mrcasals force-pushed the admin/log-actions branch 7 times, most recently from e34a382 to f506ae1 Compare February 2, 2018 13:22
#
# Returns a String.
def action_string
case action
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add .to_s to prevent possible errors when comparing against Nil or if someone returns a symbol? (yay types!)

resource.participatory_space if resource.respond_to?(:participatory_space)
end

def get_title(resource)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rename to title_for?

remove_this_file: Remove this file
log:
base_presenter:
create: "%{user_name} created %{result_name} in %{space_name}"
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Result?

organization: organization || build(:organization)
)
end
participatory_space { build :participatory_process, organization: organization || build(:organization) }
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe you should get the organization from the user

3. Create a `Presenter` for your model for the log you need. Presenters should be named as `Decidim::<your module name>::<log name>::<your model name>Presenter`. There's currently only one log, `AdminLog`. This means that if your model is `Decidim::Accountability::Result`, then your admin log presenter is must be `Decidim::Accountability::AdminLog::ResultPresenter`

## Multiple logs
Alhotugh Decidim currently only has a log for the admin section, in the future we might need an activity log for the public part. It's easy to assume we might need to render the same data in different formats, so we need to differentiate the presenters for each log. The current system handles the case for multiple logs, although we only have one. No newline at end of file
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Although


In order to make your component compatible with the activity log, you need to follow these steps:

1. Make your model include the `Decidim::Traceable` module. This will enable Decidim to create versions every time your model records are changed. It uses [`paper_trail`](https://github.com/airblade/paper_trail) to generate the versions.
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this really mandatory or recommended?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It will be needed because the Product team wants to see all changes in all fields when the user updates a resource. For this, the best way is to have paper_trail included though the Traceable module

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Then maybe we need some kind of validation?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done, logs are not saved if the resource is not traceable.

1. Make your model include the `Decidim::Traceable` module. This will enable Decidim to create versions every time your model records are changed. It uses [`paper_trail`](https://github.com/airblade/paper_trail) to generate the versions.
2. Make your commands use `Decidim.traceability` to create and update records. Documentation can be found in `Decidim::Traceability`. This should properly set the author of the change in your record.
3. Create a `Presenter` for your model for the log you need. Presenters should be named as `Decidim::<your module name>::<log name>::<your model name>Presenter`. There's currently only one log, `AdminLog`. This means that if your model is `Decidim::Accountability::Result`, then your admin log presenter is must be `Decidim::Accountability::AdminLog::ResultPresenter`

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

An example would be nice.

@mrcasals mrcasals force-pushed the admin/log-actions branch 4 times, most recently from f6a5c02 to c6f5915 Compare February 5, 2018 09:56
@oriolgual oriolgual dismissed their stale review February 5, 2018 10:22

Requested changes solved

@mrcasals mrcasals force-pushed the admin/log-actions branch 2 times, most recently from 182ecf4 to 9581f66 Compare February 5, 2018 14:23
@mrcasals mrcasals merged commit 4382b2a into master Feb 16, 2018
@ghost ghost removed the in-review label Feb 16, 2018
@mrcasals mrcasals deleted the admin/log-actions branch February 16, 2018 13:38
@mrcasals mrcasals mentioned this pull request Feb 16, 2018
2 tasks
@xabier xabier mentioned this pull request Feb 19, 2018
@mrcasals mrcasals modified the milestones: CDP1, CdP2 Dec 7, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants