ode-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From as...@apache.org
Subject svn commit: r660672 - in /ode/sandbox/singleshot: app/controllers/ app/helpers/ app/models/ app/views/sandwiches/ config/
Date Tue, 27 May 2008 19:55:23 GMT
Author: assaf
Date: Tue May 27 12:55:19 2008
New Revision: 660672

URL: http://svn.apache.org/viewvc?rev=660672&view=rev
Log:
Checkpoint: sandwich task completing and nicely with transition to tasks list.

Added:
    ode/sandbox/singleshot/app/controllers/performs_controller.rb
Modified:
    ode/sandbox/singleshot/app/controllers/sandwiches_controller.rb
    ode/sandbox/singleshot/app/controllers/tasks_controller.rb
    ode/sandbox/singleshot/app/helpers/task_helper.rb
    ode/sandbox/singleshot/app/models/sandwich.rb
    ode/sandbox/singleshot/app/models/task.rb
    ode/sandbox/singleshot/app/views/sandwiches/show.html.erb
    ode/sandbox/singleshot/config/routes.rb

Added: ode/sandbox/singleshot/app/controllers/performs_controller.rb
URL: http://svn.apache.org/viewvc/ode/sandbox/singleshot/app/controllers/performs_controller.rb?rev=660672&view=auto
==============================================================================
--- ode/sandbox/singleshot/app/controllers/performs_controller.rb (added)
+++ ode/sandbox/singleshot/app/controllers/performs_controller.rb Tue May 27 12:55:19 2008
@@ -0,0 +1,19 @@
+class PerformsController < ApplicationController
+
+  def show
+    respond_to do |wants|
+      wants.xml { render :xml=>@task }
+      wants.json { render :json=>@task }
+    end
+  end
+
+private
+
+  def authenticate
+    @task = Task.visible.find(params['task_id'])
+    authenticate_or_request_with_http_basic request.domain do |login, token|
+      login == '_token' && @authenticated = @task.authenticate(token)
+    end
+  end
+
+end

Modified: ode/sandbox/singleshot/app/controllers/sandwiches_controller.rb
URL: http://svn.apache.org/viewvc/ode/sandbox/singleshot/app/controllers/sandwiches_controller.rb?rev=660672&r1=660671&r2=660672&view=diff
==============================================================================
--- ode/sandbox/singleshot/app/controllers/sandwiches_controller.rb (original)
+++ ode/sandbox/singleshot/app/controllers/sandwiches_controller.rb Tue May 27 12:55:19 2008
@@ -1,15 +1,17 @@
 class SandwichesController < ApplicationController
 
-  before_filter :instance
-  before_filter :update_instance, :only=>[:update, :create]
   skip_filter :authenticate
+  skip_before_filter :verify_authenticity_token
   layout false
 
+  before_filter :instance
+
   def show
-    @read_only = true unless params['perform'] == 'true'
+    #@read_only = true unless params['perform'] == 'true'
   end
 
   def update
+    @sandwich.update_attributes params['sandwich']
     if @sandwich.save
       flash[:success] = 'Changes have been saved.'
       redirect_to :action=>'show', :task_url=>@task_url, :perform=>true
@@ -19,9 +21,13 @@
   end
 
   def create
-    if @sandwich.complete
-      flash[:success] = 'Thank you.  Sandwich created!'
-      redirect_to :action=>'show', :task_url=>@task_url, :perform=>true
+    @sandwich.update_attributes params['sandwich']
+    if @sandwich.save
+
+      flash[:success] = 'Changes have been saved.'
+      #redirect_to :action=>'show', :task_url=>@task_url, :perform=>true
+      render :text=>"<script>frames.top.location.href='http://localhost:3000/tasks'</script>"
+
     else
       render :action=>'show'
     end
@@ -30,18 +36,11 @@
 private
 
   def instance
-    @task_url = params['task_url'] or raise ActiveRecord::RecordNotFound
-    @sandwich = Sandwich.load(@task_url)
-    @read_only = true if @sandwich.status == 'completed'
-  end
-
-  def update_instance
-    if params = self.params['sandwich']
-      params['toppings'] = params['toppings'] * ';'
-      @sandwich.attributes = params
-    else
-      head :bad_request
-    end 
+    #@task_url = params['task_url'] or raise ActiveRecord::RecordNotFound
+    #uri = URI(@task_url)
+    #xml = REXML::Document.new(open(uri.to_s, :http_basic_authentication=>[uri.user, uri.password]))
+    session[:sandwich] = @sandwich = Sandwich.new(session[:sandwich])
+    #@read_only = true if @sandwich.status == 'completed'
   end
 
 end

