How to use private feed with action feeds?

How can we give the user the ability to make actions private? This will provide users with the confidentiality of messages that they want only for their eyes.

I was told that this code does not work, because it may have something to do with "do not set the 'private' flag for proper operation," however the private checkbox works to hide views in public profiles (only not in the action feed).

class ActivitiesController < ApplicationController def index #Added .public @activities = Activity.visible.order("created_at desc").where(user_id: current_user.following_ids) end end class Activity < ActiveRecord::Base belongs_to :user has_many :comments, as: :commentable belongs_to :trackable, polymorphic: true scope :visible, ->{ where(:hidden => false) } def visible? !hidden end end create_table "activities", force: true do |t| t.boolean "hidden", default: false t.integer "user_id" t.string "action" t.integer "trackable_id" t.string "trackable_type" t.datetime "created_at", null: false t.datetime "updated_at", null: false end 

And in one of the _forms, for example @valuations or @goals , the user can make a difference through his presentation:

  <%= button_tag(type: 'submit', class: "btn", id: "gold") do %> <span class="glyphicon glyphicon-plus"></span> Public <% end %> <%= button_tag(type: 'submit', class: "btn") do %> <% :hidden %><span class="glyphicon glyphicon-plus"></span> Private <% end %> 

Thanks!

+4
ruby ruby-on-rails model-view-controller feed
source share
1 answer

Instead, I would use the enum column. Enumerations give you a ton of features like areas, polls, and even hacking methods to change status. But most are listed for expansion β€” let's say you want to add functionality in which users can have messages that are only viewable by friends β€” adding an extra state to the listing is easy!

First we add the database column. Run:

 rails g migration AddVisiblityToActivities visibility:integer:index 

Then edit the transition to add the default value:

 class AddVisibilityToActivities < ActiveRecord::Migration def change t.integer :visibility, index: true, default: 0 end end 

Start the migration with rake db:migrate . Then we need to add the enumeration mappings to the Activity model:

 class Activity < ActiveRecord::Base belongs_to :user has_many :comments, as: :commentable belongs_to :trackable, polymorphic: true # change the order if you want to default to private! enum visibility: [:visible, :hidden] default_scope { visible.order('created_at DESC') } end 

Please note that we are also adding a default scope. With this, we really simplify the request in our controller:

 class ActivitiesController < ApplicationController def index #Added .public @activities = Activity.where(user: current_user.following) # note that you don't have to use ids when creating a # where clause from an association. Rails does the work for you end end 

The easiest way to allow users to change visibility when creating / updating records is to use select:

 <div class="field"> <%= f.label :visibility %> <%= f.select :visibility, Activity.visibilities.keys.map(&:titleize) %> </div> 

Just remember the visibility whitelist!

 # app/controllers/activities_controller.rb # ... def create @activity = Activity.new(activity_params) do |a| a.user = current_user end # ... end # ... def activity_params params.require(:activity).permit(:visibility) end 
0
source share

All Articles