action_page_controller.rb 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. class ActionPageController < ApplicationController
  2. before_filter :set_action_page,
  3. :protect_unpublished,
  4. :redirect_to_specified_url,
  5. :redirect_from_archived_to_active_action,
  6. only: [:show, :show_by_institution, :embed_iframe, :signature_count]
  7. before_filter :redirect_to_cannonical_slug, only: [:show]
  8. before_filter :set_institution, only: [:show_by_institution, :filter]
  9. before_filter :set_action_display_variables, only: [:show, :show_by_institution, :embed_iframe, :signature_count]
  10. skip_before_filter :verify_authenticity_token, only: :embed
  11. after_action :allow_iframe, only: :embed_iframe
  12. manifest :action_page
  13. def show
  14. render @actionPage.template, layout: @actionPage.layout
  15. end
  16. def index
  17. @actionPages = ActionPage.where(published: true, archived: false, victory: false).
  18. paginate(page: params[:page], per_page: 9).
  19. order(created_at: :desc)
  20. @actionPages = @actionPages.categorized(params[:category]) if params[:category].present?
  21. respond_to do |format|
  22. format.html
  23. format.atom
  24. format.json
  25. end
  26. end
  27. def embed
  28. render "action_page/embed.js.erb", layout: false
  29. end
  30. def embed_iframe
  31. @css = params[:css] if params.include? :css
  32. @target_bioguide_ids = params[:bioguide_ids] if params.include? :bioguide_ids
  33. @location = location_params
  34. render layout: "application-blank"
  35. end
  36. def signature_count
  37. @actionPage = ActionPage.friendly.find(params[:id])
  38. if petition = @actionPage.petition
  39. render text: petition.signatures.count
  40. else
  41. render text: "0"
  42. end
  43. end
  44. def show_by_institution
  45. respond_to do |format|
  46. format.csv { send_data @petition.to_affiliation_csv(@institution) }
  47. format.html { render @actionPage.template, layout: @actionPage.layout }
  48. end
  49. end
  50. def filter
  51. redirect_to institution_action_page_url(params[:id], @institution)
  52. end
  53. private
  54. def set_action_page
  55. @actionPage = ActionPage.friendly.find(params[:id].strip)
  56. end
  57. def protect_unpublished
  58. unless @actionPage.published?
  59. if current_user.try(:admin?) || current_user.try(:collaborator?)
  60. flash.now[:notice] = "This page is not published. Only Admins and Collaborators can view it."
  61. else
  62. raise ActiveRecord::RecordNotFound
  63. end
  64. end
  65. end
  66. def redirect_to_specified_url
  67. if @actionPage.enable_redirect
  68. redirect_to @actionPage.redirect_url, status: 301
  69. end
  70. end
  71. def redirect_from_archived_to_active_action
  72. if @actionPage.redirect_from_archived_to_active_action?
  73. # Users can access actions they've taken in the past as a historical record
  74. unless current_user and (current_user.taken_action? @actionPage or current_user.admin?)
  75. redirect_to @actionPage.active_action_page_for_redirect
  76. end
  77. end
  78. end
  79. def redirect_to_cannonical_slug
  80. redirect_to(@actionPage) unless request.path == action_page_path(@actionPage)
  81. end
  82. def set_action_display_variables
  83. @title = @actionPage.title
  84. @petition = @actionPage.petition
  85. @tweet = @actionPage.tweet
  86. @email_campaign = @actionPage.email_campaign
  87. @congress_message_campaign = @actionPage.congress_message_campaign
  88. # Shows a mailing list if no tools enabled
  89. @no_tools = [:tweet, :petition, :call, :email, :congress_message].none? do |tool|
  90. @actionPage.send "enable_#{tool}".to_sym
  91. end
  92. set_signatures
  93. if @actionPage.petition and @actionPage.petition.enable_affiliations
  94. @top_institutions = @actionPage.institutions.top(300, first: @institution.try(:id))
  95. @institutions = @actionPage.institutions.order(:name)
  96. end
  97. @topic_category = nil
  98. if @email_campaign and !@email_campaign.topic_category.nil?
  99. @topic_category = @email_campaign.topic_category.as_2d_array
  100. end
  101. if @congress_message_campaign.try(:topic_category).present?
  102. @topic_category = @congress_message_campaign.topic_category.as_2d_array
  103. end
  104. # Initialize a temporary signature object for form auto-population
  105. current_zipcode = params[:zipcode] || current_user.try(:zipcode)
  106. @signature = Signature.new(petition_id: @actionPage.petition_id)
  107. @signature.attributes = { first_name: current_first_name,
  108. last_name: current_last_name,
  109. street_address: current_street_address,
  110. state: current_state,
  111. city: current_city,
  112. zipcode: current_zipcode,
  113. country_code: current_country_code,
  114. email: current_email }
  115. end
  116. def set_signatures
  117. if @petition
  118. # Signatures filtered by institution
  119. if @institution
  120. @signatures = @petition.signatures_by_institution(@institution)
  121. .paginate(page: params[:page], per_page: 9)
  122. .order(created_at: :desc)
  123. @institution_signature_count = @signatures.pretty_count
  124. # Signatures with associated affiliations
  125. elsif @petition.enable_affiliations
  126. @signatures = @petition.signatures
  127. .includes(affiliations: [:institution, :affiliation_type])
  128. .paginate(page: params[:page], per_page: 9)
  129. .order(created_at: :desc)
  130. # Signatures, no affiliations
  131. else
  132. @signatures = @petition.signatures.order(created_at: :desc).limit(5)
  133. end
  134. @signature_count = @petition.signatures.pretty_count
  135. @require_location = !@petition.enable_affiliations
  136. end
  137. end
  138. def set_institution
  139. @institution = Institution.friendly.find(params[:institution_id])
  140. end
  141. def location_params
  142. params.permit(:zip4, :zipcode, :street, :city, :state)
  143. end
  144. def allow_iframe
  145. response.headers.except! "X-Frame-Options"
  146. end
  147. end