excid3 / refer

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

Do not run custom generator if referral code already set #20

Closed cnorm35 closed 2 months ago

cnorm35 commented 2 months ago

Pull Request

Summary: Fixes issue with custom code being overridden if Refer.code_generator is set

Related Issue:

Description: Currently unable to create a new Refer::ReferralCode with a custom code value.

Testing:

Prior to these changes, passing in a referral code results in the custom code being overridden

jumpstart-app(dev)> something = user.referral_codes.create(code: "special")
  TRANSACTION (0.4ms)  BEGIN
  Refer::ReferralCode Exists? (2.0ms)  SELECT 1 AS one FROM "refer_referral_codes" WHERE "refer_referral_codes"."code" = $1 LIMIT $2  [["code", "1U8JP9wW"], ["LIMIT", 1]]
  Refer::ReferralCode Create (0.7ms)  INSERT INTO "refer_referral_codes" ("referrer_type", "referrer_id", "code", "created_at", "updated_at", "referrals_count", "visits_count") VALUES ($1, $2, $3, $4, $5, $6, $7) RETURNING "id"  [["referrer_type", "User"], ["referrer_id", 1], ["code", "1U8JP9wW"], ["created_at", "2024-09-05 15:23:14.786977"], ["updated_at", "2024-09-05 15:23:14.786977"], ["referrals_count", 0], ["visits_count", 0]]
  TRANSACTION (1.0ms)  COMMIT
=>
#<Refer::ReferralCode:0x000000012a8b9248
 id: 5,
 referrer_type: "User",
 referrer_id: 1,
 code: "1U8JP9wW",
 created_at: "2024-09-05 15:23:14.786977000 +0000",
 updated_at: "2024-09-05 15:23:14.786977000 +0000",
 referrals_count: 0,
 visits_count: 0>

Screenshots (if applicable):

Checklist:

Additional Notes: