From 41d3f7ef39f40c591bbf22621c3535e382361dc8 Mon Sep 17 00:00:00 2001 From: Ben Halpern Date: Sat, 4 May 2019 10:50:48 -0400 Subject: [PATCH] Add credits counter_culter to users and orgs (#2689) --- app/models/credit.rb | 3 +++ ...190504131412_add_credits_count_to_users.rb | 19 +++++++++++++++++++ db/schema.rb | 10 +++++++--- spec/models/credit_spec.rb | 12 +++++++++++- 4 files changed, 40 insertions(+), 4 deletions(-) create mode 100644 db/migrate/20190504131412_add_credits_count_to_users.rb diff --git a/app/models/credit.rb b/app/models/credit.rb index 4e94af59a..d8de5c1d3 100644 --- a/app/models/credit.rb +++ b/app/models/credit.rb @@ -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 diff --git a/db/migrate/20190504131412_add_credits_count_to_users.rb b/db/migrate/20190504131412_add_credits_count_to_users.rb new file mode 100644 index 000000000..922565437 --- /dev/null +++ b/db/migrate/20190504131412_add_credits_count_to_users.rb @@ -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 diff --git a/db/schema.rb b/db/schema.rb index 840d0f84e..1beff6747 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -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" diff --git a/spec/models/credit_spec.rb b/spec/models/credit_spec.rb index db6988d6c..403315920 100644 --- a/spec/models/credit_spec.rb +++ b/spec/models/credit_spec.rb @@ -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