diff options
author | Joachim Filip Ignacy Bartosik <jbartosik@gmail.com> | 2011-05-30 20:08:21 +0200 |
---|---|---|
committer | Joachim Filip Ignacy Bartosik <jbartosik@gmail.com> | 2011-06-03 19:27:10 +0200 |
commit | 7d5a200601a723b8a6acef4f49ea02bd2bba9935 (patch) | |
tree | 9c12dd3e3cf0a7d108b7eb3755c26260a80a7e5c /site | |
parent | Application receives data from IRC bot (diff) | |
download | council-webapp-7d5a200601a723b8a6acef4f49ea02bd2bba9935.tar.gz council-webapp-7d5a200601a723b8a6acef4f49ea02bd2bba9935.tar.bz2 council-webapp-7d5a200601a723b8a6acef4f49ea02bd2bba9935.zip |
Proxy model
Diffstat (limited to 'site')
-rw-r--r-- | site/app/controllers/proxies_controller.rb | 7 | ||||
-rw-r--r-- | site/app/models/proxy.rb | 57 | ||||
-rw-r--r-- | site/config/hobo_routes.rb | 10 | ||||
-rw-r--r-- | site/db/schema.rb | 16 | ||||
-rw-r--r-- | site/spec/factories.rb | 6 | ||||
-rw-r--r-- | site/spec/models/proxy_spec.rb | 114 |
6 files changed, 209 insertions, 1 deletions
diff --git a/site/app/controllers/proxies_controller.rb b/site/app/controllers/proxies_controller.rb new file mode 100644 index 0000000..afd6822 --- /dev/null +++ b/site/app/controllers/proxies_controller.rb @@ -0,0 +1,7 @@ +class ProxiesController < ApplicationController + + hobo_model_controller + + auto_actions :all + +end diff --git a/site/app/models/proxy.rb b/site/app/models/proxy.rb new file mode 100644 index 0000000..7be0a0b --- /dev/null +++ b/site/app/models/proxy.rb @@ -0,0 +1,57 @@ +class Proxy < ActiveRecord::Base + + hobo_model # Don't put anything above this + + fields do + # Remeber nicks from when meeting took place + # May be useful when reading logs (if user changes nick) + council_member_nick :string, :null => false + proxy_nick :string, :null => false + timestamps + end + + belongs_to :council_member, :class_name => 'User', :null => false + belongs_to :proxy, :class_name => 'User', :null => false + belongs_to :agenda, :null => false + + validates_presence_of :council_member, :proxy, :agenda + validates_uniqueness_of :council_member_id, :scope => :agenda_id + validates_uniqueness_of :proxy_id, :scope => :agenda_id + validate :council_member_must_be_council_member + validate :proxy_must_not_be_council_member + + # --- Permissions --- # + + def create_permitted? + return false unless acting_user.council_member? + council_member_is?(acting_user) + end + + def update_permitted? + false + end + + def destroy_permitted? + return false if agenda.state == 'old' + council_member_is?(acting_user) + end + + def view_permitted?(field) + true + end + + before_create do |p| + p.council_member_nick = p.council_member.irc_nick + p.proxy_nick = p.proxy.irc_nick + end + + protected + def council_member_must_be_council_member + return if council_member.nil? + errors.add(:council_member, 'must be council member') unless council_member.council_member? + end + def proxy_must_not_be_council_member + return if proxy.nil? + errors.add(:proxy, 'must not be council member') if proxy.council_member? + end +end diff --git a/site/config/hobo_routes.rb b/site/config/hobo_routes.rb index ad63a15..a426e53 100644 --- a/site/config/hobo_routes.rb +++ b/site/config/hobo_routes.rb @@ -5,6 +5,16 @@ Council::Application.routes.draw do + # Resource routes for controller "proxies" + get 'proxies(.:format)' => 'proxies#index', :as => 'proxies' + get 'proxies/new(.:format)', :as => 'new_proxy' + get 'proxies/:id/edit(.:format)' => 'proxies#edit', :as => 'edit_proxy' + get 'proxies/:id(.:format)' => 'proxies#show', :as => 'proxy', :constraints => { :id => %r([^/.?]+) } + post 'proxies(.:format)' => 'proxies#create', :as => 'create_proxy' + put 'proxies/:id(.:format)' => 'proxies#update', :as => 'update_proxy', :constraints => { :id => %r([^/.?]+) } + delete 'proxies/:id(.:format)' => 'proxies#destroy', :as => 'destroy_proxy', :constraints => { :id => %r([^/.?]+) } + + # Lifecycle routes for controller "users" post 'users/signup(.:format)' => 'users#do_signup', :as => 'do_user_signup' get 'users/signup(.:format)' => 'users#signup', :as => 'user_signup' diff --git a/site/db/schema.rb b/site/db/schema.rb index ea99919..fec65de 100644 --- a/site/db/schema.rb +++ b/site/db/schema.rb @@ -10,7 +10,7 @@ # # It's strongly recommended to check this file into your version control system. -ActiveRecord::Schema.define(:version => 20110601094807) do +ActiveRecord::Schema.define(:version => 20110603133359) do create_table "agenda_items", :force => true do |t| t.string "title" @@ -47,6 +47,20 @@ ActiveRecord::Schema.define(:version => 20110601094807) do add_index "participations", ["agenda_id"], :name => "index_participations_on_agenda_id" add_index "participations", ["participant_id"], :name => "index_participations_on_participant_id" + create_table "proxies", :force => true do |t| + t.string "council_member_nick", :null => false + t.string "proxy_nick", :null => false + t.datetime "created_at" + t.datetime "updated_at" + t.integer "council_member_id", :null => false + t.integer "proxy_id", :null => false + t.integer "agenda_id", :null => false + end + + add_index "proxies", ["agenda_id"], :name => "index_proxies_on_agenda_id" + add_index "proxies", ["council_member_id"], :name => "index_proxies_on_council_member_id" + add_index "proxies", ["proxy_id"], :name => "index_proxies_on_proxy_id" + create_table "users", :force => true do |t| t.string "salt", :limit => 40 t.string "remember_token" diff --git a/site/spec/factories.rb b/site/spec/factories.rb index 1691b50..dbf841e 100644 --- a/site/spec/factories.rb +++ b/site/spec/factories.rb @@ -25,3 +25,9 @@ Factory.define :voting_option do |v|; v.agenda_item { AgendaItem.create! } v.description { "example" } end + +Factory.define :proxy do |p|; + p.council_member {users_factory(:council)} + p.proxy {users_factory(:user)} + p.agenda {Factory(:agenda)} +end diff --git a/site/spec/models/proxy_spec.rb b/site/spec/models/proxy_spec.rb new file mode 100644 index 0000000..a94aa06 --- /dev/null +++ b/site/spec/models/proxy_spec.rb @@ -0,0 +1,114 @@ +require 'spec_helper' + +describe Proxy do + it 'should require agenda to be set' do + p = Proxy.new + p.should_not be_valid + p.errors.keys.include?(:agenda).should be_true + p.errors[:agenda].include?("can't be blank").should be_true + + p.agenda = Agenda.current || Factory(:agenda) + p.valid? + p.errors.keys.include?(:agenda).should be_false + end + + it 'should require council member to be set and to be council member' do + p = Proxy.new + p.should_not be_valid + p.errors.keys.include?(:council_member).should be_true + p.errors[:council_member].include?("can't be blank").should be_true + + p.council_member = Factory(:user) + p.should_not be_valid + p.errors.keys.include?(:council_member).should be_true + p.errors[:council_member].include?('must be council member').should be_true + + p.council_member = users_factory(:council) + p.valid? + p.errors.keys.include?(:council_member).should be_false + end + + it 'should require proxy to be set and not to be council member' do + p = Proxy.new + p.should_not be_valid + p.errors.keys.include?(:proxy).should be_true + p.errors[:proxy].include?("can't be blank").should be_true + + p.proxy = users_factory(:council) + p.should_not be_valid + p.errors.keys.include?(:proxy).should be_true + p.errors[:proxy].include?('must not be council member').should be_true + + p.proxy = Factory(:user) + p.valid? + p.errors.keys.include?(:proxy).should be_false + end + + it 'should allow only council members to create for their selfs' do + for u in users_factory(:user, :admin) + p = Proxy.new :council_member => u + p.should_not be_creatable_by(u) + end + + p = Proxy.new :council_member => users_factory(:council_admin) + for u in users_factory(:council, :council_admin) + p.should_not be_creatable_by(u) + end + + for u in users_factory(:council, :council_admin) + p = Proxy.new :council_member => u + p.should be_creatable_by(u) + end + end + + it 'should allow no one to update or edit' do + p = Factory(:proxy) + for u in users_factory(AllRoles) + [p.council_member, p.proxy] + p.should_not be_editable_by(u) + p.should_not be_updatable_by(u) + end + end + + + it 'should allow everyone to view' do + p = Factory(:proxy) + for u in users_factory(AllRoles) + [p.council_member, p.proxy] + p.should be_viewable_by(u) + end + end + + it 'should allow council members to destroy their own proxies for current meeting' do + a = Factory(:agenda) + p = Factory(:proxy, :agenda => a) + p.should be_destroyable_by(p.council_member) + end + + it 'should not allow council members to destroy their own proxies for old meetings' do + a = Factory(:agenda, :state => 'old') + p = Factory(:proxy, :agenda => a) + p.should_not be_destroyable_by(p.council_member) + end + + it 'should not allow users to destoy someone else proxy' do + a = Factory(:agenda) + p = Factory(:proxy, :agenda => a) + for u in users_factory(AllRoles) + p.should_not be_destroyable_by(u) + end + end + + it 'should remember nick of council member and proxy from time it was created' do + c = users_factory(:council) + u = users_factory(:user) + p = Factory(:proxy, :council_member => c, :proxy => u) + c_nick = c.irc_nick + u_nick = u.irc_nick + u.irc_nick = 'diffrent nick' + c.irc_nick = 'other nick' + u.save! + c.save! + p.reload + p.council_member_nick.should == c_nick + p.proxy_nick.should == u_nick + end +end |