Class: SourceMonitor::Item

Inherits:
ApplicationRecord show all
Includes:
Models::UrlNormalizable
Defined in:
app/models/source_monitor/item.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Models::UrlNormalizable

#url_invalid?

Class Method Details

.ransackable_associations(_auth_object = nil) ⇒ Object



52
53
54
# File 'app/models/source_monitor/item.rb', line 52

def ransackable_associations(_auth_object = nil)
  %w[source]
end

.ransackable_attributes(_auth_object = nil) ⇒ Object



48
49
50
# File 'app/models/source_monitor/item.rb', line 48

def ransackable_attributes(_auth_object = nil)
  %w[title summary url published_at created_at scrape_status]
end

Instance Method Details

#deleted?Boolean

Returns:

  • (Boolean)


65
66
67
# File 'app/models/source_monitor/item.rb', line 65

def deleted?
  deleted_at.present?
end

#ensure_feed_content_recordObject

Creates an ItemContent record for feed word count computation when one doesn’t exist. The before_save callback on ItemContent will compute feed_word_count from item.content.



40
41
42
43
44
45
# File 'app/models/source_monitor/item.rb', line 40

def ensure_feed_content_record
  return if item_content.present?
  return if content.blank?

  create_item_content!
end

#restore!Object



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

def restore!
  return unless deleted?

  self.class.transaction do
    update_columns(
      deleted_at: nil,
      updated_at: Time.current
    )

    SourceMonitor::Source.increment_counter(:items_count, source_id) if source_id
  end
end

#scraped_content=(value) ⇒ Object



61
62
63
# File 'app/models/source_monitor/item.rb', line 61

def scraped_content=(value)
  assign_content_attribute(:scraped_content, value)
end

#scraped_html=(value) ⇒ Object



57
58
59
# File 'app/models/source_monitor/item.rb', line 57

def scraped_html=(value)
  assign_content_attribute(:scraped_html, value)
end

#soft_delete!(timestamp: Time.current) ⇒ Object



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'app/models/source_monitor/item.rb', line 69

def soft_delete!(timestamp: Time.current)
  return if deleted?

  self.class.transaction do
    timestamp = timestamp.in_time_zone if timestamp.respond_to?(:in_time_zone)
    timestamp ||= Time.current

    update_columns(
      deleted_at: timestamp,
      updated_at: timestamp
    )

    SourceMonitor::Source.decrement_counter(:items_count, source_id) if source_id
  end
end