Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to block users from following other models? #43

Closed
RailsCod3rFuture opened this issue Dec 26, 2014 · 5 comments
Closed

How to block users from following other models? #43

RailsCod3rFuture opened this issue Dec 26, 2014 · 5 comments

Comments

@RailsCod3rFuture
Copy link

I have two models in my project. One for businesses and one for normal users. How would I be able to give businesses/users the ability to block each other?

@ghost
Copy link

ghost commented Dec 29, 2014

You'd implement a check on the follow method for both models that checks the ID of the follower (the person making the request) against a block list owned by the followee (the person the follower is requesting to follow).

The block list in this example is done with a database table, but you could use a text file, a YAML file, Redis, etc. depending on your requirements.

The following code is just a sample: you'll need to modify it accordingly.

# example follow method
def follow(resource)
 unless current_user.is_blocked?(resource)
  current_user.follow!(resource)
 else 
  puts "You can't follow this resource!"
 end
end

# this method goes in both models
def is_blocked?(resource)
 blocked = BlockList.where(blocker_id: resource.id, blocked_id: current_user.id).first
 if blocked then
  return true
 else
  return false
 end
end

@RailsCod3rFuture
Copy link
Author

I added the follow method that you gave me to the follow model, and I added the is_blocked method to the normal user and business model. I will probably render a view instead of showing the puts statement to provide better styling. On another note, can you try to create a sample application, I can't seem to find the one that you've posted before. I just want to understand the entire structure of the gem, so that I can maybe extend it later.

@ghost
Copy link

ghost commented Mar 1, 2015

Hi, my apologies for not seeing this sooner: I'm rarely on GitHub these days. The code I provided was just something I typed up (adapted from a client project); I don't have a sample application yet (but I could create one).

The best way to understand the structure of the gem is to read the source files for it.

@RailsCod3rFuture
Copy link
Author

You've done an awesome job with the gem. I just like to be 100% in the know with best practices for it. If you can build a bare-bones sample project. That would be nice. Thank you.

@ghost
Copy link

ghost commented Mar 3, 2015

I didn't create this gem :)

@cmer cmer closed this as completed Feb 18, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants