aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJoachim Filip Ignacy Bartosik <jbartosik@gmail.com>2011-05-18 17:19:38 +0200
committerJoachim Filip Ignacy Bartosik <jbartosik@gmail.com>2011-06-01 15:21:14 +0200
commit4b874a907e16dcfb61cc82a69f9c3891bff1bfa8 (patch)
treed364ca3c0983cfd383c550792df6529443ed8bbe /site/app/models
parentMeetBot plugin from Debian (diff)
downloadcouncil-webapp-4b874a907e16dcfb61cc82a69f9c3891bff1bfa8.tar.gz
council-webapp-4b874a907e16dcfb61cc82a69f9c3891bff1bfa8.tar.bz2
council-webapp-4b874a907e16dcfb61cc82a69f9c3891bff1bfa8.zip
Application provides data for IRC bot
Diffstat (limited to 'site/app/models')
-rw-r--r--site/app/models/agenda.rb6
-rw-r--r--site/app/models/agenda_item.rb2
-rw-r--r--site/app/models/user.rb1
-rw-r--r--site/app/models/vote.rb54
-rw-r--r--site/app/models/voting_option.rb41
5 files changed, 103 insertions, 1 deletions
diff --git a/site/app/models/agenda.rb b/site/app/models/agenda.rb
index f8f90df..ed8e385 100644
--- a/site/app/models/agenda.rb
+++ b/site/app/models/agenda.rb
@@ -81,6 +81,12 @@ class Agenda < ActiveRecord::Base
['open', 'submissions_closed'].include?(state.to_s)
end
+ def voting_array
+ agenda_items.collect do |item|
+ [item.title, item.voting_options.*.description]
+ end
+ end
+
protected
def there_is_only_one_non_archival_agenda
return if(state.to_s == 'old')
diff --git a/site/app/models/agenda_item.rb b/site/app/models/agenda_item.rb
index f5108bd..f590bb1 100644
--- a/site/app/models/agenda_item.rb
+++ b/site/app/models/agenda_item.rb
@@ -12,6 +12,7 @@ class AgendaItem < ActiveRecord::Base
belongs_to :user, :creator => true
belongs_to :agenda
+ has_many :voting_options
# --- Permissions --- #
def create_permitted?
@@ -49,5 +50,4 @@ class AgendaItem < ActiveRecord::Base
return false unless agenda.nil?
return acting_user == user if [nil, :title, :discussion, :body].include?(field)
end
-
end
diff --git a/site/app/models/user.rb b/site/app/models/user.rb
index e49fa77..738165e 100644
--- a/site/app/models/user.rb
+++ b/site/app/models/user.rb
@@ -11,6 +11,7 @@ class User < ActiveRecord::Base
timestamps
end
+ has_many :votes
# --- Signup lifecycle --- #
lifecycle do
diff --git a/site/app/models/vote.rb b/site/app/models/vote.rb
new file mode 100644
index 0000000..00c64a7
--- /dev/null
+++ b/site/app/models/vote.rb
@@ -0,0 +1,54 @@
+class Vote < ActiveRecord::Base
+
+ hobo_model # Don't put anything above this
+
+ fields do
+ timestamps
+ end
+
+ belongs_to :voting_option, :null => false
+ belongs_to :user, :null => false
+
+ index [:voting_option_id, :user_id], :unique => true
+
+ validates_presence_of :voting_option
+ validates_presence_of :user
+ validates_uniqueness_of :voting_option_id, :scope => :user_id
+ validate :user_voted_only_once
+ validate :user_is_council_member
+ # --- Permissions --- #
+
+ def create_permitted?
+ false
+ end
+
+ def update_permitted?
+ false
+ end
+
+ def destroy_permitted?
+ false
+ end
+
+ def view_permitted?(field)
+ true
+ end
+
+ protected
+ def user_voted_only_once
+ return if user.nil?
+ return if voting_option.nil?
+ return if voting_option.agenda_item.nil?
+ other_votes = Vote.joins(:voting_option).where(['voting_options.agenda_item_id = ? AND votes.user_id = ?',
+ voting_option.agenda_item_id, user_id])
+ other_votes = other_votes.id_is_not(id) unless new_record?
+ if other_votes.count > 0
+ errors.add(:user, 'User can vote only once per agenda item.')
+ end
+ end
+
+ def user_is_council_member
+ return if user.nil?
+ errors.add(:user, 'Only council members can vote.') unless user.council_member?
+ end
+end
diff --git a/site/app/models/voting_option.rb b/site/app/models/voting_option.rb
new file mode 100644
index 0000000..b9c2226
--- /dev/null
+++ b/site/app/models/voting_option.rb
@@ -0,0 +1,41 @@
+class VotingOption < ActiveRecord::Base
+
+ hobo_model # Don't put anything above this
+
+ fields do
+ description :string
+ timestamps
+ end
+
+ belongs_to :agenda_item, :null => false
+ has_many :votes
+
+ validates_presence_of :agenda_item
+ validates_uniqueness_of :description, :scope => :agenda_item_id
+
+ def name
+ description
+ end
+
+ # --- Permissions --- #
+
+ def create_permitted?
+ acting_user.council_member?
+ end
+
+ def update_permitted?
+ return false unless acting_user.council_member?
+ return true if agenda_item.nil?
+ return true if agenda_item.agenda.nil?
+ return true if agenda_item.agenda.state == 'open'
+ false
+ end
+
+ def destroy_permitted?
+ updatable_by?(acting_user)
+ end
+
+ def view_permitted?(field)
+ true
+ end
+end