How to use parameter value from gust_alivity (Rails)?

I am creating a Rails application. And there are projects in my app that users can "follow". When a user follows one of the pages, he / she will receive updates if someone downloads / creates a folder / file.

Below is a screenshot of when someone just created a new folder:

enter image description here

And below is the code for the "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 public_activity parameters when creating a new folder.

Below is a screenshot of what this parameter value looks like in the database after saving it:

enter image description here

Question:

So my question is, how do I use these parameter values ​​in my Activity_controller?

Here is the code for my activity 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 the parameter column. So how can I do this?

Thanks a lot in advanced! :)

+3


source to share


2 answers


Thanks for the answer, but I got a better solution than using parameter value or custom field. This is what my Activity_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

      

Using this method, I could easily combine the actions from "Users" as well as from "Projects" that follow it.



You can modify it to add any other actions like Comments or Vote.

Hope this helps other people who use the public_activity stone! :)

0


source


The parameters field contains a simple yaml dump, so not easy to search efficiently.

A simple solution would be to use an operator LIKE

like



PublicActivity::Activity.where("parameters LIKE '%project_id: #{@project.id}%'")

      

You might want to add custom fields instead .

0


source







All Articles