ruby-on-railsrubydeviserubygemsdevise-invitable

Using devise_invitable, upon acceptance, saving mutual friendship


I want the inviting user and the invited user to become mutual friends upon acceptance of the email invitation via devise_invitable (gem).

So the database should be like this:

 id | user_id | friend_id |         created_at         |         updated_at         
----+---------+-----------+----------------------------+----------------------------
  2 |       3 |         2 | 2020-10-13 02:42:03.33376  | 2020-10-13 02:42:03.33376
  3 |       2 |         3 | 2020-10-13 15:57:48.359378 | 2020-10-13 15:57:48.359378

Here's what I have in the invitations controller that inherits from the source. The code in the middle beginning from @inviting_user = User.find(current_user.invited_by_id) is what I inserted.

class Users::InvitationsController < Devise::InvitationsController
  

prepend_before_action :require_no_authentication, only: [:edit, :destroy]


    def update
      raw_invitation_token = update_resource_params[:invitation_token]
      self.resource = accept_resource
      invitation_accepted = resource.errors.empty?
  
      yield resource if block_given?
  
      if invitation_accepted
        if resource.class.allow_insecure_sign_in_after_accept
          flash_message = resource.active_for_authentication? ? :updated : :updated_not_active
          set_flash_message :notice, flash_message if is_flashing_format?
          resource.after_database_authentication
          sign_in(resource_name, resource)


          ### from here is my code:
          @inviting_user = User.find(current_user.invited_by_id)
          @invited_user = User.find(current_user.id)

          @friendship = @invited_user.friendships.build(:friend_id => params[:friend_id])
          @inverse_friendship = @inviting_user.friendships.build(:friend_id => params[:friend_id])

          if @friendship.save and @inverse_friendship.save
            # flash[:notice] = "Added friend."
            # redirect_back fallback_location: new_user_invitation_path
          else
            # flash[:error] = "Unable to add friend."
            # redirect_back fallback_location: new_user_invitation_path
          end
          ### my code end

          respond_with resource, location: after_accept_path_for(resource)

        else
          set_flash_message :notice, :updated_not_active if is_flashing_format?
          respond_with resource, location: new_session_path(resource_name)
        end
      else
        resource.invitation_token = raw_invitation_token
        respond_with_navigational(resource) { render :edit }
      end
    end

end

The problem is that the friendships are not created. I'd appreciate any advice.

The source code controller: https://github.com/scambra/devise_invitable/blob/master/app/controllers/devise/invitations_controller.rb

I'm starting to think these two lines are the problem, because @inviting_user and @invited_user seem to be set. Do you see any problems here, within the context of devise_invitable?

@friendship = @invited_user.friendships.build(:friend_id => params[:friend_id])
@inverse_friendship = @inviting_user.friendships.build(:friend_id => params[:friend_id])

When I switch @friendship.save to @friendship.create! I get "did you mean create_user?"


Solution

  • You really don't need even a fraction of this code. Almost all the Devise controllers yield so you can simply tap into the super method by passing a block:

    class Users::InvitationsController < Devise::InvitationsController
      def update
        super do |user|
          Friendship.transaction do 
            begin
              user.friendships.create!(friend_id: user.invited_by_id)
              User.find(user.invited_by_id)
                  .friendships.create!(friend_id: user.id)
            rescue ActiveRecord::RecordInvalid
              flash[:error] = "Unable to add friend."
              redirect_back fallback_location: new_user_invitation_path
              raise ActiveRecord::Rollback
            end
          end
        end
      end
    end