application_helper.rb 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476
  1. module ApplicationHelper
  2. def page_title
  3. [@title, I18n.t("site_title")].compact.join(" | ")
  4. end
  5. def escape_page_title
  6. URI.escape(page_title , Regexp.new("[^#{URI::PATTERN::UNRESERVED}]"))
  7. end
  8. def twitter_handle
  9. "@" + Rails.application.config.twitter_handle.to_s
  10. end
  11. def markdown(blogtext)
  12. blogtext = substitute_keywords(blogtext)
  13. renderOptions = { hard_wrap: true, filter_html: false }
  14. markdownOptions = { autolink: true, no_intra_emphasis: true, fenced_code_blocks: true }
  15. markdown = Redcarpet::Markdown.new(MarkdownRenderer.new(renderOptions), markdownOptions)
  16. markdown.render(blogtext).html_safe # rubocop:disable Rails/OutputSafety
  17. end
  18. def substitute_keywords(blogtext)
  19. if @actionPage and @actionPage.description and @petition
  20. blogtext.gsub("$SIGNATURECOUNT", @petition.signatures.pretty_count)
  21. else
  22. blogtext
  23. end
  24. end
  25. def stripdown(str)
  26. require "redcarpet/render_strip"
  27. renderer = Redcarpet::Markdown.new(Redcarpet::Render::StripDown)
  28. renderer.render(str)
  29. end
  30. def country_codes
  31. [
  32. ["Afghanistan", "AF"],
  33. ["Aland Islands", "AX"],
  34. ["Albania", "AL"],
  35. ["Algeria", "DZ"],
  36. ["American Samoa", "AS"],
  37. ["Andorra", "AD"],
  38. ["Angola", "AO"],
  39. ["Anguilla", "AI"],
  40. ["Antarctica", "AQ"],
  41. ["Antigua and Barbuda", "AG"],
  42. ["Argentina", "AR"],
  43. ["Armenia", "AM"],
  44. ["Aruba", "AW"],
  45. ["Ascension Island", "AC"],
  46. ["Australia", "AU"],
  47. ["Austria", "AT"],
  48. ["Azerbaijan", "AZ"],
  49. ["Bahamas", "BS"],
  50. ["Bahrain", "BH"],
  51. ["Bangladesh", "BD"],
  52. ["Barbados", "BB"],
  53. ["Belarus", "BY"],
  54. ["Belgium", "BE"],
  55. ["Belize", "BZ"],
  56. ["Benin", "BJ"],
  57. ["Bermuda", "BM"],
  58. ["Bhutan", "BT"],
  59. ["Bolivia, Plurinational State of", "BO"],
  60. ["Bonaire, Sint Eustatius and Saba", "BQ"],
  61. ["Bosnia and Herzegovina", "BA"],
  62. ["Botswana", "BW"],
  63. ["Bouvet Island", "BV"],
  64. ["Brazil", "BR"],
  65. ["British Indian Ocean Territory", "IO"],
  66. ["Brunei Darussalam", "BN"],
  67. ["Bulgaria", "BG"],
  68. ["Burkina Faso", "BF"],
  69. ["Burundi", "BI"],
  70. ["Cambodia", "KH"],
  71. ["Cameroon", "CM"],
  72. ["Canada", "CA"],
  73. ["Cape Verde", "CV"],
  74. ["Cayman Islands", "KY"],
  75. ["Central African Republic", "CF"],
  76. ["Chad", "TD"],
  77. ["Chile", "CL"],
  78. ["China", "CN"],
  79. ["Christmas Island", "CX"],
  80. ["Cocos (Keeling) Islands", "CC"],
  81. ["Colombia", "CO"],
  82. ["Comoros", "KM"],
  83. ["Congo", "CG"],
  84. ["Congo, the Democratic Republic of the", "CD"],
  85. ["Cook Islands", "CK"],
  86. ["Costa Rica", "CR"],
  87. ["Cote d'Ivoire", "CI"],
  88. ["Croatia", "HR"],
  89. ["Cuba", "CU"],
  90. ["Curacao", "CW"],
  91. ["Cyprus", "CY"],
  92. ["Czech Republic", "CZ"],
  93. ["Denmark", "DK"],
  94. ["Djibouti", "DJ"],
  95. ["Dominica", "DM"],
  96. ["Dominican Republic", "DO"],
  97. ["Ecuador", "EC"],
  98. ["Egypt", "EG"],
  99. ["El Salvador", "SV"],
  100. ["Equatorial Guinea", "GQ"],
  101. ["Eritrea", "ER"],
  102. ["Estonia", "EE"],
  103. ["Ethiopia", "ET"],
  104. ["Falkland Islands (Malvinas)", "FK"],
  105. ["Faroe Islands", "FO"],
  106. ["Fiji", "FJ"],
  107. ["Finland", "FI"],
  108. ["France", "FR"],
  109. ["French Guiana", "GF"],
  110. ["French Polynesia", "PF"],
  111. ["French Southern Territories", "TF"],
  112. ["Gabon", "GA"],
  113. ["Gambia", "GM"],
  114. ["Georgia", "GE"],
  115. ["Germany", "DE"],
  116. ["Ghana", "GH"],
  117. ["Gibraltar", "GI"],
  118. ["Greece", "GR"],
  119. ["Greenland", "GL"],
  120. ["Grenada", "GD"],
  121. ["Guadeloupe", "GP"],
  122. ["Guam", "GU"],
  123. ["Guatemala", "GT"],
  124. ["Guernsey", "GG"],
  125. ["Guinea", "GN"],
  126. ["Guinea-Bissau", "GW"],
  127. ["Guyana", "GY"],
  128. ["Haiti", "HT"],
  129. ["Heard Island and McDonald Islands", "HM"],
  130. ["Holy See (Vatican City State)", "VA"],
  131. ["Honduras", "HN"],
  132. ["Hong Kong", "HK"],
  133. ["Hungary", "HU"],
  134. ["Iceland", "IS"],
  135. ["India", "IN"],
  136. ["Indonesia", "ID"],
  137. ["Iran, Islamic Republic of", "IR"],
  138. ["Iraq", "IQ"],
  139. ["Ireland", "IE"],
  140. ["Isle of Man", "IM"],
  141. ["Israel", "IL"],
  142. ["Italy", "IT"],
  143. ["Jamaica", "JM"],
  144. ["Japan", "JP"],
  145. ["Jersey", "JE"],
  146. ["Jordan", "JO"],
  147. ["Kazakhstan", "KZ"],
  148. ["Kenya", "KE"],
  149. ["Kiribati", "KI"],
  150. ["Korea, Democratic People's Republic of", "KP"],
  151. ["Korea, Republic of", "KR"],
  152. ["Kosovo", "KV"],
  153. ["Kuwait", "KW"],
  154. ["Kyrgyzstan", "KG"],
  155. ["Lao People's Democratic Republic", "LA"],
  156. ["Latvia", "LV"],
  157. ["Lebanon", "LB"],
  158. ["Lesotho", "LS"],
  159. ["Liberia", "LR"],
  160. ["Libya", "LY"],
  161. ["Liechtenstein", "LI"],
  162. ["Lithuania", "LT"],
  163. ["Luxembourg", "LU"],
  164. ["Macao", "MO"],
  165. ["Macedonia, The Former Yugoslav Republic Of", "MK"],
  166. ["Madagascar", "MG"],
  167. ["Malawi", "MW"],
  168. ["Malaysia", "MY"],
  169. ["Maldives", "MV"],
  170. ["Mali", "ML"],
  171. ["Malta", "MT"],
  172. ["Marshall Islands", "MH"],
  173. ["Martinique", "MQ"],
  174. ["Mauritania", "MR"],
  175. ["Mauritius", "MU"],
  176. ["Mayotte", "YT"],
  177. ["Mexico", "MX"],
  178. ["Micronesia, Federated States of", "FM"],
  179. ["Moldova, Republic of", "MD"],
  180. ["Monaco", "MC"],
  181. ["Mongolia", "MN"],
  182. ["Montenegro", "ME"],
  183. ["Montserrat", "MS"],
  184. ["Morocco", "MA"],
  185. ["Mozambique", "MZ"],
  186. ["Myanmar", "MM"],
  187. ["Namibia", "NA"],
  188. ["Nauru", "NR"],
  189. ["Nepal", "NP"],
  190. ["Netherlands", "NL"],
  191. ["Netherlands Antilles", "AN"],
  192. ["New Caledonia", "NC"],
  193. ["New Zealand", "NZ"],
  194. ["Nicaragua", "NI"],
  195. ["Niger", "NE"],
  196. ["Nigeria", "NG"],
  197. ["Niue", "NU"],
  198. ["Norfolk Island", "NF"],
  199. ["Northern Mariana Islands", "MP"],
  200. ["Norway", "NO"],
  201. ["Oman", "OM"],
  202. ["Pakistan", "PK"],
  203. ["Palau", "PW"],
  204. ["Palestinian Territory, Occupied", "PS"],
  205. ["Panama", "PA"],
  206. ["Papua New Guinea", "PG"],
  207. ["Paraguay", "PY"],
  208. ["Peru", "PE"],
  209. ["Philippines", "PH"],
  210. ["Pitcairn", "PN"],
  211. ["Poland", "PL"],
  212. ["Portugal", "PT"],
  213. ["Puerto Rico", "PR"],
  214. ["Qatar", "QA"],
  215. ["Reunion", "RE"],
  216. ["Romania", "RO"],
  217. ["Russian Federation", "RU"],
  218. ["Rwanda", "RW"],
  219. ["Saint Barthelemy", "BL"],
  220. ["Saint Helena, Ascension and Tristan da Cunha", "SH"],
  221. ["Saint Kitts and Nevis", "KN"],
  222. ["Saint Lucia", "LC"],
  223. ["Saint Martin (French part)", "MF"],
  224. ["Saint Pierre and Miquelon", "PM"],
  225. ["Saint Vincent and the Grenadines", "VC"],
  226. ["Samoa", "WS"],
  227. ["San Marino", "SM"],
  228. ["Sao Tome and Principe", "ST"],
  229. ["Saudi Arabia", "SA"],
  230. ["Senegal", "SN"],
  231. ["Serbia", "RS"],
  232. ["Seychelles", "SC"],
  233. ["Sierra Leone", "SL"],
  234. ["Singapore", "SG"],
  235. ["Sint Maarten (Dutch part)", "SX"],
  236. ["Slovakia", "SK"],
  237. ["Slovenia", "SI"],
  238. ["Solomon Islands", "SB"],
  239. ["Somalia", "SO"],
  240. ["South Africa", "ZA"],
  241. ["South Georgia and the South Sandwich Islands", "GS"],
  242. ["South Sudan, Republic of", "SS"],
  243. ["Spain", "ES"],
  244. ["Sri Lanka", "LK"],
  245. ["Sudan", "SD"],
  246. ["Suriname", "SR"],
  247. ["Svalbard and Jan Mayen", "SJ"],
  248. ["Swaziland", "SZ"],
  249. ["Sweden", "SE"],
  250. ["Switzerland", "CH"],
  251. ["Syrian Arab Republic", "SY"],
  252. ["Taiwan", "TW"],
  253. ["Tajikistan", "TJ"],
  254. ["Tanzania, United Republic of", "TZ"],
  255. ["Thailand", "TH"],
  256. ["Timor-Leste", "TL"],
  257. ["Togo", "TG"],
  258. ["Tokelau", "TK"],
  259. ["Tonga", "TO"],
  260. ["Trinidad and Tobago", "TT"],
  261. ["Tristan da Cunha", "TA"],
  262. ["Tunisia", "TN"],
  263. ["Turkey", "TR"],
  264. ["Turkmenistan", "TM"],
  265. ["Turks and Caicos Islands", "TC"],
  266. ["Tuvalu", "TV"],
  267. ["Uganda", "UG"],
  268. ["Ukraine", "UA"],
  269. ["United Arab Emirates", "AE"],
  270. ["United Kingdom", "GB"],
  271. ["United States", "US"],
  272. ["United States Minor Outlying Islands", "UM"],
  273. ["Uruguay", "UY"],
  274. ["Uzbekistan", "UZ"],
  275. ["Vanuatu", "VU"],
  276. ["Venezuela, Bolivarian Republic of", "VE"],
  277. ["Viet Nam", "VN"],
  278. ["Virgin Islands, British", "VG"],
  279. ["Virgin Islands, U.S.", "VI"],
  280. ["Wallis and Futuna", "WF"],
  281. ["Western Sahara", "EH"],
  282. ["Yemen", "YE"],
  283. ["Zambia", "ZM"],
  284. ["Zimbabwe", "ZW"]
  285. ]
  286. end
  287. def us_states_hash
  288. {
  289. "Alabama" => "AL",
  290. "Alaska" => "AK",
  291. "American Samoa" => "AS",
  292. "Arizona" => "AZ",
  293. "Arkansas" => "AR",
  294. "California" => "CA",
  295. "Colorado" => "CO",
  296. "Connecticut" => "CT",
  297. "Delaware" => "DE",
  298. "District of Columbia" => "DC",
  299. "Florida" => "FL",
  300. "Georgia" => "GA",
  301. "Guam" => "GU",
  302. "Hawaii" => "HI",
  303. "Idaho" => "ID",
  304. "Illinois" => "IL",
  305. "Indiana" => "IN",
  306. "Iowa" => "IA",
  307. "Kansas" => "KS",
  308. "Kentucky" => "KY",
  309. "Louisiana" => "LA",
  310. "Maine" => "ME",
  311. "Maryland" => "MD",
  312. "Massachusetts" => "MA",
  313. "Michigan" => "MI",
  314. "Minnesota" => "MN",
  315. "Mississippi" => "MS",
  316. "Missouri" => "MO",
  317. "Montana" => "MT",
  318. "Nebraska" => "NE",
  319. "Nevada" => "NV",
  320. "New Hampshire" => "NH",
  321. "New Jersey" => "NJ",
  322. "New Mexico" => "NM",
  323. "New York" => "NY",
  324. "North Carolina" => "NC",
  325. "North Dakota" => "ND",
  326. "Northern Mariana Islands" => "MP",
  327. "Ohio" => "OH",
  328. "Oklahoma" => "OK",
  329. "Oregon" => "OR",
  330. "Pennsylvania" => "PA",
  331. "Puerto Rico" => "PR",
  332. "Rhode Island" => "RI",
  333. "South Carolina" => "SC",
  334. "South Dakota" => "SD",
  335. "Tennessee" => "TN",
  336. "Texas" => "TX",
  337. "Utah" => "UT",
  338. "Vermont" => "VT",
  339. "Virgin Islands" => "VI",
  340. "Virginia" => "VA",
  341. "Washington" => "WA",
  342. "West Virginia" => "WV",
  343. "Wisconsin" => "WI",
  344. "Wyoming" => "WY"
  345. }
  346. end
  347. def us_states
  348. us_states_hash.to_a
  349. end
  350. def current_email
  351. current_user_data(:email)
  352. end
  353. def current_first_name
  354. current_user_data(:first_name)
  355. end
  356. def current_last_name
  357. current_user_data(:last_name)
  358. end
  359. def current_street_address
  360. current_user_data(:street_address)
  361. end
  362. def current_city
  363. current_user_data(:city)
  364. end
  365. def current_state
  366. current_user_data(:state)
  367. end
  368. def current_zipcode
  369. current_user_data(:zipcode)
  370. end
  371. def current_country_code
  372. current_user_data(:country_code)
  373. end
  374. def congress_forms_date_fills_url(campaign_tag = nil, date_start = nil, date_end = nil, bioguide_id = nil)
  375. url = Rails.application.config.congress_forms_url + "/successful-fills-by-date/"
  376. url = url + bioguide_id unless bioguide_id.nil?
  377. params = {
  378. campaign_tag: campaign_tag,
  379. time_zone: Time.zone.name,
  380. give_as_utc: true,
  381. debug_key: Rails.application.secrets.congress_forms_debug_key,
  382. date_start: date_start,
  383. date_end: date_end
  384. }
  385. params.reject! { |_, v| v.to_param.nil? }
  386. url << "?#{params.to_query}"
  387. end
  388. def congress_forms_hour_fills_url(campaign_tag = nil, date = nil, bioguide_id = nil)
  389. url = Rails.application.config.congress_forms_url + "/successful-fills-by-hour/"
  390. url = url + bioguide_id unless bioguide_id.nil?
  391. params = {
  392. campaign_tag: campaign_tag,
  393. time_zone: Time.zone.name,
  394. give_as_utc: true,
  395. debug_key: Rails.application.secrets.congress_forms_debug_key,
  396. date: date.strftime("%Y-%m-%d")
  397. }
  398. params.reject! { |_, v| v.to_param.nil? }
  399. url << "?#{params.to_query}"
  400. end
  401. def congress_forms_member_fills_url(campaign_tag = nil)
  402. url = Rails.application.config.congress_forms_url + "/successful-fills-by-member/"
  403. params = {
  404. campaign_tag: campaign_tag,
  405. debug_key: Rails.application.secrets.congress_forms_debug_key
  406. }
  407. params.reject! { |_, v| v.to_param.nil? }
  408. url << "?#{params.to_query}"
  409. end
  410. def update_user_data(params = {})
  411. params = params.with_indifferent_access.slice(*user_session_data_whitelist)
  412. if user_signed_in?
  413. p = params.clone
  414. p.delete(:email)
  415. current_user.update_attributes p
  416. end
  417. end
  418. def sanitize_filename(filename)
  419. # Split the name when finding a period which is preceded by some
  420. # character, and is followed by some character other than a period,
  421. # if there is no following period that is followed by something
  422. # other than a period (yeah, confusing, I know)
  423. fn = filename.split /(?<=.)\.(?=[^.])(?!.*\.[^.])/m
  424. # We now have one or two parts (depending on whether we could find
  425. # a suitable period). For each of these parts, replace any unwanted
  426. # sequence of characters with an underscore
  427. fn.map! { |s| s.gsub /[^a-z0-9\-]+/i, "_" }
  428. # Finally, join the parts with a period and return the result
  429. return fn.join "."
  430. end
  431. def can?(ability)
  432. current_user.try(:can?, ability)
  433. end
  434. def pluralize_with_span(count, noun)
  435. noun = count == 1 ? noun : noun.pluralize
  436. content_tag(:span, count) + " #{noun}"
  437. end
  438. private
  439. def user_session_data_whitelist
  440. [:email, :last_name, :first_name, :street_address, :city, :state, :zipcode,
  441. :country_code, :phone]
  442. end
  443. def current_user_data(field)
  444. return nil unless user_session_data_whitelist.include? field
  445. current_user.try(field)
  446. end
  447. end