Skip to content

Commit 0c99dea

Browse files
committed
introduce Enum
1 parent 0c8c41b commit 0c99dea

45 files changed

Lines changed: 242 additions & 216 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

app/controllers/admin/flags_controller.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ def index
2121
limit 100
2222
"
2323

24-
sql.where2 "post_action_type_id in (:flag_types)", flag_types: PostActionType.FlagTypes
24+
sql.where2 "post_action_type_id in (:flag_types)", flag_types: PostActionType.flag_types.values
2525

2626

2727
# it may make sense to add a view that shows flags on deleted posts,
@@ -62,7 +62,7 @@ def index
6262
from post_actions a
6363
/*where*/
6464
"
65-
sql.where("post_action_type_id in (:flag_types)", flag_types: PostActionType.FlagTypes)
65+
sql.where("post_action_type_id in (:flag_types)", flag_types: PostActionType.flag_types.values)
6666
sql.where("post_id in (:posts)", posts: posts.map{|p| p["id"].to_i})
6767

6868
if params[:filter] == 'old'

app/controllers/post_actions_controller.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ class PostActionsController < ApplicationController
88
def create
99
id = params[:post_action_type_id].to_i
1010
if action = PostActionType.where(id: id).first
11-
guardian.ensure_post_can_act!(@post, PostActionType.Types.invert[id])
11+
guardian.ensure_post_can_act!(@post, PostActionType.types[id])
1212

1313
post_action = PostAction.act(current_user, @post, action.id, params[:message])
1414

app/controllers/posts_controller.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -134,9 +134,9 @@ def bookmark
134134
post = find_post_from_params
135135
if current_user
136136
if params[:bookmarked] == "true"
137-
PostAction.act(current_user, post, PostActionType.Types[:bookmark])
137+
PostAction.act(current_user, post, PostActionType.types[:bookmark])
138138
else
139-
PostAction.remove_act(current_user, post, PostActionType.Types[:bookmark])
139+
PostAction.remove_act(current_user, post, PostActionType.types[:bookmark])
140140
end
141141
end
142142
render :nothing => true

app/mailers/user_notifications.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ def notification_template(user, opts)
6464
return unless @post.present?
6565

6666
username = @notification.data_hash[:display_username]
67-
notification_type = Notification.InvertedTypes[opts[:notification].notification_type].to_s
67+
notification_type = Notification.types[opts[:notification].notification_type].to_s
6868

