Module: TheRole::Api::Role

Extended by:
ActiveSupport::Concern
Includes:
BaseMethods
Included in:
Role
Defined in:
app/models/concerns/the_role/api/role.rb

Defined Under Namespace

Modules: ClassMethods

Instance Method Summary collapse

Methods included from BaseMethods

#admin?, #any_role?, #has_role?, #moderator?

Instance Method Details

#_jsonable(val) ⇒ Object



19
20
21
# File 'app/models/concerns/the_role/api/role.rb', line 19

def _jsonable val
  val.is_a?(Hash) ? val.to_json : val.to_s
end

#create_rule(section_name, rule_name) ⇒ Object



88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'app/models/concerns/the_role/api/role.rb', line 88

def create_rule section_name, rule_name
  return false if     rule_name.blank?
  return false unless create_section(section_name)

  role         = to_hash
  rule_name    = rule_name.to_slug_param(sep: '_')
  section_name = section_name.to_slug_param(sep: '_')

  return true if role[section_name][rule_name]

  role[section_name][rule_name] = false
  update_attribute(:the_role,  _jsonable(role))
end

#create_section(section_name = nil) ⇒ Object

C



75
76
77
78
79
80
81
82
83
84
85
86
# File 'app/models/concerns/the_role/api/role.rb', line 75

def create_section section_name = nil
  return false unless section_name

  role         = to_hash
  section_name = section_name.to_slug_param(sep: '_')

  return false if section_name.blank?
  return true  if role[section_name]

  role[section_name] = {}
  update_attribute(:the_role, _jsonable(role))
end

#delete_rule(section_name, rule_name) ⇒ Object



159
160
161
162
163
164
165
166
167
168
169
# File 'app/models/concerns/the_role/api/role.rb', line 159

def delete_rule section_name, rule_name
  role         = to_hash
  rule_name    = rule_name.to_slug_param(sep: '_')
  section_name = section_name.to_slug_param(sep: '_')

  return false unless role[section_name]
  return false unless role[section_name].key? rule_name

  role[section_name].delete rule_name
  update_attribute(:the_role,  _jsonable(role))
end

#delete_section(section_name = nil) ⇒ Object

D



146
147
148
149
150
151
152
153
154
155
156
157
# File 'app/models/concerns/the_role/api/role.rb', line 146

def delete_section section_name = nil
  return false unless section_name

  role = to_hash
  section_name = section_name.to_slug_param(sep: '_')

  return false if section_name.blank?
  return false unless role[section_name]

  role.delete section_name
  update_attribute(:the_role,  _jsonable(role))
end

#has_section?(section_name) ⇒ Boolean

Returns:

  • (Boolean)


36
37
38
# File 'app/models/concerns/the_role/api/role.rb', line 36

def has_section? section_name
  to_hash.key? section_name.to_slug_param(sep: '_')
end

#role_hashObject

version for ‘Role` model



11
12
13
# File 'app/models/concerns/the_role/api/role.rb', line 11

def role_hash
  to_hash
end

#rule_off(section_name, rule_name) ⇒ Object



131
132
133
134
135
136
137
138
139
140
141
142
# File 'app/models/concerns/the_role/api/role.rb', line 131

def rule_off section_name, rule_name
  role         = to_hash
  rule_name    = rule_name.to_slug_param(sep: '_')
  section_name = section_name.to_slug_param(sep: '_')

  return false unless role[section_name]
  return false unless role[section_name].key? rule_name
  return true  unless role[section_name][rule_name]

  role[section_name][rule_name] = false
  update_attribute(:the_role,  _jsonable(role))
end

#rule_on(section_name, rule_name) ⇒ Object



118
119
120
121
122
123
124
125
126
127
128
129
# File 'app/models/concerns/the_role/api/role.rb', line 118

def rule_on section_name, rule_name
  role         = to_hash
  rule_name    = rule_name.to_slug_param(sep: '_')
  section_name = section_name.to_slug_param(sep: '_')

  return false unless role[section_name]
  return false unless role[section_name].key? rule_name
  return true  if     role[section_name][rule_name]

  role[section_name][rule_name] = true
  update_attribute(:the_role,  _jsonable(role))
end

#the_role=(val) ⇒ Object



15
16
17
# File 'app/models/concerns/the_role/api/role.rb', line 15

def the_role= val
  self[:the_role] = _jsonable val
end

#to_hashObject



23
24
25
# File 'app/models/concerns/the_role/api/role.rb', line 23

def to_hash
  begin JSON.load(the_role) rescue {} end
end

#to_jsonObject



27
28
29
# File 'app/models/concerns/the_role/api/role.rb', line 27

def to_json
  the_role
end

#update_role(new_role_hash) ⇒ Object

source_hash will be reset to false except true items from new_role_hash all keys will become ‘strings’ look at lib/the_role/hash.rb to find definition of underscorify_keys method



108
109
110
111
112
113
114
115
116
# File 'app/models/concerns/the_role/api/role.rb', line 108

def update_role new_role_hash
  new_role_hash = new_role_hash.try(:to_hash) || {}

  new_role = new_role_hash.underscorify_keys
  role     = to_hash.underscorify_keys.deep_reset(false)

  role.deep_merge! new_role
  update_attribute(:the_role,  _jsonable(role))
end