Skip to content

Commit

Permalink
DEV: Revert 3 commits for plugin API to add directory columns (#13423)
Browse files Browse the repository at this point in the history
  • Loading branch information
markvanlan committed Jun 17, 2021
1 parent c47f552 commit 95b5166
Show file tree
Hide file tree
Showing 27 changed files with 102 additions and 241 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ export default Component.extend({
chevronIcon: null,
columnIcon: null,
translated: false,
automatic: false,
onActiveRender: null,

toggleProperties() {
Expand All @@ -32,9 +31,6 @@ export default Component.extend({
},
didReceiveAttrs() {
this._super(...arguments);
if (!this.automatic && !this.translated) {
this.set("labelKey", this.field);
}
this.set("id", `table-header-toggle-${this.field.replace(/\s/g, "")}`);
this.toggleChevron();
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ export default Controller.extend(ModalFunctionality, {
.forEach((column, index) => {
column.setProperties({
position: column.automatic_position || index + 1,
enabled: column.type === "automatic",
enabled: column.automatic,
});
});
this.set("columns", resetColumns);
Expand Down
19 changes: 5 additions & 14 deletions app/assets/javascripts/discourse/app/controllers/users.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,22 +28,13 @@ export default Controller.extend({
this.set("nameInput", params.name);
this.set("order", params.order);

const userFieldColumns = this.columns.filter(
(c) => c.type === "user_field"
);
const userFieldIds = userFieldColumns.map((c) => c.user_field_id).join("|");

const pluginColumns = this.columns.filter((c) => c.type === "plugin");
const pluginColumnIds = pluginColumns.map((c) => c.id).join("|");
const custom_field_columns = this.columns.filter((c) => !c.automatic);
const user_field_ids = custom_field_columns
.map((c) => c.user_field_id)
.join("|");

return this.store
.find(
"directoryItem",
Object.assign(params, {
user_field_ids: userFieldIds,
plugin_column_ids: pluginColumnIds,
})
)
.find("directoryItem", Object.assign(params, { user_field_ids }))
.then((model) => {
const lastUpdatedAt = model.get("resultSetMeta.last_updated_at");
this.setProperties({
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { htmlSafe } from "@ember/template";
import { registerUnbound } from "discourse-common/lib/helpers";
import I18n from "I18n";

export default registerUnbound("mobile-directory-item-label", function (args) {
// Args should include key/values { item, column }

const count = args.item.get(args.column.name);
return htmlSafe(I18n.t(`directory.${args.column.name}`, { count }));
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { htmlSafe } from "@ember/template";
import { registerUnbound } from "discourse-common/lib/helpers";

export default registerUnbound(
"directory-item-user-field-value",
function (args) {
// Args should include key/values { item, column }

const value =
args.item.user && args.item.user.user_fields
? args.item.user.user_fields[args.column.user_field_id]
: null;
const content = value || "-";
return htmlSafe(`<span class='user-field-value'>${content}</span>`);
}
);
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { htmlSafe } from "@ember/template";
import { registerUnbound } from "discourse-common/lib/helpers";
import { number } from "discourse/lib/formatter";

export default registerUnbound("directory-item-value", function (args) {
// Args should include key/values { item, column }

return htmlSafe(
`<span class='number'>${number(args.item.get(args.column.name))}</span>`
);
});
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
<td>{{user-info user=item.user}}</td>
{{#each columns as |column|}}
<td>
{{#if (directory-column-is-user-field column=column)}}
{{directory-item-user-field-value item=item column=column}}
{{else}}
{{#if column.automatic}}
{{directory-item-value item=item column=column}}
{{else}}
{{directory-item-user-field-value item=item column=column}}
{{/if}}
</td>
{{/each}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
icon=column.icon
order=order
asc=asc
automatic=(directory-column-is-automatic column=column)
translated=column.user_field_id
onActiveRender=setActiveHeader
}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,11 @@
{{d-button action=(action "bulkClearAll") label="topics.bulk.clear_all"}}
</th>
{{/if}}
{{table-header-toggle order=order asc=asc field="username_lower" labelKey="username" class="username" automatic=true}}
{{table-header-toggle order=order asc=asc field="username_lower" labelKey="username" class="username"}}
<th class="group-owner"></th>
{{table-header-toggle order=order asc=asc field="added_at" labelKey="groups.member_added" automatic=true}}
{{table-header-toggle order=order asc=asc field="last_posted_at" labelKey="last_post" automatic=true}}
{{table-header-toggle order=order asc=asc field="last_seen_at" labelKey="last_seen" automatic=true}}
{{table-header-toggle order=order asc=asc field="added_at" labelKey="groups.member_added"}}
{{table-header-toggle order=order asc=asc field="last_posted_at" labelKey="last_post"}}
{{table-header-toggle order=order asc=asc field="last_seen_at" labelKey="last_seen"}}
<th>
{{#if isBulk}}
{{group-member-dropdown
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
{{#load-more selector=".group-members tr" action=(action "loadMore")}}
<table class="group-members">
<thead>
{{table-header-toggle order=order asc=asc field="username_lower" labelKey="username" automatic=true}}
{{table-header-toggle order=order asc=asc field="requested_at" labelKey="groups.member_requested" automatic=true}}
{{table-header-toggle order=order asc=asc field="username_lower" labelKey="username"}}
{{table-header-toggle order=order asc=asc field="requested_at" labelKey="groups.member_requested"}}
<th>{{i18n "groups.requests.reason"}}</th>
<th></th>
<th></th>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{{user-info user=item.user}}

{{#each columns as |column|}}
{{#if (directory-column-is-automatic column=column)}}
{{#if column.automatic}}
<div class="user-stat">
<span class="value">
{{directory-item-value item=item column=column}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,10 @@
<div class="left-content">
<label class="column-name">
{{input type="checkbox" checked=column.enabled}}
{{#if (directory-column-is-automatic column=column)}}
{{#if column.automatic}}
{{directory-table-header-title field=column.name labelKey=labelKey icon=column.icon}}
{{else if (directory-column-is-user-field column=column)}}
{{directory-table-header-title field=column.user_field.name translated=true}}
{{else}}
{{directory-table-header-title field=(i18n column.name) translated=true}}
{{directory-table-header-title field=column.user_field.name translated=true}}
{{/if}}
</label>
</div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -944,7 +944,7 @@ export function applyDefaultHandlers(pretender) {
{
id: 1,
name: "likes_received",
type: "automatic",
automatic: true,
enabled: true,
automatic_position: 1,
position: 1,
Expand All @@ -954,7 +954,7 @@ export function applyDefaultHandlers(pretender) {
{
id: 2,
name: "likes_given",
type: "automatic",
automatic: true,
enabled: true,
automatic_position: 2,
position: 2,
Expand All @@ -964,7 +964,7 @@ export function applyDefaultHandlers(pretender) {
{
id: 3,
name: "topic_count",
type: "automatic",
automatic: true,
enabled: true,
automatic_position: 3,
position: 3,
Expand All @@ -974,7 +974,7 @@ export function applyDefaultHandlers(pretender) {
{
id: 4,
name: "post_count",
type: "automatic",
automatic: true,
enabled: true,
automatic_position: 4,
position: 4,
Expand All @@ -984,7 +984,7 @@ export function applyDefaultHandlers(pretender) {
{
id: 5,
name: "topics_entered",
type: "automatic",
automatic: true,
enabled: true,
automatic_position: 5,
position: 5,
Expand All @@ -994,7 +994,7 @@ export function applyDefaultHandlers(pretender) {
{
id: 6,
name: "posts_read",
type: "automatic",
automatic: true,
enabled: true,
automatic_position: 6,
position: 6,
Expand All @@ -1004,7 +1004,7 @@ export function applyDefaultHandlers(pretender) {
{
id: 7,
name: "days_visited",
type: "automatic",
automatic: true,
enabled: true,
automatic_position: 7,
position: 7,
Expand All @@ -1014,7 +1014,7 @@ export function applyDefaultHandlers(pretender) {
{
id: 9,
name: null,
type: "user_field",
automatic: false,
enabled: false,
automatic_position: null,
position: 8,
Expand Down
2 changes: 1 addition & 1 deletion app/assets/javascripts/discourse/tests/setup-tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@ function setupTestsCommon(application, container, config) {
PreloadStore.store(
"directoryColumns",
JSON.parse(
'[{"name":"likes_given","type":"automatic","icon":"heart","user_field_id":null},{"name":"posts_read","type":"automatic","icon":null,"user_field_id":null},{"name":"likes_received","type":"automatic","icon":"heart","user_field_id":null},{"name":"topic_count","type":"automatic","icon":null,"user_field_id":null},{"name":"post_count","type":"automatic","icon":null,"user_field_id":null},{"name":"topics_entered","type":"automatic","icon":null,"user_field_id":null},{"name":"days_visited","type":"automatic","icon":null,"user_field_id":null},{"name":"Favorite Color","type":"user_field","icon":null,"user_field_id":3}]'
'[{"name":"likes_given","automatic":true,"icon":"heart","user_field_id":null},{"name":"posts_read","automatic":true,"icon":null,"user_field_id":null},{"name":"likes_received","automatic":true,"icon":"heart","user_field_id":null},{"name":"topic_count","automatic":true,"icon":null,"user_field_id":null},{"name":"post_count","automatic":true,"icon":null,"user_field_id":null},{"name":"topics_entered","automatic":true,"icon":null,"user_field_id":null},{"name":"days_visited","automatic":true,"icon":null,"user_field_id":null},{"name":"Favorite Color","automatic":false,"icon":null,"user_field_id":3}]'
)
);

Expand Down
8 changes: 3 additions & 5 deletions app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -618,18 +618,16 @@ def preload_current_user_data
end

def directory_columns_json
types = DirectoryColumn.types
DirectoryColumn
.left_joins(:user_field)
.where(enabled: true)
.order(:position)
.pluck('directory_columns.id',
'directory_columns.name',
'directory_columns.type',
.pluck('directory_columns.name',
'directory_columns.automatic',
'directory_columns.icon',
'user_fields.id',
'user_fields.name')
.map { |column| { id: column[0], name: column[1] || column[5], type: types.key(column[2]), icon: column[3], user_field_id: column[4] } }
.map { |column| { name: column[0] || column[4], automatic: column[1], icon: column[2], user_field_id: column[3] } }
.to_json
end

Expand Down
2 changes: 1 addition & 1 deletion app/controllers/directory_columns_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ def ensure_user_fields_have_columns
new_directory_column_attrs.push({
user_field_id: user_field.id,
enabled: false,
type: DirectoryColumn.types[:user_field],
automatic: false,
position: next_position
})

Expand Down
9 changes: 2 additions & 7 deletions app/controllers/directory_items_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,13 @@ def index
result = result.references(:user).where.not(users: { username: params[:exclude_usernames].split(",") })
end

order = params[:order] || DirectoryColumn.automatic_column_names.first
order = params[:order] || DirectoryItem.headings.first
dir = params[:asc] ? 'ASC' : 'DESC'
if DirectoryColumn.active_column_names.include?(order.to_sym)
if DirectoryItem.headings.include?(order.to_sym)
result = result.order("directory_items.#{order} #{dir}, directory_items.id")
elsif params[:order] === 'username'
result = result.order("users.#{order} #{dir}, directory_items.id")
else
# Ordering by user field value
user_field = UserField.find_by(name: params[:order])
if user_field
result = result
Expand Down Expand Up @@ -99,10 +98,6 @@ def index
serializer_opts[:user_field_ids] = params[:user_field_ids]&.split("|")&.map(&:to_i)
end

if params[:plugin_column_ids]
serializer_opts[:plugin_column_ids] = params[:plugin_column_ids]&.split("|")&.map(&:to_i)
end

serialized = serialize_data(result, DirectoryItemSerializer, serializer_opts)
render_json_dump(directory_items: serialized,
meta: {
Expand Down
32 changes: 0 additions & 32 deletions app/models/directory_column.rb
Original file line number Diff line number Diff line change
@@ -1,37 +1,5 @@
# frozen_string_literal: true

class DirectoryColumn < ActiveRecord::Base
self.inheritance_column = nil

def self.automatic_column_names
@automatic_column_names ||= [:likes_received,
:likes_given,
:topics_entered,
:topic_count,
:post_count,
:posts_read,
:days_visited]
end

def self.active_column_names
DirectoryColumn.where(type: [:automatic, :plugin]).where(enabled: true).pluck(:name).map(&:to_sym)
end

@@plugin_directory_columns = []

enum type: { automatic: 0, user_field: 1, plugin: 2 }

belongs_to :user_field

def self.add_plugin_directory_column(name)
@@plugin_directory_columns << name
end

def self.plugin_directory_columns
@@plugin_directory_columns
end

def self.clear_plugin_directory_columns
@@plugin_directory_columns = []
end
end
Loading

0 comments on commit 95b5166

Please sign in to comment.