Modified: ode/sandbox/singleshot/app/controllers/tasks_controller.rb
URL: http://svn.apache.org/viewvc/ode/sandbox/singleshot/app/controllers/tasks_controller.rb?rev=660672&r1=660671&r2=660672&view=diff
==============================================================================
--- ode/sandbox/singleshot/app/controllers/tasks_controller.rb (original)
+++ ode/sandbox/singleshot/app/controllers/tasks_controller.rb Tue May 27 12:55:19 2008
@@ -3,9 +3,8 @@
   access_key_authentication :only=>[:index, :completed, :following, :show]
 
   verify :params=>:task, :only=>:update, :render=>{:text=>'Missing task', :status=>:bad_request}
-  before_filter :authenticate, :except=>[:show, :update, :complete, :destroy]
-  instance :task, :only=>[:show, :update, :complete, :destroy], :check=>:instance_accessible
-  before_filter :forbid_reserved, :except=>[:update, :destroy]
+  before_filter :set_task, :only=>[:show, :update, :complete, :destroy]
+
 
   def index
     @title, @subtitle = 'Tasks', 'Tasks you are performing or can claim for your own.'
@@ -56,6 +55,9 @@
   end
 
 
+
+
+
   def show
     @alternate = { Mime::ICS=>formatted_tasks_url(:format=>:ics, :access_key=>authenticated.access_key)
}
     respond_to do |format|
@@ -131,24 +133,8 @@
 
 private
 
-  # Authenticate and make sure the instance is accessible.  Use this instead of the authentication
-  # filter to allow access control based on token authentication.  Precludes access to cancelled
tasks.:w
-
-  def instance_accessible(task)
-    # Use _token authentication (HTTP Basic) to authorize stakeholder associated with task
-    # otherwise use regular authentication.
-    authenticate_with_http_basic do |login, token|
-      @authenticated = instance.authorize(token) if login == '_token'
-    # Task accessible if:
-    # - Authenticated user is stakeholder
-    # - Task has not been cancelled (deleted)
-    end or authenticate
-    task.stakeholder?(authenticated) && !task.cancelled? if authenticated
-  end
-
-  # Return 403 (Forbidden) when accessing a reserved task (show, complete)
-  def forbid_reserved
-    raise ActiveRecord::RecordNotFound if @task && @task.reserved?
+  def set_task
+    @task = Task.for_stakeholder(authenticated).with_stakeholders.find(params[:id])
   end
 
   # Determines the outcome content type based on the request content type.

