-
Notifications
You must be signed in to change notification settings - Fork 3
/
plugin.rb
209 lines (177 loc) · 4.99 KB
/
plugin.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
# name: discourse-pavilion
# about: Pavilion customisations
# version: 0.1
# authors: Angus McLeod
# url: https://github.com/angusmcleod/discourse-pavilion
register_asset "stylesheets/common/pavilion.scss"
register_asset "stylesheets/mobile/pavilion.scss", :mobile
Discourse.filters.push(:work)
Discourse.anonymous_filters.push(:work)
after_initialize do
module ::PavilionHome
class Engine < ::Rails::Engine
engine_name "pavilion_home"
isolate_namespace PavilionHome
end
end
require 'homepage_constraint'
Discourse::Application.routes.prepend do
root to: "pavilion_home/page#index", constraints: HomePageConstraint.new("home")
get "/home" => "pavilion_home/page#index"
end
class HomepageUserSerializer < BasicUserSerializer
attributes :title,
:bio
def bio
object.user_profile.bio_processed
end
end
require_dependency 'topic_query'
class ::TopicQuery
def list_work
@options[:assigned] = @user.username
create_list(:work) do |result|
result.where("topics.id NOT IN (
SELECT topic_id FROM topic_tags
WHERE topic_tags.tag_id in (
SELECT id FROM tags
WHERE tags.name = 'done'
)
)")
end
end
end
require_dependency 'application_controller'
class PavilionHome::PageController < ApplicationController
def index
begin
json = {
members: ActiveModel::ArraySerializer.new(
Group.find_by(name: 'members').users,
each_serializer: HomepageUserSerializer
)
}
topic_list = nil
if current_user && current_user.staff?
topic_list = TopicQuery.new(current_user, per_page: 6).list_work
elsif (current_user && (home_category = current_user.home_category))
topic_list = TopicQuery.new(current_user,
category: home_category.id,
per_page: 6
).list_latest
end
if topic_list
json[:topic_list] = TopicListSerializer.new(topic_list, scope: Guardian.new(current_user)).as_json
end
render_json_dump(json)
rescue => e
puts e
end
end
end
add_to_serializer(:current_user, :homepage_id) { object.user_option.homepage_id }
module UserOptionExtension
def homepage
if homepage_id == 101
"home"
else
super
end
end
end
require_dependency 'user_option'
class ::UserOption
prepend UserOptionExtension
end
Group.register_custom_field_type('client_group', :boolean)
Group.preloaded_custom_fields << "client_group" if Group.respond_to? :preloaded_custom_fields
module ClientGroupModelExtension
def expire_cache
super
@client_groups = nil
end
end
require_dependency 'group'
class ::Group
prepend ClientGroupModelExtension
def client_group
if custom_fields['client_group'] != nil
custom_fields['client_group']
else
false
end
end
def self.client_groups
@client_groups ||= begin
Group.where("groups.id in (
SELECT group_id FROM group_custom_fields
WHERE name = 'client_group' AND
value::boolean IS TRUE
)")
end
end
end
require_dependency 'category'
class ::Category
def self.client_group_category(group_id)
Category.where("categories.id in (
SELECT category_id FROM category_groups
WHERE group_id = #{group_id}
AND permission_type = 1
)").first
end
end
module FeatureGroupUserExtension
def reload
@client_groups = nil
super
end
end
require_dependency 'user'
class ::User
prepend FeatureGroupUserExtension
def home_category
if client_groups.present?
Category.client_group_category(client_groups.pluck(:id).first)
end
end
def client_groups
Group.member_of(Group.client_groups, self)
end
end
module AdminGroupsControllerExtension
private def group_params
client_group = params.require(:group).permit(:client_group)[:client_group]
if client_group != nil
merge_params = {
custom_fields: { client_group: client_group }
}
super.merge(merge_params)
else
super
end
end
end
module GroupsControllerExtension
private def group_params(automatic: false)
client_group = params.require(:group).permit(:client_group)[:client_group]
if client_group != nil
merge_params = {
custom_fields: { client_group: client_group }
}
super.merge(merge_params)
else
super
end
end
end
require_dependency 'admin/groups_controller'
class ::Admin::GroupsController
prepend AdminGroupsControllerExtension
end
require_dependency 'groups_controller'
class ::GroupsController
prepend GroupsControllerExtension
end
add_to_serializer(:basic_group, :client_group) { object.client_group }
end