From cdee1878955638557ec9655c644037c70fd546ca Mon Sep 17 00:00:00 2001 From: Aleksandr Zykov Date: Sat, 21 Jun 2014 09:57:09 +0800 Subject: [PATCH] use decided_at instead of created_at to refund tips --- app/models/tip.rb | 8 +++++++- db/migrate/20140620123610_add_decided_at_to_tips.rb | 5 +++++ ...0620124628_update_decided_at_for_existing_tips.rb | 7 +++++++ db/schema.rb | 12 +++++++----- 4 files changed, 26 insertions(+), 6 deletions(-) create mode 100644 db/migrate/20140620123610_add_decided_at_to_tips.rb create mode 100644 db/migrate/20140620124628_update_decided_at_for_existing_tips.rb diff --git a/app/models/tip.rb b/app/models/tip.rb index d78fead8..d38ce230 100644 --- a/app/models/tip.rb +++ b/app/models/tip.rb @@ -65,11 +65,13 @@ def was_undecided? after_save :notify_user_if_just_decided + before_save :touch_decided_at_if_decided def self.refund_unclaimed unclaimed.non_refunded. - where('tips.created_at < ?', Time.now - 1.month). + where.not(decided_at: nil). + where('tips.decided_at < ?', Time.now - 1.month). find_each do |tip| tip.touch :refunded_at end @@ -96,6 +98,10 @@ def notify_user_if_just_decided notify_user if amount_was.nil? and amount end + def touch_decided_at_if_decided + self.decided_at = Time.now if amount_changed? && decided? + end + def coin_amount amount.to_f / COIN if amount end diff --git a/db/migrate/20140620123610_add_decided_at_to_tips.rb b/db/migrate/20140620123610_add_decided_at_to_tips.rb new file mode 100644 index 00000000..c9ae29ed --- /dev/null +++ b/db/migrate/20140620123610_add_decided_at_to_tips.rb @@ -0,0 +1,5 @@ +class AddDecidedAtToTips < ActiveRecord::Migration + def change + add_column :tips, :decided_at, :timestamp + end +end \ No newline at end of file diff --git a/db/migrate/20140620124628_update_decided_at_for_existing_tips.rb b/db/migrate/20140620124628_update_decided_at_for_existing_tips.rb new file mode 100644 index 00000000..17ab997f --- /dev/null +++ b/db/migrate/20140620124628_update_decided_at_for_existing_tips.rb @@ -0,0 +1,7 @@ +class UpdateDecidedAtForExistingTips < ActiveRecord::Migration + def up + Tip.where.not(amount: nil).find_each do |tip| + tip.update decided_at: tip.created_at + end + end +end diff --git a/db/schema.rb b/db/schema.rb index 899564e0..c66608a3 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -11,7 +11,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 20140616060504) do +ActiveRecord::Schema.define(version: 20140620124628) do create_table "cold_storage_transfers", force: true do |t| t.integer "project_id" @@ -179,6 +179,7 @@ add_index "tipping_policies_texts", ["user_id"], name: "index_tipping_policies_texts_on_user_id" create_table "tips", force: true do |t| + t.integer "user_id" t.integer "amount", limit: 8 t.integer "distribution_id" t.datetime "created_at" @@ -186,11 +187,11 @@ t.string "commit" t.integer "project_id" t.datetime "refunded_at" - t.string "commit_message" + t.text "commit_message" t.string "comment" t.integer "reason_id" t.string "reason_type" - t.integer "user_id" + t.datetime "decided_at" end add_index "tips", ["distribution_id"], name: "index_tips_on_distribution_id" @@ -199,6 +200,7 @@ add_index "tips", ["user_id"], name: "index_tips_on_user_id" create_table "users", force: true do |t| + t.string "email", default: "", null: false t.string "encrypted_password", default: "", null: false t.string "reset_password_token" t.datetime "reset_password_sent_at" @@ -210,6 +212,7 @@ t.string "last_sign_in_ip" t.datetime "created_at" t.datetime "updated_at" + t.string "nickname" t.string "name" t.string "image" t.string "bitcoin_address" @@ -222,10 +225,9 @@ t.datetime "confirmed_at" t.datetime "confirmation_sent_at" t.string "unconfirmed_email" - t.string "email" - t.string "nickname" end + add_index "users", ["email"], name: "index_users_on_email", unique: true add_index "users", ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true end