Guest User

Untitled

a guest
Jan 16th, 2016
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.47 KB | None | 0 0
  1. class UsersController < ApplicationController
  2. def index
  3. @users = User.all
  4. end
  5.  
  6. def create
  7. @newuser = User.new params[:user]
  8. if @newuser.save
  9. flash[:notice] = "Successfully created your account. You can now log in"
  10. redirect_to root_path
  11. else
  12. flash[:error]=@newuser.errors.full_messages;
  13. render :action => 'new'
  14. end
  15.  
  16. end
  17.  
  18. def show
  19. @user = User.find(params[:id])
  20. if session[:user_id] != @user.id
  21. flash[:notice] = "not authorised!!"
  22. redirect_to User.find(session[:user_id])
  23. end
  24.  
  25. @tasks = @user.tasks
  26. end
  27.  
  28. def add_task
  29. #render text: params.inspect
  30. Task.create(job:params[:task][:job],user_id:params[:id])
  31. flash[:notice]="Task has been added Successfully"
  32. redirect_to user_path(params[:id])
  33. end
  34.  
  35. def authenticate
  36. #render text: params.inspect
  37. @user = User.where(:username => params[:username],
  38. password:params[:password]).first
  39. if @user.nil?
  40. flash[:notice]="Invalid Username and/or Password"
  41. redirect_to root_path
  42. else
  43. session[:user_id] =@user.id
  44. flash[:notice]="Welcome #{@user.username}!!"
  45. redirect_to @user
  46. end
  47. end
  48.  
  49. def delete_task
  50. #render text: params.inspect
  51. Task.destroy(params[:task_id])
  52. flash[:notice]="Task was marked as done and hence deleted!!"
  53. redirect_to User.find(session[:user_id])
  54.  
  55. end
  56.  
  57. def home
  58. if session[:user_id]
  59. redirect_to User.find(session[:user_id])
  60. else
  61. flash[:notice]="Please sign in!"
  62. redirect_to root_path
  63. end
  64. end
  65.  
  66. def signout
  67. session[:user_id]=nil
  68. redirect_to root_path
  69. end
  70.  
  71. def updatetask
  72. @task = Task.find(params[:task_id])
  73.  
  74. if session[:user_id] != @task.user_id
  75. flash[:notice] = "not authorised!!"
  76. redirect_to User.find(session[:user_id])
  77. else
  78.  
  79. @task.planned = params[:planned]
  80. @task.started = params[:started]
  81. #@task.color = params[:color]
  82. @task.finished = params[:finished]
  83. @task.save
  84. redirect_to root_path
  85. end
  86. end
  87.  
  88. end
  89.  
  90. <% if flash[:notice] %>
  91. <div id="notice"><%=flash[:notice]%></div>
  92. <% end %>
  93. <% if @tasks.any? %>
  94. <table border="1">
  95. <tr>
  96. <th class="task-column">Task</th>
  97. <th>Planned</th>
  98. <th>Started</th>
  99. <th>Finished</th>
  100. <th>Delete Task</th>
  101. </tr>
  102.  
  103.  
  104. <% @tasks.each do |t| %>
  105. <tr>
  106. <td class="task-column"><%= t.job %></td>
  107. <td><%= check_box_tag "planned[#{t.id}]","#{t.id}", t.planned, :class => 'planned'%></td>
  108. <td><%= check_box_tag "started[#{t.id}]","#{t.id}",t.started, :class => 'started'%></td>
  109. <td><%= check_box_tag "finished[#{t.id}]","#{t.id}", t.finished, :class => 'finished' %></td>
  110. <td><%=link_to "Delete", delete_task_path(t)%></td>
  111.  
  112. </tr>
  113.  
  114. <%end%>
  115. </table>
  116.  
  117. <div id="done-container"><div id="done"></div></div>
  118. <%end%>
  119. <% if @tasks.empty? %>
  120. <p>Please Add Some Tasks.</p>
  121. <% end %>
  122.  
  123. $(document).ready(function() {
  124. $("input:checkbox:checked.started").parent().css("background-color","yellow").siblings().css("background-color","yellow");
  125. $("input:checkbox:checked.finished").parent().css("background-color","green").siblings().css("background-color","green");
  126.  
  127. function doneDisplay() {
  128. var done_div =$("#done");
  129. var done = Math.floor(($(":checked").length)/($(":checkbox").length)*100);
  130. done_div.html( "you are " + (done||"0") +"% done!!").css("width",done+"%");
  131. //$(done_div.children()[0]).css("font-color","blue");
  132. if (done <= 33){
  133. done_div.css("background-color","red");
  134. }
  135. if (done > 33){
  136. done_div.css("background-color","yellow");
  137. }
  138. if (done > 66){
  139. done_div.css("background-color","green");
  140. }
  141. if (done == 0){
  142. done_div.css("width","100%").css("background-color","inherit");
  143. }
  144.  
  145.  
  146. }
  147.  
  148. doneDisplay();
  149.  
  150. $(":checkbox").change(function(){
  151. var $this = $(this);
  152. var color,finished,started,planned;
  153. var task_id = $this.attr("value");
  154. if (($this.attr("class") =="finished") && ($this.attr("checked")=="checked")){
  155. $this.parent().css("background-color","green").siblings().css("background-color","green")
  156. .children().attr("checked",true);
  157. //color ="green";
  158. finished = started = planned =true;
  159. }
  160. if ($this.attr("class") =="planned" && !$this.attr("checked")){
  161. $this.parent().css("background-color","red").siblings().css("background-color","red")
  162. .children().attr("checked",false);
  163. //color = "red";
  164. finished = started = planned =false;
  165. }
  166. if (($this.attr("class") =="started") && ($this.attr("checked")=="checked")){
  167. $this.parent().css("background-color","yellow").siblings().css("background-color","yellow")
  168. .children(".planned").attr("checked",true);
  169. //color = "yellow";
  170. started = planned =true;
  171. finished = false;
  172.  
  173. }
  174. if ($this.attr("class") =="started" && !$this.attr("checked")){
  175. $this.parent().css("background-color","red").siblings().css("background-color","red")
  176. .children(".finished").attr("checked",false);
  177. //color = "red";
  178. planned =true;
  179. finished =started= false;
  180. }
  181. if ($this.attr("class") =="finished" && !$this.attr("checked")){
  182. $this.parent().css("background-color","yellow").siblings().css("background-color","yellow");
  183. //color = "yellow";
  184. started = planned =true;
  185. finished = false;
  186. }
  187. if (($this.attr("class") =="planned") && ($this.attr("checked")=="checked")){
  188. $this.parent().css("background-color","red").siblings().css("background-color","red")
  189. //color ="green";
  190. finished = started =false;
  191. planned =true;
  192. }
  193.  
  194.  
  195.  
  196.  
  197.  
  198.  
  199. $.ajax({
  200. type: "POST",
  201. url : "/updatetask/"+task_id+"/"+planned+"/"+started+"/"+finished,
  202. success: doneDisplay,
  203. //error: function(xhr){ alert("The error code is: "+xhr.statusText);}
  204. });
  205.  
  206.  
  207.  
  208. })
  209. });
Add Comment
Please, Sign In to add comment