require_dependency 'landable/has_assets' module Landable class PageRevision < ActiveRecord::Base include Landable::Engine.routes.url_helpers include Landable::HasAssets include Landable::TableName class << self attr_accessor :ignored_page_attributes end @ignored_page_attributes = %w(page_id imported_at created_at deleted_at updated_at published_revision_id is_publishable updated_by_author_id lock_version audit_flags page_name) belongs_to :author belongs_to :page, inverse_of: :revisions after_commit :add_screenshot!, on: :create 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 def snapshot Page.new(title: title, meta_tags: page.meta_tags, head_content: page.head_content, body: body, path: path, redirect_url: redirect_url, status_code: status_code, theme_id: theme_id, category_id: category_id, abstract: abstract, hero_asset_id: hero_asset_id) end def publish! update_attribute :is_published, true end def unpublish! update_attribute :is_published, false end def republish!(options) unpublish! PageRevision.create!(page_id: page_id, title: title, meta_tags: page.meta_tags, head_content: page.head_content, body: body, path: path, redirect_url: redirect_url, status_code: status_code, theme_id: theme_id, category_id: category_id, abstract: abstract, hero_asset_id: hero_asset_id, notes: "Publishing update for template #{options[:template]}: #{options[:notes]}", is_minor: options[:is_minor], author_id: options[:author_id], is_published: true) end def preview_url 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 def preview_path public_preview_page_revision_path(self) end mount_uploader :screenshot, Landable::AssetUploader def screenshot_url screenshot.try(:url) end 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 end end