Class: PostComment

Inherits:
ApplicationRecord show all
Includes:
ActionView::Helpers::SanitizeHelper, Models::Auditable
Defined in:
app/models/post_comment.rb

Overview

== Schema Information

Table name: post_comments
Database name: primary

id :integer not null, primary key
body :text
email :string(255)
name :string(255)
referrer :string(500)
site_url :string(255)
state :string(20) default("new"), not null
user_agent :string(255)
user_ip :string(255)
created_at :datetime
blogger_id :string(255)
parent_comment_id :integer
post_id :integer
user_id :integer

Indexes

idx_parent_comment_id (parent_comment_id)
idx_post_id_where_pc_is_null (post_id) WHERE (parent_comment_id IS NULL)
idx_state_parent_comment_id (state,parent_comment_id)
index_post_comments_on_post_id_and_created_at (post_id,created_at)

Constant Summary

Constants included from Models::Auditable

Models::Auditable::ALWAYS_IGNORED

Instance Attribute Summary collapse

Belongs to collapse

Methods included from Models::Auditable

#creator, #updater

Has many collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Models::Auditable

#all_skipped_columns, #audit_reference_data, #should_not_save_version, #stamp_record

Methods inherited from ApplicationRecord

ransackable_associations, ransackable_attributes, ransackable_scopes, ransortable_attributes, #to_relation

Methods included from Models::EventPublishable

#publish_event

Instance Attribute Details

#bodyObject (readonly)



41
# File 'app/models/post_comment.rb', line 41

validates :body, presence: true

#hostObject

Returns the value of attribute host.



49
50
51
# File 'app/models/post_comment.rb', line 49

def host
  @host
end

Class Method Details

.only_parentsActiveRecord::Relation<PostComment>

A relation of PostComments that are only parents. Active Record Scope

Returns:

See Also:



46
# File 'app/models/post_comment.rb', line 46

scope :only_parents, -> { where(parent_comment_id: nil) }

.publishedActiveRecord::Relation<PostComment>

A relation of PostComments that are published. Active Record Scope

Returns:

See Also:



47
# File 'app/models/post_comment.rb', line 47

scope :published, -> { where(state: 'published') }

.states_for_selectObject



73
74
75
# File 'app/models/post_comment.rb', line 73

def self.states_for_select
  state_machines[:state].states.map { |s| [s.human_name.capitalize, s.value] }
end

Instance Method Details

#articleArticle

Returns:

See Also:



36
# File 'app/models/post_comment.rb', line 36

belongs_to :article, optional: true

#can_publish?Boolean

Returns:

  • (Boolean)


77
78
79
# File 'app/models/post_comment.rb', line 77

def can_publish?
  parent_comment.nil? || parent_comment&.published?
end

#childObject



115
116
117
# File 'app/models/post_comment.rb', line 115

def child
  child_comments.first
end

#child_commentsActiveRecord::Relation<PostComment>

Returns:

See Also:



39
# File 'app/models/post_comment.rb', line 39

has_many :child_comments, -> { order(:created_at) }, class_name: 'PostComment', foreign_key: :parent_comment_id

#formatted_created_atObject



81
82
83
# File 'app/models/post_comment.rb', line 81

def formatted_created_at
  created_at.strftime('%A, %B %e, %Y')
end

#make_html_safeObject



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

def make_html_safe
  # puts "make_html_safe, BEFORE: #{body}"
  self.body = sanitize(body).to_s.html_safe
  # puts "make_html_safe, AFTER: #{body}"
  self.site_url = sanitize(site_url).to_s.html_safe if site_url
  self.name = sanitize(name).to_s.html_safe if name
  self.email = sanitize(email).to_s.html_safe if email
  true
end

#parent_commentPostComment



37
# File 'app/models/post_comment.rb', line 37

belongs_to :parent_comment, class_name: 'PostComment', optional: true

#parsed_bodyObject



85
86
87
# File 'app/models/post_comment.rb', line 85

def parsed_body
  body
end

#postPost

Returns:

See Also:



35
# File 'app/models/post_comment.rb', line 35

belongs_to :post, optional: true

#request=(request) ⇒ Object

Sets request metadata for tracking purposes



94
95
96
97
98
99
# File 'app/models/post_comment.rb', line 94

def request=(request)
  self.user_ip = request.remote_ip
  self.user_agent = request.env['HTTP_USER_AGENT']
  self.referrer   = request.env['HTTP_REFERER']
  self.host = request.host_with_port
end

#send_alertObject



111
112
113
# File 'app/models/post_comment.rb', line 111

def send_alert
  InternalMailer.blog_comment(self).deliver_later
end

#states_for_selectObject



69
70
71
# File 'app/models/post_comment.rb', line 69

def states_for_select
  state_transitions.map(&:event).sort
end

#userParty

Returns:

See Also:



34
# File 'app/models/post_comment.rb', line 34

belongs_to :user, class_name: 'Party', optional: true

#user_nameObject



89
90
91
# File 'app/models/post_comment.rb', line 89

def user_name
  user ? user.name : name
end