diff --git a/app/models/poll.rb b/app/models/poll.rb index f5734b83e..a274b4df7 100644 --- a/app/models/poll.rb +++ b/app/models/poll.rb @@ -1,8 +1,10 @@ class Poll < ActiveRecord::Base has_many :booth_assignments, class_name: "Poll::BoothAssignment" has_many :booths, through: :booth_assignments + has_many :officer_assignments, class_name: "Poll::OfficerAssignment" + has_many :officers, through: :officer_assignments + has_many :voters, through: :booths, class_name: "Poll::Voter" - has_many :officers, through: :booths, class_name: "Poll::Officer" has_many :questions validates :name, presence: true diff --git a/app/models/poll/booth.rb b/app/models/poll/booth.rb index 3b71983fa..805686976 100644 --- a/app/models/poll/booth.rb +++ b/app/models/poll/booth.rb @@ -3,8 +3,6 @@ class Poll has_many :booth_assignments, class_name: "Poll::BoothAssignment" has_many :polls, through: :booth_assignments has_many :voters - has_many :officing_booths, dependent: :destroy - has_many :officers, through: :officing_booths validates :name, presence: true, uniqueness: true end diff --git a/app/models/poll/booth_assignment.rb b/app/models/poll/booth_assignment.rb index 152f3ffb0..d9d784179 100644 --- a/app/models/poll/booth_assignment.rb +++ b/app/models/poll/booth_assignment.rb @@ -2,5 +2,8 @@ class Poll class BoothAssignment < ActiveRecord::Base belongs_to :booth belongs_to :poll + + has_many :officer_assignments, class_name: "Poll::OfficerAssignment", dependent: :destroy + has_many :officers, through: :officer_assignments end end diff --git a/app/models/poll/officer_assignment.rb b/app/models/poll/officer_assignment.rb new file mode 100644 index 000000000..25d1628d5 --- /dev/null +++ b/app/models/poll/officer_assignment.rb @@ -0,0 +1,8 @@ +class Poll + class OfficerAssignment < ActiveRecord::Base + belongs_to :officer + belongs_to :booth_assignment + + delegate :poll_id, :booth_id, to: :booth_assignment + end +end diff --git a/app/models/poll/officing_booth.rb b/app/models/poll/officing_booth.rb deleted file mode 100644 index f0a64721a..000000000 --- a/app/models/poll/officing_booth.rb +++ /dev/null @@ -1,6 +0,0 @@ -class Poll - class OfficingBooth < ActiveRecord::Base - belongs_to :officer - belongs_to :booth - end -end diff --git a/db/migrate/20161206130836_delete_officing_booths.rb b/db/migrate/20161206130836_delete_officing_booths.rb new file mode 100644 index 000000000..2ef1180ab --- /dev/null +++ b/db/migrate/20161206130836_delete_officing_booths.rb @@ -0,0 +1,8 @@ +class DeleteOfficingBooths < ActiveRecord::Migration + def self.up + drop_table :poll_officing_booths + end + + def self.down + end +end diff --git a/db/migrate/20161206131125_create_poll_officer_assignments.rb b/db/migrate/20161206131125_create_poll_officer_assignments.rb new file mode 100644 index 000000000..f3a40017c --- /dev/null +++ b/db/migrate/20161206131125_create_poll_officer_assignments.rb @@ -0,0 +1,9 @@ +class CreatePollOfficerAssignments < ActiveRecord::Migration + def change + create_table :poll_officer_assignments do |t| + t.integer :booth_assignment_id + t.integer :officer_id + t.timestamps null: false + end + end +end diff --git a/db/schema.rb b/db/schema.rb index 9f180a5f4..ed8e3f151 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: 20161130123347) do +ActiveRecord::Schema.define(version: 20161206131125) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" @@ -290,15 +290,15 @@ ActiveRecord::Schema.define(version: 20161130123347) do t.string "location" end - create_table "poll_officers", force: :cascade do |t| - t.integer "user_id" + create_table "poll_officer_assignments", force: :cascade do |t| + t.integer "booth_assignment_id" + t.integer "officer_id" + t.datetime "created_at", null: false + t.datetime "updated_at", null: false end - create_table "poll_officing_booths", force: :cascade do |t| - t.integer "officer_id" - t.integer "booth_id" - t.datetime "created_at", null: false - t.datetime "updated_at", null: false + create_table "poll_officers", force: :cascade do |t| + t.integer "user_id" end create_table "poll_partial_results", force: :cascade do |t| diff --git a/spec/features/admin/poll/officers_spec.rb b/spec/features/admin/poll/officers_spec.rb index 981b17f13..932bec212 100644 --- a/spec/features/admin/poll/officers_spec.rb +++ b/spec/features/admin/poll/officers_spec.rb @@ -35,125 +35,4 @@ feature 'Admin poll officers' do end end - context "Booth" do - - scenario 'No officers assigned to booth' do - poll = create(:poll) - booth = create(:poll_booth, poll: poll) - - visit admin_poll_booth_path(poll, booth) - - within("#assigned_officers") do - expect(page).to have_css ".officer", count: 0 - end - - expect(page).to have_content "There are no officers assigned to this booth" - end - - scenario "Assigned to booth" do - john = create(:poll_officer) - isabel = create(:poll_officer) - eve = create(:poll_officer) - - poll = create(:poll) - booth = create(:poll_booth, poll: poll) - - officing_booth1 = create(:officing_booth, officer: john, booth: booth) - officing_booth2 = create(:officing_booth, officer: isabel, booth: booth) - - visit admin_poll_booth_path(poll, booth) - - within("#assigned_officers") do - expect(page).to have_css ".officer", count: 2 - expect(page).to have_content john.name - expect(page).to have_content isabel.name - - expect(page).to_not have_content eve.name - end - - expect(page).to_not have_content "There are no officers assigned to this booth" - end - - scenario 'Assign to booth' do - john = create(:poll_officer) - isabel = create(:poll_officer) - - poll = create(:poll) - booth = create(:poll_booth, poll: poll) - - visit admin_poll_booth_path(poll, booth) - - check "#{john.name} - #{john.email}" - click_button "Assign officer" - - expect(page).to have_content "Booth updated successfully." - within("#assigned_officers") do - expect(page).to have_css ".officer", count: 1 - expect(page).to have_content john.name - end - end - - scenario "Unassign from booth" do - john = create(:poll_officer) - isabel = create(:poll_officer) - - poll = create(:poll) - booth = create(:poll_booth, poll: poll) - - officing_booth = create(:officing_booth, officer: john, booth: booth) - officing_booth = create(:officing_booth, officer: isabel, booth: booth) - - visit admin_poll_booth_path(poll, booth) - - uncheck "#{john.name} - #{john.email}" - click_button "Assign officer" - - expect(page).to have_content "Booth updated successfully." - within("#assigned_officers") do - expect(page).to have_css ".officer", count: 1 - expect(page).to have_content isabel.name - - expect(page).to_not have_content john.name - end - end - - scenario "Assigned multiple officers to different booths" do - john = create(:poll_officer) - isabel = create(:poll_officer) - eve = create(:poll_officer) - peter = create(:poll_officer) - - poll1 = create(:poll) - poll2 = create(:poll) - - booth1 = create(:poll_booth, poll: poll1) - booth2 = create(:poll_booth, poll: poll1) - booth3 = create(:poll_booth, poll: poll2) - - officing_booth = create(:officing_booth, officer: john, booth: booth1) - officing_booth = create(:officing_booth, officer: isabel, booth: booth1) - officing_booth = create(:officing_booth, officer: eve, booth: booth2) - officing_booth = create(:officing_booth, officer: peter, booth: booth3) - - visit admin_poll_booth_path(poll1, booth1) - within("#assigned_officers") do - expect(page).to have_css ".officer", count: 2 - expect(page).to have_content john.name - expect(page).to have_content isabel.name - end - - visit admin_poll_booth_path(poll1, booth2) - within("#assigned_officers") do - expect(page).to have_css ".officer", count: 1 - expect(page).to have_content eve.name - end - - visit admin_poll_booth_path(poll2, booth3) - within("#assigned_officers") do - expect(page).to have_css ".officer", count: 1 - expect(page).to have_content peter.name - end - end - end - end \ No newline at end of file diff --git a/spec/features/admin/poll/polls_spec.rb b/spec/features/admin/poll/polls_spec.rb index e6b9eba69..afaaf5e7f 100644 --- a/spec/features/admin/poll/polls_spec.rb +++ b/spec/features/admin/poll/polls_spec.rb @@ -116,4 +116,37 @@ feature 'Admin polls' do end end + context "Officers" do + + context "Poll show" do + + scenario "No officers", :js do + poll = create(:poll) + visit admin_poll_path(poll) + click_link "Officers (0)" + + expect(page).to have_content "There are no officers assigned to this poll" + end + + scenario "Officer list", :js do + poll = create(:poll) + booth = create(:poll_booth, polls: [poll]) + + visit admin_poll_path(poll) + click_link "Officers (3)" + + expect(page).to have_css ".officer", count: 3 + + officers = Poll::Officer.all + officers.each do |officer| + within("#officer_#{officer.id}") do + expect(page).to have_content officer.name + expect(page).to have_content officer.email + end + end + expect(page).to_not have_content "There are no officers assigned to this poll" + end + end + end + end \ No newline at end of file