Class: Group

Inherits:
ActiveRecord::Base
  • Object
show all
Includes:
ActiveModel::ForbiddenAttributesProtection, PublicActivity::Common
Defined in:
app/models/group.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#new_leaderObject

have getters and setters for a new_leader virtual attribute



30
31
32
# File 'app/models/group.rb', line 30

def new_leader
  @new_leader
end

Instance Method Details

#can_resign_leadership?(membership) ⇒ Boolean

Test if a user can be demoted to regular member or removed from a group



68
69
70
# File 'app/models/group.rb', line 68

def can_resign_leadership?(membership)
  return self.leaders.count > 1 || !membership.is_leader?
end

#create_leaderObject



39
40
41
# File 'app/models/group.rb', line 39

def create_leader
  self.new_leader = Membership.create(:user_id => new_leader, :group_id => self.id, :level => Membership::LEVELS[:leader]) if new_leader.present?
end

#has_member?(user) ⇒ Boolean

Test if a user is part of this group



58
59
60
# File 'app/models/group.rb', line 58

def has_member?(user)
  users.include?(user)
end

#is_deletable?Boolean



53
54
55
# File 'app/models/group.rb', line 53

def is_deletable?
  self.screens.size == 0 && self.feeds.size == 0
end

#made_request?(user) ⇒ Boolean

Test if a user has requested membership in this group



63
64
65
# File 'app/models/group.rb', line 63

def made_request?(user)
  all_users.include?(user)
end

#update_membership_permsObject

Manually cascade the callbacks for membership permissions.



33
34
35
36
37
# File 'app/models/group.rb', line 33

def update_membership_perms
  self.memberships.each do |m|
    m.run_callbacks(:save)
  end
end

#user_has_permissions?(user, level, type, permissions) ⇒ Boolean

Test if a user has a certain permission at a level within a group. Sample usage: user_has_permissions?(user, :regular, :screen, [:subscribe, :all]) will test if the `user` has either :all or :subscribe permissions as a supporter in the screen permission type of the current group.



76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'app/models/group.rb', line 76

def user_has_permissions?(user, level, type, permissions)
  return false if user.nil?

  m = memberships.where(:user_id => user, :level => Membership::LEVELS[level]).first
  return false if m.nil?
  return false unless m.perms.include?(type)

  permissions = [permissions] if permissions.is_a? Symbol
  permissions.each do |p|
    return true if m.perms[type] == p
  end
  return false
end

#users_not_in_groupObject

Deliver a list of only users not currently in the group Used for adding new users to a group and avoiding duplication



45
46
47
48
49
50
51
# File 'app/models/group.rb', line 45

def users_not_in_group
  users = User.all.to_a
  self.memberships.each do |m|
    users.delete_if { |key, value| key.id == m.user_id }
  end
  return users
end