diff --git a/.gitignore b/.gitignore index 050c9d9..edf8e85 100644 --- a/.gitignore +++ b/.gitignore @@ -15,3 +15,5 @@ /log/* !/log/.keep /tmp + +*.DS_Store diff --git a/Gemfile b/Gemfile index d0ca1fd..8adf1b6 100644 --- a/Gemfile +++ b/Gemfile @@ -23,6 +23,8 @@ gem 'jbuilder', '~> 2.0' # bundle exec rake doc:rails generates the API under doc/api. gem 'sdoc', '~> 0.4.0', group: :doc +gem 'bootstrap-sass' + # Use ActiveModel has_secure_password # gem 'bcrypt', '~> 3.1.7' diff --git a/Gemfile.lock b/Gemfile.lock index 25c08b2..faadecf 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -37,8 +37,13 @@ GEM thread_safe (~> 0.3, >= 0.3.4) tzinfo (~> 1.1) arel (6.0.3) + autoprefixer-rails (6.4.0.1) + execjs binding_of_caller (0.7.2) debug_inspector (>= 0.0.1) + bootstrap-sass (3.3.7) + autoprefixer-rails (>= 5.2.1) + sass (>= 3.3.4) builder (3.2.2) byebug (9.0.5) coffee-rails (4.1.1) @@ -145,6 +150,7 @@ PLATFORMS ruby DEPENDENCIES + bootstrap-sass byebug coffee-rails (~> 4.1.0) jbuilder (~> 2.0) diff --git a/app/assets/images/button.png b/app/assets/images/button.png new file mode 100644 index 0000000..870c992 Binary files /dev/null and b/app/assets/images/button.png differ diff --git a/app/assets/javascripts/application.js b/app/assets/javascripts/application.js index e07c5a8..1662961 100644 --- a/app/assets/javascripts/application.js +++ b/app/assets/javascripts/application.js @@ -13,4 +13,5 @@ //= require jquery //= require jquery_ujs //= require turbolinks +//= require bootstrap //= require_tree . diff --git a/app/assets/javascripts/button.js b/app/assets/javascripts/button.js new file mode 100644 index 0000000..a23049e --- /dev/null +++ b/app/assets/javascripts/button.js @@ -0,0 +1,27 @@ +// we want this global timer to be accessed by all user threads + +var userOwns = false; + +$(document).on('click', '#button', function() { + if(userOwns == false) { + startClock(); + userOwns = true; + } + else { + // when someone else clicks button, you lose it and get set to zero + // store session name and time in db + + var stopTime = $time.innerHTML; + $.post("/users", + { + time: stopTime + }, + function() { + alert("congratz u got a time of: " + stopTime); + } + ); + stopClock(); + resetClock(); + userOwns = false; + } +}); \ No newline at end of file diff --git a/app/assets/javascripts/clock.js b/app/assets/javascripts/clock.js new file mode 100644 index 0000000..2b3c31e --- /dev/null +++ b/app/assets/javascripts/clock.js @@ -0,0 +1,81 @@ +var clsStopwatch = function() { + // Private vars + var startAt = 0; // Time of last start / resume. (0 if not running) + var lapTime = 0; // Time on the clock when last stopped in milliseconds + + var now = function() { + return (new Date()).getTime(); + }; + + // Public methods + // Start or resume + this.startClock = function() { + startAt = startAt ? startAt : now(); + }; + + // Stop or pause + this.stopClock = function() { + // If running, update elapsed time otherwise keep it + lapTime = startAt ? lapTime + now() - startAt : lapTime; + startAt = 0; // Paused + }; + + // Reset + this.resetClock = function() { + lapTime = startAt = 0; + }; + + // Duration + this.time = function() { + return lapTime + (startAt ? now() - startAt : 0); + }; +}; + +var x = new clsStopwatch(); +var $time; +var clocktimer; + +function pad(num, size) { + var s = "0000" + num; + return s.substr(s.length - size); +} + +function formatTime(time) { + var h = m = s = ms = 0; + var newTime = ''; + + h = Math.floor( time / (60 * 60 * 1000) ); + time = time % (60 * 60 * 1000); + m = Math.floor( time / (60 * 1000) ); + time = time % (60 * 1000); + s = Math.floor( time / 1000 ); + ms = time % 1000; + + newTime = pad(h, 2) + ':' + pad(m, 2) + ':' + pad(s, 2) + ':' + pad(ms, 3); + return newTime; +} + +function showClock() { + $time = document.getElementById('time'); + updateClock(); +} + +function updateClock() { + $time.innerHTML = formatTime(x.time()); +} + +function startClock() { + clocktimer = setInterval("updateClock()", 1); + x.startClock(); +} + +function stopClock() { + x.stopClock(); + clearInterval(clocktimer); +} + +function resetClock() { + stopClock(); + x.resetClock(); + updateClock(); +} \ No newline at end of file diff --git a/app/assets/javascripts/home.coffee b/app/assets/javascripts/home.coffee new file mode 100644 index 0000000..24f83d1 --- /dev/null +++ b/app/assets/javascripts/home.coffee @@ -0,0 +1,3 @@ +# Place all the behaviors and hooks related to the matching controller here. +# All this logic will automatically be available in application.js. +# You can use CoffeeScript in this file: http://coffeescript.org/ diff --git a/app/assets/javascripts/modal.js b/app/assets/javascripts/modal.js new file mode 100644 index 0000000..b3675c2 --- /dev/null +++ b/app/assets/javascripts/modal.js @@ -0,0 +1,7 @@ +$(window).load(function(){ + $('#signinModal').modal('show'); +}); + +function closeModal() { + $('#signinModal').modal('hide'); +}; \ No newline at end of file diff --git a/app/assets/stylesheets/application.css b/app/assets/stylesheets/application.scss similarity index 97% rename from app/assets/stylesheets/application.css rename to app/assets/stylesheets/application.scss index f9cd5b3..19d6d1a 100644 --- a/app/assets/stylesheets/application.css +++ b/app/assets/stylesheets/application.scss @@ -13,3 +13,5 @@ *= require_tree . *= require_self */ + +@import "bootstrap"; \ No newline at end of file diff --git a/app/assets/stylesheets/home.scss b/app/assets/stylesheets/home.scss new file mode 100644 index 0000000..f0ddc68 --- /dev/null +++ b/app/assets/stylesheets/home.scss @@ -0,0 +1,3 @@ +// Place all the styles related to the home controller here. +// They will automatically be included in application.css. +// You can use Sass (SCSS) here: http://sass-lang.com/ diff --git a/app/controllers/home_controller.rb b/app/controllers/home_controller.rb new file mode 100644 index 0000000..003b553 --- /dev/null +++ b/app/controllers/home_controller.rb @@ -0,0 +1,5 @@ +class HomeController < ApplicationController + def index + @user = User.new + end +end diff --git a/app/controllers/users_controller.rb b/app/controllers/users_controller.rb index 2ec9ecc..be97c23 100644 --- a/app/controllers/users_controller.rb +++ b/app/controllers/users_controller.rb @@ -1,4 +1,21 @@ class UsersController < ApplicationController - def new + def create + User.create( + name: session[:name], + time: params[:time] + ) + redirect_to root_path + end + + def store_user + # so user can stay on same page and reuse session name + session[:name] = user_params[:name] + redirect_to root_path + end + + private + + def user_params + params.require(:user).permit(:name) end end diff --git a/app/helpers/home_helper.rb b/app/helpers/home_helper.rb new file mode 100644 index 0000000..23de56a --- /dev/null +++ b/app/helpers/home_helper.rb @@ -0,0 +1,2 @@ +module HomeHelper +end diff --git a/app/views/home/index.html.erb b/app/views/home/index.html.erb new file mode 100644 index 0000000..a6b60f0 --- /dev/null +++ b/app/views/home/index.html.erb @@ -0,0 +1,5 @@ +<%= image_tag "button.png", id: "button" %> + +
Find me in app/views/users/new.html.erb
diff --git a/config/routes.rb b/config/routes.rb index 89e124f..4c6d224 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -1,58 +1,5 @@ Rails.application.routes.draw do - get 'users/new' - - # The priority is based upon order of creation: first created -> highest priority. - # See how all your routes lay out with "rake routes". - - # You can have the root of your site routed with "root" - # root 'welcome#index' - - # Example of regular route: - # get 'products/:id' => 'catalog#view' - - # Example of named route that can be invoked with purchase_url(id: product.id) - # get 'products/:id/purchase' => 'catalog#purchase', as: :purchase - - # Example resource route (maps HTTP verbs to controller actions automatically): - # resources :products - - # Example resource route with options: - # resources :products do - # member do - # get 'short' - # post 'toggle' - # end - # - # collection do - # get 'sold' - # end - # end - - # Example resource route with sub-resources: - # resources :products do - # resources :comments, :sales - # resource :seller - # end - - # Example resource route with more complex sub-resources: - # resources :products do - # resources :comments - # resources :sales do - # get 'recent', on: :collection - # end - # end - - # Example resource route with concerns: - # concern :toggleable do - # post 'toggle' - # end - # resources :posts, concerns: :toggleable - # resources :photos, concerns: :toggleable - - # Example resource route within a namespace: - # namespace :admin do - # # Directs /admin/products/* to Admin::ProductsController - # # (app/controllers/admin/products_controller.rb) - # resources :products - # end + root 'home#index' + resources :users + post 'store_user', to: 'users#store_user', as: 'store_user' end diff --git a/db/migrate/20160615013423_create_users.rb b/db/migrate/20160615013423_create_users.rb index 1525504..c8e2037 100644 --- a/db/migrate/20160615013423_create_users.rb +++ b/db/migrate/20160615013423_create_users.rb @@ -2,8 +2,7 @@ class CreateUsers < ActiveRecord::Migration def change create_table :users do |t| t.string :name - t.string :email - t.integer :score + t.string :time t.timestamps null: false end diff --git a/db/schema.rb b/db/schema.rb new file mode 100644 index 0000000..c38eb7b --- /dev/null +++ b/db/schema.rb @@ -0,0 +1,23 @@ +# encoding: UTF-8 +# This file is auto-generated from the current state of the database. Instead +# of editing this file, please use the migrations feature of Active Record to +# incrementally modify your database, and then regenerate this schema definition. +# +# Note that this schema.rb definition is the authoritative source for your +# database schema. If you need to create the application database on another +# system, you should be using db:schema:load, not running all the migrations +# from scratch. The latter is a flawed and unsustainable approach (the more migrations +# you'll amass, the slower it'll run and the greater likelihood for issues). +# +# It's strongly recommended that you check this file into your version control system. + +ActiveRecord::Schema.define(version: 20160615013423) do + + create_table "users", force: :cascade do |t| + t.string "name" + t.string "time" + t.datetime "created_at", null: false + t.datetime "updated_at", null: false + end + +end diff --git a/test/controllers/home_controller_test.rb b/test/controllers/home_controller_test.rb new file mode 100644 index 0000000..730478d --- /dev/null +++ b/test/controllers/home_controller_test.rb @@ -0,0 +1,7 @@ +require 'test_helper' + +class HomeControllerTest < ActionController::TestCase + # test "the truth" do + # assert true + # end +end