Convert Post type constants to Enum

This commit is contained in:
Robin Ward 2013-03-18 16:03:46 -04:00
parent 897d48b145
commit c1e40f5d19
6 changed files with 11 additions and 10 deletions

View file

@ -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 @@ class Post < ActiveRecord::Base
@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 @@ class Post < ActiveRecord::Base
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 file

@ -320,7 +320,7 @@ class Topic < ActiveRecord::Base
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 file

@ -15,7 +15,7 @@ class SiteSerializer < ApplicationSerializer
end
def post_types
{regular: Post::REGULAR, moderator_action: Post::MODERATOR_ACTION}
Post.types
end
end

View file

@ -16,7 +16,7 @@ end
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 file

@ -579,7 +579,7 @@ describe Post do
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 file

@ -514,7 +514,7 @@ describe Topic do
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