Class: Landable::PageRevision

Inherits:
ActiveRecord::Base
  • Object
show all
Includes:
HasAssets, TableName
Defined in:
app/models/landable/page_revision.rb

Constant Summary collapse

@@ignored_page_attributes =
[
  'page_id',
  'imported_at',
  'created_at',
  'deleted_at',
  'updated_at',
  'published_revision_id',
  'is_publishable',
  'updated_by_author_id',
  'lock_version'
]

Instance Method Summary collapse

Instance Method Details

#add_screenshot!Object



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'app/models/landable/page_revision.rb', line 87

def add_screenshot!
  return nil if preview_url.blank?

  unless Landable.configuration.screenshots_enabled
    Rails.logger.info "Screenshots disabled; skipping for #{path}"
    return
  end

  attempts_left = 3

  begin
    attempts_left -= 1

    self.screenshot = ScreenshotService.capture(preview_url)

    # we've got a trigger preventing updates to other columns, so! muck
    # about under the hood to commit the asset, and explicitly only update
    # this column.
    store_screenshot!
    write_screenshot_identifier
    update_column :screenshot, self[:screenshot]

  rescue ScreenshotService::Error => error
    Rails.logger.warn "Failed to generate screenshot (#{attempts_left} attempt(s) left) for #{path}: #{error.inspect}"

    retry if attempts_left > 0
  end
end

#page_id=(id) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'app/models/landable/page_revision.rb', line 28

def page_id=(id)
  # set the value
  self[:page_id] = id

  # copy grab attributes from the page
  self.title          = page.title
  self.body           = page.body
  self.head_content   = page.head_content
  self.path           = page.path
  self.status_code    = page.status_code
  self.category_id    = page.category_id
  self.theme_id       = page.theme_id
  self.meta_tags      = page.meta_tags
  self.redirect_url   = page.redirect_url
  self.abstract       = page.abstract
  self.hero_asset_id  = page.hero_asset_id
end

#preview_pathObject



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

def preview_path
  public_preview_page_revision_path(self)
end

#preview_urlObject



68
69
70
71
72
73
74
75
# File 'app/models/landable/page_revision.rb', line 68

def preview_url
  begin
    public_preview_page_revision_url(self, host: Landable.configuration.public_host)
  rescue ArgumentError
    Rails.logger.warn "Failed to generate preview url for page revision #{id} - missing Landable.configuration.public_host"
    nil
  end
end

#publish!Object



60
61
62
# File 'app/models/landable/page_revision.rb', line 60

def publish!
  update_attribute :is_published, true
end

#screenshot_urlObject



83
84
85
# File 'app/models/landable/page_revision.rb', line 83

def screenshot_url
  screenshot.try(:url)
end

#snapshotObject



46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'app/models/landable/page_revision.rb', line 46

def snapshot
  Page.new(title: self.title,
           meta_tags: page.meta_tags,
           head_content: page.head_content,
           body: self.body,
           path: self.path,
           redirect_url: self.redirect_url,
           status_code: self.status_code,
           theme_id: self.theme_id,
           category_id: self.category_id,
           abstract: self.abstract,
           hero_asset_id: self.hero_asset_id)
end

#unpublish!Object



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

def unpublish!
  update_attribute :is_published, false
end