Skip to content

Commit

Permalink
move alias levels into enums
Browse files Browse the repository at this point in the history
  • Loading branch information
gnunicorn committed Jan 7, 2014
1 parent c743a98 commit 7cbe92d
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 9 deletions.
19 changes: 14 additions & 5 deletions app/assets/javascripts/admin/models/group.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,15 @@
Discourse.Group = Discourse.Model.extend({
loaded: false,


ALIAS_LEVELS : {
"nobody": 0,
"only_admins": 1,
"mods_and_admins": 2,
"members_mods_and_admins": 3,
"everyone": 99
},

userCountDisplay: function(){
var c = this.get('user_count');
// don't display zero its ugly
Expand Down Expand Up @@ -37,11 +46,11 @@ Discourse.Group = Discourse.Model.extend({

validValues: function() {
return Em.A([
{ name: I18n.t("admin.groups.alias_levels.nobody"), value: 0},
{ name: I18n.t("admin.groups.alias_levels.only_admins"), value: 1},
{ name: I18n.t("admin.groups.alias_levels.mods_and_admins"), value: 2},
{ name: I18n.t("admin.groups.alias_levels.members_mods_and_admins"), value: 3},
{ name: I18n.t("admin.groups.alias_levels.everyone"), value: 99}
{ name: I18n.t("admin.groups.alias_levels.nobody"), value: this.ALIAS_LEVELS.nobody},
{ name: I18n.t("admin.groups.alias_levels.only_admins"), value: this.ALIAS_LEVELS.only_admins},
{ name: I18n.t("admin.groups.alias_levels.mods_and_admins"), value: this.ALIAS_LEVELS.mods_and_admins},
{ name: I18n.t("admin.groups.alias_levels.members_mods_and_admins"), value: this.ALIAS_LEVELS.members_mods_and_admins},
{ name: I18n.t("admin.groups.alias_levels.everyone"), value: this.ALIAS_LEVELS.everyone}
]);
}.property(),

Expand Down
23 changes: 19 additions & 4 deletions app/models/group.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,16 @@ class Group < ActiveRecord::Base
:trust_level_5 => 15
}

ALIAS_LEVELS = {
:nobody => 0,
:only_admins => 1,
:mods_and_admins => 2,
:members_mods_and_admins => 3,
:everyone => 99
}

validate :alias_level, inclusion: { in: ALIAS_LEVELS.values}

def self.trust_group_ids
(10..19).to_a
end
Expand Down Expand Up @@ -101,16 +111,21 @@ def self.[](name)

def self.search_group(name, current_user)

levels = [99]
levels = [ALIAS_LEVELS[:everyone]]
if current_user.admin?
levels = [99, 1, 2, 3]
levels = [ALIAS_LEVELS[:everyone],
ALIAS_LEVELS[:only_admins],
ALIAS_LEVELS[:mods_and_admins],
ALIAS_LEVELS[:members_mods_and_admins]]
elsif current_user.moderator?
levels = [99, 2, 3]
levels = [ALIAS_LEVELS[:everyone],
ALIAS_LEVELS[:mods_and_admins],
ALIAS_LEVELS[:members_mods_and_admins]]
end

return Group.where("name LIKE :term_like AND (" +
" alias_level in (:levels)" +
" OR (alias_level = 3 AND id in (" +
" OR (alias_level = #{ALIAS_LEVELS[:everyone]} AND id in (" +
"SELECT group_id FROM group_users WHERE user_id= :user_id)" +
")" +
")", term_like: "#{name.downcase}%", levels: levels, user_id: current_user.id)
Expand Down

0 comments on commit 7cbe92d

Please sign in to comment.