Class: Comment

Inherits:
ApplicationRecord
  • Object
show all
Includes:
Checkable, HasOwner, Toggleable, VotableItem
Defined in:
app/models/comment.rb

Overview

Comment

Attributes:

agent_id [Agent], optional
approved [Boolean]
author_email [String], optional
author_name [String], optional
body [Text]
commentable_id [Integer]
commentable_type [String]
created_at [DateTime]
deleted [Boolean]
downvote_count [Integer]
ip [Inet], optional
locked [Boolean]
parent_id [Comment], optional
spam [Boolean]
updated_at [DateTime]
upvote_count [Integer]
visible [Boolean]
vote_result [Integer]

Constant Summary collapse

AUTHOR_LIMIT =
100
BODY_LIMIT =
1_048_576

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.administrative_parametersObject



82
83
84
# File 'app/models/comment.rb', line 82

def self.administrative_parameters
  entity_parameters + %i[deleted visible spam]
end

.creation_parametersObject



78
79
80
# File 'app/models/comment.rb', line 78

def self.creation_parameters
  entity_parameters + %i[commentable_id commentable_type parent_id]
end

.entity_parametersObject



74
75
76
# File 'app/models/comment.rb', line 74

def self.entity_parameters
  %i[author_name author_email body]
end

.page_for_administration(page = 1) ⇒ Object

Parameters:

  • page (Integer) (defaults to: 1)


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

def self.page_for_administration(page = 1)
  list_for_administration.page(page)
end

.page_for_owner(user, page = 1) ⇒ Object

Parameters:

  • user (User)
  • page (Integer) (defaults to: 1)


70
71
72
# File 'app/models/comment.rb', line 70

def self.page_for_owner(user, page = 1)
  list_for_owner(user).page(page)
end

.page_for_visitor(page = 1) ⇒ Object

Parameters:

  • page (Integer) (defaults to: 1)


64
65
66
# File 'app/models/comment.rb', line 64

def self.page_for_visitor(page = 1)
  list_for_visitors.page(page)
end

.tree(collection) ⇒ Object



86
87
88
89
90
91
92
93
94
95
96
97
# File 'app/models/comment.rb', line 86

def self.tree(collection)
  result = {}

  collection.each do |entity|
    result[entity.id] = {
      parent_id: entity.parent_id,
      comment: entity,
    }
  end

  result
end

Instance Method Details

#commentable_nameObject



125
126
127
# File 'app/models/comment.rb', line 125

def commentable_name
  "#{commentable.class.model_name.human} #{commentable_id}"
end

#commentable_titleObject



129
130
131
132
133
134
135
# File 'app/models/comment.rb', line 129

def commentable_title
  if commentable.respond_to?(:title)
    commentable.title
  else
    commentable_name
  end
end

#notify_entry_owner?Boolean

Returns:

  • (Boolean)


112
113
114
115
116
117
118
119
# File 'app/models/comment.rb', line 112

def notify_entry_owner?
  entry_owner = commentable.user
  if entry_owner.is_a?(User) && !owned_by?(entry_owner)
    entry_owner.can_receive_letters?
  else
    false
  end
end

#profile_nameObject



137
138
139
# File 'app/models/comment.rb', line 137

def profile_name
  user.nil? ? author_name : user.profile_name
end


121
122
123
# File 'app/models/comment.rb', line 121

def text_for_link
  "#{self.class.model_name.human} #{id}"
end

#visible_to?(user) ⇒ Boolean

Parameters:

  • user (User)

Returns:

  • (Boolean)


100
101
102
103
104
105
106
107
108
109
110
# File 'app/models/comment.rb', line 100

def visible_to?(user)
  if commentable.respond_to? :visible_to?
    if deleted?
      UserPrivilege.user_has_privilege?(user, :administrator) && commentable.visible_to?(user)
    else
      commentable.visible_to?(user)
    end
  else
    !deleted? || UserPrivilege.user_has_privilege?(user, :administrator)
  end
end