Using Public_Activity with Acts_As_Follower and Devise, How to Show Only the User's Activities and Those of the People He Follows

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?

It turns out that notify_user_of = current_user.following_users was not an array as I had thought, but an active record object. By manually creating the array and adding individual users to it I was able to achieve the desired result.

...
notify_user_of = []
notify_user_of << current_user
for user in current_user.following_users
notify_user_of << user
end
...

Public Activity - show only activity about self created objects

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 3 public_activity, destroy record

I think this is what the OP was looking for / may have figured out, but didn't post in a solution.

Quick example scenario:

A user creates a comment, therefore a Public Activity Record (key: comment.create) is created for that comment.

Now, lets say the user deletes his comment.

There is still an activity (key: comment.create) stored in the activities table that relates to the original comment that just got deleted.

To delete that original activity all together when a user deletes their corresponding activity (key: comment.create). Simply do the following.

#comments_controller.rb or whatever class you are tracking
def destroy
@comment = current_user.comments.find(params[:id])
@activity = PublicActivity::Activity.find_by(trackable_id: (params[:id]), trackable_type: controller_path.classify)
@activity.destroy
@comment.destroy
end

Hope this helps someone.

Rails 4 public_activity with two types of owner

Actually owner is polymorphic as can be seen here:

create_table :activities do |t|
t.belongs_to :trackable, :polymorphic => true
t.belongs_to :owner, :polymorphic => true
t.string :key
t.text :parameters
t.belongs_to :recipient, :polymorphic => true

t.timestamps
end

So it's not a problem to assign owner of different type.

If you want to assign company as owner when company's user is signed-in and a person, when personal account is signed in, you should just make appropriate changes in your code:

tracked owner: ->(controller, model) { controller.current_user.organization? ? controller.current_user.organization : controller.current_user.person }

Devise: How to prevent users from seeing other users information

Filter on the current_user.id in your events method instead of params[:id]

@event = User.find(current_user.id)

However, an even better way would be to have a special route that doesn't include the id

get 'events' => 'users#events', as: :users_events

and use it like so

= link_to 'Events', users_events_path

Check if current_user is the owner of a resource and allow edit/delete actions

In your PhotosController:

before_filter :require_permission, only: :edit

def require_permission
if current_user != Photo.find(params[:id]).user
redirect_to root_path
#Or do something else here
end
end

How to show only articles written by users themselves through Devise in Rails

You probably have the following line in your Event model:

class Event < ApplicationRecord
belongs_to :user

And the reflection in the User model:

class User < ApplicationRecord
has_many :events

With these relations, you can simply do the following in your controller:

@events = current_user.events

Because current_user returns a User instance, and when you defined the has_many :events in the User model, Rails created this events instance method on User.

Turn omniauth facebook login into a popup

Sure, you can easily.

In your view:

=link_to "Log in with Facebook", omniauth_authorize_path(:user, :facebook), :class => "popup", :"data-width" => 600, :"data-height" => 400

In your application.js:

function popupCenter(url, width, height, name) {
var left = (screen.width/2)-(width/2);
var top = (screen.height/2)-(height/2);
return window.open(url, name, "menubar=no,toolbar=no,status=no,width="+width+",height="+height+",toolbar=no,left="+left+",top="+top);
}

$("a.popup").click(function(e) {
popupCenter($(this).attr("href"), $(this).attr("data-width"), $(this).attr("data-height"), "authPopup");
e.stopPropagation(); return false;
});

And then in your callback view:

:javascript
if(window.opener) {
window.opener.location.reload(true);
window.close()
}

This'll pop up your Facebook auth in a centered 600x400 popup, then when the user returns from authentication, the view will close the popup and refresh the parent page. It degrades gracefully if a user ctrl-clicks the link, or doesn't have Javascript enabled, too.

How to specify devise_parameter_sanitizer for edit action?

Once again, it was a matter of reading the manual ...

The magic word is :account_update and thus the working version becomes

def configure_permitted_parameters
devise_parameter_sanitizer.for(:sign_up) { |u| u.permit(:email, :password, :password_confirmation, :firstname, :middlename, :lastname, :nickname) }
devise_parameter_sanitizer.for(:account_update) { |u| u.permit(:email, :password, :password_confirmation, :current_password, :firstname, :middlename, :lastname, :nickname) }
end

Note that if you're in the business of signing in using non-standard parameters, the word you're looking for is :sign_in (as expected).



Related Topics



Leave a reply



Submit