whether the paper trail has a way to track the user activity based on a specific role - paper-trail-gem

I'm trying to use paper trail to track user activity in my application. i have set up the gem, followed the tutorial and made it work fine in my application..
but, when I test it as an ordinary user (as a guest user), paper trail also tracks this guest activity which the same model and controller...
Is there any way how to track the user activity based on role ?
i mean, i just want to track the activity of a specific role..
Need help..
paper trail

i just want to track the activity of a specific [user] role
You can turn PaperTrail off in your controller. From the documentation:
Turning PaperTrail Off
Per Request
Add a paper_trail_enabled_for_controller method to your controller.
class ApplicationController < ActionController::Base
def paper_trail_enabled_for_controller
!(cool_user? || trustworthy?) # :) this part is up to you
end
end
If that doesn't work, there are many other ways to turn PaperTrail on/off described the documentation.

Related

Public Activity - show only activity about self created objects

I followed Ryan Bates Tutorial on Public Acticvity. I'm trying to show ONLY notifications about objects the current_user owns.
In my Situation => Comments.
my Activities Controller
class ActivitiesController < ApplicationController
def index
#activities = PublicActivity::Activity.order('created_at desc').where(owner_id: current_user.following_users, owner_type: "User")
end
end
I'm using current_user.following_users to get all followed Users. To get activity if they Upload a Picture.
AT THIS POINT: Activities are shown from all followed User's, and THATS the problem.. All activities.
I want to show only activities that concern the current_user, only activities about his own objects.
For example.
current_user Uploads a Picture, UserX comments on this Picture. I want the Notification.
UserX comments on a Picture from UserY. I don't want this notification.
For now if current_user follows UserX, i'm getting all the notifications from UserX's Activities, and not only the notifications that concerns the current_user.
But i'm completely clueless on how to achieve this. Has anyone some helping hand unoccupied ?
I found a pretty similar problem, but i don't understand the Answer -> Using public_activity with acts_as_follower and devise, how can I show only the user's activities and those of the people he follows?
Another one: Rails getting activity feed that only involves current_user
I think the feature you asked is a bit beyond the scope of general activities, but rather like notifications.
The "recipient" solution should be able to solve this exact problem. But you may still want the owner to show this activity, as well as the current_user. If so you need to create two activities and there needs workaround not to show them all in public. So, this may work, but duplicate record and extra code.
A better logic may be to process activities after created, judge the logic, and send notification, either on request or backend(better).
Notice: Sharmeless ad below :)
I have similar concern before and found it hard to reuse Public Activity's activity records again for other purpose. So I made a gem simple_activity which is even simpler on displaying activities but open the door to reuse them again. This gem is still at very early stage so be cautious. Check it if it helps.

Rails User tracking activity

I want to track user activity on site. I tried ActiveSupport::Notifications but hasn't user_id or something to define a user. As I see, there are 2 ways to do this: through ActiveRecord queries, and through Page Requests. In the first case I should subscribe to all possible user actions with model (after_create, after_destroy etc.), and I have about 20 associated models, that is a lot of duplicate code. The second - I don't know how to do this, but it's seems simply to me.
I'm not using Device or any gem.
Perhaps in the first way, I could include module and make all logic in it, but It's not working.
The problem was solved by adding before_filter :track_activity in ApplicationController, ti calls every time when user enter any page.

Paper Trail Gem: Tracking user activity to give reputation points

I have a question about the gem called Paper Trail. I want to give reputation points to the ones who are the most active on the website. Can this gem help me with this? Also, Is it possible to track which users edited or created a model that doesn't belong to them?
Any time a change is made in your system, paper trail can track the user id via a whodunnit property. This is set automatically if your controller makes a current_user object available. I know authlogic does this and I bet devise does too. so yes, it will track any user, regardless of whether there is an actual relationship between the user and the model.

How can I pull in data from other controllers/tables into one list?

I have a rails project where users sign up and login. Users will create "Music" so I have a "Music" controller, the user will fill in a title, and upload their MP3. Every user can do this -- this is dandy.
When a user logs in I want them to see everyone's uploaded music in a nice list view at http://example.org/explore. I will have some "latest additions since your last login" somewhere on the sidebar of this "explore" page in the future among some other features.
My question is, should I create a controller called "Explore" with an index action for this listing with something like the following?
class ExploresController < ApplicationController
def index
#music = Music.all
end
def show
end
end
I thought this might be the right way to do this but it seems odd to have the "Explore" controller be using all of the actions you'd typically see in the "Music" controller. The Music controller is going to be CRUD for the current_user.
You can easily have the music#index method available to everyone and put user authentication on the CRUD methods. That way everyone can see all the uploaded music on the /music page, but only the owner of each one can alter them.
If users need an easy way to see a list of use their own mp3s then you can just use a nested resource, so it would be available at /users/:id/music.
If you need some more info on how to implement that stuff then leave a comment. The specifics would depend on what (if any) authentication systems you were using.
It doesn't look a Rails way.
Use same Music controller( if it's just a CRUD inherited_resources is your choice ) with has_scope gem and manage user access with cancan.
This way you have a 5lines MusicController and no any additional controlers
It's perfectly fine to add an 'explore' action to the Music controller.
As to whether or not a new controller is warranted, how much in common does the Explore page you have in mind have common with functions from the Music controller? Can they share partials and helpers? Will they utilize the same callbacks?
If your only plan is to list all from another model in the new controller as shown, then I would say the answer is straightforward: keep in the in the Music controller.

How would one implement this sort of gradual engagement/lazy registration in Rails?

A long time ago I ran into a website (I unfortunately lost the address, it was some kind of newspaper site) that allowed you to make use of everything as if you were a registered user. You could rate, favorite and comment articles, and when you did it would display a discreet, embedded message saying you had to register to the website for your contributions to be saved. Then it had the link for you to see how your profile would look like if you did, and I was surprised to see it had all my activity there; the articles I read and saved, comments, etc. I left the site and when I came back to it later just out of curiosity, it still had my activity saved.
I thought it was the greatest thing ever, and now that I am in the process of building a website with social features, I would like to take that approach as well. But I am still pretty much a noob and so I don't have much clue as to how to go about it. How would you do it?
I would create a Profile model which is automatically created for any user that visits your site and adds the first favourite, rates the first item, etc. The Profile should be saved to your database including a suitably random and unique string. This string can be stored as a cookie on the client side, and will be used later to retrieve your profile. It should be random and long enough so that you cannot easily tamper with your cookie and get other anonymous people's profiles, but this is not entirely avoidable (so beware you store no sensitive data in anonymous profiles!).
Once a user registers, you can associate their Profile with their new User record and remove the cookie and the unique string identifier. You can now simply retrieve their profiles when they log in, based on their User record.
The Profile model can contain any information you would like to store.
If you want to differentiate between registered users and anonymous users, you could create an AnonymousProfile model and a Profile model (each with different attributes), and simply copy over all data from the anonymous profile to the user profile when someone registers.
Update:
Throughout your application you can decide to only use this information when a user is logged in. You might define a before_filter that grabs the current user, and only if there is an actual user logged in, do you use the profile data:
class ApplicationController < ActionController::Base
before_filter :fetch_user_data
def fetch_user_data
#current_user = ... # Work your magic to get current user
end
private
def current_profile
#current_user and #current_user.profile # Use profile association
end
end
Somewhere in a controller action:
if current_profile
# Do stuff with current_profile
# Only available to registered users...
end
You can later change the implementation of current_profile if you change your mind and want anonymous profiles to have effect for your anonymous users.
The only way to identify users is to use cookies. What the site you were using is likely doing is:
For a first time user create an entry in the 'users' table and add them to the 'guests' group. Save down an identifier cookie to the users machine so you can look them up again later.
If the user decides to register you can fill out the rest of their details in the user table (you might even want to have a separate table for user details and registration details like username/password etc...) and add them to the registered users group.
The way you manage your groups can be as simple as a flag in the database.
As this is a rails question...
I would probably handle most of this in a before_filter in your application_controller.rb. The steps would be something like:
if has_cookie
#user = lookup_user
else
#user = create_new_guest_user
end
You could very easily extend one of the existing authentication frameworks like acts_as_authenticated or clearance to do this.

Resources