Skip to content

Commit

Permalink
create the User.admins and User.moderators scopes
Browse files Browse the repository at this point in the history
  • Loading branch information
goshacmd committed Mar 29, 2013
1 parent a0867bf commit bdfa9b0
Show file tree
Hide file tree
Showing 5 changed files with 10 additions and 7 deletions.
6 changes: 3 additions & 3 deletions app/models/admin_dashboard_data.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ def as_json
@json ||= {
reports: REPORTS.map { |type| Report.find(type) },
problems: [rails_env_check, host_names_check, gc_checks, sidekiq_check || clockwork_check, ram_check].compact,
admins: User.where(admin: true).count,
moderators: User.where(moderator: true).count
admins: User.admins.count,
moderators: User.moderators.count
}.merge(
SiteSetting.version_checks? ? {version_check: DiscourseUpdates.check_version} : {}
)
Expand Down Expand Up @@ -43,4 +43,4 @@ def clockwork_check
def ram_check
I18n.t('dashboard.memory_warning') if MemInfo.new.mem_total and MemInfo.new.mem_total < 1_000_000
end
end
end
2 changes: 1 addition & 1 deletion app/models/post_action.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ def self.update_flagged_posts_count
'topics.deleted_at' => nil).count('DISTINCT posts.id')

$redis.set('posts_flagged_count', posts_flagged_count)
admins = User.where(admin: true).select(:id).map {|u| u.id}
admins = User.admins.select(:id).map {|u| u.id}
MessageBus.publish('/flagged_counts', { total: posts_flagged_count }, { user_ids: admins })
end

Expand Down
3 changes: 3 additions & 0 deletions app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@ class User < ActiveRecord::Base
# This is just used to pass some information into the serializer
attr_accessor :notification_channel_position

scope :admins, ->{ where(admin: true) }
scope :moderators, ->{ where(moderator: true) }

module NewTopicDuration
ALWAYS = -1
LAST_VISIT = -2
Expand Down
4 changes: 2 additions & 2 deletions lib/admin_constraint.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ class AdminConstraint

def matches?(request)
return false unless request.session[:current_user_id].present?
User.where(id: request.session[:current_user_id].to_i).where(admin: true).exists?
User.admins.where(id: request.session[:current_user_id].to_i).exists?
end

end
end
2 changes: 1 addition & 1 deletion lib/system_message.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ def create(type, params = {})
# Either returns the system_username user or the first admin.
def self.system_user
user = User.where(username_lower: SiteSetting.system_username).first if SiteSetting.system_username.present?
user = User.where(admin: true).order(:id).first if user.blank?
user = User.admins.order(:id).first if user.blank?
user
end

Expand Down

0 comments on commit bdfa9b0

Please sign in to comment.