diff options
Diffstat (limited to 'lib/tasks')
| -rw-r--r-- | lib/tasks/assets.rake | 20 | ||||
| -rw-r--r-- | lib/tasks/node_actions.rake | 61 |
2 files changed, 81 insertions, 0 deletions
diff --git a/lib/tasks/assets.rake b/lib/tasks/assets.rake index 563dfad..9d4b048 100644 --- a/lib/tasks/assets.rake +++ b/lib/tasks/assets.rake | |||
| @@ -16,4 +16,24 @@ namespace :assets do | |||
| 16 | puts "Regenerated variants for Asset##{asset.id} (#{asset.name})" | 16 | puts "Regenerated variants for Asset##{asset.id} (#{asset.name})" |
| 17 | end | 17 | end |
| 18 | end | 18 | end |
| 19 | |||
| 20 | desc "One-shot: flag each live page's current first-by-position image as its headline" | ||
| 21 | task backfill_headline: :environment do | ||
| 22 | count = 0 | ||
| 23 | page_ids = (Node.where.not(head_id: nil).pluck(:head_id) + | ||
| 24 | Node.where.not(draft_id: nil).pluck(:draft_id)).uniq | ||
| 25 | |||
| 26 | Page.where(id: page_ids).find_each do |page| | ||
| 27 | next if page.related_assets.where(headline: true).exists? | ||
| 28 | |||
| 29 | first_image = page.related_assets.joins(:asset).merge(Asset.images).order(:position).first | ||
| 30 | next unless first_image | ||
| 31 | |||
| 32 | first_image.update!(headline: true) | ||
| 33 | count += 1 | ||
| 34 | end | ||
| 35 | |||
| 36 | puts "Flagged #{count} pages' current first image as headline." | ||
| 37 | end | ||
| 38 | |||
| 19 | end | 39 | end |
diff --git a/lib/tasks/node_actions.rake b/lib/tasks/node_actions.rake new file mode 100644 index 0000000..fdd286c --- /dev/null +++ b/lib/tasks/node_actions.rake | |||
| @@ -0,0 +1,61 @@ | |||
| 1 | namespace :node_actions do | ||
| 2 | desc "Rebuild inferred NodeAction entries from existing revisions and " \ | ||
| 3 | "node timestamps. Witnessed entries (inferred_from IS NULL) are " \ | ||
| 4 | "never touched; previously inferred ones are deleted and " \ | ||
| 5 | "regenerated, so the task is idempotent. Scope with NODE_ID=n." | ||
| 6 | task :backfill => :environment do | ||
| 7 | scope = Node.where.not(:parent_id => nil) | ||
| 8 | scope = scope.where(:id => ENV["NODE_ID"]) if ENV["NODE_ID"] | ||
| 9 | |||
| 10 | created = 0 | ||
| 11 | |||
| 12 | ActiveRecord::Base.transaction do | ||
| 13 | stale = NodeAction.where.not(:inferred_from => nil) | ||
| 14 | stale = stale.where(:node_id => ENV["NODE_ID"]) if ENV["NODE_ID"] | ||
| 15 | puts "Removing #{stale.count} previously inferred entries" | ||
| 16 | stale.delete_all | ||
| 17 | |||
| 18 | witnessed_creates = NodeAction.where(:action => "create", :inferred_from => nil).pluck(:node_id).to_set | ||
| 19 | witnessed_promotes = NodeAction.where(:action => "publish", :inferred_from => nil).pluck(:page_id).to_set | ||
| 20 | |||
| 21 | scope.find_each do |node| | ||
| 22 | # Every page row except the current draft was once promoted to | ||
| 23 | # head. Autosaves never carry node_id, so they cannot appear. | ||
| 24 | revisions = node.pages.to_a.reject { |page| page.id == node.draft_id } | ||
| 25 | first_page = node.pages.first | ||
| 26 | |||
| 27 | unless witnessed_creates.include?(node.id) | ||
| 28 | NodeAction.record!( | ||
| 29 | :node => node, :page => first_page, :user => first_page&.editor, | ||
| 30 | :action => "create", | ||
| 31 | :occurred_at => node.created_at, | ||
| 32 | :inferred_from => "from_node_created_at", | ||
| 33 | :title => first_page&.translations&.find_by(:locale => I18n.default_locale)&.title, | ||
| 34 | :path => node.unique_name, | ||
| 35 | :human_readable_node_name => | ||
| 36 | first_page&.translations&.find_by(:locale => I18n.default_locale)&.title) | ||
| 37 | created += 1 | ||
| 38 | end | ||
| 39 | |||
| 40 | previous = nil | ||
| 41 | revisions.each do |page| | ||
| 42 | unless witnessed_promotes.include?(page.id) | ||
| 43 | diff = NodeAction.head_diff(previous, page) | ||
| 44 | NodeAction.record!( | ||
| 45 | :node => node, :page => page, :user => page.editor, | ||
| 46 | :action => "publish", | ||
| 47 | :occurred_at => page.updated_at, | ||
| 48 | :inferred_from => "from_page_revision", | ||
| 49 | :via => "draft", | ||
| 50 | :human_readable_node_name => diff[:title]["to"], | ||
| 51 | **diff) | ||
| 52 | created += 1 | ||
| 53 | end | ||
| 54 | previous = page | ||
| 55 | end | ||
| 56 | end | ||
| 57 | end | ||
| 58 | |||
| 59 | puts "Created #{created} inferred entries" | ||
| 60 | end | ||
| 61 | end | ||
