Author: assaf
Date: Fri May  9 17:15:43 2008
New Revision: 654987

URL: http://svn.apache.org/viewvc?rev=654987&view=rev
Log:
Migrated to timestamped migrations, changed Stakeholder role to be string 
instead of integer.

Added:
    ode/sandbox/singleshot/spec/models/stakeholder_spec.rb
Removed:
    ode/sandbox/singleshot/db/migrate/001_create_people.rb
    ode/sandbox/singleshot/db/migrate/002_create_tasks.rb
    ode/sandbox/singleshot/db/migrate/003_create_stakeholders.rb
Modified:
    ode/sandbox/singleshot/app/models/stakeholder.rb

Modified: ode/sandbox/singleshot/app/models/stakeholder.rb
URL: 
http://svn.apache.org/viewvc/ode/sandbox/singleshot/app/models/stakeholder.rb?rev=654987&r1=654986&r2=654987&view=diff
==============================================================================
--- ode/sandbox/singleshot/app/models/stakeholder.rb (original)
+++ ode/sandbox/singleshot/app/models/stakeholder.rb Fri May  9 17:15:43 2008
@@ -1,12 +1,12 @@
 # == Schema Information
-# Schema version: 3
+# Schema version: 20080506015153
 #
 # Table name: stakeholders
 #
 #  id         :integer       not null, primary key
 #  task_id    :integer       not null
 #  person_id  :integer       not null
-#  role       :integer(2)    not null
+#  role       :string(255)   not null
 #  created_at :datetime      
 #  updated_at :datetime      
 #
@@ -16,41 +16,61 @@
 # the Task itself.
 class Stakeholder < ActiveRecord::Base
 
-  module Roles
+  # A task will only have one stakeholder in this role:
+  # * creator         -- Person who created the task, specified at creation.
+  # * owner           -- Person who currently owns (performs) the task.
+  SINGULAR_ROLES = ['creator', 'owner']
+
+  # A task will have multiple stakeholders in this role:
+  # * potential -- Person who is allowed to claim (become owner of) the task.
+  # * excluded  -- Person who is not allowed to claim the task.
+  # * admin     -- Admins are allowed to modify the task, change its status, 
etc.
+  # * observer  -- Watches and receives notifications about the task.
+  PLURAL_ROLES = ['potential', 'excluded', 'observer', 'admin']
 
-    # Roles for stakeholders associated with a task.  Supports the following 
roles:
-    # * :creator         -- Person who created the task, specified at creation.
-    # * :owner           -- Person who currently owns (performs) the task.
-    # * :potential_owner -- Person who is allowed to claim (become owner of) 
the task.
-    # * :excluded_owner  -- Person who is not allowed to claim the task.
-    # * :admin           -- Admins are allowed to modify the task, change its 
status, etc.
-    #
-    # Role is stored as index value, but presented elsewhere as symbol, so 
when adding new roles,
-    # make sure to append but not to remove or change the order of roles in 
this list.
-    ROLES = [:creator, :owner, :potential_owner, :excluded_owner, :observer, 
:admin]
-
-    # The following roles allow one stakeholder per task: creator, owner.
-    SINGULAR_ROLES = [:creator, :owner]
-
-    # These roles allow multiple stakeholders per task.
-    PLURAL_ROLES = ROLES - SINGULAR_ROLES
-    
-    # Based on PLURAL_ROLES, but takes the pluralized form (i.e. admin becomes 
admins)
-    PLURALIZED_ROLES = PLURAL_ROLES.map { |role| role.to_s.pluralize.to_sym }
-
-  end
-
-  include Roles
+  # All supported roles.
+  ROLES = SINGULAR_ROLES + PLURAL_ROLES
 
   # Stakeholder associated with a task.
   belongs_to :task
+  validates_presence_of :task
 
   # Stakeholder associated with a person.
   belongs_to :person
-  validates_presence_of :person_id
+  validates_presence_of :person
+
+  # Role for this stakeholder.
+  validates_inclusion_of :role, :in=>ROLES
 
-  # Enumerated role, see Task::Roles for more details.
-  enumerable :role, ROLES, :check_methods=>:true
-  validates_presence_of :role
+  validates_uniqueness_of :role, :scope=>[:task_id, :person_id]
+
+  module SingularRoleMethods
+
+    def self.included(base)
+      SINGULAR_ROLES.each do |role|
+        base.belongs_to role, :class_name=>'Person'
+        base.define_method("#{role}?") { |identity| send(role) == 
Person.identify(identity)  }
+        base.define_method "#{role}_with_identify=" do |identity|
+          send "#{role}_without_identify=", identity.blank? ? nil : 
identify_or_create(identity)
+        end
+        base.alias_method_chain "#{role}=", :identify
+      end
+
+      base.before_validation do |record|
+        # Assign owner if only one potential owner specified.
+        unless record.owner
+          potential = record.potential_owners - record.excluded_owners
+          record.owner = potentia.first if potential.size == 1
+        end
+      end
+
+      base.validate do |record|
+        record.errors.add :owner, "#{record.owner.fullname} is on the excluded 
owners list and cannot claim this task." if
+          record.owner && 
record.excluded_owners.map(&:identity).include?(record.owner.identity)
+        record.errors.add :creator unless record.creator.nil? || 
(record.new_record? == record.creator.new_record?)
+      end
+    end
+
+  end
 
 end

Added: ode/sandbox/singleshot/spec/models/stakeholder_spec.rb
URL: 
http://svn.apache.org/viewvc/ode/sandbox/singleshot/spec/models/stakeholder_spec.rb?rev=654987&view=auto
==============================================================================
--- ode/sandbox/singleshot/spec/models/stakeholder_spec.rb (added)
+++ ode/sandbox/singleshot/spec/models/stakeholder_spec.rb Fri May  9 17:15:43 
2008
@@ -0,0 +1,40 @@
+require File.dirname(__FILE__) + '/../spec_helper'
+
+describe Stakeholder do
+  include Specs::Tasks
+
+  before :all do
+    @person = person('person')
+    @task = Task.create(default_task)
+  end
+
+  it 'should have person' do
+    Stakeholder.create(:task=>@task, :role=>'admin').should 
have(1).error_on(:person)
+  end
+
+  it 'should have task' do
+    Stakeholder.create(:person=>@person, :role=>'admin').should 
have(1).error_on(:task)
+  end
+
+  it 'should have role' do
+    Stakeholder.create(:person=>@person, :task=>@task).should 
have(1).error_on(:role)
+  end
+
+  it 'should have supported role' do
+    Stakeholder::ROLES.each do |role|
+      Stakeholder.create(:person=>@person, :task=>@task, :role=>role).should 
have(:no).errors
+    end
+  end
+
+  it 'should not have unsupported role' do
+    ['foo', 'bar'].each do |role|
+      Stakeholder.create(:person=>@person, :task=>@task, :role=>role).should 
have(1).error_on(:role)
+    end
+  end
+
+  it 'should be unique combination of task person and role' do
+    Stakeholder.create(:person=>@person, :task=>@task, :role=>'admin').should 
have(:no).errors
+    Stakeholder.create(:person=>@person, :task=>@task, 
:role=>'observer').should have(:no).errors
+    Stakeholder.create(:person=>@person, :task=>@task, :role=>'admin').should 
have(1).errors_on(:role)
+  end
+end


Reply via email to