# LoyaltyRef ๐
A flexible Ruby gem for building loyalty and referral systems in Rails apps.
- ๐ Reward users with points based on their activity
- ๐งฑ Create dynamic tier levels (Silver, Gold, Platinum)
- ๐ค Add referral rewards with unique referral codes
- ๐ง Fully customizable and configurable
- ๐๏ธ Admin dashboard (ERB based)
- ๐ Supports any user model:
User,Customer,Account, etc.
๐ Documentation
- Quick Start Guide - Get running in 5 minutes
- Complete Documentation - Full developer guide with examples
- API Reference - All methods and options
๐ Installation
# Gemfile
gem 'loyalty_ref'
$ bundle install
$ rails generate loyalty_ref:install
$ rails db:migrate
๐ ๏ธ Configuration
Create your configuration block in an initializer:
# config/initializers/loyalty_ref.rb
LoyaltyRef.configure do |config|
# Define which model represents your app's user/customer
config.reference_class_name = 'User' # or 'Customer', 'Account', etc.
config.earning_rule = ->(user, event) do
# Example: earn 10 points per โน100 spent
event.amount / 10
end
config.redeem_rule = ->(user, offer) do
offer.cost_in_points
end
config.tier_thresholds = {
"Silver" => 500,
"Gold" => 1000,
"Platinum" => 2500
}
config.reward_modifier = ->(user) do
case user.tier
when "Silver" then 1.0
when "Gold" then 1.2
when "Platinum" then 1.5
else 1.0
end
end
config.referral_reward = ->(referrer, referee) do
LoyaltyRef.earn_points(referrer, 100)
LoyaltyRef.earn_points(referee, 50)
end
end
โ Features
๐ Loyalty Program
| Feature | Description |
|---|---|
| Points system | Earn points via config lambda |
| Custom earning rules | Define rules per event/user |
| Redeem points | Redeem points for offers |
| Manual adjustment | Admins can modify balances |
| Points expiration | e.g. 90 days |
| Transaction logging | All activity is logged |
| Check balance | Easy method to check |
๐งฑ Tier System (Dynamic)
| Feature | Description |
|---|---|
| Dynamic definitions | e.g. Silver => 500 points |
| Auto tier assignment | Based on balance |
| Callbacks on promotion | on_tier_changed hook |
| Reward modifier by tier | e.g. Gold users get +20% |
| DB persistence | Can store or calculate tier |
๐ค Referral System
| Feature | Description |
|---|---|
| Unique referral codes | Auto-generated or custom |
| ?ref=code tracking | Via signup links |
| Multi-level referrals | Parent/child tree |
| Referral rewards | Custom logic supported |
| Referral analytics | Track clicks, accepted signups, geo-location, and device type |
โ๏ธ Core Gem Features
- Developer config block
- Extensible models
- Simple public API:
earn_points,redeem_points,balance,track_referral - Rails generators for setup
- Support for any user model (User, Account, Customer, etc.)
๐ฅ๏ธ Admin UI
- Mountable engine with ERB templates
- Routes like
/loyalty - Views to list users, transactions, referrals
๐ฎ Future Scope
๐ Analytics & Admin
- Leaderboards by points
- Referral tree visualizer
- ActiveAdmin / custom dashboard
- Export CSV/JSON of logs
๐ Engagement
- Email / in-app notifications
- Badges based on milestones
- Activity calendar
- Social sharing for referral links
๐ Folder Structure (Gem)
lib/
โโโ loyalty_ref.rb
โโโ loyalty_ref/
โ โโโ engine.rb
โ โโโ configuration.rb
โ โโโ version.rb
โ โโโ models/
โ โ โโโ transaction.rb
โ โ โโโ referral_log.rb
โ โโโ controllers/
โ โโโ dashboard_controller.rb
app/views/loyalty_ref/dashboard/
โโโ index.html.erb
โโโ show.html.erb
๐งช Usage Examples
# Earn points
LoyaltyRef.earn_points(current_user, event: order)
# Redeem points
LoyaltyRef.redeem_points(current_user, offer: coupon)
# Check balance
LoyaltyRef.balance(current_user)
# Track referral
LoyaltyRef.track_referral(ref_code: params[:ref])
๐ฌ Contribution
PRs are welcome ๐ โ help improve the gem or suggest features.
๐ License
MIT ยฉ 2025 Kapil Pal