11class Subscription < ApplicationRecord
2- self . inheritance_column = 'sti_type'
3-
42 include CommunityRelated
53 include Timestamped
64
5+ self . inheritance_column = 'sti_type'
6+
7+ BASE_TYPES = [ 'all' , 'tag' , 'user' , 'interesting' , 'category' ] . freeze
8+ MOD_ONLY_TYPES = [ 'moderators' ] . freeze
9+ QUALIFIED_TYPES = [ 'category' , 'tag' , 'user' ] . freeze
10+
711 belongs_to :user
812
9- validates :type , presence : true , inclusion : [ 'all' , 'tag' , 'user' , 'interesting' , 'category' , 'moderators' ]
13+ validates :type , presence : true , inclusion : BASE_TYPES + MOD_ONLY_TYPES
1014 validates :frequency , numericality : { minimum : 1 , maximum : 90 }
1115
1216 validate :qualifier_presence
@@ -15,8 +19,7 @@ class Subscription < ApplicationRecord
1519 # @param user [User] user to check type access for
1620 # @return [Array<String>] list of available types
1721 def self . types_accessible_to ( user )
18- base_types = [ 'all' , 'tag' , 'user' , 'interesting' , 'category' ]
19- user . at_least_moderator? ? base_types << 'moderators' : base_types
22+ user . at_least_moderator? ? BASE_TYPES + MOD_ONLY_TYPES : BASE_TYPES
2023 end
2124
2225 def questions
@@ -44,10 +47,17 @@ def questions
4447 end &.order ( created_at : :desc ) &.limit ( 25 )
4548 end
4649
50+ # Is the subscription's type qualified (bound to an entity)?
51+ # @param type [String] type to check
52+ # @return [Boolean] check result
53+ def qualified?
54+ QUALIFIED_TYPES . include? ( type )
55+ end
56+
4757 private
4858
4959 def qualifier_presence
50- return unless [ 'tag' , 'user' , 'category' ] . include? type
60+ return unless qualified?
5161
5262 if type == 'tag' && ( qualifier . blank? || Tag . find_by ( name : qualifier ) . nil? )
5363 errors . add ( :qualifier , 'must provide a valid tag name for tag subscriptions' )
0 commit comments