Guest User

Untitled

a guest
Apr 10th, 2018
555
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.98 KB | None | 0 0
  1. class Politicians < Application
  2.  
  3. def postcode
  4. load_pollies()
  5. render
  6. end
  7.  
  8. def index()
  9. @pollies = Politician.find(:all, :order => "postcode")
  10. render
  11. end
  12.  
  13. def email()
  14. @postcode = request.params[:postcode]
  15. result = Politician.find_by_postcode(@postcode)
  16.  
  17. if result.nil?
  18. redirect ("postcode", :message => "There are no politicians listed in #{@postcode}")
  19. else
  20. @pollies = result.is_a?(Array) ? result : [result]
  21. render
  22. end
  23. end
  24.  
  25. def send_email()
  26. politicians_to_email = []
  27. for key in params.keys()
  28. if key =~/pol_/
  29. id = key[3..-1]
  30. politician = Politician.find(id.to_i)
  31. #politicians_to_email << politician.email
  32. politicians_to_email << "nicholas.faiz@gmail.com"
  33. end
  34. end
  35.  
  36. for email in politicians_to_email
  37. Merb.logger.info("Emailing #{email}")
  38. send_mail(PoliticianMailer, :deliver, {
  39. :from => "nicholas.faiz@gmail.com",
  40. :to => email,
  41. :subject => "Message from #{params[:emailer_name]}"},
  42. {:message => params[:message]})
  43. end
  44.  
  45. render :template => "politicians/email_sent"
  46. end
  47.  
  48. def send_to_friend()
  49. friend_email = params[:friend_email]
  50.  
  51. send_mail(PoliticianMailer, :send_to_friend, {
  52. :from => "nicholas.faiz@gmail.com",
  53. :to => friend_email,
  54. :subject => "Message from #{params[:emailer_name]}"},
  55. {:message => params[:message]})
  56.  
  57. render :template => "politicians/sent_to_friend"
  58. end
  59.  
  60. private
  61. require 'csv'
  62. def load_pollies()
  63. if Politician.find(:all).empty?
  64. CSV.open("#{Merb.root}/reps.csv", "r", ?,, ?\r) do |row|
  65. polly = Politician.new(:party => row[0], :surname => row[1], :firstname => row[2],
  66. :email => row[10], :postcode => row[18], :electorate => row[14])
  67. begin
  68. polly.save
  69. rescue
  70. end
  71. end
  72. end
  73. end
  74.  
  75. end
Add Comment
Please, Sign In to add comment