From a6de1561461cc719fddd8175c93588a47513a4b8 Mon Sep 17 00:00:00 2001 From: jessib Date: Fri, 5 Oct 2012 15:41:03 -0700 Subject: Rough code to add & comment on tickets. --- help/app/controllers/tickets_controller.rb | 41 +++++++++++++++++++++++++ help/app/models/ticket.rb | 15 ++++++--- help/app/models/ticket_comment.rb | 12 +++++--- help/app/views/tickets/_comment.html.haml | 10 ++++++ help/app/views/tickets/_new_comment.html.haml | 2 ++ help/app/views/tickets/index.html.haml | 5 +++ help/app/views/tickets/new.html.haml | 14 +++++++++ help/app/views/tickets/show.html.haml | 10 ++++++ help/config/routes.rb | 3 ++ help/test/functional/tickets_controller_test.rb | 15 +++++++++ help/test/unit/ticket_comment_test.rb | 11 ++++--- help/test/unit/ticket_test.rb | 8 +++-- 12 files changed, 130 insertions(+), 16 deletions(-) create mode 100644 help/app/controllers/tickets_controller.rb create mode 100644 help/app/views/tickets/_comment.html.haml create mode 100644 help/app/views/tickets/_new_comment.html.haml create mode 100644 help/app/views/tickets/index.html.haml create mode 100644 help/app/views/tickets/new.html.haml create mode 100644 help/app/views/tickets/show.html.haml create mode 100644 help/test/functional/tickets_controller_test.rb (limited to 'help') diff --git a/help/app/controllers/tickets_controller.rb b/help/app/controllers/tickets_controller.rb new file mode 100644 index 0000000..9383d7e --- /dev/null +++ b/help/app/controllers/tickets_controller.rb @@ -0,0 +1,41 @@ +class TicketsController < ApplicationController + + def new + @ticket = Ticket.new + end + + def create + # @ticket = Ticket.new :posted_by => current_user + @ticket = Ticket.new :created_by => User.current_test.id + @ticket.attributes = params[:ticket] + + add_comment + redirect_to @ticket + end + + def show + @ticket = Ticket.find(params[:id]) + end + + def update + @ticket = Ticket.find(params[:id]) + add_comment + redirect_to @ticket + end + + def index + @tickets = Ticket.by_title #not actually what we will want + end + + private + + def add_comment + comment = TicketComment.new(params[:comment]) + #comment.posted_by = current_user #could be nil + comment.posted_by = User.current_test.id #could be nil + comment.posted_at = Time.now # TODO: it seems strange to have this here, and not in model. + @ticket.comments << comment + @ticket.save + end + +end diff --git a/help/app/models/ticket.rb b/help/app/models/ticket.rb index 784d7ef..355ae02 100644 --- a/help/app/models/ticket.rb +++ b/help/app/models/ticket.rb @@ -29,7 +29,8 @@ class Ticket < CouchRest::Model::Base timestamps! - before_validation :set_created_by, :set_code, :on => :create + #before_validation :set_created_by, :set_code, :set_email, :on => :create + before_validation :set_code, :set_email, :on => :create design do view :by_title @@ -38,9 +39,10 @@ class Ticket < CouchRest::Model::Base validates :email, :format => /\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\Z/, :if => :email #email address is optional - def set_created_by - self.created_by = User.current if User.current - end + #TODO: + #def set_created_by + # self.created_by = User.current if User.current + #end def is_creator_validated? !!created_by @@ -51,6 +53,11 @@ class Ticket < CouchRest::Model::Base self.code = SecureRandom.hex(8) if !is_creator_validated? end + + def set_email + #self.email = current users email if is_creator_validated? + end + def close self.is_open = false save diff --git a/help/app/models/ticket_comment.rb b/help/app/models/ticket_comment.rb index 652133a..6b6b4db 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, Integer#, :protected => true# maybe this should be current_user if that is set, meaning the user is logged in #String # user?? # 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_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 - before_validation :set_time#, :set_posted_by + #before_validation :set_time#, :set_posted_by #design do # view :by_posted_at @@ -18,10 +18,14 @@ class TicketComment def is_comment_validated? !!posted_by end - + +=begin + #TODO. + #this is resetting all comments associated with the ticket: def set_time self.posted_at = Time.now end +=end =begin def set_posted_by diff --git a/help/app/views/tickets/_comment.html.haml b/help/app/views/tickets/_comment.html.haml new file mode 100644 index 0000000..77e29b8 --- /dev/null +++ b/help/app/views/tickets/_comment.html.haml @@ -0,0 +1,10 @@ +%div + - if User.find(comment.posted_by) + Posted by + = User.find(comment.posted_by).login + %p + Posted at + = comment.posted_at + %p + = comment.body + %p \ No newline at end of file diff --git a/help/app/views/tickets/_new_comment.html.haml b/help/app/views/tickets/_new_comment.html.haml new file mode 100644 index 0000000..bf88da6 --- /dev/null +++ b/help/app/views/tickets/_new_comment.html.haml @@ -0,0 +1,2 @@ += simple_fields_for :comment do |c| + = c.input :body, :label => 'Comment', :as => :text diff --git a/help/app/views/tickets/index.html.haml b/help/app/views/tickets/index.html.haml new file mode 100644 index 0000000..55bfa79 --- /dev/null +++ b/help/app/views/tickets/index.html.haml @@ -0,0 +1,5 @@ +%h2 Tickets +- @tickets.each do |ticket| + %p + = link_to ticket.title, ticket += #render(:partial => "ticket", :collection => @tickets) \ No newline at end of file diff --git a/help/app/views/tickets/new.html.haml b/help/app/views/tickets/new.html.haml new file mode 100644 index 0000000..fd1bcd4 --- /dev/null +++ b/help/app/views/tickets/new.html.haml @@ -0,0 +1,14 @@ +%h2=t :new_ticket += simple_form_for @ticket do |f| + = f.input :title + = #f.input :email #if there is no current_user + = f.input :email if !User.current_test + = #simple_fields_for :comment do |c| + = #c.input :body, :label => 'Comment', :as => :text + = #f.input :comment + = render :partial => 'new_comment' + = # regarding_user if not logged in + = # email if not logged in + = #f.button :submit, :value => t(:submit), :class => 'btn-primary' + = f.button :submit + = link_to t(:cancel), root_url, :class => :btn diff --git a/help/app/views/tickets/show.html.haml b/help/app/views/tickets/show.html.haml new file mode 100644 index 0000000..a37f5c8 --- /dev/null +++ b/help/app/views/tickets/show.html.haml @@ -0,0 +1,10 @@ +%h2= @ticket.title +is open? += @ticket.is_open +code: += @ticket.code += render(:partial => "comment", :collection => @ticket.comments) + += simple_form_for @ticket do |f| + = render :partial => 'new_comment' + = f.button :submit \ No newline at end of file diff --git a/help/config/routes.rb b/help/config/routes.rb index 1daf9a4..5e57e02 100644 --- a/help/config/routes.rb +++ b/help/config/routes.rb @@ -1,2 +1,5 @@ Rails.application.routes.draw do + + resources :tickets, :only => [:new, :create, :index, :show, :update] + #resources :ticket, :only => [:show] end diff --git a/help/test/functional/tickets_controller_test.rb b/help/test/functional/tickets_controller_test.rb new file mode 100644 index 0000000..6d9ff09 --- /dev/null +++ b/help/test/functional/tickets_controller_test.rb @@ -0,0 +1,15 @@ +require 'test_helper' + +class TicketsControllerTest < ActionController::TestCase + # test "the truth" do + # assert true + # end + test "should get new" do + get :new + assert_equal Ticket, assigns(:ticket).class + assert_response :success + end + + + +end diff --git a/help/test/unit/ticket_comment_test.rb b/help/test/unit/ticket_comment_test.rb index 883720f..1fe1fe2 100644 --- a/help/test/unit/ticket_comment_test.rb +++ b/help/test/unit/ticket_comment_test.rb @@ -16,8 +16,8 @@ class TicketCommentTest < ActiveSupport::TestCase comment2 = TicketComment.new :body => "help my email is broken!" assert comment2.valid? - assert_not_nil comment2.posted_at - assert_nil comment2.posted_by #if not logged in + #assert_not_nil comment2.posted_at #? + #assert_nil comment2.posted_by #if not logged in #TODO #comment.ticket = testticket #Ticket.find_by_title("testing") #assert_equal testticket.title, comment.ticket.title @@ -49,9 +49,10 @@ class TicketCommentTest < ActiveSupport::TestCase testticket.comments << comment2 #this should validate comment2 testticket.valid? assert_equal testticket.comments.count, 2 - assert_not_nil comment.posted_at - assert_not_nil testticket.comments.last.posted_at - assert testticket.comments.first.posted_at < testticket.comments.last.posted_at + # where should posted_at be set? + #assert_not_nil comment.posted_at + #assert_not_nil testticket.comments.last.posted_at + #assert testticket.comments.first.posted_at < testticket.comments.last.posted_at end end diff --git a/help/test/unit/ticket_test.rb b/help/test/unit/ticket_test.rb index c3a4759..6b63a23 100644 --- a/help/test/unit/ticket_test.rb +++ b/help/test/unit/ticket_test.rb @@ -41,18 +41,20 @@ class TicketTest < ActiveSupport::TestCase assert @sample.is_creator_validated? end +=begin +# TODO: do once have current_user stuff in order test "code if & only if not creator-validated" do + User.current_test = nil t1 = Ticket.create :title => 'test title' assert_not_nil t1.code assert_nil t1.created_by - User.current = 4 + User.current_test = 4 t2 = Ticket.create :title => 'test title' assert_nil t2.code assert_not_nil t2.created_by - - end +=end end -- cgit v1.2.3 From c27f2a0686db0705553eda88c799d4c486c20bac Mon Sep 17 00:00:00 2001 From: jessib Date: Wed, 10 Oct 2012 10:55:25 -0700 Subject: Pushing some tweaks as I try to get server-side validation working. --- help/app/models/ticket.rb | 16 ++++++++++++++-- help/app/models/ticket_comment.rb | 2 ++ help/app/views/tickets/index.html.haml | 5 ++++- help/app/views/tickets/new.html.haml | 15 +++++++++++---- help/app/views/tickets/show.html.haml | 5 +++-- 5 files changed, 34 insertions(+), 9 deletions(-) (limited to 'help') diff --git a/help/app/models/ticket.rb b/help/app/models/ticket.rb index 355ae02..8cec0df 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, Integer #nil unless user was authenticated for ticket creation, #THIS should not be changed after being set - property :regarding_user, 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#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 @@ -29,6 +29,8 @@ class Ticket < CouchRest::Model::Base timestamps! + #accepts_nested_attributes_for :ticketcomments #?? + #before_validation :set_created_by, :set_code, :set_email, :on => :create before_validation :set_code, :set_email, :on => :create @@ -36,7 +38,11 @@ class Ticket < CouchRest::Model::Base view :by_title end + validates :title, :presence => true + validates :comments, :presence => true #do we want it like this? + + # html5 has built-in validation which isn't ideal, as it says 'please enter an email address' for invalid email addresses, which implies an email address is required, and it is not. validates :email, :format => /\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\Z/, :if => :email #email address is optional #TODO: @@ -55,6 +61,7 @@ class Ticket < CouchRest::Model::Base def set_email + self.email = nil if self.email == "" #self.email = current users email if is_creator_validated? end @@ -68,6 +75,11 @@ class Ticket < CouchRest::Model::Base save end + #probably not useful, but trying it: + def ticket_comment_attributes=(attributes) + @ticket_comment = TicketComment.new(attributes) + end + =begin def validate if email_address and not email_address.strip =~ RFC822::EmailAddress diff --git a/help/app/models/ticket_comment.rb b/help/app/models/ticket_comment.rb index 6b6b4db..9026bc1 100644 --- a/help/app/models/ticket_comment.rb +++ b/help/app/models/ticket_comment.rb @@ -8,6 +8,8 @@ class TicketComment #property :posted_verified, TrueClass, :protected => true #should be true if current_user is set when the comment is created property :body, String + + validates :body, :presence => true #before_validation :set_time#, :set_posted_by #design do diff --git a/help/app/views/tickets/index.html.haml b/help/app/views/tickets/index.html.haml index 55bfa79..d2e0ea0 100644 --- a/help/app/views/tickets/index.html.haml +++ b/help/app/views/tickets/index.html.haml @@ -2,4 +2,7 @@ - @tickets.each do |ticket| %p = link_to ticket.title, ticket -= #render(:partial => "ticket", :collection => @tickets) \ No newline at end of file +%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 fd1bcd4..d0e6939 100644 --- a/help/app/views/tickets/new.html.haml +++ b/help/app/views/tickets/new.html.haml @@ -1,12 +1,19 @@ %h2=t :new_ticket -= simple_form_for @ticket do |f| += 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 - = #simple_fields_for :comment do |c| + = f.input :email if !User.current_test #hmm--might authenticated users want to submit an alternate email? + = #f.simple_fields_for :comment do |c| = #c.input :body, :label => 'Comment', :as => :text + = #f.input :comments, :label => 'Comment', :as => :text + + = f.fields_for :comment do |c| + = c.input :body, :label => 'Comment', :as => :text + + = #f.input :comment - = render :partial => 'new_comment' + = #render :partial => 'new_comment' #what we were using = # regarding_user if not logged in = # email if not logged in = #f.button :submit, :value => t(:submit), :class => 'btn-primary' diff --git a/help/app/views/tickets/show.html.haml b/help/app/views/tickets/show.html.haml index a37f5c8..1e1fab3 100644 --- a/help/app/views/tickets/show.html.haml +++ b/help/app/views/tickets/show.html.haml @@ -5,6 +5,7 @@ code: = @ticket.code = render(:partial => "comment", :collection => @ticket.comments) -= simple_form_for @ticket do |f| += simple_form_for (@ticket, :html => {:novalidate => true}) do |f| #turn off html5 validations to test = render :partial => 'new_comment' - = f.button :submit \ No newline at end of file + = f.button :submit + = link_to t(:cancel), root_url, :class => :btn \ No newline at end of file -- cgit v1.2.3 From 0bdfbdb57ab7c29d0d87dc1a44b17eb32f98439b Mon Sep 17 00:00:00 2001 From: jessib Date: Wed, 10 Oct 2012 11:00:51 -0700 Subject: Forgot to commit controller --- help/app/controllers/tickets_controller.rb | 27 +++++++++++++++++---------- 1 file changed, 17 insertions(+), 10 deletions(-) (limited to 'help') diff --git a/help/app/controllers/tickets_controller.rb b/help/app/controllers/tickets_controller.rb index 9383d7e..2e681b2 100644 --- a/help/app/controllers/tickets_controller.rb +++ b/help/app/controllers/tickets_controller.rb @@ -1,16 +1,23 @@ class TicketsController < ApplicationController + respond_to :html #, :json + def new @ticket = Ticket.new end def create - # @ticket = Ticket.new :posted_by => current_user - @ticket = Ticket.new :created_by => User.current_test.id - @ticket.attributes = params[:ticket] - + @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 add_comment - redirect_to @ticket + + if @ticket.save + respond_with(@ticket) + else + respond_with(@ticket, :location => new_ticket_path ) + end + end def show @@ -20,22 +27,22 @@ class TicketsController < ApplicationController def update @ticket = Ticket.find(params[:id]) add_comment + @ticket.save redirect_to @ticket end def index - @tickets = Ticket.by_title #not actually what we will want + # @tickets = Ticket.by_title #not actually what we will want + respond_with(@tickets = Ticket.all) end private def add_comment comment = TicketComment.new(params[:comment]) - #comment.posted_by = current_user #could be nil - comment.posted_by = User.current_test.id #could be nil - comment.posted_at = Time.now # TODO: it seems strange to have this here, and not in model. + comment.posted_by = User.current_test.id if User.current_test #could be nil + comment.posted_at = Time.now # TODO: it seems strange to have this here, and not in model @ticket.comments << comment - @ticket.save end end -- cgit v1.2.3 From cf9ed38ab1840092352efdbb71bfeb5bc3b9f9d5 Mon Sep 17 00:00:00 2001 From: jessib Date: Wed, 10 Oct 2012 15:52:08 -0700 Subject: Some tweaks to get server-side validation working when adding the embedded ticket comment to a new ticket. --- help/app/controllers/tickets_controller.rb | 10 +++++++--- help/app/models/ticket.rb | 11 ++++++----- help/app/views/tickets/new.html.haml | 9 ++------- 3 files changed, 15 insertions(+), 15 deletions(-) (limited to 'help') diff --git a/help/app/controllers/tickets_controller.rb b/help/app/controllers/tickets_controller.rb index 2e681b2..be07309 100644 --- a/help/app/controllers/tickets_controller.rb +++ b/help/app/controllers/tickets_controller.rb @@ -4,13 +4,14 @@ class TicketsController < ApplicationController def new @ticket = Ticket.new + @ticket.comments.build 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 - add_comment + #instead of calling add_comment, we are using comment_attributes= from the Ticket model if @ticket.save respond_with(@ticket) @@ -22,13 +23,14 @@ class TicketsController < ApplicationController def show @ticket = Ticket.find(params[:id]) + # build ticket comments? end def update @ticket = Ticket.find(params[:id]) - add_comment + add_comment #or should we use ticket attributes? @ticket.save - redirect_to @ticket + redirect_to @ticket #difft behavior on failure? end def index @@ -38,6 +40,8 @@ class TicketsController < ApplicationController private + # not using now when creating tickets, we are using comment_attributes= from the Ticket model + #not yet sure about updating tickets def add_comment comment = TicketComment.new(params[:comment]) comment.posted_by = User.current_test.id if User.current_test #could be nil diff --git a/help/app/models/ticket.rb b/help/app/models/ticket.rb index 8cec0df..e829a5f 100644 --- a/help/app/models/ticket.rb +++ b/help/app/models/ticket.rb @@ -29,8 +29,6 @@ class Ticket < CouchRest::Model::Base timestamps! - #accepts_nested_attributes_for :ticketcomments #?? - #before_validation :set_created_by, :set_code, :set_email, :on => :create before_validation :set_code, :set_email, :on => :create @@ -75,9 +73,12 @@ class Ticket < CouchRest::Model::Base save end - #probably not useful, but trying it: - def ticket_comment_attributes=(attributes) - @ticket_comment = TicketComment.new(attributes) + 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_at = Time.now + comments << comment + end =begin diff --git a/help/app/views/tickets/new.html.haml b/help/app/views/tickets/new.html.haml index d0e6939..0a6b25b 100644 --- a/help/app/views/tickets/new.html.haml +++ b/help/app/views/tickets/new.html.haml @@ -1,18 +1,13 @@ %h2=t :new_ticket = simple_form_for (@ticket, :html => {:novalidate => true}) do |f| #turn off html5 validations to test - = @ticket.errors.messages + = #@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.simple_fields_for :comment do |c| - = #c.input :body, :label => 'Comment', :as => :text - = #f.input :comments, :label => 'Comment', :as => :text - = f.fields_for :comment do |c| + = f.simple_fields_for :comments do |c| = c.input :body, :label => 'Comment', :as => :text - - = #f.input :comment = #render :partial => 'new_comment' #what we were using = # regarding_user if not logged in = # email if not logged in -- cgit v1.2.3 From 56273c13f54a872d02db286c90a8d5103cf7a663 Mon Sep 17 00:00:00 2001 From: jessib Date: Fri, 12 Oct 2012 14:42:57 -0700 Subject: more work on ticket creation/updating functionality --- help/app/controllers/tickets_controller.rb | 34 +++++++++++++++++++-------- help/app/models/ticket.rb | 2 +- help/app/views/tickets/_comment.html.haml | 3 ++- help/app/views/tickets/_new_comment.html.haml | 1 + help/app/views/tickets/new.html.haml | 2 +- help/app/views/tickets/show.html.haml | 10 ++++++-- 6 files changed, 37 insertions(+), 15 deletions(-) (limited to 'help') diff --git a/help/app/controllers/tickets_controller.rb b/help/app/controllers/tickets_controller.rb index be07309..f4b38de 100644 --- a/help/app/controllers/tickets_controller.rb +++ b/help/app/controllers/tickets_controller.rb @@ -13,24 +13,39 @@ class TicketsController < ApplicationController @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 - if @ticket.save - respond_with(@ticket) - else - respond_with(@ticket, :location => new_ticket_path ) - end + flash[:notice] = 'Ticket was successfully created.' if @ticket.save + respond_with(@ticket) end +=begin + def edit + @ticket = Ticket.find(params[:id]) + @ticket.comments.build + # build ticket comments? + end +=end + def show @ticket = Ticket.find(params[:id]) + # @ticket.comments.build # build ticket comments? end def update @ticket = Ticket.find(params[:id]) - add_comment #or should we use ticket attributes? - @ticket.save - redirect_to @ticket #difft behavior on failure? + @ticket.attributes = params[:ticket] + #add_comment #or should we use ticket attributes? + # @ticket.save + if @ticket.save + flash[:notice] = 'Ticket was successfully updated.' + respond_with @ticket + else + #redirect_to [:show, @ticket] # + flash[:alert] = 'Ticket has not been changed' + redirect_to @ticket + #respond_with(@ticket) # why does this go to edit?? redirect??? + end end def index @@ -40,8 +55,7 @@ class TicketsController < ApplicationController private - # not using now when creating tickets, we are using comment_attributes= from the Ticket model - #not yet sure about updating tickets + # not using now, as we are using comment_attributes= from the Ticket model def add_comment comment = TicketComment.new(params[:comment]) comment.posted_by = User.current_test.id if User.current_test #could be nil diff --git a/help/app/models/ticket.rb b/help/app/models/ticket.rb index e829a5f..537a7c6 100644 --- a/help/app/models/ticket.rb +++ b/help/app/models/ticket.rb @@ -37,7 +37,7 @@ class Ticket < CouchRest::Model::Base end validates :title, :presence => true - validates :comments, :presence => true #do we want it like this? + #validates :comments, :presence => true #do we want it like this? # html5 has built-in validation which isn't ideal, as it says 'please enter an email address' for invalid email addresses, which implies an email address is required, and it is not. diff --git a/help/app/views/tickets/_comment.html.haml b/help/app/views/tickets/_comment.html.haml index 77e29b8..19e1ddf 100644 --- a/help/app/views/tickets/_comment.html.haml +++ b/help/app/views/tickets/_comment.html.haml @@ -1,4 +1,5 @@ -%div +- # style is super ugly but just for now +%div{:style => "border: solid 1px"} - if User.find(comment.posted_by) Posted by = User.find(comment.posted_by).login diff --git a/help/app/views/tickets/_new_comment.html.haml b/help/app/views/tickets/_new_comment.html.haml index bf88da6..a924dfd 100644 --- a/help/app/views/tickets/_new_comment.html.haml +++ b/help/app/views/tickets/_new_comment.html.haml @@ -1,2 +1,3 @@ += #do we want this partial? not using it now = simple_fields_for :comment do |c| = c.input :body, :label => 'Comment', :as => :text diff --git a/help/app/views/tickets/new.html.haml b/help/app/views/tickets/new.html.haml index 0a6b25b..d784720 100644 --- a/help/app/views/tickets/new.html.haml +++ b/help/app/views/tickets/new.html.haml @@ -13,4 +13,4 @@ = # email if not logged in = #f.button :submit, :value => t(:submit), :class => 'btn-primary' = f.button :submit - = link_to t(:cancel), root_url, :class => :btn + = link_to t(:cancel), tickets_path, :class => :btn diff --git a/help/app/views/tickets/show.html.haml b/help/app/views/tickets/show.html.haml index 1e1fab3..04dd676 100644 --- a/help/app/views/tickets/show.html.haml +++ b/help/app/views/tickets/show.html.haml @@ -1,3 +1,7 @@ +- if flash[:notice] + =flash[:notice] +- if flash[:alert] + =flash[:alert] %h2= @ticket.title is open? = @ticket.is_open @@ -6,6 +10,8 @@ code: = render(:partial => "comment", :collection => @ticket.comments) = simple_form_for (@ticket, :html => {:novalidate => true}) do |f| #turn off html5 validations to test - = render :partial => 'new_comment' + = f.simple_fields_for :comments, TicketComment.new do |c| + = c.input :body, :label => 'Comment', :as => :text + = #render :partial => 'new_comment' = f.button :submit - = link_to t(:cancel), root_url, :class => :btn \ No newline at end of file + = link_to t(:cancel), tickets_path, :class => :btn \ No newline at end of file -- cgit v1.2.3 From 48d6c2aac9ae2bf1c140e734a576e45289c99150 Mon Sep 17 00:00:00 2001 From: jessib Date: Tue, 16 Oct 2012 15:51:35 -0700 Subject: Some functional tests and other tweaks. --- help/app/controllers/tickets_controller.rb | 6 +++-- help/app/models/ticket.rb | 12 ++++++---- help/app/models/ticket_comment.rb | 4 ++-- help/app/views/tickets/_comment.html.haml | 2 ++ help/app/views/tickets/index.html.haml | 5 ++-- help/app/views/tickets/new.html.haml | 2 +- help/app/views/tickets/show.html.haml | 13 ++++++++-- help/test/functional/tickets_controller_test.rb | 32 ++++++++++++++++++++++--- 8 files changed, 59 insertions(+), 17 deletions(-) (limited to 'help') diff --git a/help/app/controllers/tickets_controller.rb b/help/app/controllers/tickets_controller.rb index f4b38de..be9a2b5 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,9 +9,9 @@ class TicketsController < ApplicationController end def create - @ticket = Ticket.new #:created_by => User.current_test.id - @ticket.attributes = params[:ticket]#.except(:comments) + @ticket = Ticket.new(params[:ticket]) @ticket.created_by = User.current_test.id if User.current_test + @ticket.email = User.current_test.email if User.current_test.email #instead of calling add_comment, we are using comment_attributes= from the Ticket model flash[:notice] = 'Ticket was successfully created.' if @ticket.save @@ -35,6 +36,7 @@ class TicketsController < ApplicationController def update @ticket = Ticket.find(params[:id]) @ticket.attributes = params[:ticket] + #add_comment #or should we use ticket attributes? # @ticket.save if @ticket.save diff --git a/help/app/models/ticket.rb b/help/app/models/ticket.rb index 537a7c6..76fa5c8 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_test.id if User.current_test #should we not access User.current here? 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..a8639a1 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= # 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..f328ca2 100644 --- a/help/app/views/tickets/index.html.haml +++ b/help/app/views/tickets/index.html.haml @@ -1,8 +1,7 @@ +Create a += link_to "new ticket", new_ticket_path %h2 Tickets - @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..8c660c9 100644 --- a/help/app/views/tickets/new.html.haml +++ b/help/app/views/tickets/new.html.haml @@ -1,5 +1,5 @@ %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 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..7af4c22 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,27 @@ class TicketsControllerTest < ActionController::TestCase end + test "should create authenticated 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_test.email + assert_equal User.find(assigns(:ticket).created_by).login, User.current_test.login + 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 -- cgit v1.2.3 From 8b9d5235faed6c15e8ef2e2dc76aec7f24d0bb50 Mon Sep 17 00:00:00 2001 From: jessib Date: Thu, 18 Oct 2012 13:42:37 -0700 Subject: Uses the working authentication code. --- help/app/controllers/tickets_controller.rb | 21 ++++++++++++------- help/app/models/ticket.rb | 2 +- help/app/models/ticket_comment.rb | 2 +- help/app/views/tickets/index.html.haml | 3 +++ help/app/views/tickets/new.html.haml | 2 +- help/test/functional/tickets_controller_test.rb | 28 ++++++++++++++++++++++--- 6 files changed, 44 insertions(+), 14 deletions(-) (limited to 'help') diff --git a/help/app/controllers/tickets_controller.rb b/help/app/controllers/tickets_controller.rb index be9a2b5..4c7415b 100644 --- a/help/app/controllers/tickets_controller.rb +++ b/help/app/controllers/tickets_controller.rb @@ -10,9 +10,13 @@ class TicketsController < ApplicationController def create @ticket = Ticket.new(params[:ticket]) - @ticket.created_by = User.current_test.id if User.current_test - @ticket.email = User.current_test.email if User.current_test.email - #instead of calling add_comment, we are using comment_attributes= from the Ticket model + 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) @@ -37,8 +41,8 @@ class TicketsController < ApplicationController @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 @@ -52,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 76fa5c8..f38fed2 100644 --- a/help/app/models/ticket.rb +++ b/help/app/models/ticket.rb @@ -79,7 +79,7 @@ class Ticket < CouchRest::Model::Base def comments_attributes=(attributes) comment = TicketComment.new(attributes.values.first) #TicketComment.new(attributes) - comment.posted_by = User.current_test.id if User.current_test #should we not access User.current here? + #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 a8639a1..49e5c6c 100644 --- a/help/app/models/ticket_comment.rb +++ b/help/app/models/ticket_comment.rb @@ -2,7 +2,7 @@ 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, 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= + 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 diff --git a/help/app/views/tickets/index.html.haml b/help/app/views/tickets/index.html.haml index f328ca2..6db2140 100644 --- a/help/app/views/tickets/index.html.haml +++ b/help/app/views/tickets/index.html.haml @@ -1,6 +1,9 @@ +%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 diff --git a/help/app/views/tickets/new.html.haml b/help/app/views/tickets/new.html.haml index 8c660c9..537b97f 100644 --- a/help/app/views/tickets/new.html.haml +++ b/help/app/views/tickets/new.html.haml @@ -3,7 +3,7 @@ = #@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/test/functional/tickets_controller_test.rb b/help/test/functional/tickets_controller_test.rb index 7af4c22..7a03a86 100644 --- a/help/test/functional/tickets_controller_test.rb +++ b/help/test/functional/tickets_controller_test.rb @@ -15,7 +15,7 @@ class TicketsControllerTest < ActionController::TestCase end - test "should create authenticated ticket" do + test "should create unauthenticated ticket" do params = {:title => "ticket test title", :comments_attributes => {"0" => {"body" =>"body of test ticket"}}} assert_difference('Ticket.count') do @@ -23,8 +23,30 @@ class TicketsControllerTest < ActionController::TestCase end assert_response :redirect - assert_equal assigns(:ticket).email, User.current_test.email - assert_equal User.find(assigns(:ticket).created_by).login, User.current_test.login + #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 -- cgit v1.2.3