excid3 / refer

Referral codes for Ruby on Rails applications
MIT License
219 stars 9 forks source link

Make set_refer_cookie public and more flexible #24

Closed marckohlbrugge closed 4 days ago

marckohlbrugge commented 1 month ago

Pull Request

Summary: I made set_refer_cookie a public method, allowed passing in a code, and overriding track_visit.

Description: The reason for these changes are to provide more flexibility on when and how refer cookies are set.

For example, let's say you have an app where users can create public listings. When someone visits a user's listing, you want to set a cookie to "reward" the user if someone new signs up through their public listing.

With the existing Refer implementation, this requires ensuring that the public listings always have a ?ref=code parameter set. But with a more flexible set_refer_cookie method we can do something like this:

class ListingsController < ApplicationController
  def show
    @listing = Listing.find(params[:id])

    # Set cookie
    code = @listing.user.referral_codes.first.code
    set_refer_cookie(code: code, track_visit: false)
  end
end

Testing: I haven't tested it yet 😅

Checklist:

Additional Notes:

If you approve of this change, I can add tests.

excid3 commented 1 month ago

Thanks Marc, makes sense to me and happy to include this. 👍