mirror of
				https://github.com/fspc/BikeShed-1.git
				synced 2025-10-31 08:55:36 -04:00 
			
		
		
		
	Merge pull request #18 from spacemunkay/denney-check-in
Denney check in
This commit is contained in:
		
						commit
						19adc354a1
					
				
							
								
								
									
										43
									
								
								app/assets/javascripts/login.js
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										43
									
								
								app/assets/javascripts/login.js
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,43 @@ | ||||
| $("#checkin_menu").show(); | ||||
| $("#checkin").click( function(e){ | ||||
|   var username = $("#user_email").val(); | ||||
|   var password = $("#user_password").val(); | ||||
|   $.ajax({ | ||||
|     type: 'POST', | ||||
|     url: '/api/v1/checkin', | ||||
|     dataType: 'json', | ||||
|     contentType: 'application/json', | ||||
|     processData: false, | ||||
|     data: JSON.stringify({"username": username, "password": password }), | ||||
|     complete: function() { }, | ||||
|     success: function(data) { | ||||
|       alert("Checked IN!"); | ||||
|       $("#user_email").val(''); | ||||
|       $("#user_password").val(''); | ||||
|     }, | ||||
|     error: function(data,textStatus) { | ||||
|       alert( "Error: " + JSON.parse(data.responseText)["error"]); | ||||
|     } | ||||
|   }) | ||||
| }); | ||||
| $("#checkout").click( function(e){ | ||||
|   var username = $("#user_email").val(); | ||||
|   var password = $("#user_password").val(); | ||||
|   $.ajax({ | ||||
|     type: 'POST', | ||||
|     url: '/api/v1/checkout', | ||||
|     dataType: 'json', | ||||
|     contentType: 'application/json', | ||||
|     processData: false, | ||||
|     data: JSON.stringify({"username": username, "password": password }), | ||||
|     complete: function() { }, | ||||
|     success: function(data) { | ||||
|       alert("Checked OUT!"); | ||||
|       $("#user_email").val(''); | ||||
|       $("#user_password").val(''); | ||||
|     }, | ||||
|     error: function(data,textStatus) { | ||||
|       alert( "Error: " + JSON.parse(data.responseText)["error"]); | ||||
|     } | ||||
|   }) | ||||
| }); | ||||
| @ -6,6 +6,11 @@ class AppTabPanel < Netzke::Basepack::TabPanel | ||||
|     c.text = "Sign out #{controller.current_user.email}" if controller.current_user | ||||
|   end | ||||
| 
 | ||||
|   action :check_out do |c| | ||||
|     c.icon = :door_out | ||||
|     c.text = "CHECK OUT" if controller.current_user | ||||
|   end | ||||
| 
 | ||||
|   def configure(c) | ||||
| 
 | ||||
|     #all users | ||||
| @ -49,7 +54,7 @@ class AppTabPanel < Netzke::Basepack::TabPanel | ||||
|     end | ||||
| 
 | ||||
|     c.prevent_header = true | ||||
|     c.tbar = [:sign_out] | ||||
|     c.tbar = [:sign_out, :check_out] | ||||
|     c.items = @@app_tab_panel_items | ||||
|     super | ||||
|   end | ||||
| @ -58,6 +63,5 @@ class AppTabPanel < Netzke::Basepack::TabPanel | ||||
|     #gets js from app_tab_panel/javascripts/sign_out.js | ||||
|     c.mixin :sign_out | ||||
|   end | ||||
| 
 | ||||
| end | ||||
| 
 | ||||
|  | ||||
| @ -5,5 +5,17 @@ | ||||
|        url: '/users/sign_out', | ||||
|        method: 'DELETE' | ||||
|     }); | ||||
|   }, | ||||
|   onCheckOut: function(){ | ||||
|     Ext.Ajax.request({ | ||||
|        url: '/api/v1/checkout', | ||||
|        method: 'POST', | ||||
|        success: function(response, opts) { | ||||
|          Ext.Ajax.request({ | ||||
|             url: '/users/sign_out', | ||||
|             method: 'DELETE' | ||||
|          }); | ||||
|        } | ||||
|     }); | ||||
|   } | ||||
| } | ||||
|  | ||||
							
								
								
									
										22
									
								
								app/controllers/api/v1/base_controller.rb
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										22
									
								
								app/controllers/api/v1/base_controller.rb
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,22 @@ | ||||
| class Api::V1::BaseController < ActionController::Base | ||||
|   respond_to :json | ||||
| 
 | ||||
|   before_filter :authenticate_user | ||||
| 
 | ||||
|   private | ||||
|     def authenticate_user | ||||
|       if params[:username] | ||||
|         user = User.find_for_database_authentication( :email => params[:username] ) | ||||
|         @current_user = user if user && user.valid_password?( params[:password] ) | ||||
| 
 | ||||
|         if @current_user.nil? | ||||
|           msg = "Username/Password/Token invalid" | ||||
|           render :json => {:error => msg }, :status => 403 and return | ||||
|         end | ||||
|       else | ||||
|         authenticate_user! | ||||
|         @current_user = current_user | ||||
|       end | ||||
|     end | ||||
| end | ||||
| 
 | ||||
							
								
								
									
										22
									
								
								app/controllers/api/v1/logs_controller.rb
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										22
									
								
								app/controllers/api/v1/logs_controller.rb
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,22 @@ | ||||
| class Api::V1::LogsController < Api::V1::BaseController | ||||
| 
 | ||||
