How to Use Private Submit to Hide from Profile

How to use private submit to hide from profile?

Add a field 'private' to the User model with its default value 'false'. All normal user informations will be flagged as 'public' (because the private field has the value false) Only if params[:private], then the value of the private field will be set to 'true'.

Next you can add a method to the user model which will grab only the data of user with the private = false flag (for public views).

EDIT:

Displaying public or private:

Add a field 'private' to each of your related models which possibly could be marked as private. Don't forget to add this in your migrations. Set the private's default to false.

Include in valuation & user migration/schema

t.boolean :private, default: false

valuation.rb

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

user.rb

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

Do this in the same way for each of your wanted relations. It enables you to get the public informations via

@valuations = @user.public_valuations

Your current show action displays now all additional user's informations - public and private - which are should be only displayed if the current_user = @user.

At last you have to insert a condition in your show action:

def show
@user = User.find(params[:id])
if current_user == @user
@habits = @user.habits
@valuations = @user.valuations
@accomplished_goals = @user.goals.accomplished
@unaccomplished_goals = @user.goals.unaccomplished
@averaged_quantifieds = @user.quantifieds.averaged
@instance_quantifieds = @user.quantifieds.instance
else
@valuations = @user.public_valuations
end
end

That solution depends on current_user, i.e. you must have a method which returns the object of the currently logged_in user (maybe in a session). Michael Hartl wrote an awesome tutorial about user authentication. *RubyonRailsBeginner used Hartl Tutorial for this :)

Creating public or private records

Since you had set the private's default to false, you can use your existing code to create public entries.

For private entries you must set the corresponding attribute in your user_params to true.

EDIT with params.require:

I set the [:private] in the else clause explicit to false, so that a user might set his private attributes to public, if wanted.

def user_params
if params[:private] = true
params.require(:user).permit(:name, :email, :password, :private, :password_confirmation, valuations_attributes: [:name, :tag_list, :private])
else
params[:user][:valuations][:private] = false
params.require(:user).permit(:name, :email, :password, :password_confirmation, valuations_attributes: [:name, :tag_list])
end
end

The Rails Api gives you some hints about strong parameters with nested attributes.

Hope that helps!

How to Hide Private Show Pages from Other Users?

Is private_submit a boolean field?

If so, here's a quick way to make the show page private if the private_submit field has a value of "true".

class GoalsController < ApplicationController

# Remove :edit, :update, destroy, and :user_gmails from below as the action is duplicated
before_action :set_goal, only: [:show, :like]

def show
## Remove: @goal = Goal.find(params[:id])
end

def like
# Remove this as it's being called ready in set_goal:
# @goal = Goal.find(params[:id])
...
end

...

def set_goal
@goal = Goal.find(params[:id])
redirect_to(:back) unless @goal.user_id == current_user.id or @goal.private_submit == false
end

end

How to use private submit with activities feed?

I would use an enum column instead. Enums give you tons of functionality such as scopes, interrogation and even bang methods to change the status. But most of all enums are built to extend - lets say you want to add the functionality that users can have posts that are only viewable by friends - adding an additional state to an enum is easy!

First we add a database column. Run:

rails g migration AddVisiblityToActivities visibility:integer:index

Then edit the migration to add a default:

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

Run the migration with rake db:migrate. Then we need to add the enum 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

Note that we also add a default scope. With that we can really simplify the query 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 let users alter the visibility when creating/updating records is to use a select:

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

Just remember to whitelist the visibility property!

# 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

Is it possible to hide certain activity from my GitHub profile?

2018: You would need to change the author information of your local commits (using another account name), and then push --force back to the origin/upstream repo.

That push --force is not always possible, and you will have to contact the owner of the organization for them to replace the history of one branch by another, and make sure everybody reset his/her own local repo accordingly.

But the point remains: you cannot hide "some" of your activities.


Apr. 2022: unless... you:

  • use a different account for that organization,
  • set that second account as private, which will hide any activity.

How to make private activities?

class User < ActiveRecord::Base
has_many :activities
def public_activities
activities.find(&:public?)
end
end

This has defined a new instance method called public_activities - you will only be able to use it on an instance of a user

class ActivitiesController < ApplicationController
def index #Added .public_activities
@activities = Activity.public_activities.order("created_at desc").where(current_user.following_ids)
end
end

Here you are trying to call a class method on the Activity class instead.

If you want to do the above, then you'll need to create a scope on the Activity class.

in which case, it's better not to repeat the "activities" part in the name, but just call it "public"

eg

class Activity < ActiveRecord::Base
belongs_to :user
has_many :comments, as: :commentable
belongs_to :trackable, polymorphic: true

scope :public, ->{ where(:private => false) }

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

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

How to hide /profile Endpoint in Spring Boot

This looks like Hateoas response.

If you have any dependency for spring-boot-starter-hateoas or spring-hateoas, please remove them. This shall turn it off

If you are using spring-data-rest, then you may need to set spring boot as following:
@SpringBootApplication(exclude = RepositoryRestMvcAutoConfiguration.class)



Related Topics



Leave a reply



Submit