ruby-on-railsrubygemspublic-activity

How to use parameters value from public_activity gem (Rails)?


I am building a Rails app. And in my app, there are Projects where users can "Follow". When a user follows one of the pages, he/she will get updates if somebody uploads/creates a folder/file.

Below is the screenshot when somebody just created a new folder:

enter image description here

And below is the code for "Create" action in my Folder controller:

 def create
 @folder = current_user.folders.where(project_id: params[:project_id]).create(folder_params)

 respond_to do |format|
  if @folder.save
    @folder.create_activity :create, owner: current_user, :params => {
       :project_id => proc {|controller, project| @folder.project.id},
       :project_name => proc {|controller, project| @folder.project.name},
      }

    format.html { redirect_to @folder.project, notice: 'Folder was successfully created.' }
    format.json { render :show, status: :ok, location: @folder }
  else
    format.html { render :new }
    format.json { render json: @folder.errors, status: :unprocessable_entity }
  end
end

end

As you can see :project_id and :project_name are the parameters for the public_activity when a new folder being created.

And below is the screenshot on how this parameters value looks like in the database after they were saved:

enter image description here

QUESTION:

So my question is, how do i use this parameters values in my activities_controller?

Here is the code for my activities controller right now:

class ActivitiesController < ApplicationController
  def index
    @activities = PublicActivity::Activity.order("created_at desc").where(owner_id: current_user.following_users, owner_type: "User")
  end
end

Instead of using "owner_id:", I want to use the "project_id" value from parameters column. So how can i do this?

Thank you very much in advanced! :)


Solution

  • Thanks for the answer, but I got a better solution than using the parameters value or custom field. Here is how my activities_controller looks like right now:

    class ActivitiesController < ApplicationController
    
      def index
        activity_table = PublicActivity::Activity.arel_table
    
        # We want to view all activity of folders related to projects we are follwing
        folder_ids = Folder.where(project_id: current_user.following_projects.pluck(:id)).pluck(:id)
    
        # Generate query for all activity related to folders we care about
        folders_query = activity_table[:trackable_type].eq('Folder').and(
             activity_table[:trackable_id].in(folder_ids)
        )
    
        # Generate query for all users that we follow
        users_query = activity_table[:owner_id].in(current_user.following_users.pluck(:id))
    
        activity_query = folders_query.or(users_query)
        @activities = PublicActivity::Activity.where(activity_query)
      end
    end
    

    By using this way, I could easily combine the activities from the "Users" and also from the "Projects" that the user follows.

    You can modify it to add any other activities such as from the "Comments" or "Voting".

    Hope this will help other people out there that are using public_activity gem! :)