user upgrades: add index action.

This commit is contained in:
evazion
2020-12-25 01:21:54 -06:00
parent 2d50ba6fd5
commit e030a07816
5 changed files with 98 additions and 1 deletions

View File

@@ -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)

View File

@@ -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

View File

@@ -0,0 +1,38 @@
<div id="c-user-upgrades">
<div id="a-index">
<%= 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) %>
</div>
</div>

View File

@@ -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

View File

@@ -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