Class: StatusGroupMembership

Inherits:
UserGroupMembership show all
Defined in:
app/models/status_group_membership.rb

Overview

This class represents the membership of a user in a status group, i.e. a subgroup of a corporation representing a member status, e.g. the subgroup ‘guests’ or ‘presidents’.

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from UserGroupMembership

#corporation, #destroy, #direct_groups, #direct_memberships, #direct_memberships_now_and_in_the_past, find_all_by, find_all_by_group, find_all_by_user_and_group, find_by, #group, #group_id, #indirect_memberships, #move_to, #move_to_group, #promote_to, #title, #user, #user=, #user_id, #user_title, #user_title=

Methods inherited from DagLink

#delete_cache, #fill_cache

Methods inherited from ActiveRecord::Base

#readonly?

Class Method Details

.create(params) ⇒ Object

Creator



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

def self.create( params )
  super( params ).becomes StatusGroupMembership 
end

.find_all_by_corporation(corporation) ⇒ Object

Returns all memberships in status groups that belong to the given corporation.

corporation A

|------------- status group 1
|                      |-------- user 1
|                      |-------- user 2
|------------- status group 2
                       |-------- user 3

The method therefore will return all memberships of subgroups of the corporation.



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

def self.find_all_by_corporation( corporation )
  raise 'Expect parameter to be a Corporation' unless corporation.kind_of? Corporation
  status_groups = corporation.status_groups
  status_group_ids = status_groups.collect { |group| group.id }
  links = self
    .where(:descendant_type => "User")
    .where(:ancestor_type => "Group")
    .where(:ancestor_id => status_group_ids)
    .order('valid_from')
  return links
end

.find_all_by_user(user) ⇒ Object

Returns all memberships of the given user in status groups.



91
92
93
94
95
96
97
98
99
100
101
102
# File 'app/models/status_group_membership.rb', line 91

def self.find_all_by_user( user )
  raise 'Expect parameter to be a User' unless user.kind_of? User
  status_groups = user.status_groups(with_invalid: true)
  status_group_ids = status_groups.collect { |group| group.id }
  links = self
    .where(:descendant_type => "User")
    .where(:descendant_id => user.id)
    .where(:ancestor_type => "Group")
    .where(:ancestor_id => status_group_ids)
    .order('valid_from')
  return links
end

.find_all_by_user_and_corporation(user, corporation) ⇒ Object

Returns all memberships of the given user in the given corporation.



106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'app/models/status_group_membership.rb', line 106

def self.find_all_by_user_and_corporation( user, corporation )
  raise 'Expect parameter to be a User' unless user.kind_of? User
  status_groups = user.status_groups(with_invalid: true)
  status_groups &= corporation.status_groups
  status_group_ids = status_groups.collect { |group| group.id }
  links = self
    .where(:descendant_type => "User")
    .where(:descendant_id => user.id)
    .where(:ancestor_type => "Group")
    .where(:ancestor_id => status_group_ids)
    .order('valid_from')
  return links
end

.find_by_user_and_group(user, group) ⇒ Object

This method overrides the default finder method in order to make sure the returned object is of the StatusGroupMembership type.



123
124
125
126
127
128
129
130
131
132
133
# File 'app/models/status_group_membership.rb', line 123

def self.find_by_user_and_group( user, group )
  self
    .where(ancestor_id: group.id, ancestor_type: 'Group')
    .where(descendant_id: user.id, descendant_type: 'User')
    .limit(1)
    .first
  
  # The #becomes method won't work here.
  #membership = super( user, group )
  #membership ? StatusGroupMembership.with_invalid.find(membership.id) : nil
end

Instance Method Details

#create_event(params) ⇒ Object

Alias Methods For Delegated Methods



26
27
28
# File 'app/models/status_group_membership.rb', line 26

def create_event( params )
  find_or_create_status_group_membership_info.create_promoted_on_event( params )
end

#event_by_nameObject

Access the event (promoted_on_event) by its name, since this is the way most likely done by a user interface.

If a new event is created, assign the corporation associated with this status group as the group of the event.



36
37
38
# File 'app/models/status_group_membership.rb', line 36

def event_by_name
  self.event.name if self.event
end

#event_by_name=(event_name) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'app/models/status_group_membership.rb', line 39

def event_by_name=( event_name )
  if event_name.present?
    if Event.find_by_name( event_name )
      self.event = Event.find_by_name( event_name )
    else
      self.create_event( name: event_name )
      self.event.group ||= self.corporation if self.corporation
      self.event.start_at = self.created_at
      self.event.save
    end
  else
    self.event = nil
  end
end

#save(*args) ⇒ Object

Since several important attributes of this model are delegated, it is likely to change a delegated attribute without changing a direct attribute. For example:

membership.workflow = some_workflow                # workflow is delegated
membership.changed?                                # => false
membership.status_group_membership_info.changed?   # => true
membership.save

The regular ‘save` method would fail, because there are `no changes` to the membership itself.

To circumvent this, this save method first saves the delegate model if necessary and then calls the regular ‘save` method.



153
154
155
156
157
158
159
160
# File 'app/models/status_group_membership.rb', line 153

def save(*args)
  save_status_group_membership_info_if_changed
  if changed?
    return super(*args)
  else
    return true
  end
end

#update_attributes(attributes, options = {}) ⇒ Object



162
163
164
165
# File 'app/models/status_group_membership.rb', line 162

def update_attributes( attributes, options = {} )
  self.assign_attributes( attributes, options )
  save
end