From 67c287dc143cc0cb253545b6bfa31104767808c0 Mon Sep 17 00:00:00 2001
From: kikito
Date: Fri, 20 Jan 2017 16:50:03 +0100
Subject: [PATCH 01/17] Migrates geozones from poll questions to polls
---
...4_move_geozones_from_poll_questions_to_polls.rb | 10 ++++++++++
db/schema.rb | 14 +++++++-------
2 files changed, 17 insertions(+), 7 deletions(-)
create mode 100644 db/migrate/20170120153244_move_geozones_from_poll_questions_to_polls.rb
diff --git a/db/migrate/20170120153244_move_geozones_from_poll_questions_to_polls.rb b/db/migrate/20170120153244_move_geozones_from_poll_questions_to_polls.rb
new file mode 100644
index 000000000..a2606c370
--- /dev/null
+++ b/db/migrate/20170120153244_move_geozones_from_poll_questions_to_polls.rb
@@ -0,0 +1,10 @@
+class MoveGeozonesFromPollQuestionsToPolls < ActiveRecord::Migration
+ def change
+ drop_table :geozones_poll_questions
+
+ create_table :geozones_polls do |t|
+ t.references :geozone, index: true, foreign_key: true
+ t.references :poll, index: true, foreign_key: true
+ end
+ end
+end
diff --git a/db/schema.rb b/db/schema.rb
index 5385ef19b..56ec117e7 100644
--- a/db/schema.rb
+++ b/db/schema.rb
@@ -11,7 +11,7 @@
#
# It's strongly recommended that you check this file into your version control system.
-ActiveRecord::Schema.define(version: 20170114154421) do
+ActiveRecord::Schema.define(version: 20170120153244) do
# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"
@@ -307,13 +307,13 @@ ActiveRecord::Schema.define(version: 20170114154421) do
t.string "census_code"
end
- create_table "geozones_poll_questions", force: :cascade do |t|
+ create_table "geozones_polls", force: :cascade do |t|
t.integer "geozone_id"
- t.integer "question_id"
+ t.integer "poll_id"
end
- add_index "geozones_poll_questions", ["geozone_id"], name: "index_geozones_poll_questions_on_geozone_id", using: :btree
- add_index "geozones_poll_questions", ["question_id"], name: "index_geozones_poll_questions_on_question_id", using: :btree
+ add_index "geozones_polls", ["geozone_id"], name: "index_geozones_polls_on_geozone_id", using: :btree
+ add_index "geozones_polls", ["poll_id"], name: "index_geozones_polls_on_poll_id", using: :btree
create_table "identities", force: :cascade do |t|
t.integer "user_id"
@@ -768,8 +768,8 @@ ActiveRecord::Schema.define(version: 20170114154421) do
add_foreign_key "annotations", "users"
add_foreign_key "failed_census_calls", "users"
add_foreign_key "flags", "users"
- add_foreign_key "geozones_poll_questions", "geozones"
- add_foreign_key "geozones_poll_questions", "poll_questions", column: "question_id"
+ add_foreign_key "geozones_polls", "geozones"
+ add_foreign_key "geozones_polls", "polls"
add_foreign_key "identities", "users"
add_foreign_key "locks", "users"
add_foreign_key "managers", "users"
From d53c867ba43b16f2bec09a86665d758b6c95a267 Mon Sep 17 00:00:00 2001
From: kikito
Date: Fri, 20 Jan 2017 17:45:11 +0100
Subject: [PATCH 02/17] Adds geozone_restricted to polls
---
db/migrate/20170120161058_add_geozone_restricted_to_polls.rb | 5 +++++
db/schema.rb | 5 +++--
2 files changed, 8 insertions(+), 2 deletions(-)
create mode 100644 db/migrate/20170120161058_add_geozone_restricted_to_polls.rb
diff --git a/db/migrate/20170120161058_add_geozone_restricted_to_polls.rb b/db/migrate/20170120161058_add_geozone_restricted_to_polls.rb
new file mode 100644
index 000000000..d6ed3a2c7
--- /dev/null
+++ b/db/migrate/20170120161058_add_geozone_restricted_to_polls.rb
@@ -0,0 +1,5 @@
+class AddGeozoneRestrictedToPolls < ActiveRecord::Migration
+ def change
+ add_column :polls, :geozone_restricted, :boolean, default: false, index: true
+ end
+end
diff --git a/db/schema.rb b/db/schema.rb
index 56ec117e7..fcc836f01 100644
--- a/db/schema.rb
+++ b/db/schema.rb
@@ -11,7 +11,7 @@
#
# It's strongly recommended that you check this file into your version control system.
-ActiveRecord::Schema.define(version: 20170120153244) do
+ActiveRecord::Schema.define(version: 20170120161058) do
# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"
@@ -459,7 +459,8 @@ ActiveRecord::Schema.define(version: 20170120153244) do
t.string "name"
t.datetime "starts_at"
t.datetime "ends_at"
- t.boolean "published", default: false
+ t.boolean "published", default: false
+ t.boolean "geozone_restricted", default: false
end
create_table "proposal_notifications", force: :cascade do |t|
From 063c94822d090a228066720de5aab85ed6c18f9c Mon Sep 17 00:00:00 2001
From: kikito
Date: Fri, 20 Jan 2017 17:47:49 +0100
Subject: [PATCH 03/17] Removes all_geozones from poll_questions table
---
...20170120164547_remove_all_geozones_from_poll_questions.rb | 5 +++++
db/schema.rb | 3 +--
2 files changed, 6 insertions(+), 2 deletions(-)
create mode 100644 db/migrate/20170120164547_remove_all_geozones_from_poll_questions.rb
diff --git a/db/migrate/20170120164547_remove_all_geozones_from_poll_questions.rb b/db/migrate/20170120164547_remove_all_geozones_from_poll_questions.rb
new file mode 100644
index 000000000..b4fcecd20
--- /dev/null
+++ b/db/migrate/20170120164547_remove_all_geozones_from_poll_questions.rb
@@ -0,0 +1,5 @@
+class RemoveAllGeozonesFromPollQuestions < ActiveRecord::Migration
+ def change
+ remove_column :poll_questions, :all_geozones, :boolean
+ end
+end
diff --git a/db/schema.rb b/db/schema.rb
index fcc836f01..8eb3c5ce3 100644
--- a/db/schema.rb
+++ b/db/schema.rb
@@ -11,7 +11,7 @@
#
# It's strongly recommended that you check this file into your version control system.
-ActiveRecord::Schema.define(version: 20170120161058) do
+ActiveRecord::Schema.define(version: 20170120164547) do
# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"
@@ -424,7 +424,6 @@ ActiveRecord::Schema.define(version: 20170120161058) do
t.datetime "hidden_at"
t.datetime "created_at"
t.datetime "updated_at"
- t.boolean "all_geozones", default: false
t.tsvector "tsv"
end
From d0245059604879bcd7684e92c54263986b81d9d4 Mon Sep 17 00:00:00 2001
From: kikito
Date: Fri, 20 Jan 2017 18:58:03 +0100
Subject: [PATCH 04/17] moves geozones from poll question to poll in models
---
app/models/poll.rb | 10 ++++++++--
app/models/poll/question.rb | 25 ++++---------------------
2 files changed, 12 insertions(+), 23 deletions(-)
diff --git a/app/models/poll.rb b/app/models/poll.rb
index 79726715e..52afaafa5 100644
--- a/app/models/poll.rb
+++ b/app/models/poll.rb
@@ -6,6 +6,8 @@ class Poll < ActiveRecord::Base
has_many :officers, through: :officer_assignments
has_many :questions
+ has_and_belongs_to_many :geozones
+
validates :name, presence: true
validate :date_range
@@ -14,6 +16,7 @@ class Poll < ActiveRecord::Base
scope :incoming, -> { where('? < starts_at', Time.current) }
scope :expired, -> { where('ends_at < ?', Time.current) }
scope :published, -> { where('published = ?', true) }
+ scope :by_geozone_id, ->(geozone_id) { where(geozones: {id: geozone_id}.joins(:geozones)) }
scope :sort_for_list, -> { order(:starts_at) }
@@ -30,12 +33,15 @@ class Poll < ActiveRecord::Base
end
def answerable_by?(user)
- user.present? && user.level_two_or_three_verified? && current?
+ user.present? &&
+ user.level_two_or_three_verified? &&
+ current? &&
+ (!geozone_restricted || geozone_ids.include?(user.geozone_id))
end
def self.answerable_by(user)
return none if user.nil? || user.unverified?
- current
+ current.joins(:geozones).where('geozone_restricted = ? or geozones.id = ?', false, user.geozone_id)
end
def document_has_voted?(document_number, document_type)
diff --git a/app/models/poll/question.rb b/app/models/poll/question.rb
index 9f680c739..16786a9b6 100644
--- a/app/models/poll/question.rb
+++ b/app/models/poll/question.rb
@@ -11,7 +11,6 @@ class Poll::Question < ActiveRecord::Base
has_many :comments, as: :commentable
has_many :answers
has_many :partial_results
- has_and_belongs_to_many :geozones
belongs_to :proposal
validates :title, presence: true
@@ -21,9 +20,7 @@ class Poll::Question < ActiveRecord::Base
validates :title, length: { in: 4..Poll::Question.title_max_length }
validates :description, length: { maximum: Poll::Question.description_max_length }
- scope :no_poll, -> { where(poll_id: nil) }
- scope :by_poll_id, -> (poll_id) { where(poll_id: poll_id) }
- scope :by_geozone_id, -> (geozone_id) { where(geozones: {id: geozone_id}.joins(:geozones)) }
+ scope :by_poll_id, ->(poll_id) { where(poll_id: poll_id) }
scope :sort_for_list, -> { order('poll_questions.proposal_id IS NULL', :created_at)}
scope :for_render, -> { includes(:author, :proposal) }
@@ -41,9 +38,7 @@ class Poll::Question < ActiveRecord::Base
summary => 'B',
description => 'C',
author.username => 'C',
- author_visible_name => 'C',
- geozones.pluck(:name).join(' ') => 'C'
- }
+ author_visible_name => 'C' }
end
def description
@@ -62,29 +57,17 @@ class Poll::Question < ActiveRecord::Base
self.title = proposal.title
self.description = proposal.description
self.summary = proposal.summary
- self.all_geozones = true
self.valid_answers = I18n.t('poll_questions.default_valid_answers')
end
end
def answerable_by?(user)
- poll.answerable_by?(user) && (self.all_geozones || self.geozone_ids.include?(user.geozone_id))
+ poll.answerable_by?(user)
end
def self.answerable_by(user)
return none if user.nil? || user.unverified?
-
- where(poll_id: answerable_polls(user),
- geozones: { id: answerable_geozones(user) }).
- joins(:geozones)
- end
-
- def self.answerable_polls(user)
- Poll.answerable_by(user)
- end
-
- def self.answerable_geozones(user)
- user.geozone || Geozone.city
+ where(poll_id: Poll.answerable_by(user).pluck(:id))
end
end
From 8dbc72c37473cada4c7c5fecb41b977a87ae1909 Mon Sep 17 00:00:00 2001
From: kikito
Date: Fri, 20 Jan 2017 18:58:19 +0100
Subject: [PATCH 05/17] moves geozones from poll question to poll in dev_seeds
---
db/dev_seeds.rb | 37 ++++++++++++++++++++-----------------
1 file changed, 20 insertions(+), 17 deletions(-)
diff --git a/db/dev_seeds.rb b/db/dev_seeds.rb
index efdf0fa6c..066603e3c 100644
--- a/db/dev_seeds.rb
+++ b/db/dev_seeds.rb
@@ -466,27 +466,32 @@ end
puts "Creating polls"
-puts "Active Poll"
-poll = Poll.create(name: "Poll 1",
- starts_at: 1.month.ago,
- ends_at: 1.month.from_now)
-puts " #{poll.name}"
+puts "Active Polls"
+(1..5).each do |i|
+ poll = Poll.create(name: "Active Poll #{i}",
+ starts_at: 1.month.ago,
+ ends_at: 1.month.from_now,
+ geozone_restricted: true,
+ geozones: Geozone.reorder("RANDOM()").limit(3)
+ )
+ puts " #{poll.name}"
+end
puts "Upcoming Poll"
-poll = Poll.create(name: "Poll 2",
+poll = Poll.create(name: "Upcoming Poll",
starts_at: 1.month.from_now,
ends_at: 2.months.from_now)
puts " #{poll.name}"
puts "Expired Poll"
-poll = Poll.create(name: "Poll 3",
+poll = Poll.create(name: "Expired Poll",
starts_at: 2.months.ago,
ends_at: 1.months.ago)
puts " #{poll.name}"
puts "Creating Poll Questions"
-(1..20).each do |i|
+(1..50).each do |i|
poll = Poll.reorder("RANDOM()").first
author = User.reorder("RANDOM()").first
description = "#{Faker::Lorem.paragraphs.join('
')}
"
@@ -496,14 +501,12 @@ puts "Creating Poll Questions"
summary: Faker::Lorem.sentence(3),
description: description,
valid_answers: Faker::Lorem.words(3).join(', '),
- poll: poll,
- geozones: Geozone.reorder("RANDOM()").limit(3),
- all_geozones: [true, false].sample)
+ poll: poll)
puts " #{question.title}"
end
puts "Creating Poll Booths"
-10.times.each_with_index do |i|
+30.times.each_with_index do |i|
Poll::Booth.create(name: "Booth #{i}", polls: [Poll.all.sample])
end
@@ -515,7 +518,7 @@ end
puts "Creating Poll Officer Assignments"
(1..10).to_a.sample.times do |i|
Poll::BoothAssignment.all.sample(i).each do |booth_assignment|
- Poll::OfficerAssignment.create(officer: poll_officer,
+ Poll::OfficerAssignment.create(officer: poll_officer.poll_officer,
booth_assignment: booth_assignment,
date: booth_assignment.poll.starts_at)
end
@@ -523,7 +526,7 @@ end
puts "Creating Poll Question from Proposals"
-(1..3).each do |i|
+(1..3).each do
proposal = Proposal.reorder("RANDOM()").first
poll = Poll.current.first
question = Poll::Question.create(valid_answers: "Yes, No")
@@ -535,7 +538,7 @@ end
puts "Creating Successful Proposals"
-(1..10).each do |i|
+(1..10).each do
proposal = Proposal.reorder("RANDOM()").first
poll = Poll.current.first
question = Poll::Question.create(valid_answers: "Yes, No")
@@ -547,11 +550,11 @@ end
puts "Commenting Poll Questions"
-(1..30).each do |i|
+(1..30).each do
author = User.reorder("RANDOM()").first
question = Poll::Question.reorder("RANDOM()").first
Comment.create!(user: author,
created_at: rand(question.created_at .. Time.current),
commentable: question,
body: Faker::Lorem.sentence)
-end
\ No newline at end of file
+end
From 86b895a7af2a5932297400610cf075be98c057aa Mon Sep 17 00:00:00 2001
From: kikito
Date: Tue, 24 Jan 2017 17:51:18 +0100
Subject: [PATCH 06/17] Adds checkbox toggle js library
---
app/assets/javascripts/application.js | 2 ++
app/assets/javascripts/checkbox_toggle.js.coffee | 12 ++++++++++++
2 files changed, 14 insertions(+)
create mode 100644 app/assets/javascripts/checkbox_toggle.js.coffee
diff --git a/app/assets/javascripts/application.js b/app/assets/javascripts/application.js
index fba539eeb..8b2e2674b 100644
--- a/app/assets/javascripts/application.js
+++ b/app/assets/javascripts/application.js
@@ -49,6 +49,7 @@
//= require fixed_bar
//= require banners
//= require social_share
+//= require checkbox_toggle
//= require custom
var initialize_modules = function() {
@@ -74,6 +75,7 @@ var initialize_modules = function() {
App.FixedBar.initialize();
App.Banners.initialize();
App.SocialShare.initialize();
+ App.CheckboxToggle.initialize();
};
$(function(){
diff --git a/app/assets/javascripts/checkbox_toggle.js.coffee b/app/assets/javascripts/checkbox_toggle.js.coffee
new file mode 100644
index 000000000..096ce7e25
--- /dev/null
+++ b/app/assets/javascripts/checkbox_toggle.js.coffee
@@ -0,0 +1,12 @@
+App.CheckboxToggle =
+
+ initialize: ->
+ $('[data-checkbox-toggle]').on 'change', ->
+ $this = $(this)
+ $target = $($this.data('checkbox-toggle'))
+ if $this.is(':checked')
+ $target.show()
+ else
+ $target.hide()
+
+
From 65b0d279954aeb0faa848f5429a11210b5e22f84 Mon Sep 17 00:00:00 2001
From: kikito
Date: Tue, 24 Jan 2017 18:06:41 +0100
Subject: [PATCH 07/17] Moves geozones from questions to polls in admin
controllers
---
app/controllers/admin/poll/polls_controller.rb | 9 +++++++--
app/controllers/admin/poll/questions_controller.rb | 10 ++--------
2 files changed, 9 insertions(+), 10 deletions(-)
diff --git a/app/controllers/admin/poll/polls_controller.rb b/app/controllers/admin/poll/polls_controller.rb
index 0c2e2797c..5895a10bf 100644
--- a/app/controllers/admin/poll/polls_controller.rb
+++ b/app/controllers/admin/poll/polls_controller.rb
@@ -1,6 +1,8 @@
class Admin::Poll::PollsController < Admin::BaseController
load_and_authorize_resource
+
before_action :load_search, only: [:search_booths, :search_questions, :search_officers]
+ before_action :load_geozones, only: [:new, :create, :edit, :update]
def index
end
@@ -78,9 +80,12 @@ class Admin::Poll::PollsController < Admin::BaseController
end
private
+ def load_geozones
+ @geozones = Geozone.all.order(:name)
+ end
def poll_params
- params.require(:poll).permit(:name, :starts_at, :ends_at)
+ params.require(:poll).permit(:name, :starts_at, :ends_at, :geozone_restricted, geozone_ids: [])
end
def search_params
@@ -91,4 +96,4 @@ class Admin::Poll::PollsController < Admin::BaseController
@search = search_params[:search]
end
-end
\ No newline at end of file
+end
diff --git a/app/controllers/admin/poll/questions_controller.rb b/app/controllers/admin/poll/questions_controller.rb
index b7b16e379..e257d8821 100644
--- a/app/controllers/admin/poll/questions_controller.rb
+++ b/app/controllers/admin/poll/questions_controller.rb
@@ -2,8 +2,6 @@ class Admin::Poll::QuestionsController < Admin::BaseController
load_and_authorize_resource :poll
load_and_authorize_resource :question, class: 'Poll::Question'
- before_action :load_geozones, only: [:new, :create, :edit, :update]
-
def index
@polls = Poll.all
@search = search_params[:search]
@@ -55,16 +53,12 @@ class Admin::Poll::QuestionsController < Admin::BaseController
private
- def load_geozones
- @geozones = Geozone.all.order(:name)
- end
-
def question_params
- params.require(:poll_question).permit(:title, :question, :summary, :description, :proposal_id, :valid_answers, :poll_id, :geozone_ids => [])
+ params.require(:poll_question).permit(:title, :question, :summary, :description, :proposal_id, :valid_answers)
end
def search_params
params.permit(:poll_id, :search)
end
-end
\ No newline at end of file
+end
From 176a190149cfdd4beb704bfdbf23b91094af4680 Mon Sep 17 00:00:00 2001
From: kikito
Date: Tue, 24 Jan 2017 18:08:15 +0100
Subject: [PATCH 08/17] Moves geozones from questions to polls in admin forms
---
app/views/admin/poll/polls/_form.html.erb | 28 +++++++++++++------
app/views/admin/poll/questions/_form.html.erb | 13 +--------
2 files changed, 21 insertions(+), 20 deletions(-)
diff --git a/app/views/admin/poll/polls/_form.html.erb b/app/views/admin/poll/polls/_form.html.erb
index 8de8af670..0b99ea9f5 100644
--- a/app/views/admin/poll/polls/_form.html.erb
+++ b/app/views/admin/poll/polls/_form.html.erb
@@ -1,34 +1,46 @@
<%= form_for [:admin, @poll] do |f| %>
- <%= f.text_field :name,
- placeholder: t('admin.polls.form.name'),
- label: t("admin.polls.form.name") %>
+ <%= f.text_field :name %>
- <%= f.label :starts_at, t("admin.polls.form.starts_at") %>
<%= f.text_field :starts_at,
- label: false,
value: @poll.starts_at.present? ? l(@poll.starts_at.to_date) : nil,
class: "js-calendar-full" %>
- <%= f.label :ends_at, t("admin.polls.form.ends_at") %>
<%= f.text_field :ends_at,
- label: false,
value: @poll.ends_at.present? ? l(@poll.ends_at.to_date) : nil,
class: "js-calendar-full" %>
+
+
+ <%= f.check_box :geozone_restricted, data: { checkbox_toggle: "#geozones" } %>
+
+
+
+
+
+ <%= f.collection_check_boxes(:geozone_ids, @geozones, :id, :name) do |b| %>
+
+ <%= b.label do %>
+ <%= b.check_box + b.text %>
+ <% end %>
+
+ <% end %>
+
+
+
<%= f.submit t("admin.polls.#{admin_submit_action(@poll)}.submit_button"),
class: "button success expanded" %>
-<% end %>
\ No newline at end of file
+<% end %>
diff --git a/app/views/admin/poll/questions/_form.html.erb b/app/views/admin/poll/questions/_form.html.erb
index dbc8883e3..e5c228b4e 100644
--- a/app/views/admin/poll/questions/_form.html.erb
+++ b/app/views/admin/poll/questions/_form.html.erb
@@ -29,17 +29,6 @@
ckeditor: { language: I18n.locale } %>
-
- <%= f.collection_check_boxes(:geozone_ids, @geozones, :id, :name) do |b| %>
-
- <%= b.label do %>
- <%= b.check_box + b.text %>
- <% end %>
-
- <% end %>
-
- <%# TODO include all link %>
-
<%= f.submit(class: "button expanded", value: t("shared.save")) %>
@@ -48,4 +37,4 @@
-<% end %>
\ No newline at end of file
+<% end %>
From ab63c18d01d8d5b2704698636b26f430bb8530a6 Mon Sep 17 00:00:00 2001
From: kikito
Date: Tue, 24 Jan 2017 18:08:48 +0100
Subject: [PATCH 09/17] Moves geozones from questions to polls in admin show
views
---
app/views/admin/poll/polls/show.html.erb | 12 +++++++++---
app/views/admin/poll/questions/show.html.erb | 9 ---------
2 files changed, 9 insertions(+), 12 deletions(-)
diff --git a/app/views/admin/poll/polls/show.html.erb b/app/views/admin/poll/polls/show.html.erb
index fdc00b949..363d14630 100644
--- a/app/views/admin/poll/polls/show.html.erb
+++ b/app/views/admin/poll/polls/show.html.erb
@@ -6,9 +6,15 @@
<%= @poll.name %>
-
+
(<%= l @poll.starts_at.to_date %> - <%= l @poll.ends_at.to_date %>)
-
+
+<% if @poll.geozone_restricted %>
+ •
+
+ <%= @poll.geozones.pluck(:name).to_sentence %>
+
+<% end %>
<%= render "filter_subnav" %>
@@ -27,4 +33,4 @@
<%= render "search_officers" %>
<%= render 'officers' %>
-
\ No newline at end of file
+
diff --git a/app/views/admin/poll/questions/show.html.erb b/app/views/admin/poll/questions/show.html.erb
index 91bf398af..72109693b 100644
--- a/app/views/admin/poll/questions/show.html.erb
+++ b/app/views/admin/poll/questions/show.html.erb
@@ -46,15 +46,6 @@
<%= @question.description %>
-
- <%= t("admin.questions.show.geozones") %>
-
- <% @question.geozones.each do |geozone| %>
-
- <%= geozone.name %>
-
- <% end %>
-
<%= link_to t("admin.questions.show.preview"), question_path(@question) %>
From 6711063c7d8069562a690129efbd8cc8e1f129c5 Mon Sep 17 00:00:00 2001
From: kikito
Date: Tue, 24 Jan 2017 18:09:27 +0100
Subject: [PATCH 10/17] Moves geozones from question to poll in user show views
---
app/views/polls/questions/show.html.erb | 6 ------
app/views/polls/show.html.erb | 5 +++++
2 files changed, 5 insertions(+), 6 deletions(-)
diff --git a/app/views/polls/questions/show.html.erb b/app/views/polls/questions/show.html.erb
index 91dcff01d..04d4a3372 100644
--- a/app/views/polls/questions/show.html.erb
+++ b/app/views/polls/questions/show.html.erb
@@ -14,12 +14,6 @@
<% end %>
<%= @question.summary %>
-
-
diff --git a/app/views/polls/show.html.erb b/app/views/polls/show.html.erb
index fb207618d..5270e746d 100644
--- a/app/views/polls/show.html.erb
+++ b/app/views/polls/show.html.erb
@@ -11,6 +11,11 @@
<%= t("polls.show.question_count_html",
count: @poll.questions.count) %>
+
<%= render "callout" %>
From ddf8d5f811d608d87ea7f0d35a4b58d4797ab638 Mon Sep 17 00:00:00 2001
From: kikito
Date: Tue, 24 Jan 2017 18:10:13 +0100
Subject: [PATCH 11/17] Removes answerable/non_answerable questions in polls
Now they are either all answerable or non-answerable, so it is not
worth it to make the distinction in code
---
app/controllers/polls_controller.rb | 3 +--
app/views/polls/show.html.erb | 16 +---------------
2 files changed, 2 insertions(+), 17 deletions(-)
diff --git a/app/controllers/polls_controller.rb b/app/controllers/polls_controller.rb
index 9c83bc3e3..a93c84e9d 100644
--- a/app/controllers/polls_controller.rb
+++ b/app/controllers/polls_controller.rb
@@ -9,8 +9,7 @@ class PollsController < ApplicationController
end
def show
- @answerable_questions = @poll.questions.answerable_by(current_user).for_render.sort_for_list
- @non_answerable_questions = @poll.questions.where.not(id: @answerable_questions.map(&:id)).for_render.sort_for_list
+ @questions = @poll.questions.for_render.sort_for_list
@answers_by_question_id = {}
poll_partial_results = Poll::PartialResult.by_question(@poll.question_ids).by_author(current_user.try(:id))
diff --git a/app/views/polls/show.html.erb b/app/views/polls/show.html.erb
index 5270e746d..c5613e580 100644
--- a/app/views/polls/show.html.erb
+++ b/app/views/polls/show.html.erb
@@ -35,21 +35,7 @@
- <% @answerable_questions.each do |question| %>
- <%= render 'polls/questions/question', question: question %>
- <% end %>
-
- <% if can?(:answer, @poll) &&
- @non_answerable_questions.present? %>
-
-
- <%= t("polls.show.cant_answer_wrong_geozone",
- count: @non_answerable_questions.count) %>
-
-
- <% end %>
-
- <% @non_answerable_questions.each do |question| %>
+ <% @questions.each do |question| %>
<%= render 'polls/questions/question', question: question %>
<% end %>
From 6ed73f34b6d8706d50bbd09b88d22c3f843bd86c Mon Sep 17 00:00:00 2001
From: kikito
Date: Tue, 24 Jan 2017 18:10:30 +0100
Subject: [PATCH 12/17] Adds activerecord translations
---
config/locales/activerecord.es.yml | 5 +++++
1 file changed, 5 insertions(+)
diff --git a/config/locales/activerecord.es.yml b/config/locales/activerecord.es.yml
index bdaeec551..644207b86 100644
--- a/config/locales/activerecord.es.yml
+++ b/config/locales/activerecord.es.yml
@@ -100,6 +100,11 @@ es:
external_url: "Enlace a documentación adicional"
geozone_id: "Ámbito de actuación"
title: "Título"
+ poll:
+ name: "Nombre"
+ starts_at: "Fecha de apertura"
+ ends_at: "Fecha de cierre"
+ geozone_restricted: "Restringida por zonas"
poll/question:
title: "Pregunta"
valid_answers: "Posibles respuestas"
From 15b531f06870dcc3cd84bf9b2b8ea9b04548adfb Mon Sep 17 00:00:00 2001
From: kikito
Date: Tue, 24 Jan 2017 18:10:41 +0100
Subject: [PATCH 13/17] Fixes failing specs
---
spec/features/admin/poll/questions_spec.rb | 7 ++--
spec/features/polls/polls_spec.rb | 42 +++++++++++++---------
spec/features/polls/questions_spec.rb | 18 +++++-----
spec/models/abilities/common_spec.rb | 25 ++++++++-----
spec/models/poll/question_spec.rb | 1 -
5 files changed, 54 insertions(+), 39 deletions(-)
diff --git a/spec/features/admin/poll/questions_spec.rb b/spec/features/admin/poll/questions_spec.rb
index 90e55107c..a5e155848 100644
--- a/spec/features/admin/poll/questions_spec.rb
+++ b/spec/features/admin/poll/questions_spec.rb
@@ -18,7 +18,8 @@ feature 'Admin poll questions' do
scenario 'Show' do
geozone = create(:geozone)
- question = create(:poll_question, geozone_ids: geozone.id)
+ poll = create(:poll, geozone_restricted: true, geozone_ids: [geozone.id])
+ question = create(:poll_question, poll: poll)
visit admin_question_path(question)
@@ -27,7 +28,6 @@ feature 'Admin poll questions' do
expect(page).to have_content(question.summary)
expect(page).to have_content(question.author.name)
expect(page).to have_content(question.valid_answers.join(" "))
- expect(page).to have_content(geozone.name)
end
scenario 'Create' do
@@ -53,7 +53,6 @@ feature 'Admin poll questions' do
end
scenario 'Create from successful proposal index' do
- geozones = create_list(:geozone, 3)
proposal = create(:proposal, :successful)
visit proposals_path
@@ -115,4 +114,4 @@ feature 'Admin poll questions' do
pending "Mark all city by default when creating a poll question from a successful proposal"
-end
\ No newline at end of file
+end
diff --git a/spec/features/polls/polls_spec.rb b/spec/features/polls/polls_spec.rb
index df8ee5105..c4a8f55a2 100644
--- a/spec/features/polls/polls_spec.rb
+++ b/spec/features/polls/polls_spec.rb
@@ -82,7 +82,9 @@ feature 'Polls' do
end
scenario 'Level 1 users' do
- create(:poll_question, poll: poll, geozone_ids: [geozone.id], valid_answers: 'Han Solo, Chewbacca')
+ poll.update(geozone_restricted: true)
+ poll.geozones << geozone
+ create(:poll_question, poll: poll, valid_answers: 'Han Solo, Chewbacca')
login_as(create(:user, geozone: geozone))
visit poll_path(poll)
@@ -96,8 +98,9 @@ feature 'Polls' do
end
scenario 'Level 2 users in an incoming poll' do
- incoming_poll = create(:poll, :incoming)
- create(:poll_question, poll: incoming_poll, geozone_ids: [geozone.id], valid_answers: 'Rey, Finn')
+ incoming_poll = create(:poll, :incoming, geozone_restricted: true)
+ incoming_poll.geozones << geozone
+ create(:poll_question, poll: incoming_poll, valid_answers: 'Rey, Finn')
login_as(create(:user, :level_two, geozone: geozone))
visit poll_path(incoming_poll)
@@ -111,8 +114,9 @@ feature 'Polls' do
end
scenario 'Level 2 users in an expired poll' do
- expired_poll = create(:poll, :expired)
- create(:poll_question, poll: expired_poll, geozone_ids: [geozone.id], valid_answers: 'Luke, Leia')
+ expired_poll = create(:poll, :expired, geozone_restricted: true)
+ expired_poll.geozones << geozone
+ create(:poll_question, poll: expired_poll, valid_answers: 'Luke, Leia')
login_as(create(:user, :level_two, geozone: geozone))
visit poll_path(expired_poll)
@@ -126,21 +130,23 @@ feature 'Polls' do
end
scenario 'Level 2 users in a poll with questions for a geozone which is not theirs' do
- create(:poll_question, poll: poll, geozone_ids: [], valid_answers: 'Vader, Palpatine')
+ poll.update(geozone_restricted: true)
+ poll.geozones << create(:geozone)
+ create(:poll_question, poll: poll, valid_answers: 'Vader, Palpatine')
login_as(create(:user, :level_two))
visit poll_path(poll)
- expect(page).to have_content("You can't answers the following 1 questions because are not available in your geozone.")
-
expect(page).to have_content('Vader')
expect(page).to have_content('Palpatine')
expect(page).to_not have_link('Vader')
expect(page).to_not have_link('Palpatine')
end
- scenario 'Level 2 users reading a same-geozone question' do
- create(:poll_question, poll: poll, geozone_ids: [geozone.id], valid_answers: 'Han Solo, Chewbacca')
+ scenario 'Level 2 users reading a same-geozone poll' do
+ poll.update(geozone_restricted: true)
+ poll.geozones << geozone
+ create(:poll_question, poll: poll, valid_answers: 'Han Solo, Chewbacca')
login_as(create(:user, :level_two, geozone: geozone))
visit poll_path(poll)
@@ -148,9 +154,9 @@ feature 'Polls' do
expect(page).to have_link('Chewbacca')
end
- scenario 'Level 2 users reading a all-geozones question' do
- create(:poll_question, poll: poll, all_geozones: true, valid_answers: 'Han Solo, Chewbacca')
- login_as(create(:user, :level_two, geozone: geozone))
+ scenario 'Level 2 users reading a all-geozones poll' do
+ create(:poll_question, poll: poll, valid_answers: 'Han Solo, Chewbacca')
+ login_as(create(:user, :level_two))
visit poll_path(poll)
expect(page).to have_link('Han Solo')
@@ -158,8 +164,8 @@ feature 'Polls' do
end
scenario 'Level 2 users who have already answered' do
- question = create(:poll_question, poll: poll, geozone_ids:[geozone.id], valid_answers: 'Han Solo, Chewbacca')
- user = create(:user, :level_two, geozone: geozone)
+ question = create(:poll_question, poll: poll, valid_answers: 'Han Solo, Chewbacca')
+ user = create(:user, :level_two)
create(:poll_partial_result, question: question, author: user, answer: 'Chewbacca')
login_as user
@@ -171,7 +177,9 @@ feature 'Polls' do
end
scenario 'Level 2 users answering', :js do
- create(:poll_question, poll: poll, geozone_ids: [geozone.id], valid_answers: 'Han Solo, Chewbacca')
+ poll.update(geozone_restricted: true)
+ poll.geozones << geozone
+ create(:poll_question, poll: poll, valid_answers: 'Han Solo, Chewbacca')
user = create(:user, :level_two, geozone: geozone)
login_as user
visit poll_path(poll)
@@ -183,4 +191,4 @@ feature 'Polls' do
end
end
-end
\ No newline at end of file
+end
diff --git a/spec/features/polls/questions_spec.rb b/spec/features/polls/questions_spec.rb
index 8e6574c5f..af4c1058c 100644
--- a/spec/features/polls/questions_spec.rb
+++ b/spec/features/polls/questions_spec.rb
@@ -14,7 +14,7 @@ feature 'Poll Questions' do
context 'Answering' do
let(:geozone) { create(:geozone) }
-
+ let(:poll) { create(:poll, geozone_restricted: true, geozone_ids: [geozone.id]) }
scenario 'Non-logged in users' do
question = create(:poll_question, valid_answers: 'Han Solo, Chewbacca')
@@ -29,7 +29,7 @@ feature 'Poll Questions' do
end
scenario 'Level 1 users' do
- question = create(:poll_question, geozone_ids: [geozone.id], valid_answers: 'Han Solo, Chewbacca')
+ question = create(:poll_question, poll: poll, valid_answers: 'Han Solo, Chewbacca')
login_as(create(:user, geozone: geozone))
visit question_path(question)
@@ -43,9 +43,11 @@ feature 'Poll Questions' do
end
scenario 'Level 2 users in an poll question for a geozone which is not theirs' do
- question = create(:poll_question, geozone_ids: [], valid_answers: 'Vader, Palpatine')
- login_as(create(:user, :level_two))
+ other_poll = create(:poll, geozone_restricted: true, geozone_ids: [create(:geozone).id])
+ question = create(:poll_question, poll: other_poll, valid_answers: 'Vader, Palpatine')
+
+ login_as(create(:user, :level_two, geozone: geozone))
visit question_path(question)
expect(page).to have_content('Vader')
@@ -55,7 +57,7 @@ feature 'Poll Questions' do
end
scenario 'Level 2 users who can answer' do
- question = create(:poll_question, geozone_ids: [geozone.id], valid_answers: 'Han Solo, Chewbacca')
+ question = create(:poll_question, poll: poll, valid_answers: 'Han Solo, Chewbacca')
login_as(create(:user, :level_two, geozone: geozone))
visit question_path(question)
@@ -65,7 +67,7 @@ feature 'Poll Questions' do
end
scenario 'Level 2 users who have already answered' do
- question = create(:poll_question, geozone_ids:[geozone.id], valid_answers: 'Han Solo, Chewbacca')
+ question = create(:poll_question, poll: poll, valid_answers: 'Han Solo, Chewbacca')
user = create(:user, :level_two, geozone: geozone)
create(:poll_partial_result, question: question, author: user, answer: 'Chewbacca')
@@ -79,7 +81,7 @@ feature 'Poll Questions' do
end
scenario 'Level 2 users answering', :js do
- question = create(:poll_question, geozone_ids: [geozone.id], valid_answers: 'Han Solo, Chewbacca')
+ question = create(:poll_question, poll: poll, valid_answers: 'Han Solo, Chewbacca')
user = create(:user, :level_two, geozone: geozone)
login_as user
@@ -92,4 +94,4 @@ feature 'Poll Questions' do
end
end
-end
\ No newline at end of file
+end
diff --git a/spec/models/abilities/common_spec.rb b/spec/models/abilities/common_spec.rb
index ff4fd315e..d1eddcd51 100644
--- a/spec/models/abilities/common_spec.rb
+++ b/spec/models/abilities/common_spec.rb
@@ -27,19 +27,26 @@ describe "Abilities::Common" do
let(:current_poll) { create(:poll) }
let(:incoming_poll) { create(:poll, :incoming) }
+ let(:incoming_poll_from_own_geozone) { create(:poll, :incoming, geozone_restricted: true, geozones: [geozone]) }
+ let(:incoming_poll_from_other_geozone) { create(:poll, :incoming, geozone_restricted: true, geozones: [create(:geozone)]) }
let(:expired_poll) { create(:poll, :expired) }
+ let(:expired_poll_from_own_geozone) { create(:poll, :expired, geozone_restricted: true, geozones: [geozone]) }
+ let(:expired_poll_from_other_geozone) { create(:poll, :expired, geozone_restricted: true, geozones: [create(:geozone)]) }
+ let(:poll) { create(:poll, geozone_restricted: false) }
+ let(:poll_from_own_geozone) { create(:poll, geozone_restricted: true, geozones: [geozone]) }
+ let(:poll_from_other_geozone) { create(:poll, geozone_restricted: true, geozones: [create(:geozone)]) }
- let(:poll_question_from_own_geozone) { create(:poll_question, geozones: [geozone]) }
- let(:poll_question_from_other_geozone) { create(:poll_question, geozones: [create(:geozone)]) }
- let(:poll_question_from_all_geozones) { create(:poll_question, all_geozones: true) }
+ let(:poll_question_from_own_geozone) { create(:poll_question, poll: poll_from_own_geozone) }
+ let(:poll_question_from_other_geozone) { create(:poll_question, poll: poll_from_other_geozone) }
+ let(:poll_question_from_all_geozones) { create(:poll_question, poll: poll) }
- let(:expired_poll_question_from_own_geozone) { create(:poll_question, poll: expired_poll, geozones: [geozone]) }
- let(:expired_poll_question_from_other_geozone) { create(:poll_question, poll: expired_poll, geozones: [create(:geozone)]) }
- let(:expired_poll_question_from_all_geozones) { create(:poll_question, poll: expired_poll, all_geozones: true) }
+ let(:expired_poll_question_from_own_geozone) { create(:poll_question, poll: expired_poll_from_own_geozone) }
+ let(:expired_poll_question_from_other_geozone) { create(:poll_question, poll: expired_poll_from_other_geozone) }
+ let(:expired_poll_question_from_all_geozones) { create(:poll_question, poll: expired_poll) }
- let(:incoming_poll_question_from_own_geozone) { create(:poll_question, poll: incoming_poll, geozones: [geozone]) }
- let(:incoming_poll_question_from_other_geozone) { create(:poll_question, poll: incoming_poll, geozones: [create(:geozone)]) }
- let(:incoming_poll_question_from_all_geozones) { create(:poll_question, poll: incoming_poll, all_geozones: true) }
+ let(:incoming_poll_question_from_own_geozone) { create(:poll_question, poll: incoming_poll_from_own_geozone) }
+ let(:incoming_poll_question_from_other_geozone) { create(:poll_question, poll: incoming_poll_from_other_geozone) }
+ let(:incoming_poll_question_from_all_geozones) { create(:poll_question, poll: incoming_poll) }
it { should be_able_to(:index, Debate) }
it { should be_able_to(:show, debate) }
diff --git a/spec/models/poll/question_spec.rb b/spec/models/poll/question_spec.rb
index fa572467a..057dfebca 100644
--- a/spec/models/poll/question_spec.rb
+++ b/spec/models/poll/question_spec.rb
@@ -20,7 +20,6 @@ RSpec.describe Poll::Question, type: :model do
expect(q.author_visible_name).to eq(p.author.name)
expect(q.proposal_id).to eq(p.id)
expect(q.title).to eq(p.title)
- expect(q.all_geozones).to be true
end
end
From e935629fdb3c768fa7b06270cf8cdbf42ca15b7d Mon Sep 17 00:00:00 2001
From: kikito
Date: Tue, 24 Jan 2017 19:01:28 +0100
Subject: [PATCH 14/17] i18n
---
config/locales/admin.en.yml | 5 -----
config/locales/admin.es.yml | 5 -----
config/locales/en.yml | 1 -
config/locales/es.yml | 1 -
4 files changed, 12 deletions(-)
diff --git a/config/locales/admin.en.yml b/config/locales/admin.en.yml
index c61a75bf1..3d0d324fa 100755
--- a/config/locales/admin.en.yml
+++ b/config/locales/admin.en.yml
@@ -301,10 +301,6 @@ en:
edit:
title: "Edit poll"
submit_button: "Update poll"
- form:
- name: "Name"
- starts_at: "Open date"
- ends_at: "Close date"
show:
questions_tab: Questions
booths_tab: Booths
@@ -357,7 +353,6 @@ en:
valid_answers: Valid answers
summary: Summary
description: Description
- geozones: Geozones
preview: View on website
booths:
index:
diff --git a/config/locales/admin.es.yml b/config/locales/admin.es.yml
index 8efda5869..cca26a737 100644
--- a/config/locales/admin.es.yml
+++ b/config/locales/admin.es.yml
@@ -301,10 +301,6 @@ es:
edit:
title: "Editar votación"
submit_button: "Actualizar votación"
- form:
- name: "Nombre"
- starts_at: "Fecha de apertura"
- ends_at: "Fecha de cierre"
show:
questions_tab: Preguntas
booths_tab: Urnas
@@ -357,7 +353,6 @@ es:
valid_answers: Respuestas válidas
summary: Resumen
description: Descripción
- geozones: Distritos
preview: Ver en la web
booths:
index:
diff --git a/config/locales/en.yml b/config/locales/en.yml
index 1fe8ea956..2697fb396 100755
--- a/config/locales/en.yml
+++ b/config/locales/en.yml
@@ -411,7 +411,6 @@ en:
verify_link: "verify your account"
cant_answer_incoming: "This poll has not yet started."
cant_answer_expired: "This poll has finished."
- cant_answer_wrong_geozone: "You can't answers the following %{count} questions because are not available in your geozone."
question_count_html: "This poll has a total of %{count} qeustions."
poll_questions:
create_question: "Create question"
diff --git a/config/locales/es.yml b/config/locales/es.yml
index 5b42d0bd3..2ae41a5ce 100755
--- a/config/locales/es.yml
+++ b/config/locales/es.yml
@@ -411,7 +411,6 @@ es:
verify_link: "verifica tu cuenta"
cant_answer_incoming: "Esta votación todavía no ha comenzado."
cant_answer_expired: "Esta votación ha terminado."
- cant_answer_wrong_geozone: "No puedes responder a las siguientes %{count} preguntas porque no están disponibles en tu zona:"
question_count_html: "Esta votación tiene un total de %{count} preguntas."
poll_questions:
create_question: "Crear pregunta para votación"
From 25930563e9e57fb3a55c67b7b87162e71b1ee09f Mon Sep 17 00:00:00 2001
From: kikito
Date: Wed, 25 Jan 2017 10:15:38 +0100
Subject: [PATCH 15/17] adds missing i18n
---
config/locales/activerecord.en.yml | 5 +++++
1 file changed, 5 insertions(+)
diff --git a/config/locales/activerecord.en.yml b/config/locales/activerecord.en.yml
index 7676cf394..302b6767a 100644
--- a/config/locales/activerecord.en.yml
+++ b/config/locales/activerecord.en.yml
@@ -105,6 +105,11 @@ en:
external_url: "Link to additional documentation"
geozone_id: "Scope of operation"
title: "Title"
+ poll:
+ name: "Name"
+ starts_at: "Start Date"
+ ends_at: "Closing Date"
+ geozone_restricted: "Restricted by geozone"
poll/question:
title: "Question"
valid_answers: "Posibles answers"
From 2be88cb316c94906c9f96e2d521700ff21e181f2 Mon Sep 17 00:00:00 2001
From: kikito
Date: Wed, 25 Jan 2017 10:46:57 +0100
Subject: [PATCH 16/17] Adds missing validation to poll::question. Fixes specs
---
app/controllers/admin/poll/questions_controller.rb | 2 +-
app/models/poll/question.rb | 1 +
spec/features/admin/poll/questions_spec.rb | 5 +++++
3 files changed, 7 insertions(+), 1 deletion(-)
diff --git a/app/controllers/admin/poll/questions_controller.rb b/app/controllers/admin/poll/questions_controller.rb
index e257d8821..a9a1d0c76 100644
--- a/app/controllers/admin/poll/questions_controller.rb
+++ b/app/controllers/admin/poll/questions_controller.rb
@@ -54,7 +54,7 @@ class Admin::Poll::QuestionsController < Admin::BaseController
private
def question_params
- params.require(:poll_question).permit(:title, :question, :summary, :description, :proposal_id, :valid_answers)
+ params.require(:poll_question).permit(:poll_id, :title, :question, :summary, :description, :proposal_id, :valid_answers)
end
def search_params
diff --git a/app/models/poll/question.rb b/app/models/poll/question.rb
index 16786a9b6..7d3da1b00 100644
--- a/app/models/poll/question.rb
+++ b/app/models/poll/question.rb
@@ -13,6 +13,7 @@ class Poll::Question < ActiveRecord::Base
has_many :partial_results
belongs_to :proposal
+ validates :poll_id, presence: true
validates :title, presence: true
validates :summary, presence: true
validates :author, presence: true
diff --git a/spec/features/admin/poll/questions_spec.rb b/spec/features/admin/poll/questions_spec.rb
index a5e155848..de26889d6 100644
--- a/spec/features/admin/poll/questions_spec.rb
+++ b/spec/features/admin/poll/questions_spec.rb
@@ -31,6 +31,7 @@ feature 'Admin poll questions' do
end
scenario 'Create' do
+ poll = create(:poll, name: 'Movies')
title = "Star Wars: Episode IV - A New Hope"
summary = "It is a period of civil war. Rebel spaceships, striking from a hidden base, have won their first victory against the evil Galactic Empire"
description = %{
@@ -41,6 +42,7 @@ feature 'Admin poll questions' do
visit admin_questions_path
click_link "Create question"
+ select 'Movies', from: 'poll_question_poll_id'
fill_in 'poll_question_title', with: title
fill_in 'poll_question_summary', with: summary
fill_in 'poll_question_description', with: description
@@ -53,6 +55,7 @@ feature 'Admin poll questions' do
end
scenario 'Create from successful proposal index' do
+ poll = create(:poll, name: 'Proposals')
proposal = create(:proposal, :successful)
visit proposals_path
@@ -64,6 +67,8 @@ feature 'Admin poll questions' do
expect(page).to have_field('poll_question_description', with: proposal.description)
expect(page).to have_field('poll_question_valid_answers', with: "Yes, No")
+ select 'Proposals', from: 'poll_question_poll_id'
+
click_button 'Save'
expect(page).to have_content(proposal.title)
From 499f9b51e23cee95a50da556e4f285e06533e16e Mon Sep 17 00:00:00 2001
From: kikito
Date: Wed, 25 Jan 2017 11:19:18 +0100
Subject: [PATCH 17/17] makes it possible to create orphan poll questions
---
app/models/poll/question.rb | 1 -
1 file changed, 1 deletion(-)
diff --git a/app/models/poll/question.rb b/app/models/poll/question.rb
index 7d3da1b00..16786a9b6 100644
--- a/app/models/poll/question.rb
+++ b/app/models/poll/question.rb
@@ -13,7 +13,6 @@ class Poll::Question < ActiveRecord::Base
has_many :partial_results
belongs_to :proposal
- validates :poll_id, presence: true
validates :title, presence: true
validates :summary, presence: true
validates :author, presence: true