Last active
November 23, 2015 16:39
-
-
Save surrealroad/d99d351afa9582606ed6 to your computer and use it in GitHub Desktop.
Shotgun: find all pages a field is used on (Courtesy pboucher)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
=begin | |
Find pages a field is used on | |
1. Edit the last line in the following script to reference the | |
appropriate field & entity | |
2. Paste the whole thing into the Ruby console for the site in | |
question | |
=end | |
class PageUtils | |
# Traverses through all of the PageSettings for | |
grid widgets that are set to "All Results" and resaves | |
them | |
# with results_per_page (100 by | |
default) | |
def | |
self.reset_results_per_page(results_per_page=100) | |
puts "Resetting all Pages | |
with All Results to #{results_per_page} per page" | |
fp = proc do |spec, list, | |
page_id| | |
if | |
spec['settings']['records_per_page'] == 5000 | |
puts | |
"resetting Page ##{page_id}" | |
spec['settings']['records_per_page'] | |
= results_per_page | |
pp | |
page_id | |
end | |
end | |
PageSetting.each do | |
|p| | |
ps = | |
p.settings | |
PageSetting.recurse_widget_spec_by_type(ps,'SG.Widget.NewGrid', | |
fp, [], p.page_id) | |
p.settings = | |
ps | |
p.save | |
end | |
end | |
# Find all pages that a certain field shows up | |
on | |
def self.get_pages_with_field(entity_type, | |
field) | |
# proc to print out where on page | |
field is used | |
fp = proc do |spec, ancestors, | |
info| | |
crumbs = | |
ancestors.collect do |s| | |
s["type"] | |
end | |
crumbs = | |
crumbs.join("->") | |
widget_entity_type = | |
PageSetting.find_entity_type(info[:page_entity_type], spec, | |
ancestors) | |
if (widget_entity_type == | |
entity_type) | |
# Look for | |
strings where you have entity_type.field_name (following a path | |
that refers to the field) or | |
# that has | |
"field_name (ie, a string that starts with field name) | |
if | |
info[:settings_json].include? "\"#{field}" || | |
info[:settings_json].include?("#{entity_type}.#{field}") | |
puts | |
"Page #{info[:page_id]} contains field at | |
#{crumbs}->#{spec["type"]}" | |
end | |
end | |
end | |
info = {} | |
PageSetting.each do | |
|page_setting| | |
next if | |
page_setting.page.nil? | |
spec = | |
page_setting.create_full_merged_settings | |
next if | |
spec.nil? | |
page = | |
page_setting.page | |
info[:page_entity_type] = | |
( page.page_type == 'home' || page.page_type == 'canvas' ) ? | |
'HumanUser' : page.entity_type | |
info[:page_id] = | |
page.id | |
info[:settings_json] = | |
page_setting.settings_json | |
PageSetting.recurse_widget_spec_by_type(spec, | |
"*", fp, [], info) | |
end | |
end | |
end | |
PageUtils.get_pages_with_field('Version', 'sg_editorial_story_status') | |
=begin | |
Ways to parse the output: | |
(assuming output is piped or copied into a text file called | |
fields.txt) | |
To list just the URLs of each line (will include | |
duplicates): | |
cat fields.txt | awk '{printf "https://foo.bar/page/%s\n", | |
$2}' | |
https://foo.bar/page/1558 | |
https://foo.bar/page/1558 | |
https://foo.bar/page/1558 | |
To list the individual page numbers, with a count of how many times | |
they appeared in the list: | |
cat fields.txt | grep -v 'PageUtils' | awk '{print $2}' | sort | | |
uniq -c | |
21 1558 | |
12 1731 | |
15 | |
2543 | |
=end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment