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. --- Gemfile | 2 +- app/views/home/index.html.haml | 8 +++++ 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 +++-- users/app/models/user.rb | 4 +++ 15 files changed, 143 insertions(+), 17 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 diff --git a/Gemfile b/Gemfile index 0bdea00..7666805 100644 --- a/Gemfile +++ b/Gemfile @@ -16,7 +16,7 @@ group :assets do gem 'coffee-rails', '~> 3.2.2' # See https://github.com/sstephenson/execjs#readme for more supported runtimes - # gem 'therubyracer', :platforms => :ruby + gem 'therubyracer', :platforms => :ruby gem 'uglifier', '>= 1.2.7' end diff --git a/app/views/home/index.html.haml b/app/views/home/index.html.haml index 11f18de..c27ba75 100644 --- a/app/views/home/index.html.haml +++ b/app/views/home/index.html.haml @@ -2,3 +2,11 @@ Try to fetch a = link_to "cert", cert_path + +%p +Try to create a += link_to "ticket", new_ticket_path + +%p +See all += link_to "tickets", tickets_path \ No newline at end of file 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 diff --git a/users/app/models/user.rb b/users/app/models/user.rb index a6aab84..33c77ce 100644 --- a/users/app/models/user.rb +++ b/users/app/models/user.rb @@ -59,4 +59,8 @@ class User < CouchRest::Model::Base Thread.current[:user] = user end + def self.current_test + User.first + end + end -- cgit v1.2.3