Skip to content
Browse files

Convert Post type constants to Enum

  • Loading branch information...
1 parent 897d48b commit c1e40f5d19c5eea2b6a78605e4a7e5119c647ebc @eviltrout eviltrout committed
View
11 app/models/post.rb
@@ -16,7 +16,6 @@ class Post < ActiveRecord::Base
acts_as_paranoid
after_recover :update_flagged_posts_count
- after_destroy :update_flagged_posts_count
belongs_to :user
belongs_to :topic, counter_cache: :posts_count
@@ -38,10 +37,6 @@ class Post < ActiveRecord::Base
SHORT_POST_CHARS = 1200
- # Post Types
- REGULAR = 1
- MODERATOR_ACTION = 2
-
scope :by_newest, order('created_at desc, id desc')
scope :with_user, includes(:user)
@@ -49,6 +44,10 @@ def self.hidden_reasons
@hidden_reasons ||= Enum.new(:flag_threshold_reached, :flag_threshold_reached_again)
end
+ def self.types
+ @types ||= Enum.new(:regular, :moderator_action)
+ end
+
def raw_quality
sentinel = TextSentinel.new(raw, min_entropy: SiteSetting.body_min_entropy)
if sentinel.valid?
@@ -151,12 +150,14 @@ def delete_by(deleted_by)
Post.transaction do
self.destroy
Topic.reset_highest(topic_id)
+ update_flagged_posts_count
end
elsif deleted_by.id == user_id
# As the poster, make a revision that says deleted.
Post.transaction do
revise(deleted_by, I18n.t('js.post.deleted_by_author'), force_new_version: true)
update_column(:user_deleted, true)
+ update_flagged_posts_count
end
end
end
View
2 app/models/topic.rb
@@ -320,7 +320,7 @@ def changed_to_category(cat)
def add_moderator_post(user, text, opts={})
new_post = nil
Topic.transaction do
- new_post = posts.create(user: user, raw: text, post_type: Post::MODERATOR_ACTION, no_bump: opts[:bump].blank?)
+ new_post = posts.create(user: user, raw: text, post_type: Post.types[:moderator_action], no_bump: opts[:bump].blank?)
increment!(:moderator_posts_count)
new_post
end
View
2 app/serializers/site_serializer.rb
@@ -15,7 +15,7 @@ def default_archetype
end
def post_types
- {regular: Post::REGULAR, moderator_action: Post::MODERATOR_ACTION}
+ Post.types
end
end
View
2 spec/fabricators/post_fabricator.rb
@@ -16,7 +16,7 @@
Fabricator(:moderator_post, from: :post) do
user
topic {|attrs| Fabricate(:topic, user: attrs[:user] ) }
- post_type Post::MODERATOR_ACTION
+ post_type Post.types[:moderator_action]
raw "Hello world"
end
View
2 spec/models/post_spec.rb
@@ -579,7 +579,7 @@
end
it 'is of the regular post type' do
- post.post_type.should == Post::REGULAR
+ post.post_type.should == Post.types[:regular]
end
it 'has no versions' do
View
2 spec/models/topic_spec.rb
@@ -514,7 +514,7 @@
end
it 'has the moderator action type' do
- @mod_post.post_type.should == Post::MODERATOR_ACTION
+ @mod_post.post_type.should == Post.types[:moderator_action]
end
it 'increases the moderator_posts count' do

0 comments on commit c1e40f5

Please sign in to comment.
Something went wrong with that request. Please try again.