diff --git a/app/controllers/admin/users_controller.rb b/app/controllers/admin/users_controller.rb index 9eac16b3b..64a317543 100644 --- a/app/controllers/admin/users_controller.rb +++ b/app/controllers/admin/users_controller.rb @@ -12,6 +12,7 @@ module Admin sanitize_params! @user.level = params[:user][:level] @user.inviter_id = CurrentUser.id + TransactionLogItem.record_account_upgrade(@user) @user.save redirect_to edit_admin_user_path(@user), :notice => "User updated" end diff --git a/app/controllers/users_controller.rb b/app/controllers/users_controller.rb index 5141f955b..75529c76f 100644 --- a/app/controllers/users_controller.rb +++ b/app/controllers/users_controller.rb @@ -59,6 +59,12 @@ class UsersController < ApplicationController respond_with(@user) end + def upgrade_information + unless CurrentUser.user.is_anonymous? + TransactionLogItem.record_account_upgrade_view(CurrentUser.user, request.referer) + end + end + def upgrade @user = User.find(params[:id]) diff --git a/app/models/transaction_log_item.rb b/app/models/transaction_log_item.rb new file mode 100644 index 000000000..52da8971e --- /dev/null +++ b/app/models/transaction_log_item.rb @@ -0,0 +1,27 @@ +class TransactionLogItem < ActiveRecord::Base + attr_accessible :category, :data, :user_id + validates_inclusion_of :category, :in => %w( + account_upgrade_basic_to_gold + account_upgrade_basic_to_platinum + account_upgrade_gold_to_platinum + account_upgrade_view + ) + + def self.record_account_upgrade_view(user, referrer) + create(:category => "account_upgrade_view", :user_id => user.id, :data => referrer) + end + + def self.record_account_upgrade(user) + attributes = {:user_id => user.id} + + if user.level_was < User::Levels::PLATINUM && user.level == User::Levels::PLATINUM + attributes[:category] = "account_upgrade_gold_to_platinum" + elsif user.level_was < User::Levels::GOLD && user.level == User::Levels::GOLD + attributes[:category] = "account_upgrade_basic_to_gold" + elsif user.level_was < User::Levels::GOLD && user.level == User::Levels::PLATINUM + attributes[:category] = "account_upgrade_basic_to_platinum" + end + + create(attributes) + end +end diff --git a/db/migrate/20140204233337_create_transaction_log_items.rb b/db/migrate/20140204233337_create_transaction_log_items.rb new file mode 100644 index 000000000..bc23fe69d --- /dev/null +++ b/db/migrate/20140204233337_create_transaction_log_items.rb @@ -0,0 +1,14 @@ +class CreateTransactionLogItems < ActiveRecord::Migration + def change + create_table :transaction_log_items do |t| + t.string :category + t.integer :user_id + t.text :data + + t.timestamps + end + + add_index :transaction_log_items, :user_id + add_index :transaction_log_items, :created_at + end +end diff --git a/db/structure.sql b/db/structure.sql index 21512a011..492df159c 100644 --- a/db/structure.sql +++ b/db/structure.sql @@ -2586,6 +2586,39 @@ CREATE SEQUENCE tags_id_seq ALTER SEQUENCE tags_id_seq OWNED BY tags.id; +-- +-- Name: transaction_log_items; Type: TABLE; Schema: public; Owner: -; Tablespace: +-- + +CREATE TABLE transaction_log_items ( + id integer NOT NULL, + category character varying(255), + user_id integer, + data text, + created_at timestamp without time zone NOT NULL, + updated_at timestamp without time zone NOT NULL +); + + +-- +-- Name: transaction_log_items_id_seq; Type: SEQUENCE; Schema: public; Owner: - +-- + +CREATE SEQUENCE transaction_log_items_id_seq + START WITH 1 + INCREMENT BY 1 + NO MAXVALUE + NO MINVALUE + CACHE 1; + + +-- +-- Name: transaction_log_items_id_seq; Type: SEQUENCE OWNED BY; Schema: public; Owner: - +-- + +ALTER SEQUENCE transaction_log_items_id_seq OWNED BY transaction_log_items.id; + + -- -- Name: uploads; Type: TABLE; Schema: public; Owner: -; Tablespace: -- @@ -3812,6 +3845,13 @@ ALTER TABLE ONLY tag_subscriptions ALTER COLUMN id SET DEFAULT nextval('tag_subs ALTER TABLE ONLY tags ALTER COLUMN id SET DEFAULT nextval('tags_id_seq'::regclass); +-- +-- Name: id; Type: DEFAULT; Schema: public; Owner: - +-- + +ALTER TABLE ONLY transaction_log_items ALTER COLUMN id SET DEFAULT nextval('transaction_log_items_id_seq'::regclass); + + -- -- Name: id; Type: DEFAULT; Schema: public; Owner: - -- @@ -4141,6 +4181,14 @@ ALTER TABLE ONLY tags ADD CONSTRAINT tags_pkey PRIMARY KEY (id); +-- +-- Name: transaction_log_items_pkey; Type: CONSTRAINT; Schema: public; Owner: -; Tablespace: +-- + +ALTER TABLE ONLY transaction_log_items + ADD CONSTRAINT transaction_log_items_pkey PRIMARY KEY (id); + + -- -- Name: uploads_pkey; Type: CONSTRAINT; Schema: public; Owner: -; Tablespace: -- @@ -6220,6 +6268,20 @@ CREATE UNIQUE INDEX index_tags_on_name ON tags USING btree (name); CREATE INDEX index_tags_on_name_pattern ON tags USING btree (name text_pattern_ops); +-- +-- Name: index_transaction_log_items_on_created_at; Type: INDEX; Schema: public; Owner: -; Tablespace: +-- + +CREATE INDEX index_transaction_log_items_on_created_at ON transaction_log_items USING btree (created_at); + + +-- +-- Name: index_transaction_log_items_on_user_id; Type: INDEX; Schema: public; Owner: -; Tablespace: +-- + +CREATE INDEX index_transaction_log_items_on_user_id ON transaction_log_items USING btree (user_id); + + -- -- Name: index_uploads_on_uploader_id; Type: INDEX; Schema: public; Owner: -; Tablespace: -- @@ -6572,4 +6634,6 @@ INSERT INTO schema_migrations (version) VALUES ('20131225002748'); INSERT INTO schema_migrations (version) VALUES ('20131228230219'); -INSERT INTO schema_migrations (version) VALUES ('20140111191413'); \ No newline at end of file +INSERT INTO schema_migrations (version) VALUES ('20140111191413'); + +INSERT INTO schema_migrations (version) VALUES ('20140204233337'); \ No newline at end of file diff --git a/test/fixtures/transaction_log_items.yml b/test/fixtures/transaction_log_items.yml new file mode 100644 index 000000000..ef6db0c73 --- /dev/null +++ b/test/fixtures/transaction_log_items.yml @@ -0,0 +1,9 @@ +# Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/Fixtures.html + +one: + category: MyString + data: MyText + +two: + category: MyString + data: MyText diff --git a/test/unit/transaction_log_item_test.rb b/test/unit/transaction_log_item_test.rb new file mode 100644 index 000000000..63cf57a91 --- /dev/null +++ b/test/unit/transaction_log_item_test.rb @@ -0,0 +1,28 @@ +require 'test_helper' + +class TransactionLogItemTest < ActiveSupport::TestCase + setup do + @user = FactoryGirl.create(:user) + end + + context "Promoting a user" do + should "create a new line item in the transaction log" do + @user.level = User::Levels::GOLD + assert_difference("TransactionLogItem.count", 1) do + TransactionLogItem.record_account_upgrade(@user) + end + + item = TransactionLogItem.last + assert_equal(@user.id, item.user_id) + assert_equal("account_upgrade_basic_to_gold", item.category) + end + end + + context "Viewing the account upgrade page" do + should "create a new line item in the transaction log" do + assert_difference("TransactionLogItem.count", 1) do + TransactionLogItem.record_account_upgrade_view(@user, "xxx") + end + end + end +end