|   def checkin | ||||
|     #must use @current_user since user may not have signed in | ||||
|     if @current_user.checked_in? | ||||
|       render :json => { "error" => "You are already checked in."}, :status => 404 and return | ||||
|     else | ||||
|       @current_user.checkin | ||||
|       render :nothing => true, :status => 204 and return | ||||
|     end | ||||
|   end | ||||
| 
 | ||||
|   def checkout | ||||
|     #must use @current_user since user may not have signed in | ||||
|     if !@current_user.checked_in? | ||||
|       render :json => { "error" => "You were not even checked in."}, :status => 404 and return | ||||
|     else | ||||
|       @current_user.checkout | ||||
|       render :nothing => true, :status => 204 and return | ||||
|     end | ||||
|   end | ||||
| end | ||||
| @ -36,6 +36,8 @@ class User < ActiveRecord::Base | ||||
|     user_role.to_s == role.to_s | ||||
|   end | ||||
| 
 | ||||
| ### TODO methods below probably belong somewhere else | ||||
| 
 | ||||
|   def total_hours | ||||
|     ActsAsLoggable::Log.where( :loggable_type => self.class.to_s, :loggable_id => self.id).sum { |l| (l.end_date - l.start_date)/3600 }.round(2) | ||||
|   end | ||||
| @ -49,4 +51,33 @@ class User < ActiveRecord::Base | ||||
|       .sum { |l| (l.end_date - l.start_date)/3600 } | ||||
|       .round(2) | ||||
|   end | ||||
| 
 | ||||
|   def checked_in? | ||||
|     log_action = ::ActsAsLoggable::UserAction.find_by_action("CHECKIN") | ||||
|     checked = logs.where( log_action_id: log_action.id). | ||||
|       where("start_date >= ?", Time.zone.now.beginning_of_day). | ||||
|       where("start_date = end_date") | ||||
|     !checked.empty? | ||||
|   end | ||||
| 
 | ||||
|   def checkin | ||||
|     log_action = ::ActsAsLoggable::UserAction.find_by_action("CHECKIN") | ||||
|     time = Time.now | ||||
|     logs.create( logger_id: self.id, | ||||
|                  logger_type: self.class.to_s, | ||||
|                  start_date: time, | ||||
|                  end_date: time, | ||||
|                  log_action_id: log_action.id, | ||||
|                  log_action_type: log_action.class.to_s) | ||||
|     save | ||||
|   end | ||||
| 
 | ||||
|   def checkout | ||||
|     log_action = ::ActsAsLoggable::UserAction.find_by_action("CHECKIN") | ||||
|     checked = logs.where( log_action_id: log_action.id). | ||||
|       where("start_date >= ?", Time.zone.now.beginning_of_day). | ||||
|       where("start_date = end_date").first | ||||
|     checked.end_date = Time.now | ||||
|     checked.save | ||||
|   end | ||||
| end | ||||
|  | ||||
| @ -1,6 +1,6 @@ | ||||
| <%= stylesheet_link_tag "bootstrap_and_overrides", :media => "all" %> | ||||
| 
 | ||||
| <h2>Sign in</h2> | ||||
| <h2>Velocipede</h2> | ||||
| 
 | ||||
| <%= form_for(resource, :as => resource_name, :url => session_path(resource_name)) do |f| %> | ||||
|   <div><%= f.label :email %><br /> | ||||
| @ -14,8 +14,15 @@ | ||||
|   <% end -%> | ||||
| 
 | ||||
|   <div><%= f.submit "Sign in" %></div> | ||||
| 
 | ||||
|   <div id="checkin_menu"style="display:none;"> | ||||
|     <input id="checkin" name="checkin" type="button" value="CHECK IN"> | ||||
|     <input id="checkout" name="checkout" type="button" value="CHECK OUT"> | ||||
|   </div> | ||||
| <% end %> | ||||
| 
 | ||||
| 
 | ||||
| 
 | ||||
| <%= render "links" %> | ||||
| 
 | ||||
| <% if Rails.env.development? %> | ||||
|  | ||||
| @ -221,3 +221,9 @@ Devise.setup do |config| | ||||
|   #   manager.default_strategies(:scope => :user).unshift :some_external_strategy | ||||
|   # end | ||||
| end | ||||
| 
 | ||||
| #Check in the user if they sign in. (Devise uses Warden) | ||||
| Warden::Manager.after_authentication do |user,auth,opts| | ||||
|   user.checkin unless user.checked_in? | ||||
| end | ||||
| 
 | ||||
|  | ||||
| @ -4,4 +4,13 @@ Velocipede::Application.routes.draw do | ||||
|   netzke | ||||
| 
 | ||||
|   root :to => 'site#index' | ||||
|   ########################### | ||||
|   # API Routes | ||||
|   scope 'api', :module => :api do | ||||
|     scope 'v1', :module => :v1 do | ||||
|       post 'checkin' => "logs#checkin", :as => "api_checkin" | ||||
|       post 'checkout' => "logs#checkout", :as => "api_checkout" | ||||
|     end | ||||
|   end | ||||
| 
 | ||||
| end | ||||
|  | ||||
| @ -13,3 +13,8 @@ staff: | ||||
|   action: STAFF | ||||
|   created_at: <%= Time.now %> | ||||
|   updated_at: <%= Time.now %> | ||||
| checkin: | ||||
|   id: 4 | ||||
|   action: CHECKIN | ||||
|   created_at: <%= Time.now %> | ||||
|   updated_at: <%= Time.now %> | ||||
|  | ||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user