Skip to content

Instantly share code, notes, and snippets.

@thescubageek
Created September 14, 2016 18:38
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thescubageek/81275185b96c40da7a3f05a87fc1a309 to your computer and use it in GitHub Desktop.
Save thescubageek/81275185b96c40da7a3f05a87fc1a309 to your computer and use it in GitHub Desktop.
Repairs bad drop targets for CMS
def repair_drop_targets(html_id, widgets=[])
bad = Website.all.select do |web|
!web.website_template.blank? && (
web.website_template.drop_targets.find_by_html_id(html_id).blank? ||
web.website_template.drop_targets.find_by_html_id(html_id).widgets.blank?)
end
bad.each do |web|
wt = web.website_template
dt = wt.drop_targets.find_by_html_id(html_id)
if dt.blank?
dt = DropTarget.new({web_template: wt, html_id: html_id})
dt.save
end
web.reload
wt.reload
l = web.owner
widgets.each_with_index do |slug, idx|
unless dt.widgets.any? { |widget| widget.name == slug }
WidgetAdderJob.new.perform(l.id, html_id.gsub('drop-target-',''), slug, idx)
end
end
end
puts "#{bad.count} bad websites repaired: #{bad.map(&:name).join(', ')}"
end
@tmyrick
Copy link

tmyrick commented Sep 14, 2016

This works in production, it found all 3 bad websites and fixed each of them with the appropriate drop target and widgets.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment