Skip to content

Commit

Permalink
Added UI for "force subscription" acording to #7
Browse files Browse the repository at this point in the history
  • Loading branch information
asterite committed Mar 13, 2014
1 parent ddcd1ff commit 38dffe6
Show file tree
Hide file tree
Showing 7 changed files with 67 additions and 33 deletions.
6 changes: 3 additions & 3 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -126,10 +126,10 @@ GEM
ntlm-http (~> 0.1, >= 0.1.1)
webrobots (~> 0.0, >= 0.0.9)
meta_programming (0.2.2)
metaclass (0.0.1)
metaclass (0.0.4)
method_source (0.8.1)
mime-types (1.21)
mocha (0.10.0)
mocha (1.0.0)
metaclass (~> 0.0.1)
multi_json (1.6.0)
mysql2 (0.3.11)
Expand Down Expand Up @@ -180,7 +180,7 @@ GEM
rake (>= 0.8.7)
rdoc (~> 3.4)
thor (~> 0.14.6)
rake (10.0.3)
rake (10.1.1)
rcov (0.9.10)
rdoc (3.12.1)
json (~> 1.4)
Expand Down
9 changes: 9 additions & 0 deletions app/assets/javascripts/polls.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,15 @@ $ ->
$('#poll_form_url').focus()
false

checkForceSubscription()
$('#poll_force_subscription_false').change checkForceSubscription
$('#poll_force_subscription_true').change checkForceSubscription

checkForceSubscription = ->
readonly = $('#poll_force_subscription_true').is(':checked')
$('#poll_welcome_message').attr('readonly', readonly)
$('#poll_confirmation_word').attr('readonly', readonly)

setupValidation = () ->
jQuery.validator.addMethod(
"hasChildren"
Expand Down
36 changes: 18 additions & 18 deletions app/models/poll.rb
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
# Copyright (C) 2011-2012, InSTEDD
#
#
# This file is part of Pollit.
#
#
# Pollit is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
#
# Pollit is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
#
# You should have received a copy of the GNU General Public License
# along with Pollit. If not, see <http://www.gnu.org/licenses/>.

Expand All @@ -27,18 +27,18 @@ class Poll < ActiveRecord::Base
validates :title, :presence => true, :length => {:maximum => 64}, :uniqueness => {:scope => :owner_id}
validates :owner, :presence => true
validates :form_url, :presence => true
validates :welcome_message, :presence => true, :length => {:maximum => 140}
validates :welcome_message, :presence => true, :length => {:maximum => 140}, :unless => :force_subscription
validates :post_url, :presence => true
validates :confirmation_word, :presence => true
validates :confirmation_word, :presence => true, :unless => :force_subscription
validates :goodbye_message, :presence => true, :length => {:maximum => 140}
validates :questions, :presence => true

accepts_nested_attributes_for :questions

after_initialize :default_values

enum_attr :status, %w(^configuring started paused)

include Parser
include AcceptAnswers

Expand All @@ -57,7 +57,7 @@ def start
raise Exception.new("Cannot start poll #{self.id}") unless can_be_started?

invite respondents

self.status = :started
save
end
Expand All @@ -78,13 +78,13 @@ def pause

def resume
raise Exception.new("Cannot resume unpaused poll #{self.id}") unless self.status_paused?

messages = []

# Invite respondents that were added while the poll was paused
respondents_to_invite = self.respondents.where(:current_question_sent => false).where(:confirmed => false)
invite respondents_to_invite

# Sends next questions to users with a current question and without the current_question_sent mark
respondents_to_send_next_question = self.respondents.where(:current_question_sent => false).where('current_question_id IS NOT NULL')
respondents_to_send_next_question.each do |r|
Expand All @@ -99,7 +99,7 @@ def resume

send_messages messages

[respondents_to_send_next_question, respondents_to_goodbye].each do |rs|
[respondents_to_send_next_question, respondents_to_goodbye].each do |rs|
rs.update_all :current_question_sent => true
end

Expand Down Expand Up @@ -140,28 +140,28 @@ def google_form_key
query = URI.parse(form_url || post_url).query
CGI::parse(query)['formkey'][0]
end

def on_respondents_added
invite_new_respondents if status_started?
end

def invite_new_respondents
respondents_to_invite = self.respondents.where(:current_question_sent => false).where(:confirmed => false)
invite respondents_to_invite
end

private

def invite(respondents)
messages = []

respondents.each do |respondent|
messages << message_to(respondent, welcome_message)
end

# mark respondents as invited
respondents.update_all :current_question_sent => true

send_messages messages
end

Expand Down
13 changes: 8 additions & 5 deletions app/views/polls/_form.haml
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,18 @@
.field
= f.label :title
= f.text_field :title, :class => 'required w60'

.field
= f.label :description
%p This is an internal description of the Poll, it will not be sent to your users
= f.text_area :description, :class => 'w60', :rows => 5


.field
= f.label :welcome_message
= f.label :force_subscription, "#{f.radio_button(:force_subscription, true)} Force subscription".html_safe, value: true
%p Select this options <b>only</b> if you have explicit permission from the respondents to participate in your poll with their mobile phones.

.field
= f.label :force_subscription, "#{f.radio_button(:force_subscription, false)} Welcome message".html_safe, value: false
%p This message will be sent to all respondents asking them if they agree to participate in the poll
= f.text_area :welcome_message, :size => "28x5", :maxlength => 140, :class => 'required ux-wajbar w60', :readonly => [email protected]?
.field
Expand All @@ -31,7 +34,7 @@
= f.label :goodbye_message
%p This message will be sent to respondents when they complete the poll
= f.text_area :goodbye_message, :size => "28x5", :maxlength => 140, :class => 'ux-wajbar w60'

%hr

= render :partial => 'form_questions', :locals => {f:f}
Expand All @@ -45,4 +48,4 @@
= grey_button _("Next")
- else
= grey_button _("Save")
= white_link_to _("Cancel"), back_path
= white_link_to _("Cancel"), back_path
5 changes: 5 additions & 0 deletions db/migrate/20140313123128_add_force_subscription_to_polls.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class AddForceSubscriptionToPolls < ActiveRecord::Migration
def change
add_column :polls, :force_subscription, :boolean, default: false
end
end
5 changes: 3 additions & 2 deletions db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
#
# It's strongly recommended to check this file into your version control system.

ActiveRecord::Schema.define(:version => 20111213154341) do
ActiveRecord::Schema.define(:version => 20140313123128) do

create_table "answers", :force => true do |t|
t.integer "respondent_id"
Expand All @@ -37,11 +37,12 @@
t.string "form_url"
t.datetime "created_at"
t.datetime "updated_at"
t.string "welcome_message", :default => "YES"
t.string "welcome_message", :default => "YES"
t.string "status"
t.string "post_url"
t.string "confirmation_word"
t.string "goodbye_message"
t.boolean "force_subscription", :default => false
end

create_table "questions", :force => true do |t|
Expand Down
26 changes: 21 additions & 5 deletions spec/models/poll_spec.rb
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
# encoding: UTF-8
# Copyright (C) 2011-2012, InSTEDD
#
#
# This file is part of Pollit.
#
#
# Pollit is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
#
# Pollit is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
#
# You should have received a copy of the GNU General Public License
# along with Pollit. If not, see <http://www.gnu.org/licenses/>.

Expand All @@ -39,6 +39,22 @@
it "must require questions if specified" do
Poll.make_unsaved(:questions => []).should be_invalid
end

it "must require welcome message" do
Poll.make_unsaved(welcome_message: nil).should be_invalid
end

it "must require confirmation word" do
Poll.make_unsaved(confirmation_word: nil).should be_invalid
end

it "can have welcome message empty if force subscription" do
Poll.make_unsaved(welcome_message: nil, force_subscription: true).should be_valid
end

it "can have confirmation word empty if force subscription" do
Poll.make_unsaved(confirmation_word: nil, force_subscription: true).should be_valid
end
end

context "parsing google form" do
Expand Down Expand Up @@ -375,4 +391,4 @@ def messages
end

end
end
end

0 comments on commit 38dffe6

Please sign in to comment.