diff --git a/app/controllers/user_upgrades_controller.rb b/app/controllers/user_upgrades_controller.rb
index 62e4cee10..80c8fa303 100644
--- a/app/controllers/user_upgrades_controller.rb
+++ b/app/controllers/user_upgrades_controller.rb
@@ -15,6 +15,13 @@ class UserUpgradesController < ApplicationController
respond_with(@user_upgrade)
end
+ def index
+ @user_upgrades = authorize UserUpgrade.visible(CurrentUser.user).paginated_search(params, count_pages: true)
+ @user_upgrades = @user_upgrades.includes(:recipient, :purchaser) if request.format.html?
+
+ respond_with(@user_upgrades)
+ end
+
def show
@user_upgrade = authorize UserUpgrade.find(params[:id])
respond_with(@user_upgrade)
diff --git a/app/models/user_upgrade.rb b/app/models/user_upgrade.rb
index 41aa69c6e..acb2eb32b 100644
--- a/app/models/user_upgrade.rb
+++ b/app/models/user_upgrade.rb
@@ -14,6 +14,9 @@ class UserUpgrade < ApplicationRecord
complete: 20
}
+ scope :gifted, -> { where("recipient_id != purchaser_id") }
+ scope :self_upgrade, -> { where("recipient_id = purchaser_id") }
+
def self.enabled?
stripe_secret_key.present? && stripe_publishable_key.present? && stripe_webhook_secret.present?
end
@@ -89,6 +92,27 @@ class UserUpgrade < ApplicationRecord
recipient != purchaser
end
+ def self.visible(user)
+ if user.is_owner?
+ all
+ else
+ where(recipient: user).or(where(purchaser: user))
+ end
+ end
+
+ def self.search(params)
+ q = search_attributes(params, :id, :created_at, :updated_at, :upgrade_type, :status, :stripe_id, :recipient, :purchaser)
+
+ if params[:is_gifted].to_s.truthy?
+ q = q.gifted
+ elsif params[:is_gifted].to_s.falsy?
+ q = q.self_upgrade
+ end
+
+ q = q.apply_default_order(params)
+ q
+ end
+
concerning :UpgradeMethods do
def process_upgrade!(payment_status)
recipient.with_lock do
diff --git a/app/views/user_upgrades/index.html.erb b/app/views/user_upgrades/index.html.erb
new file mode 100644
index 000000000..611ea071e
--- /dev/null
+++ b/app/views/user_upgrades/index.html.erb
@@ -0,0 +1,38 @@
+
+
+ <%= search_form_for(user_upgrades_path) do |f| %>
+ <%= f.input :recipient_name, label: "Recipient", input_html: { value: params[:search][:recipient_name], data: { autocomplete: "user" } } %>
+ <%= f.input :purchaser_name, label: "Purchaser", input_html: { value: params[:search][:purchaser_name], data: { autocomplete: "user" } } %>
+ <%= f.input :upgrade_type, collection: UserUpgrade.upgrade_types, include_blank: true, selected: params[:search][:upgrade_type] %>
+ <%= f.input :status, collection: UserUpgrade.statuses, include_blank: true, selected: params[:search][:status] %>
+ <%= f.input :is_gifted, label: "Gifted?", as: :select, include_blank: true, selected: params[:search][:is_gifted] %>
+ <%= f.submit "Search" %>
+ <% end %>
+
+ <%= table_for @user_upgrades, class: "striped autofit" do |t| %>
+ <% t.column "Recipient" do |user_upgrade| %>
+ <%= link_to_user user_upgrade.recipient %>
+ <% end %>
+
+ <% t.column "Purchaser" do |user_upgrade| %>
+ <%= link_to_user user_upgrade.purchaser %>
+ <% end %>
+
+ <% t.column :upgrade_type do |user_upgrade| %>
+ <%= user_upgrade.upgrade_type.humanize %>
+ <% end %>
+
+ <% t.column "Gifted?" do |user_upgrade| %>
+ <%= "Yes" if user_upgrade.is_gift? %>
+ <% end %>
+
+ <% t.column :status %>
+
+ <% t.column "Updated" do |artist| %>
+ <%= time_ago_in_words_tagged(artist.updated_at) %>
+ <% end %>
+ <% end %>
+
+ <%= numbered_paginator(@user_upgrades) %>
+
+
diff --git a/config/routes.rb b/config/routes.rb
index 1f39cbf94..a20f927f7 100644
--- a/config/routes.rb
+++ b/config/routes.rb
@@ -254,7 +254,7 @@ Rails.application.routes.draw do
get :custom_style
end
end
- resources :user_upgrades, only: [:new, :create, :show]
+ resources :user_upgrades, only: [:new, :create, :show, :index]
resources :user_feedbacks, except: [:destroy]
resources :user_name_change_requests, only: [:new, :create, :show, :index]
resources :webhooks do
diff --git a/test/functional/user_upgrades_controller_test.rb b/test/functional/user_upgrades_controller_test.rb
index 4f466c86e..75ff4f2f1 100644
--- a/test/functional/user_upgrades_controller_test.rb
+++ b/test/functional/user_upgrades_controller_test.rb
@@ -45,6 +45,34 @@ class UserUpgradesControllerTest < ActionDispatch::IntegrationTest
end
end
+ context "index action" do
+ setup do
+ @self_upgrade = create(:self_gold_upgrade)
+ @gift_upgrade = create(:gift_gold_upgrade)
+ end
+
+ should "show the purchaser's upgrades to the purchaser" do
+ get_auth user_upgrades_path, @gift_upgrade.purchaser
+
+ assert_response :success
+ assert_select "#user-upgrade-#{@gift_upgrade.id}", count: 1
+ end
+
+ should "show the recipient's upgrades to the recipient" do
+ get_auth user_upgrades_path, @gift_upgrade.recipient
+
+ assert_response :success
+ assert_select "#user-upgrade-#{@gift_upgrade.id}", count: 1
+ end
+
+ should "not show upgrades to unrelated users" do
+ get_auth user_upgrades_path, create(:user)
+
+ assert_response :success
+ assert_select "#user-upgrade-#{@gift_upgrade.id}", count: 0
+ end
+ end
+
context "show action" do
context "for a completed upgrade" do
should "render for a self upgrade" do