6969
email_opts = {
7070
topic_title: @notification.data_hash[:topic_title],

app/models/invite.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ def redeem
7272
end
7373

7474
# Notify the invitee
75-
invited_by.notifications.create(notification_type: Notification.Types[:invitee_accepted],
75+
invited_by.notifications.create(notification_type: Notification.types[:invitee_accepted],
7676
data: { display_username: result.username }.to_json)
7777

7878
else

app/models/notification.rb

Lines changed: 10 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
class Notification < ActiveRecord::Base
1+
require_dependency 'enum'
22

3+
class Notification < ActiveRecord::Base
34
belongs_to :user
45
belongs_to :topic
56

@@ -9,21 +10,11 @@ class Notification < ActiveRecord::Base
910
scope :unread, lambda { where(read: false) }
1011
scope :recent, lambda { order('created_at desc').limit(10) }
1112

12-
def self.Types
13-
{:mentioned => 1,
14-
:replied => 2,
15-
:quoted => 3,
16-
:edited => 4,
17-
:liked => 5,
18-
:private_message => 6,
19-
:invited_to_private_message => 7,
20-
:invitee_accepted => 8,
21-
:posted => 9,
22-
:moved_post => 10}
23-
end
24-
25-
def self.InvertedTypes
26-
@inverted_types ||= Notification.Types.invert
13+
def self.types
14+
@types ||= Enum.new(
15+
:mentioned, :replied, :quoted, :edited, :liked, :private_message,
16+
:invited_to_private_message, :invitee_accepted, :posted, :moved_post
17+
)
2718
end
2819

2920
def self.mark_posts_read(user, topic_id, post_numbers)
@@ -35,8 +26,8 @@ def self.interesting_after(min_date)
3526
.includes(:topic)
3627
.unread
3728
.limit(20)
38-
.order("CASE WHEN notification_type = #{Notification.Types[:replied]} THEN 1
39-
WHEN notification_type = #{Notification.Types[:mentioned]} THEN 2
29+
.order("CASE WHEN notification_type = #{Notification.types[:replied]} THEN 1
30+
WHEN notification_type = #{Notification.types[:mentioned]} THEN 2
4031
ELSE 3
4132
END, created_at DESC").to_a
4233

@@ -69,7 +60,7 @@ def data_hash
6960

7061
def text_description
7162
link = block_given? ? yield : ""
72-
I18n.t("notification_types.#{Notification.InvertedTypes[notification_type]}", data_hash.merge(link: link))
63+
I18n.t("notification_types.#{Notification.types[notification_type]}", data_hash.merge(link: link))
7364
end
7465

7566
def url

app/models/post.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -279,7 +279,7 @@ def all_versions
279279
end
280280

281281
def is_flagged?
282-
post_actions.where(post_action_type_id: PostActionType.FlagTypes, deleted_at: nil).count != 0
282+
post_actions.where(post_action_type_id: PostActionType.flag_types.values, deleted_at: nil).count != 0
283283
end
284284

285285
def unhide!

app/models/post_action.rb

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ class AlreadyFlagged < StandardError; end
2020

2121
def self.update_flagged_posts_count
2222
posts_flagged_count = PostAction.joins(post: :topic)
23-
.where('post_actions.post_action_type_id' => PostActionType.FlagTypes,
23+
.where('post_actions.post_action_type_id' => PostActionType.flag_types.values,
2424
'posts.deleted_at' => nil,
2525
'topics.deleted_at' => nil).count('DISTINCT posts.id')
2626

@@ -55,13 +55,12 @@ def self.clear_flags!(post, moderator_id, action_type_id = nil)
5555
actions = if action_type_id
5656
[action_type_id]
5757
else
58-
moderator_id == -1 ? PostActionType.AutoActionFlagTypes : PostActionType.FlagTypes
58+
moderator_id == -1 ? PostActionType.auto_action_flag_types.values : PostActionType.flag_types.values
5959
end
6060

6161
PostAction.update_all({ deleted_at: Time.now, deleted_by: moderator_id }, { post_id: post.id, post_action_type_id: actions })
6262

63-
r = PostActionType.Types.invert
64-
f = actions.map { |t| ["#{r[t]}_count", 0] }
63+
f = actions.map{|t| ["#{PostActionType.types[t]}_count", 0]}
6564

6665
Post.with_deleted.update_all(Hash[*f.flatten], id: post.id)
6766

@@ -87,15 +86,15 @@ def self.remove_act(user, post, post_action_type_id)
8786
end
8887

8988
def is_bookmark?
90-
post_action_type_id == PostActionType.Types[:bookmark]
89+
post_action_type_id == PostActionType.types[:bookmark]
9190
end
9291

9392
def is_like?
94-
post_action_type_id == PostActionType.Types[:like]
93+
post_action_type_id == PostActionType.types[:like]
9594
end
9695

9796
def is_flag?
98-
PostActionType.FlagTypes.include?(post_action_type_id)
97+
PostActionType.flag_types.values.include?(post_action_type_id)
9998
end
10099

101100
# A custom rate limiter for this model
@@ -124,15 +123,15 @@ def message_quality
124123
end
125124

126125
before_create do
127-
raise AlreadyFlagged if is_flag? && PostAction.where(user_id: user_id,
128-
post_id: post_id,
129-
post_action_type_id: PostActionType.FlagTypes).exists?
126+
raise AlreadyFlagged if is_flag? and PostAction.where(user_id: user_id,
127+
post_id: post_id,
128+
post_action_type_id: PostActionType.flag_types.values).exists?
130129
end
131130

132131
after_save do
133132
# Update denormalized counts
134-
post_action_type = PostActionType.Types.invert[post_action_type_id]
135-
column = "#{post_action_type}_count"
133+
post_action_type = PostActionType.types[post_action_type_id]
134+
column = "#{post_action_type.to_s}_count"
136135
delta = deleted_at.nil? ? 1 : -1
137136

138137
# Voting also changes the sort_order
@@ -144,7 +143,7 @@ def message_quality
144143
Topic.update_all ["#{column} = #{column} + ?", delta], id: post.topic_id
145144

146145

147-
if PostActionType.FlagTypes.include?(post_action_type_id)
146+
if PostActionType.flag_types.values.include?(post_action_type_id)
148147
PostAction.update_flagged_posts_count
149148
end
150149

@@ -153,7 +152,7 @@ def message_quality
153152
flag_counts = exec_sql("SELECT SUM(CASE WHEN deleted_at IS NULL THEN 1 ELSE 0 END) AS new_flags,
154153
SUM(CASE WHEN deleted_at IS NOT NULL THEN 1 ELSE 0 END) AS old_flags
155154
FROM post_actions
156-
WHERE post_id = ? AND post_action_type_id IN (?)", post.id, PostActionType.AutoActionFlagTypes).first
155+
WHERE post_id = ? AND post_action_type_id IN (?)", post.id, PostActionType.auto_action_flag_types.values).first
157156
old_flags, new_flags = flag_counts['old_flags'].to_i, flag_counts['new_flags'].to_i
158157

159158
if new_flags >= SiteSetting.flags_required_to_hide_post

app/models/post_action_type.rb

Lines changed: 19 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,28 @@
1+
require_dependency 'enum'
2+
13
class PostActionType < ActiveRecord::Base
24
attr_accessible :id, :is_flag, :name_key, :icon
35

4-
def self.ordered
5-
order('position asc').all
6-
end
6+
class << self
7+
def ordered
8+
order('position asc').all
9+
end
710

8-
def self.Types
9-
{
10-
bookmark: 1,
11-
like: 2,
12-
off_topic: 3,
13-
inappropriate: 4,
14-
vote: 5,
15-
custom_flag: 6,
16-
spam: 8
17-
}
18-
end
11+
def types
12+
@types ||= Enum.new(:bookmark, :like, :off_topic, :inappropriate, :vote,
13+
:custom_flag, :spam)
14+
end
1915

20-
def self.is_flag?(sym)
21-
self.FlagTypes.include?(self.Types[sym])
22-
end
16+
def auto_action_flag_types
17+
@auto_action_flag_types ||= flag_types.except(:custom_flag)
18+
end
2319

24-
def self.AutoActionFlagTypes
25-
@auto_action_flag_types ||= [self.Types[:off_topic], self.Types[:spam], self.Types[:inappropriate]]
26-
end
20+
def flag_types
21+
@flag_types ||= types.only(:off_topic, :spam, :inappropriate, :custom_flag)
22+
end
2723

28-
def self.FlagTypes
29-
@flag_types ||= self.AutoActionFlagTypes + [self.Types[:custom_flag]]
24+
def is_flag?(sym)
25+
flag_types.valid?(sym)
26+
end
3027
end
3128
end

app/models/post_alert_observer.rb

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ def after_create_post_action(post_action)
3838
return if post.topic.private_message?
3939

4040
create_notification(post.user,
41-
Notification.Types[:liked],
41+
Notification.types[:liked],
4242
post,
4343
display_username: post_action.user.username,
4444
post_action_id: post_action.id)
@@ -52,14 +52,14 @@ def after_create_version(version)
5252
return if version.user_id == post.user_id
5353
return if post.topic.private_message?
5454

55-
create_notification(post.user, Notification.Types[:edited], post, display_username: version.user.username)
55+
create_notification(post.user, Notification.types[:edited], post, display_username: version.user.username)
5656
end
5757

5858
def after_create_post(post)
5959
if post.topic.private_message?
6060
# If it's a private message, notify the topic_allowed_users
61-
post.topic.topic_allowed_users.reject { |a| a.user_id == post.user_id }.each do |a|
62-
create_notification(a.user, Notification.Types[:private_message], post)
61+
post.topic.topic_allowed_users.reject{ |a| a.user_id == post.user_id }.each do |a|
62+
create_notification(a.user, Notification.types[:private_message], post)
6363
end
6464
else
6565
# If it's not a private message, notify the users
@@ -114,7 +114,7 @@ def extract_quoted_users(post)
114114
def notify_users(users, type, post)
115115
users = [users] unless users.is_a?(Array)
116116
users.each do |u|
117-
create_notification(u, Notification.Types[type], post)
117+
create_notification(u, Notification.types[type], post)
118118
end
119119
end
120120

@@ -133,7 +133,7 @@ def notify_post_users(post)
133133
exclude_user_ids << extract_quoted_users(post).map(&:id)
134134
exclude_user_ids.flatten!
135135
TopicUser.where(topic_id: post.topic_id, notification_level: TopicUser::NotificationLevel::WATCHING).includes(:user).each do |tu|
136-
create_notification(tu.user, Notification.Types[:posted], post) unless exclude_user_ids.include?(tu.user_id)
136+
create_notification(tu.user, Notification.types[:posted], post) unless exclude_user_ids.include?(tu.user_id)
137137
end
138138
end
139139
end

0 commit comments

Comments
 (0)