Class: ClarkKent::SharingScopeKind

Inherits:
Object
  • Object
show all
Defined in:
app/models/clark_kent/sharing_scope_kind.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(class_name, user_association = nil) ⇒ SharingScopeKind

Returns a new instance of SharingScopeKind.



63
64
65
66
67
68
69
70
71
# File 'app/models/clark_kent/sharing_scope_kind.rb', line 63

def initialize(class_name,user_association = nil)
  if 'Everyone' == class_name
    @class_name = ''
    @human_name = 'Everyone'
  else
    @class_name = class_name
  end
  @user_association = user_association
end

Instance Attribute Details

#class_nameObject

Returns the value of attribute class_name.



3
4
5
# File 'app/models/clark_kent/sharing_scope_kind.rb', line 3

def class_name
  @class_name
end

#human_nameObject

Returns the value of attribute human_name.



3
4
5
# File 'app/models/clark_kent/sharing_scope_kind.rb', line 3

def human_name
  @human_name
end

#nameObject

Returns the value of attribute name.



3
4
5
# File 'app/models/clark_kent/sharing_scope_kind.rb', line 3

def name
  @name
end

#typeObject

Returns the value of attribute type.



3
4
5
# File 'app/models/clark_kent/sharing_scope_kind.rb', line 3

def type
  @type
end

#user_associationObject

Returns the value of attribute user_association.



3
4
5
# File 'app/models/clark_kent/sharing_scope_kind.rb', line 3

def user_association
  @user_association
end

Class Method Details

.allObject



5
6
7
8
9
10
11
12
13
14
15
16
17
18
# File 'app/models/clark_kent/sharing_scope_kind.rb', line 5

def self.all
  return @ss_types if @ss_types
  if ClarkKent.user_class_name
    user_ss = self.new(ClarkKent.user_class_name)
    @ss_types = [user_ss]
  else
    @ss_types = []
  end
  ClarkKent.other_sharing_scopes.each do |ss_type|
    @ss_types << self.new(*ss_type)
  end
  @ss_types << self.new('Everyone')
  @ss_types
end

.customObject



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

def self.custom
  all.select{|ssk| ['everyone','personal'].exclude? ssk.type}
end

.custom_for_user(user) ⇒ Object



40
41
42
# File 'app/models/clark_kent/sharing_scope_kind.rb', line 40

def self.custom_for_user(user)
  custom.select{|ssk| has_some(ssk.associated_containers_for(user)) }
end

.find_by_class_name(klass_name) ⇒ Object



20
21
22
# File 'app/models/clark_kent/sharing_scope_kind.rb', line 20

def self.find_by_class_name(klass_name)
  self.all.detect{|ssk| klass_name == ssk.class_name}
end

.has_some(thing) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
# File 'app/models/clark_kent/sharing_scope_kind.rb', line 24

def self.has_some(thing)
  if thing.present?
    if thing.respond_to? :any?
      thing.any?
    else
      true
    end
  else
    false
  end
end

.select_optionsObject



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

def self.select_options
  return @sharing_scope_options if @sharing_scope_options
  @sharing_scope_options = {}
  self.all.each do |sharing_scope_kind|
    @sharing_scope_options[sharing_scope_kind.human_name] = sharing_scope_kind.class_name
  end
  @sharing_scope_options
end

.select_options_for_user(user) ⇒ Object



53
54
55
56
57
58
59
60
61
# File 'app/models/clark_kent/sharing_scope_kind.rb', line 53

def self.select_options_for_user(user)
  sharing_scope_options = {}
  self.all.each do |sharing_scope_kind|
    if custom.exclude?(sharing_scope_kind) || has_some(sharing_scope_kind.associated_containers_for(user))
      sharing_scope_options[sharing_scope_kind.human_name] = sharing_scope_kind.class_name
    end
  end
  sharing_scope_options
end

Instance Method Details

#associated_containers_for(user) ⇒ Object



91
92
93
# File 'app/models/clark_kent/sharing_scope_kind.rb', line 91

def associated_containers_for(user)
  user.send @user_association
end

#basic_association_id_collection_nameObject



87
88
89
# File 'app/models/clark_kent/sharing_scope_kind.rb', line 87

def basic_association_id_collection_name
  (class_name.underscore + '_ids').to_sym
end

#scopes_for(user) ⇒ Object



95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'app/models/clark_kent/sharing_scope_kind.rb', line 95

def scopes_for(user)
  case type
  when 'everyone'
    [ClarkKent::SharingScope.new('Everyone',self)]
  when 'personal'
    [ClarkKent::SharingScope.new(user,self)]
  else
    if associated_containers_for(user).respond_to? :map
      associated_containers_for(user).map do |associated_container|
        ClarkKent::SharingScope.new(associated_container,self)
      end
    else
      if associated_containers_for(user).present?
        [ClarkKent::SharingScope.new(associated_containers_for(user),self)]
      else
        []
      end
    end
  end
end