How to use private submit to hide from feed?

2019-04-25 22:42发布

问题:

In the valuations form there is a submit button and a <%= f.submit :private %> button. If private submit is clicked the submitted info will be hidden to other user's who view the profile.

How can we also use <%= f.submit :private %> to hide submitted info from showing on the feed?

activities/index.html.erb

<h1>Feed</h1>
<% @activities.each do |activity| %>
<% if current_user == @user %>
    <%= render_activity activity %>
  <% else %>
    <%= render_activity activity %> #We'd need to make .public_valuations work with this without getting an undefined method error.
  <% end %>
<% end %>

activities_controller.rb

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

For brevity I'll only include _create (there is also update and destroy). Every time a user submits a valuation it pops up on the feed, how can we make only public_valuations show?

public_activity/valuation/_create.html.erb

<% if activity.trackable %>
  <%= link_to activity.trackable.name, activity.trackable %></b>
<% else %>
  which has since been removed 
<% end %>

valuation.rb

class Valuation < ActiveRecord::Base
  belongs_to :user
  acts_as_taggable
  validates :name, presence: true
  has_many :comments, as: :commentable
  include PublicActivity::Model
  tracked owner: ->(controller, model) { controller && controller.current_user }

    def public?
      private == true ? false : true
    end

  scope :randomize, -> do
      order('RANDOM()').
      take(1)
    end
end

users_controller

 def show
   if
     @valuations = @user.valuations
   else
     @valuations = @user.public_valuations
   end
 end

user.rb

#gets public valutations or nil, if there's no public valutation
def public_valuations
  valuations.find(&:public?)
end

I gained almost all of my activities code form this railscasts episode: http://railscasts.com/episodes/406-public-activity.

This is how we made the private submit work for the profile: How to use private submit to hide from profile?

Thank you so much for your time!

UPDATE

Since we couldn't resolve the issue via the public_activity gem I created the public activity from scratch and am attempting to resolve this issue here: How to make private activities?

回答1:

FAILED ATTEMPT

at Avdept's suggestions:

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

In valuations.rb

def public? !private end => def public?; !private; end;
scope :not_private, -> { where(private: false) }

This gives:

SyntaxError in ActivitiesController#index (for line: def public? !private end => def public?; !private; end;)
/Users/galli01anthony/Desktop/Pecoce/app/models/valuation.rb:9: syntax error, unexpected '!', expecting ';' or '\n' def public? !private end => def public?; !private; end; ^ /Users/galli01anthony/Desktop/Pecoce/app/models/valuation.rb:9: syntax error, unexpected =>, expecting end-of-input def public? !private end => def public?; !private; end; ^