Add credits counter_culter to users and orgs (#2689)

This commit is contained in:
Ben Halpern 2019-05-04 10:50:48 -04:00 committed by GitHub
parent 393acc3ab3
commit 41d3f7ef39
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 40 additions and 4 deletions

View file

@ -4,6 +4,9 @@ class Credit < ApplicationRecord
belongs_to :user, optional: true
belongs_to :organization, optional: true
counter_culture :user # Only counting credits_count right now. TODO: spent_credits_count and unspent_credits_count (counter culture gem)
counter_culture :organization # Only counting credits_count right now. TODO: spent_credits_count and unspent_credits_count (counter culture gem)
def self.add_to(user, amount)
credit_objects = []
amount.times do

View file

@ -0,0 +1,19 @@
class AddCreditsCountToUsers < ActiveRecord::Migration[5.2]
def self.up
add_column :users, :credits_count, :integer, null: false, default: 0
add_column :users, :spent_credits_count, :integer, null: false, default: 0
add_column :users, :unspent_credits_count, :integer, null: false, default: 0
add_column :organizations, :credits_count, :integer, null: false, default: 0
add_column :organizations, :spent_credits_count, :integer, null: false, default: 0
add_column :organizations, :unspent_credits_count, :integer, null: false, default: 0
end
def self.down
remove_column :users, :credits_count
remove_column :users, :spent_credits_count
remove_column :users, :unspent_credits_count
remove_column :users, :credits_count
remove_column :users, :spent_credits_count
remove_column :users, :unspent_credits_count
end
end

View file

@ -10,9 +10,8 @@
#
# It's strongly recommended that you check this file into your version control system.
ActiveRecord::Schema.define(version: 2019_05_02_165056) do
ActiveRecord::Schema.define(version: 2019_05_04_131412) do
# These are extensions that must be enabled in order to support this database
enable_extension "pg_stat_statements"
enable_extension "plpgsql"
create_table "ahoy_messages", id: :serial, force: :cascade do |t|
@ -248,7 +247,6 @@ ActiveRecord::Schema.define(version: 2019_05_02_165056) do
t.string "title"
t.datetime "updated_at", null: false
t.bigint "user_id"
t.string "slug"
t.index ["organization_id"], name: "index_classified_listings_on_organization_id"
t.index ["user_id"], name: "index_classified_listings_on_user_id"
end
@ -542,6 +540,7 @@ ActiveRecord::Schema.define(version: 2019_05_02_165056) do
t.string "company_size"
t.string "country"
t.datetime "created_at", null: false
t.integer "credits_count", default: 0, null: false
t.text "cta_body_markdown"
t.string "cta_button_text"
t.string "cta_button_url"
@ -563,6 +562,7 @@ ActiveRecord::Schema.define(version: 2019_05_02_165056) do
t.text "proof"
t.string "secret"
t.string "slug"
t.integer "spent_credits_count", default: 0, null: false
t.text "sponsorship_blurb_html"
t.integer "sponsorship_featured_number", default: 0
t.string "sponsorship_tagline"
@ -574,6 +574,7 @@ ActiveRecord::Schema.define(version: 2019_05_02_165056) do
t.string "tech_stack"
t.string "text_color_hex"
t.string "twitter_username"
t.integer "unspent_credits_count", default: 0, null: false
t.datetime "updated_at", null: false
t.string "url"
t.string "zip_code"
@ -829,6 +830,7 @@ ActiveRecord::Schema.define(version: 2019_05_02_165056) do
t.datetime "confirmed_at"
t.boolean "contact_consent", default: false
t.datetime "created_at", null: false
t.integer "credits_count", default: 0, null: false
t.datetime "current_sign_in_at"
t.inet "current_sign_in_ip"
t.string "currently_hacking_on"
@ -937,6 +939,7 @@ ActiveRecord::Schema.define(version: 2019_05_02_165056) do
t.string "signup_refer_code"
t.string "signup_referring_site"
t.string "specialty"
t.integer "spent_credits_count", default: 0, null: false
t.string "stackoverflow_url"
t.string "stripe_id_code"
t.text "summary"
@ -951,6 +954,7 @@ ActiveRecord::Schema.define(version: 2019_05_02_165056) do
t.integer "twitter_following_count"
t.string "twitter_username"
t.string "unconfirmed_email"
t.integer "unspent_credits_count", default: 0, null: false
t.datetime "updated_at", null: false
t.string "username"
t.string "website_url"

View file

@ -1,5 +1,15 @@
require "rails_helper"
RSpec.describe Credit, type: :model do
pending "add some examples to (or delete) #{__FILE__}"
it "counts credits for user" do
user = create(:user)
Credit.create(user_id: user.id)
Credit.create(user_id: user.id)
expect(user.reload.credits_count).to eq(2)
end
it "counts credits for organization" do
organization = create(:organization)
Credit.create(organization_id: organization.id)
expect(organization.reload.credits_count).to eq(1)
end
end