Module: Authorization

Defined in:
app/helpers/kit/authorization.rb

Instance Method Summary collapse

Instance Method Details

#collaborators_controls_for(object) ⇒ Object



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'app/helpers/kit/authorization.rb', line 62

def collaborators_controls_for object
   object.must_not_be.nil

   all_selected_roles = []
   all_avaliable_roles = []

   # Ordinary Roles
   controls = {
     'member' => link_to(
       t(:member_role),
       collaborators_path(object, remove_roles: 'user', add_roles: 'member', format: :js), method: :post
     ),
     'user' => link_to(
       t(:user_role),
       collaborators_path(object, remove_roles: 'member', add_roles: 'user', format: :js), method: :post
     ),
   }

   selected_role = Role.lower_role object.collaborators
   selected_role = nil if selected_role == 'manager'
   avaliable_roles = Role::ORDERED_ROLES.select{|role| role != selected_role and role != 'manager'}
   avaliable_roles = avaliable_roles.collect{|role| controls[role]}

   all_selected_roles << t("#{selected_role}_role") if selected_role
   all_avaliable_roles.push *avaliable_roles

   # Custom Roles
   custom_roles = rad.config.custom_roles
   selected_roles = custom_roles.select{|role| object.collaborators.include? role}
   avaliable_roles = custom_roles - selected_roles

   # selected_roles.collect! do |role|
   #   link_to(role, collaborators_path(object, remove_roles: role, format: :js), method: :post)
   # end
   avaliable_roles.collect! do |role|
     link_to(role, collaborators_path(object, add_roles: role, format: :js), method: :post)
   end

   all_selected_roles.push *selected_roles
   all_avaliable_roles.push *avaliable_roles

   # Clear All
   if all_selected_roles.blank?
     all_selected_roles << t(:only_owner)
   else
     current_roles = object.collaborators.select{|r| r != 'manager' and r != "user:#{object.owner_name}"}
     all_avaliable_roles.unshift(
       link_to(
         t(:only_owner),
         collaborators_path(object, remove_roles: current_roles.join("\n"), format: :js), method: :post
       )
     )
   end

   [
     %[#{t(:collaborators)}: <span class='m_bold'>#{all_selected_roles.join(', ')}</span>],
     %[<span class='m_tiny'>(#{all_avaliable_roles.join(', ')})</span>]
   ]
end

#viewers_controls_for(object) ⇒ Object



2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'app/helpers/kit/authorization.rb', line 2

def viewers_controls_for object
   object.must_not_be.nil

   all_selected_roles = []
   all_avaliable_roles = []

   # Ordinary Roles
   controls = {
     'member' => link_to(
       t(:member_role),
       viewers_path(object, remove_roles: 'user', add_roles: 'member', format: :js), method: :post
     ),
     'user' => link_to(
       t(:user_role),
       viewers_path(object, remove_roles: 'member', add_roles: 'user', format: :js), method: :post
     ),
   }

   selected_role = Role.lower_role object.viewers
   selected_role = nil if selected_role == 'manager'
   avaliable_roles = Role::ORDERED_ROLES.select{|role| role != selected_role and role != 'manager'}
   avaliable_roles = avaliable_roles.collect{|role| controls[role]}

   all_selected_roles << t("#{selected_role}_role") if selected_role
   all_avaliable_roles.push *avaliable_roles

   # Custom Roles
   custom_roles = rad.config.custom_roles
   selected_roles = custom_roles.select{|role| object.viewers.include? role}
   avaliable_roles = custom_roles - selected_roles

   # selected_roles.collect! do |role|
   #   link_to(role, viewers_path(object, remove_roles: role, format: :js), method: :post)
   # end
   avaliable_roles.collect! do |role|
     link_to(role, viewers_path(object, add_roles: role, format: :js), method: :post)
   end

   all_selected_roles.push *selected_roles
   all_avaliable_roles.push *avaliable_roles

   # Clear All
   if all_selected_roles.blank?
     all_selected_roles << t(:only_owner)
   else
     current_roles = object.viewers.select{|r| r != 'manager' and r != "user:#{object.owner_name}"}
     all_avaliable_roles.unshift(
       link_to(
         t(:only_owner),
         viewers_path(object, remove_roles: current_roles.join("\n"), format: :js), method: :post
       )
     )
   end

   [
     %[#{t(:viewers)}: <span class='m_bold'>#{all_selected_roles.join(', ')}</span>],
     %[<span class='m_tiny'>(#{all_avaliable_roles.join(', ')})</span>]
   ]
end