-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathplugin.rb
86 lines (73 loc) · 2.86 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
# frozen_string_literal: true
# name: discourse-cakeday
# about: Show a birthday cake beside the user's name on their birthday and/or on the date they joined Discourse.
# version: 0.3
# authors: Alan Tan
# url: https://github.com/discourse/discourse-cakeday
register_asset "stylesheets/cakeday.scss"
register_asset "stylesheets/emoji-images.scss"
register_svg_icon "cake-candles" if respond_to?(:register_svg_icon)
enabled_site_setting :cakeday_enabled
after_initialize do
module ::DiscourseCakeday
PLUGIN_NAME = "discourse-cakeday"
class Engine < ::Rails::Engine
engine_name PLUGIN_NAME
isolate_namespace DiscourseCakeday
end
end
::DiscourseCakeday::Engine.routes.draw do
get "birthdays" => "birthdays#index"
get "birthdays/:filter" => "birthdays#index"
get "anniversaries" => "anniversaries#index"
get "anniversaries/:filter" => "anniversaries#index"
end
Discourse::Application.routes.append { mount ::DiscourseCakeday::Engine, at: "/cakeday" }
require_relative "app/jobs/onceoff/fix_invalid_date_of_birth"
require_relative "app/jobs/onceoff/migrate_date_of_birth_to_users_table"
require_relative "app/serializers/discourse_cakeday/cakeday_user_serializer"
require_relative "app/controllers/discourse_cakeday/cakeday_controller"
require_relative "app/controllers/discourse_cakeday/anniversaries_controller"
require_relative "app/controllers/discourse_cakeday/birthdays_controller"
# overwrite the user and user_card serializers to show
# the cakes on the user card and on the user profile pages
%i[user user_card].each do |serializer|
add_to_serializer(
serializer,
:cakedate,
include_condition: -> { scope.user.present? && object.user_option&.hide_profile != true },
) do
timezone = scope.user.user_option&.timezone.presence || "UTC"
object.created_at.in_time_zone(timezone).strftime("%Y-%m-%d")
end
add_to_serializer(
serializer,
:birthdate,
include_condition: -> do
SiteSetting.cakeday_birthday_enabled && scope.user.present? &&
object.user_option&.hide_profile != true
end,
) { object.date_of_birth }
end
# overwrite the post serializer to show the cakes next to the
# username in the posts stream
add_to_serializer(
:post,
:user_cakedate,
include_condition: -> do
scope.user.present? && object.user&.created_at.present? &&
object.user.user_option&.hide_profile != true
end,
) do
timezone = scope.user.user_option&.timezone.presence || "UTC"
object.user.created_at.in_time_zone(timezone).strftime("%Y-%m-%d")
end
add_to_serializer(
:post,
:user_birthdate,
include_condition: -> do
SiteSetting.cakeday_birthday_enabled && scope.user.present? &&
object.user&.date_of_birth.present? && object.user.user_option&.hide_profile != true
end,
) { object.user.date_of_birth }
end