Skip to content

Commit

Permalink
Merge pull request #43 from datamade/master
Browse files Browse the repository at this point in the history
wire up 'sign on' buttons for questions

Most of remaining for #20
Closes #43, #38
  • Loading branch information
walter committed Aug 13, 2013
2 parents 9b200d6 + 4d75c24 commit 559cb4e
Show file tree
Hide file tree
Showing 11 changed files with 142 additions and 16 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@ latest-mongo-dump.tar.gz
/config/initializers/mongoid_billy_shim.rb
/coverage
/public/uploads
.DS_Store
.DS_Store
dump.rdb
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ We require these to be installed:
* nodejs
* mongodb # should also be running
* redis # should also be running
* phantomjs # for running the request spec

Depending on your OS , you may also need to install these:

Expand Down
1 change: 1 addition & 0 deletions app/assets/javascripts/application.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,4 @@
//= require responsive
//= require forms
//= require rails.validations
//= require sign_on_user
16 changes: 16 additions & 0 deletions app/assets/javascripts/sign_on_user.js.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
$(".sign-on-user").click ->
self = $(this)
$.ajax
url: "/signatures?question_id=" + self.data("question-id")
type: "POST"
success: (resp) ->
self.after "<a class='sign_on'>Signed On</a>"
self.hide()
question_id = self.data("question-id")
current_count = $("[data-signature-question-id='" + question_id + "']")
current_count.html parseInt(current_count.html()) + 1

error: (resp) ->
console.log "coundnt save!"

false
33 changes: 33 additions & 0 deletions app/controllers/registrations_controller.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,29 @@
class RegistrationsController < Devise::RegistrationsController

def create
build_resource

if resource.save
if resource.active_for_authentication?
set_flash_message :notice, :signed_up if is_navigational_format?
sign_up(resource_name, resource)
if params[:question_id]
add_signature_to_question resource, params[:question_id]
else
respond_with resource, :location => after_sign_up_path_for(resource)
end
else
set_flash_message :notice, :"signed_up_but_#{resource.inactive_message}" if is_navigational_format?
expire_session_data_after_sign_in!
respond_with resource, :location => after_inactive_sign_up_path_for(resource)
end
else
clean_up_passwords resource
respond_with resource
end
end


# Unlike Devise, no destruction (until we sort out orphaned content).
def destroy
not_found
Expand All @@ -9,4 +34,12 @@ def destroy
def after_update_path_for(resource)
user_path(resource)
end

def add_signature_to_question(user, question_id)
if question_id
question = Question.find(question_id)
user.signatures.new(question_id: question.id).save
respond_with user, :location => question_path(question.state, question.id)
end
end
end
11 changes: 8 additions & 3 deletions app/controllers/signatures_controller.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
class SignaturesController < ApplicationController
respond_to :html, :js
respond_to :json
before_filter :authenticate_user!

def create
@signature = current_user.signatures.create!(question_id: params[:question_id])
respond_with(@signature) # @todo redirect if HTML, success if JS, rescue and failure if uniqueness validations fail
@signature = current_user.signatures.new(question_id: params[:question_id])

if @signature.save
render json: @signature, status: :created
else
render json: @signature.errors, status: :unprocessable_entity
end
end
end
4 changes: 4 additions & 0 deletions app/helpers/questions_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,8 @@ module QuestionsHelper
def step_class_for(step, relevant_steps)
raw('class="not-first-step"') if relevant_steps.first.to_s != step
end

def question_progress_percent(question)
question.signature_count.to_f / question.person.signature_threshold.to_f * 100.0
end
end
5 changes: 5 additions & 0 deletions app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,11 @@ def questions_signed
Question.find(signatures.map(&:question_id))
end

# @return [Boolean] a given question has been signed by the user
def question_signed?(question_id)
questions_signed.map(&:id).include? question_id
end

# @return [String] the user's address for geocoding
def address_for_geocoding
[street_address, locality, region, country, postal_code] * ', '
Expand Down
18 changes: 16 additions & 2 deletions app/views/questions/_question.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,23 @@
</div>
<ul class="activity">
<li>
<%= link_to pluralize(question.signature_count, 'signature'), '#', class: 'support' %><%# @todo link to remote %>
<div class="question_progress">
<div class="progress_filler" style="width: <%= question_progress_percent(question) %>%;"></div>
</div>
<span class="question_progress_count">
<strong><span class="question-signatures" data-signature-question-id="<%= question.id %>"><%= question.signature_count -%></span></strong>
out of
<strong><span class="question-signature-threshold"><%= question.person.signature_threshold -%></span></strong>
Signatures
</span>
</li>
</ul>
<%= link_to 'Sign On', '#', class: 'sign_on' %><%# @todo link to remote %>
<% if user_signed_in? %>
<% if current_user.question_signed? question.id %>
<a class='sign_on'>Signed On</a>
<% else %>
<%= link_to 'Sign On', '#', class: 'sign_on sign-on-user', :'data-question-id' => question.id %><%# @todo link to remote %>
<% end %>
<% end %>
<div class="clear"></div>
</li>
27 changes: 17 additions & 10 deletions app/views/questions/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,10 @@

