Permalink
Browse files

Now sigining the petition takes you back to the petition page and we …

…track whether you have signed by session
  • Loading branch information...
1 parent 1fb9586 commit c29ee5fc40927523d1a0bcc6ae70ce087d21d23b @jennifersmith jennifersmith committed Apr 25, 2012
View
@@ -0,0 +1,4 @@
+Autotest.add_hook :initialize do |at|
+ at.add_exception(%r{^\./\.git})
+ at.add_exception(%r{^\./log})
+end
@@ -7,6 +7,8 @@ def index
def show
@petition = Petition.find(params[:id])
+ signed_petitions = session[:signed_petitions] || []
+ @user_has_signed = signed_petitions.include? @petition.id
end
def new
@@ -5,5 +5,8 @@ def create
signature.ip_address = request.remote_ip
signature.user_agent = request.env["HTTP_USER_AGENT"]
petition.signatures.push(signature)
+ session[:signed_petitions] ||= []
+ session[:signed_petitions] << petition.id
+ redirect_to petition_url(petition)
end
end
@@ -1,28 +1,30 @@
-<p id="notice"><%= notice %></p>
-<p>
- <b>Title:</b>
+<h1>
<%= @petition.title %>
-</p>
+</h1>
<p>
- <b>Description:</b>
<%= @petition.description %>
</p>
-
-<%= form_for([@petition, Signature.new]) do | f | %>
- <%= f.label :name %>
- <%= f.text_field :name %>
- <%= f.label :email %>
- <%= f.text_field :email %>
- <input type="submit" value="Sign!"/>
+<% if @user_has_signed %>
+<p>Thanks for signing -- please share on Facebook:</p>
+<%= facebook_like petition_url(params[:petition_id]) %>
+ <%= link_to_if current_user, 'Edit', edit_petition_path(@petition),
+ class: "btn btn-primary icon-edit icon-white" if current_user %>
+ <%= link_to 'Back', petitions_path, class: "btn" %>
+<% else %>
+<div class="hero-unit">
+ <%= form_for([@petition, Signature.new]) do | f | %>
+ <%= f.label :name %>
+ <%= f.text_field :name %>
+ <%= f.label :email %>
+ <%= f.text_field :email %>
+ <input type="submit" value="Sign!" class="btn btn-primary btn-large"/>
+ <% end %>
+</div>
<% end %>
-
-<%= link_to 'Edit', edit_petition_path(@petition) %> |
-<%= link_to 'Back', petitions_path %>
-
<% content_for(:meta_tags) do %>
<% petition_to_open_graph(@petition).each do |key, value|%>
<%= content_tag "meta", nil, property:key, content:value %>
<% end %>
-<% end %>
+<% end %>
@@ -1,6 +0,0 @@
-<p>Thanks for signing -- please share on Facebook:</p>
-
-<%= facebook_like petition_url(params[:petition_id]) %>
-
-
-
@@ -18,10 +18,21 @@ def valid_attributes
end
describe "GET show" do
- it "assigns the requested petition as @petition" do
- petition = create(:petition)
- get :show, {:id => petition.to_param}, valid_session
- assigns(:petition).should eq(petition)
+ let(:petition) { create(:petition) }
+ before :each do
+ get :show, {:id => petition.to_param}, user_session
+ end
+ context "the user has already signed the petition" do
+ let(:user_session) { {signed_petitions: [petition.id]} }
+ it "sets a flag flag for the view" do
+ assigns(:user_has_signed).should be_true
+ end
+ end
+ context "the user has not already signed the petition" do
+ let (:user_session) { {} }
+ it "sets a flag flag for the view" do
+ assigns(:user_has_signed).should be_false
+ end
end
end
@@ -1,8 +1,8 @@
require 'spec_helper'
describe SignaturesController do
- context "signing a petition" do
- let(:petition){ create(:petition) }
+ let(:petition){ create(:petition) }
+ context "POST create" do
before(:each) do
post :create, :petition_id => petition.id, :signature => {:name => "Bob", :email => "bob@my.com"}
end
@@ -14,5 +14,7 @@
its(:ip_address) { should == "0.0.0.0" }
its(:user_agent) { should == "Rails Testing" }
end
+ it {should redirect_to petition_url(petition)}
+ its(:session) { should include(:signed_petitions => [petition.id]) }
end
end

0 comments on commit c29ee5f

Please sign in to comment.