* [gentoo-commits] proj/council-webapp:master commit in: /, site/doc/sample_configs/, site/config/, site/config/initializers/, ...
@ 2011-06-05 20:37 Petteri Räty
0 siblings, 0 replies; only message in thread
From: Petteri Räty @ 2011-06-05 20:37 UTC (permalink / raw
To: gentoo-commits
commit: 301b1015f99f8e32a4843cf5bf0243409317e53e
Author: Joachim Filip Ignacy Bartosik <jbartosik <AT> gmail <DOT> com>
AuthorDate: Thu May 26 17:07:37 2011 +0000
Commit: Petteri Räty <betelgeuse <AT> gentoo <DOT> org>
CommitDate: Fri Jun 3 17:27:05 2011 +0000
URL: http://git.overlays.gentoo.org/gitweb/?p=proj/council-webapp.git;a=commit;h=301b1015
Application receives data from IRC bot
---
.gitignore | 2 +-
site/app/controllers/agendas_controller.rb | 12 ++++++++++
site/app/models/agenda.rb | 13 +++++++++++
site/config/initializers/custom_configs.rb | 2 +
site/config/routes.rb | 1 +
site/doc/sample_configs/bot.yml | 2 +
site/spec/factories.rb | 1 +
site/spec/models/agenda_spec.rb | 31 ++++++++++++++++++++++++++++
8 files changed, 63 insertions(+), 1 deletions(-)
diff --git a/.gitignore b/.gitignore
index 1abc1e5..3487f9a 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1,8 +1,8 @@
site/.bundle
site/db/*.sqlite3
-site/config/database.yml
site/log/*.log
site/tmp/**/*
site/app/views/taglibs/auto
+site/config/*.yml
*.swp
*.pyc
diff --git a/site/app/controllers/agendas_controller.rb b/site/app/controllers/agendas_controller.rb
index 0ee7ae0..ce84f1f 100644
--- a/site/app/controllers/agendas_controller.rb
+++ b/site/app/controllers/agendas_controller.rb
@@ -2,6 +2,7 @@ class AgendasController < ApplicationController
hobo_model_controller
+ before_filter :authenticate_bot, :only => :results
auto_actions :all
def index
@@ -11,4 +12,15 @@ class AgendasController < ApplicationController
def current_items
render :json => Agenda.current.voting_array
end
+
+ def results
+ Agenda.process_results JSON.parse(request.env["rack.input"].read)
+ end
+
+ private
+ def authenticate_bot
+ authenticate_or_request_with_http_basic do |user_name, password|
+ user_name == CustomConfig['Bot']['user'] && password == CustomConfig['Bot']['password']
+ end
+ end
end
diff --git a/site/app/models/agenda.rb b/site/app/models/agenda.rb
index ed8e385..44386e3 100644
--- a/site/app/models/agenda.rb
+++ b/site/app/models/agenda.rb
@@ -62,6 +62,19 @@ class Agenda < ActiveRecord::Base
false
end
+ def self.process_results(results)
+ a = Agenda.current
+ for item_title in results.keys
+ i = AgendaItem.first :conditions => { :agenda_id => a, :title => item_title }
+ votes = results[item_title]
+ for voter in votes.keys
+ o = VotingOption.first :conditions => { :agenda_item_id => i.id, :description => votes[voter] }
+ u = ::User.find_by_irc_nick voter
+ Vote.create! :voting_option => o, :user => u
+ end
+ end
+ end
+
def possible_transitions
transitions = case state
when 'open'
diff --git a/site/config/initializers/custom_configs.rb b/site/config/initializers/custom_configs.rb
new file mode 100644
index 0000000..599646f
--- /dev/null
+++ b/site/config/initializers/custom_configs.rb
@@ -0,0 +1,2 @@
+CustomConfig = {}
+CustomConfig['Bot'] = YAML.load open('config/bot.yml').read
diff --git a/site/config/routes.rb b/site/config/routes.rb
index e3337bb..8621347 100644
--- a/site/config/routes.rb
+++ b/site/config/routes.rb
@@ -5,6 +5,7 @@ Council::Application.routes.draw do
match 'users/voters' => 'users#voters', :as => 'voters'
match 'agendas/current_items' => 'agendas#current_items', :as => 'current_items'
+ match 'agendas/results' => 'agendas#results', :as => 'results'
# The priority is based upon order of creation:
# first created -> highest priority.
diff --git a/site/doc/sample_configs/bot.yml b/site/doc/sample_configs/bot.yml
new file mode 100644
index 0000000..6af6f1a
--- /dev/null
+++ b/site/doc/sample_configs/bot.yml
@@ -0,0 +1,2 @@
+user: user
+password: password
diff --git a/site/spec/factories.rb b/site/spec/factories.rb
index bc6fe8e..1691b50 100644
--- a/site/spec/factories.rb
+++ b/site/spec/factories.rb
@@ -11,6 +11,7 @@ end
Factory.define :agenda do |a|; end
Factory.define :agenda_item do |a|
+ a.sequence(:title) { |n| "Agenda Item #{n}" }
end
Factory.define :participation do |p|; end
diff --git a/site/spec/models/agenda_spec.rb b/site/spec/models/agenda_spec.rb
index 14a1178..5943512 100644
--- a/site/spec/models/agenda_spec.rb
+++ b/site/spec/models/agenda_spec.rb
@@ -104,4 +104,35 @@ describe Agenda do
(council_names - agenda.participations.*.participant.*.name).should be_empty
(agenda.participations.*.participant.*.name - council_names).should be_empty
end
+ it 'should properly create votes' do
+ Factory(:agenda)
+ a1 = Factory(:agenda_item, :agenda => Agenda.current)
+ a2 = Factory(:agenda_item, :agenda => Agenda.current)
+ a3 = Factory(:agenda_item, :agenda => Agenda.current)
+ Agenda.current.agenda_items.each do |item|
+ Factory(:voting_option, :agenda_item => item, :description => 'Yes')
+ Factory(:voting_option, :agenda_item => item, :description => 'No')
+ Factory(:voting_option, :agenda_item => item, :description => 'Dunno')
+ end
+
+ u = users_factory(:council, :council, :council)
+ Vote.count.should be_zero
+
+ results_hash = {
+ a1.title => { u[0].irc_nick => 'Yes', u[1].irc_nick => 'Yes', u[2].irc_nick => 'Yes'},
+ a2.title => { u[0].irc_nick => 'Yes', u[1].irc_nick => 'No', u[2].irc_nick => 'Dunno'},
+ a3.title => { u[0].irc_nick => 'Dunno', u[1].irc_nick => 'Dunno', u[2].irc_nick => 'No'}
+ }
+
+ Agenda.process_results results_hash
+
+ Vote.count.should be_equal(9)
+
+ u[0].votes.*.voting_option.*.description.should == ['Yes', 'Yes', 'Dunno']
+ u[1].votes.*.voting_option.*.description.should == ['Yes', 'No', 'Dunno']
+ u[2].votes.*.voting_option.*.description.should == ['Yes', 'Dunno', 'No']
+ a1.voting_options.*.votes.flatten.*.voting_option.*.description.should == ['Yes', 'Yes', 'Yes']
+ a2.voting_options.*.votes.flatten.*.voting_option.*.description.should == ['Yes', 'No', 'Dunno']
+ a3.voting_options.*.votes.flatten.*.voting_option.*.description.should == ['No', 'Dunno', 'Dunno']
+ end
end
^ permalink raw reply related [flat|nested] only message in thread
only message in thread, other threads:[~2011-06-05 20:38 UTC | newest]
Thread overview: (only message) (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2011-06-05 20:37 [gentoo-commits] proj/council-webapp:master commit in: /, site/doc/sample_configs/, site/config/, site/config/initializers/, Petteri Räty
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox