Skip to content

Instantly share code, notes, and snippets.

View thescubageek's full-sized avatar
🤿
Are you for scuba?

Steve Craig thescubageek

🤿
Are you for scuba?
  • @evidation
  • Wilmington, NC
View GitHub Profile
@thescubageek
thescubageek / file_to_array.rb
Created February 17, 2016 16:33
File to Array - Ruby
def file_to_array(file)
arr = []
if File.exists?(file)
f = File.read(file)
f.each_line { |line| arr << line.strip unless line == "\n" }
end
arr
end
@thescubageek
thescubageek / array_to_file.rb
Created August 8, 2016 21:31
Array to File
def array_to_file(arr, file)
File.open(file, 'w') { |f| f.write(arr.join("\n")) } unless arr.blank?
end
@thescubageek
thescubageek / reseed_websites_missing_website_templates.rb
Last active September 6, 2016 14:10
Reseeds websites missing website_templates
## G5 Content Management System
Website.all.select { |web| web.website_template.blank? }.each do |web|
WebsiteSeederJob.perform_async(web.owner.id)
end
## G5 Hub
def get_urn(url)
url.gsub("https://","").gsub(".herokuapp.com","")
end
## Find all internal clients
Client.where(g5_internal: true).map { |c| get_urn(c.cms_url) }
## Find all real clients
@thescubageek
thescubageek / repair_drop_targets.rb
Created September 14, 2016 18:38
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)
@thescubageek
thescubageek / find_matching_settings.rb
Last active September 14, 2016 22:01
Finds settings (and corresponding owners) matching string pattern
## G5 Content Management System
def find_setting_value(target)
Setting.where(Setting.arel_table[:value].matches("%#{target}%"))
end
def find_setting_value_widgets(target)
find_setting_value(target).map do |setting|
setting.owner
end.uniq
class Array
def to_file(filepath, sep="\n")
File.open(filepath, 'w') { |f| f.write(join(sep)) } unless blank?
end
def squash
blank? ? [] : flatten.compact.uniq
end
end
@thescubageek
thescubageek / find_multi_website_locations.rb
Created September 19, 2016 19:18
finds locations with multiple websites - CMS
def find_multi_website_locations
Location.all.select { |l| Website.where(owner_id: l.id).count > 1 }
end
@thescubageek
thescubageek / find_phone_widgets_with_phone_number_values.rb
Last active March 4, 2017 14:34
finds phone number widgets with values for the phone_number field
widgets = Widget.includes(:garden_widget, :settings).by_name('Phone Number').all
settings = widgets.map { |w| w.settings.where_name('phone_number').all }.squish.select { |s| s.value.present? && && s.value != "{{location_phone_number}}" }
ret = settings.inject("") { |str, s| str << "- #{s.website.name} (#{s.website.owner.urn}) - #{s.website.status}): #{s.value}\n"; str }
puts "#{Client.client.name} (#{Client.client.urn}):\n#{ret}\n" unless ret.blank?
@thescubageek
thescubageek / serialize_and_upload_bad_websites.rb
Created March 16, 2017 16:45
write bad duplicate websites to s3
dups = Website.all.inject({}) { |h,w| h[w.owner_id] ||= 0; h[w.owner_id] += 1; h }.reject! { |k,v| v <= 1 }
bad_webs = Website.where(owner_id: dups.keys).all.sort_by(&:name)
bad_files = bad_webs.inject({}) do |h, w|
file_name = "#{w.id}-#{w.name.parameterize}"
mgr = Saves::WebsiteSavesManager.new("steve.craig@getg5.com", w.owner)
file_path = mgr.send(:create_save_file, file_name)
s3 = S3Bucket.new(w.owner, {s3_folder: 'websites-saves'})
file = s3.upload(file_path, file_name)
h[w.id] = { owner_id: w.owner_id, file: s3.bucket_file_url(file_name) }