Created
October 10, 2016 13:36
-
-
Save zmalltalker/81fbf5a2504ae061a3dd6dd11fa02d1d to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Controller | |
def self.create_friend_request(user, params) | |
obj = Model.new(params) | |
if obj.valid? | |
render json: obj, status: :created | |
else | |
render json: obj.errors, status: 400 | |
end | |
unless Rails.env.development? || Rails.env.test? | |
username = user.name | |
friend = User.find params[:reciever_id] | |
RpushService.send_notifications(friend, "New friend request from #{username}", | |
{notification_type: 1}) | |
end | |
return {json: "", status: 201} | |
end | |
end | |
class Model | |
validate :cannot_friend_self, on: :create | |
validate :should_be_unique, on: :create | |
private | |
def cannot_friend_self | |
count = self.class.count("sender_id=? and receiver_id=?", user.id, friend.id) | |
errors.add(:user_id, "Cannot friend self") if count > 0 | |
end | |
def should_be_unique | |
count = self.class.count("sender_id=? and receiver_id=?", user.id, friend.id) | |
errors.add(:some_other_id, "Users already friends") if count > 0 | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment