Module: HasComments::ClassMethods

Defined in:
app/models/concerns/has_comments.rb

Overview

Class methods

Instance Method Summary collapse

Instance Method Details

#allow_all_comments_to(roles) ⇒ Object

Macro-style method to define which roles are allowed to access to all the comments (public and private)

Parameters:

  • roles (Object)

    can be the name of a role (or the special value :requester) or an array of names (also supporting :requester)



36
37
38
39
40
41
42
# File 'app/models/concerns/has_comments.rb', line 36

def allow_all_comments_to(roles)
  roles = [roles].flatten.map(&:to_sym)
  @roles_for_public_comments ||= []
  @roles_for_private_comments ||= []
  @roles_for_public_comments += roles
  @roles_for_private_comments += roles
end

#allow_private_comments?(role) ⇒ Boolean

Checks if a given role has been granted access to private comments

Parameters:

  • role (#to_sym)

    name of the role

Returns:

  • (Boolean)

    true if the user is authorized



80
81
82
83
84
85
86
87
# File 'app/models/concerns/has_comments.rb', line 80

def allow_private_comments?(role)
  # :requester is not longer a role, but a special value
  if role.to_sym == :requester
    false
  else
    roles_for_private_comments.include? role.to_sym
  end
end

#allow_public_comments?(role) ⇒ Boolean

Checks if a given role has been granted access to public comments

Parameters:

  • role (#to_sym)

    name of the role

Returns:

  • (Boolean)

    true if the user is authorized



67
68
69
70
71
72
73
74
# File 'app/models/concerns/has_comments.rb', line 67

def allow_public_comments?(role)
  # :requester is not longer a role, but a special value
  if role.to_sym == :requester
    false
  else
    roles_for_public_comments.include? role.to_sym
  end
end

#allow_public_comments_to(roles) ⇒ Object

Macro-style method to define which roles are allowed to access to public comments.

Parameters:

  • roles (Object)

    can be the name of a role (or the special value :requester) or an array of names (also supporting :requester)



25
26
27
28
29
# File 'app/models/concerns/has_comments.rb', line 25

def allow_public_comments_to(roles)
  roles = [roles].flatten.map(&:to_sym)
  @roles_for_public_comments ||= []
  @roles_for_public_comments += roles
end

#private_comment_hintString

Hint about the private field

Returns:

  • (String)

    a text explaining the meaning of the private field



106
107
108
109
110
111
112
113
# File 'app/models/concerns/has_comments.rb', line 106

def private_comment_hint
  roles = roles_for_private_comments
  if roles.include?(:requester)
    I18n.t(:comment_private_hint_requester, roles: (roles - [:requester]).to_sentence)
  else
    I18n.t(:comment_private_hint, roles: roles.to_sentence)
  end
end

#private_comments_for_requester?Boolean

Checks if the requester has been granted access to private comments

Returns:

  • (Boolean)

    true if the user is authorized



92
93
94
# File 'app/models/concerns/has_comments.rb', line 92

def private_comments_for_requester?
  roles_for_private_comments.include? :requester
end

#public_comments_for_requester?Boolean

Checks if the requester has been granted access to public comments

Returns:

  • (Boolean)

    true if the user is authorized



99
100
101
# File 'app/models/concerns/has_comments.rb', line 99

def public_comments_for_requester?
  roles_for_public_comments.include? :requester
end

#roles_for_private_commentsArray

Names of the roles allowed to access private comments

Returns:

  • (Array)

    array of symbols

See Also:



59
60
61
# File 'app/models/concerns/has_comments.rb', line 59

def roles_for_private_comments
  @roles_for_private_comments.dup || []
end

#roles_for_public_commentsArray

Names of the roles allowed to access public comments

Returns:

  • (Array)

    array of symbols

See Also:



50
51
52
# File 'app/models/concerns/has_comments.rb', line 50

def roles_for_public_comments
  @roles_for_public_comments.dup || []
end