Skip to content

Commit

Permalink
Add referrals page if the refer gem is enabled (#1162)
Browse files Browse the repository at this point in the history
* Add referrals page if the refer gem is enabled

* Use refer 0.5+

* Standardize

* Disable autocomplete on referral url input
  • Loading branch information
excid3 authored Jul 30, 2024
1 parent 3a12dbb commit 64c2f94
Show file tree
Hide file tree
Showing 8 changed files with 78 additions and 2 deletions.
2 changes: 1 addition & 1 deletion Gemfile.jumpstart
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ end
gem "acts_as_tenant", "~> 1.0" if Jumpstart.config.gems.include?("acts_as_tenant")
gem "oj", "~> 3.8" if Jumpstart.config.gems.include?("oj")
gem "rack-attack", "~> 6.6" if Jumpstart.config.gems.include?("rack-attack")
gem "refer", "~> 0.2.0" if Jumpstart.config.gems.include?("refer")
gem "refer", "~> 0.5.0" if Jumpstart.config.gems.include?("refer")
gem "whenever", "~> 1.0", require: false if Jumpstart.config.gems.include?("whenever")

omniauth_providers = Jumpstart.config.omniauth_providers
Expand Down
7 changes: 7 additions & 0 deletions app/controllers/users/referrals_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
class Users::ReferralsController < ApplicationController
before_action :authenticate_user!

def index
@referral_code = current_user.referral_codes.first_or_create
end
end
4 changes: 3 additions & 1 deletion app/controllers/users/registrations_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@ def update_resource(resource, params)
end

def sign_up(resource_name, resource)
sign_in(resource_name, resource)
super

refer(resource) if defined? Refer

# If user registered through an invitation, automatically accept it after signing in
if @account_invitation
Expand Down
3 changes: 3 additions & 0 deletions app/views/application/_account_navbar.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,7 @@
<% end %>
<%= nav_link_to t(".accounts"), accounts_path, class: "block no-underline p-3 px-6", active_class: active_class, inactive_class: inactive_class, starts_with: "/accounts" %>
<%= nav_link_to t(".api_tokens"), api_tokens_path, class: "block no-underline p-3 px-6 rounded-b", active_class: active_class, inactive_class: inactive_class, starts_with: "/api_tokens" %>
<% if defined? Refer %>
<%= nav_link_to t(".referrals"), referrals_path, class: "block no-underline p-3 px-6", active_class: active_class, inactive_class: inactive_class, starts_with: "/referrals" %>
<% end %>
</div>
44 changes: 44 additions & 0 deletions app/views/users/referrals/index.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<div class="flex flex-wrap my-4 lg:px-4">
<div class="w-full mb-4 lg:w-1/4 lg:p-4">
<%= render partial: "account_navbar" %>
</div>

<div class="w-full lg:w-3/4 lg:p-4">
<div class="p-4 bg-white border border-gray-200 dark:bg-gray-900 dark:border-gray-800 rounded-lg lg:p-10">

<div class="sm:flex items-center justify-between mb-3">
<h1 class="flex-1 h3"><%= t(".title") %></h1>
</div>

<p><%= t(".instructions") %></p>
<div class="mt-2 flex items-center gap-1">
<%= text_field_tag nil, root_url(Refer.param_name => @referral_code.code), readonly: true, class: "form-control w-96", autocomplete: :off %>
<%= button_tag "Copy", class: "btn btn-secondary", data: {controller: :clipboard, clipboard_text: root_url(Refer.param_name => @referral_code.code) } %>
</div>

<div class="mt-8 lg:max-w-4xl">
<h4><%= t(".stats") %></h4>

<table class="w-full">
<tbody class="divide-y divide-gray-100">
<tr>
<td class="font-bold"><%= t(".clicks") %></td>
<td><%= t(".clicks_description") %></td>
<td class="py-2 text-right tabular-nums"><%= @referral_code.visits_count %></td>
</tr>
<tr>
<td class="font-bold"><%= t(".referrals") %></td>
<td><%= t(".referrals_description") %></td>
<td class="py-2 text-right tabular-nums"><%= @referral_code.referrals.count %></td>
</tr>
<tr>
<td class="font-bold"><%= t(".completed") %></td>
<td><%= t(".completed_description") %></td>
<td class="py-2 text-right tabular-nums"><%= @referral_code.referrals.completed.count %></td>
</tr>
</tbody>
</table>
</div>
</div>
</div>
</div>
6 changes: 6 additions & 0 deletions config/initializers/pay.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,12 @@ module ChargeExtensions

included do
has_prefix_id :ch
after_create :complete_referral, if: -> { defined?(Refer) }
end

# Mark the account owner's referral complete on the first successful payment
def complete_referral
customer.owner.owner.referral&.complete!
end
end

Expand Down
12 changes: 12 additions & 0 deletions config/locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,7 @@ en:
billing: "Billing"
accounts: "Accounts"
api_tokens: "API"
referrals: "Referrals"
flash:
confirm_payment: "Confirm your latest payment"
navbar:
Expand Down Expand Up @@ -493,6 +494,17 @@ en:
verify_description: "After scanning the QR code, the app will display a code that you can enter to continue."
cancel: "Cancel"
verify: "Verify"
referrals:
index:
title: "Referrals"
instructions: "Copy your unique link and share it with your friends and followers."
stats: "Stats"
clicks: "Clicks"
clicks_description: "Number of times your link has been clicked."
referrals: "Referrals"
referrals_description: "People who have signed up using your link."
completed: "Completed"
completed_description: "Users that have completed their first purchase."

layouts:
mailer:
Expand Down
2 changes: 2 additions & 0 deletions config/routes/users.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,6 @@
end
end

resources :referrals, module: :users if defined? Refer

post :sudo, to: "users/sudo#create"

0 comments on commit 64c2f94

Please sign in to comment.