Add raffle entries to users #datamigration

This commit is contained in:
Andrew Brown 2017-04-17 20:08:58 +01:00
parent cc0662c1c3
commit 4aef7b9d83
9 changed files with 74 additions and 3 deletions

View File

@ -78,6 +78,31 @@ class SubscriptionsController < ApplicationController
return redirect_to :back
end
# If this is the first time this user is subscribing to Premium, gift them (and their referrer, if applicable) feature votes and space
existing_premium_subscriptions = current_user.subscriptions.where(billing_plan_id: [2, 3, 4, 5, 6])
unless existing_premium_subscriptions.any?
referring_user = current_user.referrer
# First-time premium!
# +100 MB
current_user.update upload_bandwidth_kb: current_user.upload_bandwidth_kb + 100_000
current_user.reload
# +1 vote
current_user.votes.create
# +1 vote if referred
current_user.votes.create if referring_user.present?
if referring_user
# +100MB
referring_user.update upload_bandwidth_kb: referring_user.upload_bandwidth_kb + 100_000
# +2 votes
referring_user.votes.create
referring_user.votes.create
end
end
if current_user.selected_billing_plan_id.nil?
old_billing_plan = BillingPlan.new(name: 'No billing plan')
else

View File

@ -0,0 +1,3 @@
class RaffleEntry < ActiveRecord::Base
belongs_to :user
end

View File

@ -19,12 +19,14 @@ class User < ActiveRecord::Base
has_many :image_uploads
has_one :referral_code
has_many :referrals, foreign_key: :referrer_id
def referrer
referral = Referral.find_by(referred_id: self.id)
referral.referrer unless referral.nil?
end
has_many :votes
has_many :raffle_entries
after_create :initialize_stripe_customer, unless: -> { Rails.env == 'test' }
after_create :initialize_referral_code

View File

@ -199,7 +199,7 @@
<div class="col s12">
<h4>Get extra votes for Notebook.ai's first community feature</h4>
<p class="light">
Worldbuilding is better with a world of diverse experiences and ideas. For the first time, all Premium users have been given a vote to decide what Notebook.ai's first community-based feature will be! You can cast your vote at <%= link_to 'the voting page', '#' %>, and you can earn additional votes with each referral.
Worldbuilding is better with a world of diverse experiences and ideas. For the first time, all Premium users have been given a vote to decide what Notebook.ai's first community-based feature will be! You can cast your vote at <%= link_to 'the voting page', community_voting_path %>, and you can earn additional votes with each referral.
</p>
</div>
</div>
@ -250,7 +250,7 @@
</div>
<div class="col s12 center">
<%= link_to 'Cast your vote and see results on our voting page', '#' %>
<%= link_to 'Cast your vote and see results on our voting page', community_voting_path %>
</div>
</div>
</div>

View File

@ -0,0 +1,9 @@
class CreateRaffleEntries < ActiveRecord::Migration
def change
create_table :raffle_entries do |t|
t.references :user, index: true, foreign_key: true
t.timestamps null: false
end
end
end

View File

@ -11,7 +11,7 @@
#
# It's strongly recommended that you check this file into your version control system.
ActiveRecord::Schema.define(version: 20170415192437) do
ActiveRecord::Schema.define(version: 20170417190318) do
create_table "archenemyships", force: :cascade do |t|
t.integer "user_id"
@ -544,6 +544,14 @@ ActiveRecord::Schema.define(version: 20170415192437) do
t.integer "race_id"
end
create_table "raffle_entries", force: :cascade do |t|
t.integer "user_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
add_index "raffle_entries", ["user_id"], name: "index_raffle_entries_on_user_id"
create_table "referral_codes", force: :cascade do |t|
t.integer "user_id"
t.string "code"

10
docs/creating_votes.md Normal file
View File

@ -0,0 +1,10 @@
Votes are created the following ways:
- During the Referral release, each currently-premium user will be given 1 feature vote.
- Whenever a user signs up for Premium that hasn't signed up for Premium before, they are given 1 feature vote.
- Whenever a user that was referred by another user signs up for Premium for the first time,
- the user that referred that user is given 2 additional feature votes
- the user that signed up for Premium is given an additional feature vote (2 in total)
Votes are never destroyed/deleted, even when cancelling a Premium subscription. They are only ever "used" when voting, and can
only be used once per vote.

7
test/fixtures/raffle_entries.yml vendored Normal file
View File

@ -0,0 +1,7 @@
# Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html
one:
user_id:
two:
user_id:

View File

@ -0,0 +1,7 @@
require 'test_helper'
class RaffleEntryTest < ActiveSupport::TestCase
# test "the truth" do
# assert true
# end
end