Class: Membership

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

Constant Summary

LEVELS =

Membership levels

{
    # A denied member is not a member of the group.
    :denied => 0,
    # A pending member has not yet been accepted into a group.
    # We need to update authorization to reflect this.
    :pending => 1,
    # A regular member is a member of the group with full read permission.
    :regular => 2,
    # A leader controls the group.
    :leader => 9,
}
PERMISSIONS =

Membership Permissions

{
    :regular => {
        :screen => {
            :none => 0, # No screen write privledges
            :subscriptions => 3, # Can update subscriptions
            :all => 9, # Full write privledges
        },
        :feed => {
            :none => 0, # No feed write privledges
            :submissions => 3, # Can update submissions (moderate)
            :all => 9 # Full write privledges
        },
    }
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#permsObject

Returns the value of attribute perms



56
57
58
# File 'app/models/membership.rb', line 56

def perms
  @perms
end

Instance Method Details

#can_resign_leadership?Boolean

Returns whether a particular member object can resign leadership

Returns:

  • (Boolean)


176
177
178
# File 'app/models/membership.rb', line 176

def can_resign_leadership?
    return self.group.leaders.count > 1 && self.is_leader?
end

#compact_permissionsObject



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'app/models/membership.rb', line 81

def compact_permissions
  level_sym = level_name.to_sym
  if PERMISSIONS.include?(level_sym)
    local_perms = PERMISSIONS[level_sym]
    new_permissions = 0
    local_perms.each_with_index do |(key, value), index|
      p_value = 0
      if !perms[key].nil?
        p_sym = perms[key].to_sym
        p_value = local_perms[key][p_sym]
      end
      if index == 0
        if !p_value.nil?
          new_permissions = p_value
        else
          new_permissions = 0
        end
      else
        new_permissions += p_value * (10**index)
      end
    end
    self.permissions = new_permissions
  end
end

#expand_permissionsObject



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'app/models/membership.rb', line 58

def expand_permissions
  self.perms = {}
  level_sym = level_name.to_sym
  if PERMISSIONS.include?(level_sym) && !permissions.nil?
    local_perms = PERMISSIONS[level_sym]
    local_perms.each_with_index do |(key, value), index|
      if index == 0
        p_value = permissions % 10
      else
        p_value = permissions / (10**index)
        p_value = p_value % 10
      end
      type = (local_perms[key].respond_to?(:key) ? local_perms[key].key(p_value) : local_perms[key].index(p_value))
      if !type.nil?
        self.perms[key] = type
      else
        self.perms[key] = nil
      end
    end
  end
  self.perms
end

#is_approved?Boolean

Test if the membership has been approved.

Returns:

  • (Boolean)


112
113
114
# File 'app/models/membership.rb', line 112

def is_approved?
  level > Membership::LEVELS[:pending]
end

#is_denied?Boolean

Test if the membership has been denied.

Returns:

  • (Boolean)


117
118
119
# File 'app/models/membership.rb', line 117

def is_denied?
  level == Membership::LEVELS[:denied]
end

#is_leader?Boolean

A shortcut to test if a membership represents a leader

Returns:

  • (Boolean)


127
128
129
# File 'app/models/membership.rb', line 127

def is_leader?
  level == Membership::LEVELS[:leader]
end

#is_pending?Boolean

Test if the membership is pending.

Returns:

  • (Boolean)


122
123
124
# File 'app/models/membership.rb', line 122

def is_pending?
  level == Membership::LEVELS[:pending]
end

#level_nameObject

Get level name of a membership



107
108
109
# File 'app/models/membership.rb', line 107

def level_name
  name = (Membership::LEVELS.respond_to?(:key) ? Membership::LEVELS.key(level) : Membership::LEVELS.index(level)).to_s
end

#update_membership_level(action) ⇒ Object

action can be deny, promote, demote”



132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
# File 'app/models/membership.rb', line 132

def update_membership_level(action)
  case action
  when "approve"
    # Can only approve if current level is pending
    if self.level == Membership::LEVELS[:pending] && update_attributes({:level => Membership::LEVELS[:regular]})
      return true, :membership_approved
    else
      return false, :membership_approved_failure
    end
  when "deny"
    # Can only deny if current level is pending
    if self.level == Membership::LEVELS[:pending] && update_attributes({:level => Membership::LEVELS[:denied]})
      return true, :membership_denied
    else
      return false, :membership_denied_failure
    end
  when "promote"
    # Can only promote regular members
    if self.level == Membership::LEVELS[:regular] && update_attributes({:level => Membership::LEVELS[:leader]})
      return true, :membership_promoted
    else
      return false, :membership_promoted_failure
    end
  when "demote"
    # Only Leaders can be demoted, but only when there is more than one.
    if can_resign_leadership? && update_attributes({:level => Membership::LEVELS[:regular]})
      return true, :membership_demoted
      true
    else
      return false, :membership_demoted_failure
      false
    end
  when "unblock"
    if self.level == Membership::LEVELS[:denied] && update_attributes({:level => Membership::LEVELS[:regular]})
      return true, :membership_approved
    else
      return false, :membership_approved_failure
    end
  end
  # default is failure
  return false, :membership_unknown_action
end