How to :conceal from Feed?

微笑、不失礼 提交于 2020-01-06 17:09:07

问题


There are many valuations, activities, and users. Each table has this line:

t.boolean  "conceal",        default: false

When submitting a valuation it can be made true:

pry(main)> Valuation.find(16)
  Valuation Load (0.1ms)  SELECT  "valuations".* FROM "valuations" WHERE "valuations"."id" = ? LIMIT 1  [["id", 16]]
=> #<Valuation:0x007fbbee41cf60
 id: 16,
 conceal: true,
 user_id: 1,
 created_at: Thu, 23 Apr 2015 20:24:09 UTC +00:00,
 updated_at: Thu, 23 Apr 2015 20:24:09 UTC +00:00,
 likes: nil,
 name: "CONCEAL NEW">

This prevents other user's from seeing this valuation's :name on his profile via @valuations = @user.valuations.publish in the users_controller & scope :publish, ->{ where(:conceal => false) } in valuations.rb.

How can we also conceal this valuation on the activities feed? Here is this same valuation found as an activity:

Activity.find(24)
  Activity Load (0.1ms)  SELECT  "activities".* FROM "activities" WHERE "activities"."id" = ? LIMIT 1  [["id", 24]]
=> #<Activity:0x007fbbebd26438
 id: 24,
 user_id: 1,
 action: "create",
 test: nil,
 trackable_id: 16,
 trackable_type: "Valuation",
 created_at: Thu, 23 Apr 2015 20:24:09 UTC +00:00,
 updated_at: Thu, 23 Apr 2015 20:24:09 UTC +00:00,
 conceal: false>

You see how it is false here? How can we make it true?

class Activity < ActiveRecord::Base
  belongs_to :user
  belongs_to :trackable, polymorphic: true
    scope :publish, ->{ where(:conceal => false) }
end


class ActivitiesController < ApplicationController
    def index
        @activities = Activity.publish.order("created_at desc").where(user_id: current_user.following_ids)
    end
end

回答1:


You don't actually need the boolean in your Activity model. Just create a getter method that gets the conceal value from the Valuation record.

class Activity < ActiveRecord::Base
  belongs_to :user
  belongs_to :trackable, polymorphic: true
    scope :publish, ->{ where(:conceal => false) }

  def conceal
    trackable.conceal
  end
end


来源:https://stackoverflow.com/questions/29834607/how-to-conceal-from-feed

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!