mirror of
https://github.com/discourse/discourse.git
synced 2025-03-26 11:15:35 +08:00
Clean up valid order names
Add a sortable mappings list to match other endpoints and so that you don't have to use database column names. Example: 'created' => 'created_at' Also cleaned up some of the logic since a lot of it got moved into the SORTABLE_MAPPING hash.
This commit is contained in:
parent
e9d5c3265c
commit
0e6cb752da
@ -10,6 +10,20 @@ class AdminUserIndexQuery
|
|||||||
|
|
||||||
attr_reader :params, :trust_levels
|
attr_reader :params, :trust_levels
|
||||||
|
|
||||||
|
SORTABLE_MAPPING = {
|
||||||
|
'created' => 'created_at',
|
||||||
|
'last_emailed' => "COALESCE(last_emailed_at, to_date('1970-01-01', 'YYYY-MM-DD'))",
|
||||||
|
'seen' => "COALESCE(last_seen_at, to_date('1970-01-01', 'YYYY-MM-DD'))",
|
||||||
|
'username' => 'username',
|
||||||
|
'email' => 'email',
|
||||||
|
'trust_level' => 'trust_level',
|
||||||
|
'days_visited' => 'user_stats.days_visited',
|
||||||
|
'posts_read' => 'user_stats.posts_read_count',
|
||||||
|
'topics_viewed' => 'user_stats.topics_entered',
|
||||||
|
'posts' => 'user_stats.post_count',
|
||||||
|
'read_time' => 'user_stats.time_read'
|
||||||
|
}
|
||||||
|
|
||||||
def find_users(limit=100)
|
def find_users(limit=100)
|
||||||
find_users_query.limit(limit)
|
find_users_query.limit(limit)
|
||||||
end
|
end
|
||||||
@ -18,42 +32,23 @@ class AdminUserIndexQuery
|
|||||||
find_users_query.count
|
find_users_query.count
|
||||||
end
|
end
|
||||||
|
|
||||||
def self.orderable_user_columns
|
|
||||||
%w(created_at trust_level last_emailed_at last_seen_at username email)
|
|
||||||
end
|
|
||||||
|
|
||||||
def self.orderable_stat_columns
|
|
||||||
%w(days_visited posts_read_count topics_entered post_count time_read)
|
|
||||||
end
|
|
||||||
|
|
||||||
def custom_direction
|
def custom_direction
|
||||||
asc = params[:ascending]
|
asc = params[:ascending]
|
||||||
asc.present? && asc ? "ASC" : "DESC"
|
asc.present? && asc ? "ASC" : "DESC"
|
||||||
end
|
end
|
||||||
|
|
||||||
def pg_coalesce(column)
|
|
||||||
"COALESCE(#{column}, to_date('1970-01-01', 'YYYY-MM-DD'))"
|
|
||||||
end
|
|
||||||
|
|
||||||
def initialize_query_with_order(klass)
|
def initialize_query_with_order(klass)
|
||||||
order = []
|
order = []
|
||||||
|
|
||||||
custom_order = params[:order]
|
custom_order = params[:order]
|
||||||
if custom_order.present? &&
|
if custom_order.present? &&
|
||||||
without_dir = custom_order.downcase.sub(/ (asc|desc)$/, '')
|
without_dir = SORTABLE_MAPPING[custom_order.downcase.sub(/ (asc|desc)$/, '')]
|
||||||
if AdminUserIndexQuery.orderable_user_columns.include?(without_dir)
|
order << "#{without_dir} #{custom_direction}"
|
||||||
without_dir = (without_dir == "last_seen_at") ? pg_coalesce("last_seen_at") : without_dir
|
|
||||||
without_dir = (without_dir == "last_emailed_at") ? pg_coalesce("last_emailed_at") : without_dir
|
|
||||||
order << "#{without_dir} #{custom_direction}"
|
|
||||||
end
|
|
||||||
if AdminUserIndexQuery.orderable_stat_columns.include?(without_dir)
|
|
||||||
order << "user_stats.#{without_dir} #{custom_direction}"
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
if !custom_order.present?
|
if !custom_order.present?
|
||||||
if params[:query] == "active"
|
if params[:query] == "active"
|
||||||
order << "#{pg_coalesce("last_seen_at")} DESC"
|
order << "COALESCE(last_seen_at, to_date('1970-01-01', 'YYYY-MM-DD')) DESC"
|
||||||
else
|
else
|
||||||
order << "users.created_at DESC"
|
order << "users.created_at DESC"
|
||||||
end
|
end
|
||||||
|
@ -33,12 +33,12 @@ describe AdminUserIndexQuery do
|
|||||||
end
|
end
|
||||||
|
|
||||||
it "allows custom ordering for stats wtih default direction" do
|
it "allows custom ordering for stats wtih default direction" do
|
||||||
query = ::AdminUserIndexQuery.new({ order: "topics_entered" })
|
query = ::AdminUserIndexQuery.new({ order: "topics_viewed" })
|
||||||
expect(query.find_users_query.to_sql).to match("topics_entered DESC")
|
expect(query.find_users_query.to_sql).to match("topics_entered DESC")
|
||||||
end
|
end
|
||||||
|
|
||||||
it "allows custom ordering and direction for stats" do
|
it "allows custom ordering and direction for stats" do
|
||||||
query = ::AdminUserIndexQuery.new({ order: "topics_entered", ascending: true })
|
query = ::AdminUserIndexQuery.new({ order: "topics_viewed", ascending: true })
|
||||||
expect(query.find_users_query.to_sql).to match("topics_entered ASC")
|
expect(query.find_users_query.to_sql).to match("topics_entered ASC")
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
@ -91,7 +91,7 @@ describe AdminUserIndexQuery do
|
|||||||
end
|
end
|
||||||
|
|
||||||
it "shows nil values first with asc" do
|
it "shows nil values first with asc" do
|
||||||
users = ::AdminUserIndexQuery.new({ order: "last_emailed_at", ascending: true }).find_users
|
users = ::AdminUserIndexQuery.new({ order: "last_emailed", ascending: true }).find_users
|
||||||
|
|
||||||
expect(users.count).to eq(2)
|
expect(users.count).to eq(2)
|
||||||
expect(users.first.username).to eq("system")
|
expect(users.first.username).to eq("system")
|
||||||
@ -99,7 +99,7 @@ describe AdminUserIndexQuery do
|
|||||||
end
|
end
|
||||||
|
|
||||||
it "shows nil values last with desc" do
|
it "shows nil values last with desc" do
|
||||||
users = ::AdminUserIndexQuery.new({ order: "last_emailed_at"}).find_users
|
users = ::AdminUserIndexQuery.new({ order: "last_emailed"}).find_users
|
||||||
|
|
||||||
expect(users.count).to eq(2)
|
expect(users.count).to eq(2)
|
||||||
expect(users.first.last_emailed_at).to_not eq(nil)
|
expect(users.first.last_emailed_at).to_not eq(nil)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user