Permalink
Browse files

Allow pre-seeding choices

  • Loading branch information...
1 parent 92d590f commit 0d6a6fe3705666018fbb4be1b60f22d8ae50c843 @gkop gkop committed May 17, 2012
@@ -0,0 +1,9 @@
+window.GreenEggs ||= {}
+
+$(document).ready ->
+ $("ul#poll-choices input[type='text']").live "keypress", (e) ->
+ if this is jQuery("ul#poll-choices input[type='text']").last()[0] and e.keyCode is 13
+ num = jQuery('form ul li').size()
+ $('<li id="poll_choices_attributes_'+num+'_original_input" class="string optional"><input type="text" name="poll[choices_attributes]['+num+'][original]" id="poll_choices_attributes_'+num+'_original"></li>').appendTo("form.formtastic.poll div.fields ul")
+ $("div.fields ul input[type='text']").last().focus()
+ false
@@ -6,7 +6,8 @@ form.formtastic.ballot li > label {
display: none;
}
-ul#choices li {
+ul#choices li,
+ul#poll-choices li {
margin: 5px;
padding: 5px;
border: 1px solid gray;
@@ -8,7 +8,7 @@ class ApplicationController < ActionController::Base
private
def check_admin_key_and_load_poll
- @poll = Poll.find(params[:poll_id])
+ @poll = Poll.find(params[:poll_id] || params[:id])
render :file => File.join(Rails.root, "public", "404"), :status => 404 if @poll.owner_key != params[:owner_key]
end
@@ -1,5 +1,5 @@
class PollsController < ApplicationController
- before_filter :check_admin_key_and_load_poll, :only => [:edit, :update, :destroy]
+ before_filter :check_admin_key_and_load_poll, :only => [:edit, :update, :destroy, :choices]
before_filter :load_poll_and_ballot, :only => [:show]
# GET /polls
@@ -35,7 +35,7 @@ def update
respond_to do |format|
format.html do
if @poll.update_attributes(params[:poll])
- redirect_to @poll, notice: 'Poll was successfully updated.'
+ redirect_to poll_admin_path(:poll_id => @poll.id, :owner_key => @poll.owner_key), notice: 'Poll was successfully updated.'
else
render action: "edit"
end
@@ -54,6 +54,11 @@ def update
end
end
+ # GET /:poll_id/admin/choices
+ def choices
+ @poll.choices.build
+ end
+
# DELETE /polls/1
def destroy
@poll.destroy
@@ -2,8 +2,10 @@ module BallotsHelper
def existing_choices_not_on_ballot(poll, current_ballot)
choices = {}
- poll.ballots.each do |ballot|
- ballot.choices.each do |choice|
+ sets_of_choices = poll.ballots.clone
+ sets_of_choices << poll
+ sets_of_choices.each do |set|
+ set.choices.each do |choice|
if current_ballot.choices.select {|c| c.slug == choice.slug}.empty? && choice.original.present?
choices[choice.slug] = choice.original if choice.original.present?
end
View
@@ -37,7 +37,7 @@ def sort_by_priority
private
def destroy_blank_choices
- self.choices.where(:original => "").destroy_all
+ self.choices.where(:original => "").destroy_all
end
def notify_firehose
View
@@ -9,6 +9,7 @@ class Choice
before_save :create_slug
embedded_in :ballot
+ embedded_in :poll
private
View
@@ -8,11 +8,15 @@ class Poll
key :name
before_create :generate_owner_key
+ after_update :destroy_blank_choices
+ after_update :save_choices
validate :check_for_collision, :on => :create
validates_presence_of :name
validates_presence_of :owner_email
embeds_many :ballots
+ embeds_many :choices
+ accepts_nested_attributes_for :choices
set_callback(:create, :after) do |poll|
OwnerMailer.send_admin_link(poll).deliver
@@ -113,4 +117,11 @@ def check_for_collision
end
end
+ def destroy_blank_choices
+ self.choices.where(:original => "").destroy_all
+ end
+
+ def save_choices
+ self.choices.each {|c| c.save}
+ end
end
@@ -0,0 +1,12 @@
+%p#notice= notice
+
+%h1 Pre-seed choices for "#{@poll.name}"
+
+%p
+ = semantic_form_for @poll, :url => poll_path(@poll, :owner_key => @poll.owner_key) do |form|
+ .fields
+ %ul#poll-choices
+ = form.semantic_fields_for :choices do |choice|
+ = choice.input :original, :label => false
+
+ = submit_tag "Save"
@@ -8,6 +8,8 @@
%p
= link_to 'Invite voters', invite_voters_path(:poll_id => @poll.id, :owner_key => @poll.owner_key)
+%p
+ = link_to 'Pre-seed choices', poll_choices_path(:poll_id => @poll.id, :owner_key => @poll.owner_key)
%p
%h4 Outstanding ballots
View
@@ -3,55 +3,6 @@
resources :ballots
resources :polls
- # The priority is based upon order of creation:
- # first created -> highest priority.
-
- # Sample of regular route:
- # match 'products/:id' => 'catalog#view'
- # Keep in mind you can assign values other than :controller and :action
-
- # Sample of named route:
- # match 'products/:id/purchase' => 'catalog#purchase', :as => :purchase
- # This route can be invoked with purchase_url(:id => product.id)
-
- # Sample resource route (maps HTTP verbs to controller actions automatically):
- # resources :products
-
- # Sample resource route with options:
- # resources :products do
- # member do
- # get 'short'
- # post 'toggle'
- # end
- #
- # collection do
- # get 'sold'
- # end
- # end
-
- # Sample resource route with sub-resources:
- # resources :products do
- # resources :comments, :sales
- # resource :seller
- # end
-
- # Sample resource route with more complex sub-resources
- # resources :products do
- # resources :comments
- # resources :sales do
- # get 'recent', :on => :collection
- # end
- # end
-
- # Sample resource route within a namespace:
- # namespace :admin do
- # # Directs /admin/products/* to Admin::ProductsController
- # # (app/controllers/admin/products_controller.rb)
- # resources :products
- # end
-
- # You can have the root of your site routed with "root"
- # just remember to delete public/index.html.
root :to => 'home#index'
get '/:poll_id/invite_voters' => 'ballots#new', :as => :invite_voters
@@ -60,10 +11,7 @@
get '/:poll_id/:ballot_key' => 'ballots#show', :as => :vote_on_ballot
get '/:poll_id/:ballot_key/results' => 'polls#show', :as => :poll_results
post '/:poll_id/ballots/create' => 'ballots#create', :as => :create_ballot
+ get '/:poll_id/admin' => 'polls#edit', :as => :poll_admin
+ get '/:poll_id/admin/choices' => 'polls#choices', :as => :poll_choices
- # See how all your routes lay out with "rake routes"
-
- # This is a legacy wild controller route that's not recommended for RESTful applications.
- # Note: This route will make all actions in every controller accessible via GET requests.
- # match ':controller(/:action(/:id(.:format)))'
end
@@ -0,0 +1,23 @@
+Feature: Admin pre-seeds choices
+ As an admin
+ I want to seed the poll with choices
+ So voters don't have to think of their own
+
+ @javascript
+ Scenario: Create poll and seed choices
+ Given I am on the index page
+ And I follow "Create a new poll"
+ And I enter "Where should we go for drinks?" for "poll_name"
+ And I enter "wolfie@example.com" for "poll_owner_email"
+ And I press "Save"
+ Then I should see "Poll was successfully created"
+ When I follow "Pre-seed choices"
+ And I add the choice "Granny smith"
+ And I add the choice "Macintosh"
+ And I add the choice "Red delicious"
+ And I press "Save"
+ When I have a ballot for the poll
+ And am on my ballot page
+ Then I should see "Granny smith" within the choices column
+ Then I should see "Macintosh" within the choices column
+ Then I should see "Red delicious" within the choices column
@@ -0,0 +1,12 @@
+When /^I add the choice "([^"]*)"$/ do |choice|
+ num = all("li").count
+ fill_in("poll_choices_attributes_#{num-1}_original", :with => choice)
+ # simulate the voter pressing the "Enter" key
+ script = "e = jQuery.Event('keypress'); e.keyCode = 13; $('input#poll_choices_attributes_#{num-1}_original').trigger(e);"
+ page.execute_script(script)
+end
+
+When /^I have a ballot for the poll$/ do
+ @poll = Poll.last
+ @ballot = @poll.ballots.create
+end

0 comments on commit 0d6a6fe

Please sign in to comment.