summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--Gemfile.lock7
-rw-r--r--help/app/controllers/tickets_controller.rb25
-rw-r--r--help/app/models/ticket.rb12
-rw-r--r--help/app/models/ticket_comment.rb4
-rw-r--r--help/app/views/tickets/_comment.html.haml2
-rw-r--r--help/app/views/tickets/index.html.haml8
-rw-r--r--help/app/views/tickets/new.html.haml4
-rw-r--r--help/app/views/tickets/show.html.haml13
-rw-r--r--help/test/functional/tickets_controller_test.rb54
-rw-r--r--users/app/models/user.rb6
10 files changed, 106 insertions, 29 deletions
diff --git a/Gemfile.lock b/Gemfile.lock
index a982c2a..86cb8e8 100644
--- a/Gemfile.lock
+++ b/Gemfile.lock
@@ -15,6 +15,12 @@ PATH
rails (~> 3.2.8)
PATH
+ remote: help
+ specs:
+ leap_web_help (0.1.0)
+ leap_web_core (= 0.1.0)
+
+PATH
remote: users
specs:
leap_web_users (0.1.0)
@@ -173,6 +179,7 @@ DEPENDENCIES
jquery-rails
leap_web_certs!
leap_web_core!
+ leap_web_help!
leap_web_users!
mocha
ruby-debug
diff --git a/help/app/controllers/tickets_controller.rb b/help/app/controllers/tickets_controller.rb
index f4b38de..4c7415b 100644
--- a/help/app/controllers/tickets_controller.rb
+++ b/help/app/controllers/tickets_controller.rb
@@ -1,6 +1,7 @@
class TicketsController < ApplicationController
respond_to :html #, :json
+ #has_scope :open, :type => boolean
def new
@ticket = Ticket.new
@@ -8,10 +9,14 @@ class TicketsController < ApplicationController
end
def create
- @ticket = Ticket.new #:created_by => User.current_test.id
- @ticket.attributes = params[:ticket]#.except(:comments)
- @ticket.created_by = User.current_test.id if User.current_test
- #instead of calling add_comment, we are using comment_attributes= from the Ticket model
+ @ticket = Ticket.new(params[:ticket])
+ if current_user
+ @ticket.created_by = current_user.id
+ @ticket.email = current_user.email if current_user.email
+ @ticket.comments.last.posted_by = current_user.id
+ else
+ @ticket.comments.last.posted_by = nil #hacky, but protecting this attribute doesn't work right, so this should make sure it isn't set.
+ end
flash[:notice] = 'Ticket was successfully created.' if @ticket.save
respond_with(@ticket)
@@ -35,8 +40,9 @@ class TicketsController < ApplicationController
def update
@ticket = Ticket.find(params[:id])
@ticket.attributes = params[:ticket]
- #add_comment #or should we use ticket attributes?
- # @ticket.save
+
+ @ticket.comments.last.posted_by = (current_user ? current_user.id : nil) #protecting posted_by isn't working, so this should protect it.
+
if @ticket.save
flash[:notice] = 'Ticket was successfully updated.'
respond_with @ticket
@@ -50,17 +56,18 @@ class TicketsController < ApplicationController
def index
# @tickets = Ticket.by_title #not actually what we will want
- respond_with(@tickets = Ticket.all)
+ respond_with(@tickets = Ticket.all) #we'll want only tickets that this user can access
end
private
# not using now, as we are using comment_attributes= from the Ticket model
+=begin
def add_comment
comment = TicketComment.new(params[:comment])
- comment.posted_by = User.current_test.id if User.current_test #could be nil
+ comment.posted_by = User.current.id if User.current #could be nil
comment.posted_at = Time.now # TODO: it seems strange to have this here, and not in model
@ticket.comments << comment
end
-
+=end
end
diff --git a/help/app/models/ticket.rb b/help/app/models/ticket.rb
index 537a7c6..f38fed2 100644
--- a/help/app/models/ticket.rb
+++ b/help/app/models/ticket.rb
@@ -15,8 +15,8 @@ class Ticket < CouchRest::Model::Base
=end
#belongs_to :user #from leap_web_users. doesn't necessarily belong to a user though
- property :created_by, String#Integer #nil unless user was authenticated for ticket creation, #THIS should not be changed after being set
- property :regarding_user, String#Integer # form cannot be submitted if they type in a username w/out corresponding ID. this field can be nil. for authenticated ticket creation by non-admins, should this just automatically be set to be same as created_by? or maybe we don't use this field unless created_by is nil?
+ property :created_by, String, :protected => true #Integer #nil unless user was authenticated for ticket creation, #THIS should not be changed after being set
+ #property :regarding_user, String#Integer # form cannot be submitted if they type in a username w/out corresponding ID. this field can be nil. for authenticated ticket creation by non-admins, should this just automatically be set to be same as created_by? or maybe we don't use this field unless created_by is nil?
#also, both created_by and regarding_user could be nil---say user forgets username, or has general question
property :title, String
property :email, String #verify
@@ -32,6 +32,9 @@ class Ticket < CouchRest::Model::Base
#before_validation :set_created_by, :set_code, :set_email, :on => :create
before_validation :set_code, :set_email, :on => :create
+
+ #named_scope :open, :conditions => {:is_open => true} #??
+
design do
view :by_title
end
@@ -60,7 +63,7 @@ class Ticket < CouchRest::Model::Base
def set_email
self.email = nil if self.email == ""
- #self.email = current users email if is_creator_validated?
+ # in controller set to be current users email if that exists
end
def close
@@ -74,8 +77,9 @@ class Ticket < CouchRest::Model::Base
end
def comments_attributes=(attributes)
+
comment = TicketComment.new(attributes.values.first) #TicketComment.new(attributes)
- comment.posted_by = User.current_test.id if User.current_test
+ #comment.posted_by = User.current.id if User.current #we want to avoid User.current, and current_user won't work here. instead will set in tickets_controller
comment.posted_at = Time.now
comments << comment
diff --git a/help/app/models/ticket_comment.rb b/help/app/models/ticket_comment.rb
index 9026bc1..49e5c6c 100644
--- a/help/app/models/ticket_comment.rb
+++ b/help/app/models/ticket_comment.rb
@@ -2,13 +2,13 @@ class TicketComment
include CouchRest::Model::Embeddable
#belongs_to :ticket #is this best way to do it? will want to access all of a tickets comments, so maybe this isn't the way?
- property :posted_by, Integer#, :protected => true# maybe this should be current_user if that is set, meaning the user is logged in #String # user??
+ property :posted_by, String#, :protected => true #Integer#this should be current_user if that is set, meaning the user is logged in #cannot have it be protected and set via comments_attributes=. also, if it is protected and we set in the tickets_controller, it gets unset. TODO---is this okay to have it not protected and manually check it? We do not users to be able to set this.
# if the current user is not set, then we could just say the comment comes from an 'unauthenticated user', which would be somebody with the secret URL
property :posted_at, Time#, :protected => true
#property :posted_verified, TrueClass, :protected => true #should be true if current_user is set when the comment is created
property :body, String
-
+ # ? timestamps!
validates :body, :presence => true
#before_validation :set_time#, :set_posted_by
diff --git a/help/app/views/tickets/_comment.html.haml b/help/app/views/tickets/_comment.html.haml
index 19e1ddf..1ba3bd1 100644
--- a/help/app/views/tickets/_comment.html.haml
+++ b/help/app/views/tickets/_comment.html.haml
@@ -3,6 +3,8 @@
- if User.find(comment.posted_by)
Posted by
= User.find(comment.posted_by).login
+ - else
+ Unauthenticated post
%p
Posted at
= comment.posted_at
diff --git a/help/app/views/tickets/index.html.haml b/help/app/views/tickets/index.html.haml
index d2e0ea0..6db2140 100644
--- a/help/app/views/tickets/index.html.haml
+++ b/help/app/views/tickets/index.html.haml
@@ -1,8 +1,10 @@
+%h2 tickets index (just as space)
+Create a
+= link_to "new ticket", new_ticket_path
+= # below shouldn't be unless logged in
%h2 Tickets
+= # want to have selection option to see tickets, that are open, closed or all
- @tickets.each do |ticket|
%p
= link_to ticket.title, ticket
-%p
-Create a
-= link_to "new ticket", new_ticket_path
= #render(:partial => "ticket", :collection => @tickets)
diff --git a/help/app/views/tickets/new.html.haml b/help/app/views/tickets/new.html.haml
index d784720..537b97f 100644
--- a/help/app/views/tickets/new.html.haml
+++ b/help/app/views/tickets/new.html.haml
@@ -1,9 +1,9 @@
%h2=t :new_ticket
-= simple_form_for (@ticket, :html => {:novalidate => true}) do |f| #turn off html5 validations to test
+= simple_form_for(@ticket, :html => {:novalidate => true}) do |f| #turn off html5 validations to test
= #@ticket.errors.messages
= f.input :title
= #f.input :email #if there is no current_user
- = f.input :email if !User.current_test #hmm--might authenticated users want to submit an alternate email?
+ = f.input :email if !current_user #hmm--might authenticated users want to submit an alternate email?
= f.simple_fields_for :comments do |c|
= c.input :body, :label => 'Comment', :as => :text
diff --git a/help/app/views/tickets/show.html.haml b/help/app/views/tickets/show.html.haml
index 04dd676..a9b994e 100644
--- a/help/app/views/tickets/show.html.haml
+++ b/help/app/views/tickets/show.html.haml
@@ -5,8 +5,17 @@
%h2= @ticket.title
is open?
= @ticket.is_open
-code:
-= @ticket.code
+- if @ticket.code
+ code:
+ = @ticket.code
+- if @ticket.email
+ email:
+ = @ticket.email
+- if User.find(@ticket.created_by)
+ Created by
+ = User.find(@ticket.created_by).login
+- else
+ Unauthenticated ticket creator
= render(:partial => "comment", :collection => @ticket.comments)
= simple_form_for (@ticket, :html => {:novalidate => true}) do |f| #turn off html5 validations to test
diff --git a/help/test/functional/tickets_controller_test.rb b/help/test/functional/tickets_controller_test.rb
index 6d9ff09..7a03a86 100644
--- a/help/test/functional/tickets_controller_test.rb
+++ b/help/test/functional/tickets_controller_test.rb
@@ -1,9 +1,13 @@
require 'test_helper'
class TicketsControllerTest < ActionController::TestCase
- # test "the truth" do
- # assert true
- # end
+
+ test "should get index" do
+ get :index
+ assert_response :success
+ assert_not_nil assigns(:tickets)
+ end
+
test "should get new" do
get :new
assert_equal Ticket, assigns(:ticket).class
@@ -11,5 +15,49 @@ class TicketsControllerTest < ActionController::TestCase
end
+ test "should create unauthenticated ticket" do
+ params = {:title => "ticket test title", :comments_attributes => {"0" => {"body" =>"body of test ticket"}}}
+
+ assert_difference('Ticket.count') do
+ post :create, :ticket => params
+ end
+
+ assert_response :redirect
+ #assert_equal assigns(:ticket).email, User.current.email
+ #assert_equal User.find(assigns(:ticket).created_by).login, User.current.login
+ assert_nil assigns(:ticket).created_by
+
+ assert_equal assigns(:ticket).comments.count, 1
+ end
+
+
+ test "should create authenticated ticket" do
+
+ params = {:title => "ticket test title", :comments_attributes => {"0" => {"body" =>"body of test ticket"}}}
+
+ #todo: should redo this and actually authorize
+ user = User.last
+ session[:user_id] = user.id
+
+ assert_difference('Ticket.count') do
+ post :create, :ticket => params
+ end
+
+ assert_response :redirect
+ assert_equal assigns(:ticket).created_by, user.id
+ assert_equal assigns(:ticket).email, user.email
+
+ assert_equal assigns(:ticket).comments.count, 1
+ end
+
+ test "add comment to ticket" do
+
+ t = Ticket.last
+ comment_count = t.comments.count
+ put :update, :id => t.id, :ticket => {:comments_attributes => {"0" => {"body" =>"NEWER comment"}} }
+ assert_equal(comment_count + 1, assigns(:ticket).comments.count)
+ #assert_difference block isn't working
+
+ end
end
diff --git a/users/app/models/user.rb b/users/app/models/user.rb
index b57af98..a06893f 100644
--- a/users/app/models/user.rb
+++ b/users/app/models/user.rb
@@ -66,15 +66,13 @@ class User < CouchRest::Model::Base
login
end
+=begin
def self.current
Thread.current[:user]
end
def self.current=(user)
Thread.current[:user] = user
end
-
- def self.current_test
- User.first
- end
+=end
end