<div class="row">
<div class="question_progress">
<div class="progress_filler" style="width: 0%;"></div>
<div class="progress_filler" style="width: <%= question_progress_percent(@question) %>%;"></div>
</div>
<span class="question_progress_count">
<strong><span class="question-signatures"><%= @question.signatures.count -%></span></strong>
<strong><span class="question-signatures" data-signature-question-id="<%= @question.id %>"><%= @question.signature_count -%></span></strong>
out of
<strong><span class="question-signature-threshold"><%= @question.person.signature_threshold -%></span></strong>
Signatures
Expand All @@ -69,11 +69,17 @@

<% if user_signed_in? %>
<ul>
<li><%= link_to 'Sign On', signatures_path(question_id: @question.id), class: 'cta_pill' %><%# @todo link to remote %></li>
<li>
<% if current_user.question_signed? @question.id %>
<a class='sign_on'>Signed On</a>
<% else %>
<%= link_to 'Sign On', "#", class: 'cta_pill sign-on-user', :'data-question-id' => @question.id %>
<% end %>
</li>
</ul>
<% else %>
<div class="row last signup">
<%= simple_form_for(@user, as: :user, url: registration_path(:user)) do |f| %><%# @todo maybe use a custom controller %>
<%= simple_form_for(@user, as: :user, url: registration_path(:user)) do |f| %>
<%= hidden_field_tag :question_id, @question.id %>
<%= f.input :given_name, placeholder: "First Name", label: false %>
<%= f.input :family_name, placeholder: "Last Name", label: false %>
Expand Down Expand Up @@ -108,11 +114,12 @@
</section>

<%# @see http://disqus.com/admin/universalcode/ %>

<script type="text/javascript">
var disqus_shortname = 'dobot';
(function() {
var dsq = document.createElement('script'); dsq.type = 'text/javascript'; dsq.async = true;
dsq.src = 'http://' + disqus_shortname + '.disqus.com/embed.js';
(document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(dsq);
})();
var disqus_shortname = 'dobot';
(function() {
var dsq = document.createElement('script'); dsq.type = 'text/javascript'; dsq.async = true;
dsq.src = 'http://' + disqus_shortname + '.disqus.com/embed.js';
(document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(dsq);
})();
</script>
39 changes: 39 additions & 0 deletions spec/requests/questions_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,18 @@
visit '/vt/questions'
page.body.should have_selector '.question_content .title'
end

context 'as signed in user' do
it 'allows user to sign on to a question', js: true do
valid_person
as_user do
visit '/vt/questions'
click_link 'Sign On'
wait_until{ page.has_content?('Signed On')}
page.body.should have_content '1 out of'
end
end
end
end
end

Expand Down Expand Up @@ -291,6 +303,33 @@ def add_valid_values
threshold_on_page = find('span.question-signature-threshold').text.to_i
expect(threshold_on_page).to eq @person.signature_threshold
end

it 'allows new user to register and sign on to a question', js: true do
visit "/vt/questions/#{@question.id}"
script = "jQuery('#user_given_name').val('John'); "
script += "jQuery('#user_family_name').val('Doe'); "
script += "jQuery('#user_email').val('john.doe@example.com'); "
script += "jQuery('#user_street_address').val('#{street_address}'); "
script += "jQuery('#user_locality').val('#{locality}'); "
script += "jQuery('#user_region').val('#{region}'); "
script += "jQuery('#user_postal_code').val('#{postal_code}'); "
script += "jQuery('#user_password').val('testtest'); "
page.execute_script script
click_button 'Sign On'
page.body.should have_content '1 out of'
end

context 'as signed in user' do
it 'allows user to sign on to a question', js: true do
valid_person
as_user do
visit '/vt/questions'
click_link 'Sign On'
wait_until{ page.has_content?('Signed On')}
page.body.should have_content '1 out of'
end
end
end
end
end

Expand Down

0 comments on commit 559cb4e

Please sign in to comment.