Module: StructureableMixins::HasSpecialGroups::ClassMethods

Defined in:
app/models/structureable_mixins/has_special_groups.rb

Overview

Global Special Groups e.g. the everyone group: ‘Group.everyone`

These class methods may be called on each structureable model. For example, one may call ‘Group.find_or_create_special_group(:everyone)`. This is, for example, used to define the `Group.everyone` accessor.

class Group
  def self.everyone
    self.find_or_create_special_group(:everyone)
  end
end

The ‘options` hash allows to specify a `parent element`, which is a structureable element that is expected to be the parent element of the special group. Example:

find_or_create_special_group( :corporations, parent_element: 
  find_or_create_special_group(:everyone) )

Instance Method Summary collapse

Instance Method Details

#create_special_group(group_flag, options = {}) ⇒ Object



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

def create_special_group( group_flag, options = {} )
  if find_special_group( group_flag, options )
    raise "special group :#{group_flag} already exists."
  end
  object_to_create = options[:parent_element].try(:child_groups) 
  object_to_create ||= Group unless options[:local]

  new_special_group = object_to_create.create
  new_special_group.add_flag( group_flag.to_sym )
  new_special_group.update_attribute( :name, group_flag.to_s.gsub(/_parent$/, "" ) )

  return new_special_group
end

#find_or_create_special_group(group_flag, options = {}) ⇒ Object



91
92
93
94
95
96
97
98
# File 'app/models/structureable_mixins/has_special_groups.rb', line 91

def find_or_create_special_group( group_flag, options = {} )
  find_special_group(group_flag, options) or
  begin
    create_special_group(group_flag, options)
  rescue
    nil
  end
end

#find_special_group(group_flag, options = {}) ⇒ Object



71
72
73
74
75
# File 'app/models/structureable_mixins/has_special_groups.rb', line 71

def find_special_group( group_flag, options = {} )
  object_to_search = options[:parent_element].try(:child_groups) 
  object_to_search ||= Group unless options[:local]
  object_to_search.find_by_flag( group_flag.to_sym ) if object_to_search && object_to_search != [] 
end