From 0467daab52d32c46c3c688245e794ef1ed4e0586 Mon Sep 17 00:00:00 2001 From: Joachim Filip Ignacy Bartosik Date: Mon, 20 Jun 2011 17:05:30 +0200 Subject: Application receives data from bot --- site/app/controllers/agendas_controller.rb | 3 ++- site/app/models/agenda.rb | 19 +++++++++++++++++++ site/spec/models/agenda_spec.rb | 26 ++++++++++++++++++++++++++ 3 files changed, 47 insertions(+), 1 deletion(-) diff --git a/site/app/controllers/agendas_controller.rb b/site/app/controllers/agendas_controller.rb index c6865a1..18a178d 100644 --- a/site/app/controllers/agendas_controller.rb +++ b/site/app/controllers/agendas_controller.rb @@ -15,7 +15,8 @@ class AgendasController < ApplicationController def results data = JSON.parse(request.env["rack.input"].read) - Agenda.process_results data + Agenda.update_voting_options data['agenda'] + Agenda.process_results data data['votes'] Participation.mark_participations data end diff --git a/site/app/models/agenda.rb b/site/app/models/agenda.rb index 65f45b4..46cf2d8 100644 --- a/site/app/models/agenda.rb +++ b/site/app/models/agenda.rb @@ -57,6 +57,25 @@ class Agenda < ActiveRecord::Base false end + def self.update_voting_options(options) + agenda = Agenda.current + options.each do |item_info| + item = AgendaItem.first :conditions => { :agenda_id => agenda, :title => item_info.first } + new_descriptions = item_info[1] + old_descriptions = item.voting_options.*.description + + (old_descriptions - new_descriptions).each do |description| + option = VotingOption.first :conditions => { :agenda_item_id => item.id, + :description => description } + option.destroy + end + + (new_descriptions - old_descriptions ).each do |description| + VotingOption.create! :agenda_item => item, :description => description + end + end + end + def self.process_results(results) agenda = Agenda.current for item_title in results.keys diff --git a/site/spec/models/agenda_spec.rb b/site/spec/models/agenda_spec.rb index 34db02f..b84f462 100644 --- a/site/spec/models/agenda_spec.rb +++ b/site/spec/models/agenda_spec.rb @@ -248,4 +248,30 @@ describe Agenda do current_agenda.voting_array.should == [[i2.title, [v21.description, v22.description], i2.timelimits], [i3.title, [], i3.timelimits]] end + + describe '.update_voting_options' do + it 'should remove unneeded voting options and keep existing needed options' do + current_agenda = Factory(:agenda) + item = Factory(:agenda_item, :agenda => current_agenda) + unneeded_option = Factory(:voting_option, :agenda_item => item, :description => 'unneeded') + needed_option = Factory(:voting_option, :agenda_item => item, :description => 'needed') + + VotingOption.count.should be_equal(2) + + Agenda.update_voting_options [[item.title, [needed_option.description]]] + + VotingOption.count.should be_equal(1) + VotingOption.first.description.should == needed_option.description + VotingOption.first.id.should == needed_option.id + end + it 'should create requested new voting options' do + current_agenda = Factory(:agenda) + item = Factory(:agenda_item, :agenda => current_agenda) + needed_option = Factory(:voting_option, :agenda_item => item, :description => 'needed') + + Agenda.update_voting_options [[item.title, [needed_option.description, 'new option']]] + VotingOption.count.should be_equal(2) + VotingOption.last.description.should == 'new option' + end + end end -- cgit v1.2.3-65-gdbad