Modified: ode/sandbox/singleshot/app/helpers/task_helper.rb
URL: http://svn.apache.org/viewvc/ode/sandbox/singleshot/app/helpers/task_helper.rb?rev=660672&r1=660671&r2=660672&view=diff
==============================================================================
--- ode/sandbox/singleshot/app/helpers/task_helper.rb (original)
+++ ode/sandbox/singleshot/app/helpers/task_helper.rb Tue May 27 12:55:19 2008
@@ -2,7 +2,7 @@
 
   def quick_actions(task)
     [ task.admin?(authenticated) && button_to('Manage', edit_task_url(task), :method=>:get,
:title=>'Manage this task'),
-      task.can_claim?(authenticated) && button_to('Claim', task_owner_url(task, 'owner'=>authenticated.identity),
+      task.can_claim?(authenticated) && button_to('Claim', task_url(task, 'task[owner]'=>authenticated.identity),
                                                                           :method=>:put,
:title=>'Claim task')
     ].select { |action| action }.join(' ')
   end
@@ -16,7 +16,7 @@
   end
 
   def task_iframe_url(task, person = authenticated)
-    task_uri = URI(task_url(task))
+    task_uri = URI(task_perform_url(task))
     task_uri.user, task_uri.password = '_token', task.token_for(person)
     uri = URI(task.frame_url)
     uri.query = CGI.parse(uri.query || '').update('perform'=>task.owner?(person), 'task_url'=>task_uri).to_query

Modified: ode/sandbox/singleshot/app/models/sandwich.rb
URL: http://svn.apache.org/viewvc/ode/sandbox/singleshot/app/models/sandwich.rb?rev=660672&r1=660671&r2=660672&view=diff
==============================================================================
--- ode/sandbox/singleshot/app/models/sandwich.rb (original)
+++ ode/sandbox/singleshot/app/models/sandwich.rb Tue May 27 12:55:19 2008
@@ -1,12 +1,41 @@
-class Sandwich < Singleshot::Task
+class Sandwich
 
-  data_fields :bread, :spread, :toppings
+  def self.human_attribute_name(name)
+    name.titleize
+  end
+
+  def initialize(other = nil)
+    ['bread', 'spread', 'toppings'].each do |name|
+      send "#{name}=", other.send(name)
+    end if other
+  end
+
+  attr_accessor :bread, :spread, :toppings
+
+  def errors
+    @errors ||= ActiveRecord::Errors.new(self)
+  end
 
   def toppings
-    (data && data['toppings']) || ''
+    @toppings = (@toppings || []).reject(&:empty?).uniq
   end
 
-#  validates_presence_of :bread, :on=>:complete, :message=>'Spread without a bread?',
:if=>:spread
- # validates_length_of   :toppings, :on=>:complete, :minimum=>1, :message=>'Your
sandwich is short on toppings!'
+  def update_attributes(attributes)
+    ['bread', 'spread', 'toppings'].each do |name|
+      send "#{name}=", attributes[name]
+    end
+  end
+
+  def save
+    validate
+    errors.empty?
+  end
+
+  private
+
+  def validate
+    errors.add :bread, 'Spread without a bread?' if !spread.empty? && bread.empty?
+    errors.add :toppings, 'Your sandwich is short on toppings!' if toppings.size < 1
+  end
 
 end

Modified: ode/sandbox/singleshot/app/models/task.rb
URL: http://svn.apache.org/viewvc/ode/sandbox/singleshot/app/models/task.rb?rev=660672&r1=660671&r2=660672&view=diff
==============================================================================
--- ode/sandbox/singleshot/app/models/task.rb (original)
+++ ode/sandbox/singleshot/app/models/task.rb Tue May 27 12:55:19 2008
@@ -149,7 +149,7 @@
   # Load only tasks that this person is a stakeholder of (owner, observer, etc).
   named_scope :for_stakeholder, lambda { |person|
     { :joins=>'JOIN stakeholders AS involved ON involved.task_id=tasks.id',
-      :conditions=>["(involved.person_id=? AND involved.role != 'excluded')", person.id]
} }
+      :conditions=>["involved.person_id=? AND involved.role != 'excluded' AND tasks.status
!= 'reserved'", person.id] } }
 
 
   # --- Priority and ordering ---
@@ -341,8 +341,9 @@
   # by #token_for.  The person is guaranteed to be a stakeholder in the task.
   # Returns nil if the token is invalid or the person is no longer associated with
   # this task.
-  def authorize(token)
-    (stakeholders.map { |sh| sh.person } + [owner, creator].compact).uniq.find { |person|
token_for(person) == token }
+  def authenticate(token)
+    ['owner', 'creator', 'admin', 'observer', 'potential_owner'].map { |role| in_role(role)
}.flatten.
+      find { |person| token_for(person) == token }
   end
 
 
@@ -363,4 +364,6 @@
   named_scope :following, lambda { |end_date|
     { :conditions=>["involved.role IN ('creator', 'observer', 'admin') AND tasks.updated_at
>= ?", end_date || Date.today - 7.days],
       :order=>'tasks.updated_at DESC' } }
+
+  named_scope :visible, :conditions=>["tasks.status != 'reserved'"]
 end

Modified: ode/sandbox/singleshot/app/views/sandwiches/show.html.erb
URL: http://svn.apache.org/viewvc/ode/sandbox/singleshot/app/views/sandwiches/show.html.erb?rev=660672&r1=660671&r2=660672&view=diff
==============================================================================
--- ode/sandbox/singleshot/app/views/sandwiches/show.html.erb (original)
+++ ode/sandbox/singleshot/app/views/sandwiches/show.html.erb Tue May 27 12:55:19 2008
@@ -26,7 +26,7 @@
                     {}, :disabled=>@read_only %></dd>
           <dt><%= f.label :toppings, 'Top it with' %>
               <%= '(' + link_to_function('add topping', 'roomForNewTopping()') + ')' unless
@read_only %></dt>
-          <% @sandwich.toppings.split(/;/).each do |topping| %>
+            <% @sandwich.toppings.uniq.reject(&:empty?).each do |topping| %>
             <dd><%= text_field_tag 'sandwich[toppings][]', topping, :disabled=>@read_only
%></dd>
           <% end %>
           <dd><%= text_field_tag 'sandwich[toppings][]', '', :id=>'last_topping',
:disabled=>@read_only %></dd>

Modified: ode/sandbox/singleshot/config/routes.rb
URL: http://svn.apache.org/viewvc/ode/sandbox/singleshot/config/routes.rb?rev=660672&r1=660671&r2=660672&view=diff
==============================================================================
--- ode/sandbox/singleshot/config/routes.rb (original)
+++ ode/sandbox/singleshot/config/routes.rb Tue May 27 12:55:19 2008
@@ -2,7 +2,7 @@
 
   map.resource 'session'
   map.resources 'tasks', :collection=>{ 'following'=>:get, 'completed'=>:get },
:member=>{ 'activity'=>:get },
-    :has_one=>[ 'owner' ]
+    :has_one=>[ 'perform' ]
   map.resources 'activities'
   map.day_activity 'activity/:year/:month/:day', :controller=>'activities', :action=>'show',
:year =>/\d{4}/, :month=>/\d{1,2}/, :day=>/\d{1,2}/
   map.root :tasks



Mime
View raw message