7

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?

Community
  • 1
  • 1
  • 2
    I think there's better way to make something private: For ex. - add new boolean field :private. On your form add checkbox for this boolean. And than, in view, do a check ````<% if activity.private %> do something.. <% else %> do something else... <% end %>```` – Avdept Apr 08 '15 at 19:31
  • @Avdept thanks for the suggestion! I have `t.boolean :private default false` in my schema for valuations. This is represented in the valuations _form. In the view do `if else` sounds good. Should I do it in `activities/index.html.erb` or maybe it's better in the controller? Is there any more code or info I can give you to make your comment into an answer? –  Apr 08 '15 at 23:58
  • 1
    your if - else clause you have to put into view, in the moment you're rendering your entity. If i understood correctly - you dont want to show something if this 'something' has flagged as private. Am i right? – Avdept Apr 09 '15 at 07:32
  • 1
    Thanks @Avdept for helping to move the ball. I updated the question. How can we make .public_valuations work with `<%= render_activity activity %>`? I'm guessing we'd have to do something with the controller now. –  Apr 09 '15 at 17:26
  • are you going to render something for those that are private? – Avdept Apr 09 '15 at 20:58
  • I'm only going to render `public_valuations` @Avdept. I don't want the private stuff to show to other users. There are other attributes such as `public_habits`, `public_goals` but for this question I figured we'd keep it simple and focus on just making `public_valuations` work and hiding regular/private valuations. –  Apr 09 '15 at 23:34
  • There are few ways of how you can do this. First is - add named scope into your model like `scope :public, -> { where(private: false) } ` and than use this method on your collection, and it will look like `@activities = PublicActivity::Activity.public.order("created_at desc").where(owner_id: current_user.following_ids, owner_type: "User")` – Avdept Apr 10 '15 at 06:39
  • Where am I adding `:public` @Avdept I don't see it in your code snippet? And I already have `def public? private == true ? false : true end` in valuation.rb. Am I suppose to take that out to avoid the error I'm getting: `You tried to define a scope named "public" on the model "Valuation", but Active Record already defined a class method with the same name.` –  Apr 10 '15 at 16:41
  • @Avdept I got that code from his answer: http://stackoverflow.com/questions/29323201/how-to-use-private-submit-to-hide-from-profile so I think I need that snippet for the purposes of that question unless I'm mistaken. –  Apr 10 '15 at 16:44
  • You dont need public methods. Since you have private - public is opposite of private – Avdept Apr 11 '15 at 17:09
  • @Avdept I added public to: `@activities = PublicActivity::Activity.public.order` and in the valuations model I tried `def public? private == true ? false : true end` but that gave me: `NoMethodError in ActivitiesController#index private method 'public' called for...` I tried `def public? !private end` but that gave me: `SyntaxError in ActivitiesController#index /models/valuation.rb:9: syntax error, unexpected '!', expecting ';' or '\n' def public? !private end ^ /models/valuation.rb:15: syntax error, unexpected keyword_end, expecting end-of-input` –  Apr 13 '15 at 20:00
  • And like I said @Avdept I tried `scope :public, -> { where(private: false) }` but that gave me: `You tried to define a scope named "public" on the model "Valuation", but Active Record already defined a class method with the same name.` Thanks for helping me this far! Do you mind providing an answer so I can award you the bounty? –  Apr 13 '15 at 20:01
  • Try to use different scope name, like :public_records, or something similar – Avdept Apr 14 '15 at 10:14
  • I updated the question and offered an answer based upon your suggestions. Hopefully this will provide clarity for what I'm doing wrong @Avdept. Thanks again! –  Apr 14 '15 at 20:15

1 Answers1

2

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; ^
  • `def public? !private end => def public?; !private; end;` Change this way. For scope - change name from :public to something like :not_private, since public is keyword. ALso scope should be inside model, not controller – Avdept Apr 15 '15 at 11:07