spree_reffiliate 2.3.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (55) hide show
  1. checksums.yaml +7 -0
  2. data/ .travis.yml +3 -0
  3. data/.gitignore +14 -0
  4. data/.rspec +2 -0
  5. data/Gemfile +9 -0
  6. data/LICENSE +26 -0
  7. data/README.md +121 -0
  8. data/Rakefile +21 -0
  9. data/app/controllers/spree/admin/affiliates_controller.rb +23 -0
  10. data/app/controllers/spree/reffiliate_controller.rb +22 -0
  11. data/app/controllers/spree/user_controller_decorator.rb +6 -0
  12. data/app/models/spree/affiliate.rb +39 -0
  13. data/app/models/spree/promotion/rules/affiliated_promotion_rule.rb +37 -0
  14. data/app/models/spree/promotion/rules/referred_promotion_rule.rb +15 -0
  15. data/app/models/spree/referral.rb +40 -0
  16. data/app/models/spree/referred_record.rb +9 -0
  17. data/app/models/spree/user_decorator.rb +41 -0
  18. data/app/overrides/spree/admin/shared/_configuration_menu.rb +6 -0
  19. data/app/overrides/spree/admin/users/edit.rb +50 -0
  20. data/app/overrides/spree/admin/users/index.rb +18 -0
  21. data/app/overrides/spree/users/show.rb +12 -0
  22. data/app/views/spree/admin/affiliates/_form.html.erb +45 -0
  23. data/app/views/spree/admin/affiliates/edit.html.erb +76 -0
  24. data/app/views/spree/admin/affiliates/index.html.erb +55 -0
  25. data/app/views/spree/admin/affiliates/new.html.erb +23 -0
  26. data/app/views/spree/admin/promotions/rules/_affiliated_promotion_rule.erb +6 -0
  27. data/app/views/spree/admin/promotions/rules/_referred_promotion_rule.html.erb +0 -0
  28. data/bin/rails +7 -0
  29. data/config/initializers/spree.rb +6 -0
  30. data/config/locales/en.yml +10 -0
  31. data/config/routes.rb +8 -0
  32. data/db/migrate/20140907041850_create_spree_referrals.rb +8 -0
  33. data/db/migrate/20140907042024_create_spree_affiliates.rb +9 -0
  34. data/db/migrate/20140907042102_create_spree_referred_records.rb +9 -0
  35. data/db/migrate/20140907062121_add_index_to_spree_referrals.rb +5 -0
  36. data/db/migrate/20140907062422_add_index_to_spree_referred_record.rb +5 -0
  37. data/db/migrate/20140912003727_create_spree_promotion_rules_affiliates.rb +10 -0
  38. data/db/migrate/20140913042034_add_layout_to_spree_affiliate.rb +5 -0
  39. data/lib/generators/spree_reffiliate/install/install_generator.rb +21 -0
  40. data/lib/spree_reffiliate/engine.rb +20 -0
  41. data/lib/spree_reffiliate/factories.rb +12 -0
  42. data/lib/spree_reffiliate.rb +2 -0
  43. data/lib/tasks/spree_reffiliate.rake +10 -0
  44. data/spec/assets/spree/affiliates/corona.html.erb +1 -0
  45. data/spec/controllers/spree/reffiliate_controller_spec.rb +56 -0
  46. data/spec/controllers/spree/user_controller_spec.rb +20 -0
  47. data/spec/models/spree/affiliate_spec.rb +33 -0
  48. data/spec/models/spree/promotion/rules/affiliated_promotion_rule_spec.rb +46 -0
  49. data/spec/models/spree/promotion/rules/referred_promotion_rule_spec.rb +20 -0
  50. data/spec/models/spree/referral_spec.rb +41 -0
  51. data/spec/models/spree/referred_record_spec.rb +29 -0
  52. data/spec/models/spree/user_spec.rb +52 -0
  53. data/spec/spec_helper.rb +93 -0
  54. data/spree_reffiliate.gemspec +31 -0
  55. metadata +261 -0
@@ -0,0 +1,55 @@
1
+ <% content_for :page_title do %>
2
+ <%= Spree.t(:listing_affiliates) %>
3
+ <% end %>
4
+
5
+ <% content_for :page_actions do %>
6
+ <li>
7
+ <%= button_link_to Spree.t(:new_affiliate), new_admin_affiliate_url, :icon => 'plus', :id => 'admin_new_affiliate_link' %>
8
+ </li>
9
+ <% end %>
10
+ <% if @affiliates.any? %>
11
+ <table class="index" id="listing_affiliates" data-hook>
12
+ <colgroup>
13
+ <col style="width: 15%">
14
+ <col style="width: 20%">
15
+ <col style="width: 15%">
16
+ <col style="width: 15%">
17
+ <col style="width: 15%">
18
+ <col style="width: 20%">
19
+ </colgroup>
20
+ <thead>
21
+ <tr data-hook="admin_affiliates_index_headers">
22
+ <th><%= Spree.t('affiliate.name') %></th>
23
+ <th><%= Spree.t('affiliate.path') %></th>
24
+ <th><%= Spree.t('affiliate.count') %></th>
25
+ <th><%= Spree.t('affiliate.status') %></th>
26
+ <th data-hook="admin_affiliates_index_header_actions" class="actions"></th>
27
+ </tr>
28
+ </thead>
29
+ <tbody>
30
+ <% @affiliates.each do |affiliate|%>
31
+ <tr id="<%= spree_dom_id affiliate %>" data-hook="admin_affiliates_index_rows" class="<%= cycle('odd', 'even')%>">
32
+ <td class='affiliate_name'><%=link_to affiliate.name, edit_admin_affiliate_url(affiliate) %></td>
33
+ <td class='affiliate_path align-center'><%= affiliate_url(affiliate.path) %></td>
34
+ <td class='affiliate_count align-center'><%= affiliate.referred_count %></td>
35
+ <% if affiliate.partial.blank? or !affiliate_partial_exists?(affiliate.partial) %>
36
+ <td class='affiliate_status align-center' style="color: red">Not found</td>
37
+ <% elsif affiliate_partial_exists?(affiliate.partial) %>
38
+ <td class='affiliate_status align-center' style="color: green">Found</td>
39
+ <% end %>
40
+ <td data-hook="admin_affiliates_index_row_actions" class="actions">
41
+ <%= link_to_edit affiliate, :no_text => true %>
42
+ <%= link_to_delete affiliate, :no_text => true %>
43
+ </td>
44
+ </tr>
45
+ <% end %>
46
+ </tbody>
47
+ </table>
48
+
49
+ <%= paginate @affiliates %>
50
+ <% else %>
51
+ <div class="alpha twelve columns no-objects-found">
52
+ <%= Spree.t(:no_resource_found, resource: "affiliates") %>,
53
+ <%= link_to Spree.t(:add_one), spree.new_admin_affiliate_path %>!
54
+ </div>
55
+ <% end %>
@@ -0,0 +1,23 @@
1
+ <% content_for :page_title do %>
2
+ <%= Spree.t(:new_affiliate) %>
3
+ <% end %>
4
+
5
+ <% content_for :page_actions do %>
6
+ <li>
7
+ <%= button_link_to Spree.t(:back_to_affiliates_list), spree.admin_affiliates_path, :icon => 'arrow-left' %>
8
+ </li>
9
+ <% end %>
10
+
11
+ <div data-hook="admin_affiliate_new_form_header">
12
+ <%= render :partial => 'spree/shared/error_messages', :locals => { :target => @affiliate } %>
13
+ </div>
14
+
15
+ <div data-hook="admin_affiliate_new_form">
16
+ <%= form_for [:admin, @affiliate], url: admin_affiliates_url, method: :post do |f| %>
17
+ <%= render :partial => 'form', :locals => { :f => f } %>
18
+
19
+ <div data-hook="admin_affiliate_new_form_buttons">
20
+ <%= render :partial => 'spree/admin/shared/new_resource_links' %>
21
+ </div>
22
+ <% end %>
23
+ </div>
@@ -0,0 +1,6 @@
1
+ <div class="field alpha omega eight columns">
2
+ <label><%= Spree.t('affiliates_promotion_rule.choose_affiliates') %></label><br>
3
+ <% Spree::Affiliate.all.each do |r| %>
4
+ <p><label><input type='checkbox' name='<%= param_prefix %>[affiliate_list][]' id='affiliate_list_<%= r.id %>' value='<%= r.id %>' <%= 'checked="checked"' if promotion_rule.affiliate_list.include?(r.id) %> /> <%= r.name %></label></p>
5
+ <% end %>
6
+ </div>
data/bin/rails ADDED
@@ -0,0 +1,7 @@
1
+ # This command will automatically be run when you run "rails" with Rails 3 gems installed from the root of your application.
2
+
3
+ ENGINE_ROOT = File.expand_path('../..', __FILE__)
4
+ ENGINE_PATH = File.expand_path('../../lib/spree_reffiliate/engine', __FILE__)
5
+
6
+ require 'rails/all'
7
+ require 'rails/engine/commands'
@@ -0,0 +1,6 @@
1
+ Spree::PermittedAttributes.user_attributes.push :referral_code, :affiliate_code
2
+ config = Rails.application.config
3
+ config.after_initialize do
4
+ config.spree.promotions.rules << Spree::Promotion::Rules::ReferredPromotionRule
5
+ config.spree.promotions.rules << Spree::Promotion::Rules::AffiliatedPromotionRule
6
+ end
@@ -0,0 +1,10 @@
1
+ en:
2
+ spree:
3
+ promotion_rule_types:
4
+ referred_promotion_rule:
5
+ name: "Referred User"
6
+ description: "Must be a referred user"
7
+ affiliated_promotion_rule:
8
+ name: "Affiliate User"
9
+ description: "Must be an affiliated user"
10
+ editing_affiliate: "Editing Affiliate:"
data/config/routes.rb ADDED
@@ -0,0 +1,8 @@
1
+ Spree::Core::Engine.routes.draw do
2
+ get 'r/:code' => 'reffiliate#referral', as: 'referral'
3
+ get 'a/:path' => 'reffiliate#affiliate', as: 'affiliate'
4
+
5
+ namespace :admin do
6
+ resources :affiliates
7
+ end
8
+ end
@@ -0,0 +1,8 @@
1
+ class CreateSpreeReferrals < ActiveRecord::Migration
2
+ def change
3
+ create_table :spree_referrals do |t|
4
+ t.string :code
5
+ t.integer :user_id
6
+ end
7
+ end
8
+ end
@@ -0,0 +1,9 @@
1
+ class CreateSpreeAffiliates < ActiveRecord::Migration
2
+ def change
3
+ create_table :spree_affiliates do |t|
4
+ t.string :name
5
+ t.string :path
6
+ t.string :partial
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,9 @@
1
+ class CreateSpreeReferredRecords < ActiveRecord::Migration
2
+ def change
3
+ create_table :spree_referred_records do |t|
4
+ t.integer :user_id
5
+ t.integer :referral_id
6
+ t.integer :affiliate_id
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,5 @@
1
+ class AddIndexToSpreeReferrals < ActiveRecord::Migration
2
+ def change
3
+ add_index :spree_referrals, :user_id
4
+ end
5
+ end
@@ -0,0 +1,5 @@
1
+ class AddIndexToSpreeReferredRecord < ActiveRecord::Migration
2
+ def change
3
+ add_index :spree_referred_records, [:user_id, :referral_id, :affiliate_id], name: 'index_spree_referred_record_on_u_r_a'
4
+ end
5
+ end
@@ -0,0 +1,10 @@
1
+ class CreateSpreePromotionRulesAffiliates < ActiveRecord::Migration
2
+ def change
3
+ create_table :spree_affiliates_promotion_rules do |t|
4
+ t.integer :affiliate_id
5
+ t.integer :promotion_rule_id
6
+ end
7
+ add_index :spree_affiliates_promotion_rules, :affiliate_id
8
+ add_index :spree_affiliates_promotion_rules, :promotion_rule_id
9
+ end
10
+ end
@@ -0,0 +1,5 @@
1
+ class AddLayoutToSpreeAffiliate < ActiveRecord::Migration
2
+ def change
3
+ add_column :spree_affiliates, :layout, :string
4
+ end
5
+ end
@@ -0,0 +1,21 @@
1
+ module SpreeReffiliate
2
+ module Generators
3
+ class InstallGenerator < Rails::Generators::Base
4
+
5
+ class_option :auto_run_migrations, :type => :boolean, :default => false
6
+
7
+ def add_migrations
8
+ run 'bundle exec rake railties:install:migrations FROM=spree_reffiliate'
9
+ end
10
+
11
+ def run_migrations
12
+ run_migrations = options[:auto_run_migrations] || ['', 'y', 'Y'].include?(ask 'Would you like to run the migrations now? [Y/n]')
13
+ if run_migrations
14
+ run 'bundle exec rake db:migrate'
15
+ else
16
+ puts 'Skipping rake db:migrate, don\'t forget to run it!'
17
+ end
18
+ end
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,20 @@
1
+ module SpreeReffiliate
2
+ class Engine < Rails::Engine
3
+ require 'spree/core'
4
+ isolate_namespace Spree
5
+ engine_name 'spree_reffiliate'
6
+
7
+ # use rspec for tests
8
+ config.generators do |g|
9
+ g.test_framework :rspec
10
+ end
11
+
12
+ def self.activate
13
+ Dir.glob(File.join(File.dirname(__FILE__), '../../app/**/*_decorator*.rb')) do |c|
14
+ Rails.configuration.cache_classes ? require(c) : load(c)
15
+ end
16
+ end
17
+
18
+ config.to_prepare &method(:activate).to_proc
19
+ end
20
+ end
@@ -0,0 +1,12 @@
1
+ FactoryGirl.define do
2
+ factory :affiliate, class: Spree::Affiliate do
3
+ name Faker::Name.first_name
4
+ path Faker::Name.first_name
5
+ partial Faker::Name.first_name
6
+ layout nil
7
+ end
8
+
9
+ factory :referral, class: Spree::Referral
10
+ factory :referred_promotion_rule, class: Spree::Promotion::Rules::ReferredPromotionRule
11
+ factory :affiliated_promotion_rule, class: Spree::Promotion::Rules::AffiliatedPromotionRule
12
+ end
@@ -0,0 +1,2 @@
1
+ require 'spree_core'
2
+ require 'spree_reffiliate/engine'
@@ -0,0 +1,10 @@
1
+ namespace :reffiliate do
2
+ task :generate => :environment do
3
+ Spree.user_class.all.each do |user|
4
+ if user.referral.nil?
5
+ user.create_referral
6
+ end
7
+ end
8
+ puts "Referrals generated."
9
+ end
10
+ end
@@ -0,0 +1 @@
1
+ corona.html.erb
@@ -0,0 +1,56 @@
1
+ require 'spec_helper'
2
+
3
+ describe Spree::ReffiliateController, :type => :controller do
4
+ context "referral hyperlink" do
5
+ it "redirects to root path" do
6
+ spree_get :referral
7
+ expect(response).to redirect_to('/')
8
+ end
9
+ it "creates session variable with referral code" do
10
+ code = 'referral-code'
11
+ spree_get :referral, code: code
12
+ expect(session[:referral]).to eq(code)
13
+ end
14
+ end
15
+ context "affiliate hyperlink" do
16
+ before (:each) do
17
+ @affiliate = FactoryGirl.create(:affiliate)
18
+ end
19
+ it "creates session variable with affiliate path" do
20
+ path = 'affiliate'
21
+ spree_get :affiliate, :path => path
22
+ expect(session[:affiliate]).to eq(path)
23
+ end
24
+ it "redirects to root path if path params is nil" do
25
+ spree_get :affiliate
26
+ expect(response).to redirect_to('/')
27
+ end
28
+ it "redirects to root path if affiliate has no partial" do
29
+ spree_get :affiliate, path: @affiliate.path
30
+ expect(response).to redirect_to('/')
31
+ end
32
+ it "redirects to root path if affiliate partial is not found" do
33
+ spree_get :affiliate, path: @affiliate.path
34
+ expect(response).to redirect_to('/')
35
+ end
36
+ it "renders a partial if affiliate partial is found" do
37
+ @affiliate.update_attribute :partial, 'corona'
38
+ controller.prepend_view_path 'spec/assets'
39
+ spree_get :affiliate, path: @affiliate.path
40
+ response.should render_template('spree/affiliates/corona')
41
+ end
42
+ context "layout options" do
43
+ @options = Hash[Spree::Affiliate.layout_options]
44
+ @options.each do |layout, option|
45
+ it "#{layout}" do
46
+ option = 'layouts/application' if option.nil?
47
+ @affiliate = FactoryGirl.create(:affiliate, name: layout, path: layout, partial: 'corona', layout: option)
48
+ controller.prepend_view_path 'spec/assets'
49
+ allow(controller).to receive(:partial_exists).and_return(true)
50
+ spree_get :affiliate, path: @affiliate.path
51
+ response.should render_template(layout: @affiliate.get_layout)
52
+ end
53
+ end
54
+ end
55
+ end
56
+ end
@@ -0,0 +1,20 @@
1
+ require 'spec_helper'
2
+
3
+ describe Spree::UserRegistrationsController, :type => :controller do
4
+ before { @request.env['devise.mapping'] = Devise.mappings[:spree_user] }
5
+
6
+ context "#create" do
7
+ it "should add referral_code param" do
8
+ code = 'referral-code'
9
+ session[:referral] = code
10
+ spree_post :create, { spree_user: { email: 'foobar@example.com', password: 'foobar123', password_confirmation: 'foobar123' } }
11
+ expect(controller.params[:spree_user][:referral_code]).to eql(code)
12
+ end
13
+ it "should add affiliate_code param" do
14
+ code = 'affiliate-code'
15
+ session[:affiliate] = code
16
+ spree_post :create, { spree_user: { email: 'foobar@example.com', password: 'foobar123', password_confirmation: 'foobar123' } }
17
+ expect(controller.params[:spree_user][:affiliate_code]).to eql(code)
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,33 @@
1
+ require 'spec_helper'
2
+
3
+ describe Spree::Affiliate, :type => :model do
4
+ it "should have a name" do
5
+ affiliate = FactoryGirl.build(:affiliate, name: nil)
6
+ expect(affiliate).to_not be_valid
7
+ end
8
+ it "should have a path" do
9
+ affiliate = FactoryGirl.build(:affiliate, path: nil)
10
+ expect(affiliate).to_not be_valid
11
+ end
12
+
13
+ context "with user" do
14
+ before do
15
+ @affiliate = FactoryGirl.create(:affiliate)
16
+ @affiliated = FactoryGirl.create(:user, affiliate_code: @affiliate.path)
17
+ @order = FactoryGirl.create(:order, :user => @affiliated)
18
+ end
19
+
20
+ it "returns an array of user records" do
21
+ expect(@affiliate.referred_users).to eq([@affiliated])
22
+ end
23
+ it "returns an array of referred orders" do
24
+ expect(@affiliate.referred_orders).to eq([@order])
25
+ end
26
+ it "returns referred count" do
27
+ expect(@affiliate.referred_count).to eq(1)
28
+ end
29
+ it "returns an array of referred orders" do
30
+ expect(@affiliate.referred_records).to eq([@affiliated.affiliate_record])
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,46 @@
1
+ require 'spec_helper'
2
+
3
+ describe Spree::Promotion::Rules::AffiliatedPromotionRule, :type => :model do
4
+ before do
5
+ @affiliate = FactoryGirl.create(:affiliate, path: 'corona')
6
+ @affiliated = FactoryGirl.create(:user, email: Faker::Internet.email, affiliate_code: @affiliate.path)
7
+ @affiliated_order = FactoryGirl.create(:order, :user_id => @affiliated.id)
8
+
9
+ @falsy_affiliate = FactoryGirl.create(:affiliate, path: 'victoria')
10
+ @falsy_affiliated = FactoryGirl.create(:user, email: Faker::Internet.email, affiliate_code: @falsy_affiliate.path)
11
+ @falsy_affiliated_order = FactoryGirl.create(:order, :user_id => @falsy_affiliated.id)
12
+
13
+ @user = FactoryGirl.create(:user, email: Faker::Internet.email)
14
+ @organic_order = FactoryGirl.create(:order, :user_id => @user)
15
+
16
+ @rule = FactoryGirl.build(:affiliated_promotion_rule)
17
+ @rule.affiliate_list = [@affiliate.id]
18
+ end
19
+
20
+ it "is applicable for an order" do
21
+ expect(@rule.applicable?(@affiliated_order)).to be_truthy
22
+ end
23
+ it "is elegible for affiliated user" do
24
+ expect(@rule.eligible?(@affiliated_order)).to be_truthy
25
+ end
26
+ it "is not elegible for organic user" do
27
+ expect(@rule.eligible?(@organic_order)).to be_falsy
28
+ end
29
+ it "is not elegible for falsy affiliated user" do
30
+ expect(@rule.eligible?(@falsy_affiliated_order)).to be_falsy
31
+ end
32
+ it "returns string selected affiliate ids" do
33
+ expect(@rule.affiliate_ids_string).to eq(@affiliate.id.to_s)
34
+ end
35
+ it "accepts string to assign affiliate ids" do
36
+ @rule.affiliate_ids_string = "1"
37
+ expect(@rule.affiliate_ids_string).to eq(@affiliate.id.to_s)
38
+ end
39
+ it "returns an array of affiliate ids" do
40
+ expect(@rule.affiliate_list).to eq([@affiliate.id])
41
+ end
42
+ it "accepts an array to assign affiliate ids" do
43
+ @rule.affiliate_list << [1]
44
+ expect(@rule.affiliate_list).to eq([@affiliate.id])
45
+ end
46
+ end
@@ -0,0 +1,20 @@
1
+ require 'spec_helper'
2
+
3
+ describe Spree::Promotion::Rules::ReferredPromotionRule, :type => :model do
4
+ before(:each) do
5
+ @rule = FactoryGirl.build(:referred_promotion_rule)
6
+ @user = FactoryGirl.create(:user, email: Faker::Internet.email)
7
+ @referred = FactoryGirl.create(:user, email: Faker::Internet.email, referral_code: @user.referral.code)
8
+ @organic_order = FactoryGirl.create(:order, :user => @user)
9
+ @referred_order = FactoryGirl.create(:order, :user => @referred)
10
+ end
11
+ it "is applicable for an order" do
12
+ expect(@rule.applicable?(@referred_order)).to be_truthy
13
+ end
14
+ it "is elegible for referred user" do
15
+ expect(@rule.eligible?(@referred_order)).to be_truthy
16
+ end
17
+ it "is not elegible for organic user" do
18
+ expect(@rule.eligible?(@organic_order)).to be_falsy
19
+ end
20
+ end
@@ -0,0 +1,41 @@
1
+ require 'spec_helper'
2
+
3
+ describe Spree::Referral, :type => :model do
4
+ it "generates a code after created" do
5
+ referral = FactoryGirl.build(:referral)
6
+ expect{referral.save}.to change{referral.code}
7
+ end
8
+
9
+ it "returns a code" do
10
+ user = FactoryGirl.create(:user)
11
+ referral = FactoryGirl.create(:referral, user: user)
12
+ expect(referral.code).to_not be_nil
13
+ end
14
+
15
+ context "with user" do
16
+ before(:each) do
17
+ @user = FactoryGirl.create(:user)
18
+ @referred = FactoryGirl.create(:user, referral_code: @user.referral.code)
19
+ @order = FactoryGirl.create(:order, user: @referred)
20
+ @referred_no_purchase = FactoryGirl.create(:user, referral_code: @user.referral.code)
21
+ end
22
+
23
+ it "returns an associated user record" do
24
+ expect(@user.referral.user).to_not be_nil
25
+ end
26
+ it "returns referred records" do
27
+ expect(@user.referral.referred_records.count).to eq(2)
28
+ end
29
+ it "returns an array of referred users" do
30
+ expect(@user.referral.referred_users).to eq([@referred, @referred_no_purchase])
31
+ end
32
+ it "returns an array of referred orders" do
33
+ expect(@user.referral.referred_orders).to eq([@order])
34
+ end
35
+ it "returns an array of referral activated users with completed_orders" do
36
+ referred_user_with_completed_order = FactoryGirl.create(:user, referral_code: @user.referral.code)
37
+ completed_order = FactoryGirl.create(:order, completed_at: @user.created_at + 1.second, user: referred_user_with_completed_order)
38
+ expect(@user.referral.referral_activated_users).to eq([referred_user_with_completed_order])
39
+ end
40
+ end
41
+ end
@@ -0,0 +1,29 @@
1
+ require 'spec_helper'
2
+
3
+ describe Spree::ReferredRecord, :type => :model do
4
+ before(:each) do
5
+ @user = FactoryGirl.create(:user, email: Faker::Internet.email)
6
+ @referred = FactoryGirl.create(:user, email: Faker::Internet.email, referral_code: @user.referral.code)
7
+ @referral_record = @referred.referred_record
8
+ end
9
+
10
+ it "has a user id" do
11
+ expect(@referral_record.user_id).not_to be_nil
12
+ end
13
+
14
+ it "has a referral id" do
15
+ expect(@referral_record.referral_id).not_to be_nil
16
+ end
17
+
18
+ it "returns an associated user record" do
19
+ expect(@referral_record.referral.user).to eq(@user)
20
+ end
21
+
22
+ it "returns an associated referred user record" do
23
+ expect(@referral_record.user).to eq(@referred)
24
+ end
25
+
26
+ it "returns an associated referral record" do
27
+ expect(@referral_record.referral).to eq(@user.referral)
28
+ end
29
+ end
@@ -0,0 +1,52 @@
1
+ require 'spec_helper'
2
+
3
+ describe Spree::User, type: :model do
4
+ before(:each) do
5
+ @user = FactoryGirl.create(:user, email: Faker::Internet.email)
6
+ @referred = FactoryGirl.create(:user, email: Faker::Internet.email, referral_code: @user.referral.code)
7
+ @affiliate = Spree::Affiliate.create(name: Faker::Name.name, path: Faker::Name.name)
8
+ @affiliated = FactoryGirl.create(:user, email: Faker::Internet.email, affiliate_code: @affiliate.path)
9
+ end
10
+ context "referral user" do
11
+ it "has a referral record" do
12
+ expect(@user.referral).not_to be_nil
13
+ end
14
+ it "has a referral code" do
15
+ expect(@user.referral.code).not_to be_nil
16
+ end
17
+ it "has a referral count" do
18
+ expect(@user.referred_count).to eq(1)
19
+ end
20
+ it "has referred users" do
21
+ expect(@user.referral.referred_users).to eq([@referred])
22
+ end
23
+ it "has referred records" do
24
+ expect(@user.referral.referred_records).to eq([@referred.referred_record])
25
+ end
26
+ end
27
+
28
+ context "referred user" do
29
+ it "has a referred user" do
30
+ expect(@referred.referred_by).to eq(@user)
31
+ end
32
+ it "has a referred record" do
33
+ expect(@referred.referred_record).to eq(@user.referral.referred_records.first)
34
+ end
35
+ it "returns boolean if referred" do
36
+ expect(@referred.referred?).to be_truthy
37
+ expect(@user.referred?).to be_falsy
38
+ end
39
+ end
40
+
41
+ context "affiliated user" do
42
+ it "has an affiliate" do
43
+ expect(@affiliated.affiliate).to eq(@affiliate)
44
+ end
45
+ it "returns boolean if affiliated" do
46
+ expect(@affiliated.affiliate?).to be_truthy
47
+ end
48
+ it "returns an affiliated record" do
49
+ expect(@affiliated.affiliate_record).to eq(@affiliate.referred_records.first)
50
+ end
51
+ end
52
+ end