From a76847209caa44bda00614150390940b87575f22 Mon Sep 17 00:00:00 2001 From: sandri4 Date: Fri, 16 Nov 2018 11:20:23 +0200 Subject: [PATCH 1/4] session destroy --- Gemfile | 2 +- Gemfile.lock | 6 +++--- app/controllers/sessions_controller.rb | 6 ++++++ app/views/tasks/index.html.erb | 3 +-- config/database.yml | 23 +++++++---------------- config/routes.rb | 2 ++ db/schema.rb | 6 +++++- 7 files changed, 25 insertions(+), 23 deletions(-) diff --git a/Gemfile b/Gemfile index 76f9a44..5f6dd2f 100644 --- a/Gemfile +++ b/Gemfile @@ -8,7 +8,7 @@ gem 'bootstrap', '~> 4.1.3' # Bundle edge Rails instead: gem 'rails', github: 'rails/rails' gem 'rails', '~> 5.2.1' # Use sqlite3 as the database for Active Record -gem 'sqlite3' +gem 'pg' # Use Puma as the app server gem 'puma', '~> 3.11' # Use SCSS for stylesheets diff --git a/Gemfile.lock b/Gemfile.lock index d59203c..d884335 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -117,6 +117,7 @@ GEM nio4r (2.3.1) nokogiri (1.8.5) mini_portile2 (~> 2.3.0) + pg (1.1.3) popper_js (1.14.3) public_suffix (3.0.3) puma (3.12.0) @@ -180,7 +181,6 @@ GEM actionpack (>= 4.0) activesupport (>= 4.0) sprockets (>= 3.0.0) - sqlite3 (1.3.13) thor (0.20.0) thread_safe (0.3.6) tilt (2.0.8) @@ -216,13 +216,13 @@ DEPENDENCIES jbuilder (~> 2.5) jquery-rails listen (>= 3.0.5, < 3.2) + pg puma (~> 3.11) rails (~> 5.2.1) sass-rails (~> 5.0) selenium-webdriver spring spring-watcher-listen (~> 2.0.0) - sqlite3 turbolinks (~> 5) tzinfo-data uglifier (>= 1.3.0) @@ -232,4 +232,4 @@ RUBY VERSION ruby 2.5.1p57 BUNDLED WITH - 1.16.6 + 1.17.1 diff --git a/app/controllers/sessions_controller.rb b/app/controllers/sessions_controller.rb index 443a9cd..1bc02ec 100644 --- a/app/controllers/sessions_controller.rb +++ b/app/controllers/sessions_controller.rb @@ -12,6 +12,12 @@ def create end end + def destroy + User.find(session[:user_id]).destroy + session[:user_id] = nil + redirect_to :root + end + private def user_params params.require(:user).permit(:name, :password) diff --git a/app/views/tasks/index.html.erb b/app/views/tasks/index.html.erb index 15095f2..5e38b0c 100644 --- a/app/views/tasks/index.html.erb +++ b/app/views/tasks/index.html.erb @@ -1,4 +1,5 @@
+ <%= link_to "Log out", logout_path, method: "delete" %>

New Task

<%= render 'form' %> @@ -16,5 +17,3 @@ <%= render partial: 'task', collection: @tasks %>
- - diff --git a/config/database.yml b/config/database.yml index 0d02f24..c51d3fb 100644 --- a/config/database.yml +++ b/config/database.yml @@ -1,25 +1,16 @@ -# SQLite version 3.x -# gem install sqlite3 -# -# Ensure the SQLite 3 gem is defined in your Gemfile -# gem 'sqlite3' -# default: &default - adapter: sqlite3 + adapter: postgresql + encoding: unicode pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %> - timeout: 5000 + username: sandri4 + password: 3699 development: <<: *default - database: db/development.sqlite3 - -# Warning: The database defined as "test" will be erased and -# re-generated from your development database when you run "rake". -# Do not set this db to the same as development or production. + database: lesson4_development test: <<: *default - database: db/test.sqlite3 - + database: lesson4_test production: <<: *default - database: db/production.sqlite3 + database: lesson4_production diff --git a/config/routes.rb b/config/routes.rb index f597f86..24dafbd 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -4,6 +4,8 @@ post 'users/login', to: 'users#create' get 'users/registration', to: 'users#registration' get 'index', to: 'welcome#index' + delete '/logout' => 'sessions#destroy' + get '/logout' => 'sessions#destroy' root 'tasks#index' # For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html resource :session diff --git a/db/schema.rb b/db/schema.rb index ae69849..f78e5fb 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -12,6 +12,9 @@ ActiveRecord::Schema.define(version: 2018_11_12_173713) do + # These are extensions that must be enabled in order to support this database + enable_extension "plpgsql" + create_table "tasks", force: :cascade do |t| t.string "title" t.string "description" @@ -19,7 +22,7 @@ t.datetime "expire_at" t.datetime "created_at", null: false t.datetime "updated_at", null: false - t.integer "user_id" + t.bigint "user_id" t.index ["user_id"], name: "index_tasks_on_user_id" end @@ -30,4 +33,5 @@ t.datetime "updated_at", null: false end + add_foreign_key "tasks", "users" end From 0b549e1f579f5ac340bd30b910cd52988fc7a66e Mon Sep 17 00:00:00 2001 From: sandri4 Date: Sun, 18 Nov 2018 23:23:28 +0200 Subject: [PATCH 2/4] add pagination --- Gemfile | 2 ++ Gemfile.lock | 5 +++++ app/assets/javascripts/application.js | 3 +++ app/controllers/tasks_controller.rb | 2 +- app/views/tasks/_form.html.erb | 2 +- app/views/tasks/index.html.erb | 23 +++++++++++++++++------ 6 files changed, 29 insertions(+), 8 deletions(-) diff --git a/Gemfile b/Gemfile index 5f6dd2f..3977bf5 100644 --- a/Gemfile +++ b/Gemfile @@ -15,6 +15,8 @@ gem 'puma', '~> 3.11' gem 'sass-rails', '~> 5.0' # Use Uglifier as compressor for JavaScript assets gem 'uglifier', '>= 1.3.0' +gem 'will_paginate' +gem 'will_paginate-bootstrap' # See https://github.com/rails/execjs#readme for more supported runtimes # gem 'mini_racer', platforms: :ruby diff --git a/Gemfile.lock b/Gemfile.lock index d884335..aa69225 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -199,6 +199,9 @@ GEM websocket-driver (0.7.0) websocket-extensions (>= 0.1.0) websocket-extensions (0.1.3) + will_paginate (3.1.6) + will_paginate-bootstrap (1.0.1) + will_paginate (>= 3.0.3) xpath (3.2.0) nokogiri (~> 1.8) @@ -227,6 +230,8 @@ DEPENDENCIES tzinfo-data uglifier (>= 1.3.0) web-console (>= 3.3.0) + will_paginate + will_paginate-bootstrap RUBY VERSION ruby 2.5.1p57 diff --git a/app/assets/javascripts/application.js b/app/assets/javascripts/application.js index 82e6f0f..a75bcec 100644 --- a/app/assets/javascripts/application.js +++ b/app/assets/javascripts/application.js @@ -10,6 +10,9 @@ // Read Sprockets README (https://github.com/rails/sprockets#sprockets-directives) for details // about supported directives. // +//= require jquery3 +//= require popper +//= require bootstrap //= require rails-ujs //= require activestorage //= require turbolinks diff --git a/app/controllers/tasks_controller.rb b/app/controllers/tasks_controller.rb index bf173b5..dea8907 100644 --- a/app/controllers/tasks_controller.rb +++ b/app/controllers/tasks_controller.rb @@ -3,7 +3,7 @@ class TasksController < ApplicationController def index @task = Task.new - @tasks = current_user.tasks.q(params[:q]) + @tasks = current_user.tasks.q(params[:q]).paginate(:page => params[:page], :per_page => 10) end def create diff --git a/app/views/tasks/_form.html.erb b/app/views/tasks/_form.html.erb index 350ab27..8bea6c3 100644 --- a/app/views/tasks/_form.html.erb +++ b/app/views/tasks/_form.html.erb @@ -13,7 +13,7 @@ <%= f.date_field :expire_at, value: @task.expire_at %> <%= @task.errors[:expire_at].first %> - <%= f.button :OK %> + <%= f.button :OK, class: 'btn btn-outline-primary' %> <% end %> <% if notice %> diff --git a/app/views/tasks/index.html.erb b/app/views/tasks/index.html.erb index 5e38b0c..74c41ce 100644 --- a/app/views/tasks/index.html.erb +++ b/app/views/tasks/index.html.erb @@ -1,5 +1,19 @@ + +
- <%= link_to "Log out", logout_path, method: "delete" %> +

New Task

<%= render 'form' %> @@ -8,12 +22,9 @@

Tasks

- <%= form_tag tasks_path, method: :get, remote: true do |f| %> - <%= text_field_tag :q, params[:q], placeholder: 'Search', class: 'search' %> - <% end %> - -
    <%= render partial: 'task', collection: @tasks %>
+ <%= will_paginate @tasks %> + <%-# js_will_paginate -%>
From aca5064c08ec237112e467e34a96db51a9694b9b Mon Sep 17 00:00:00 2001 From: sandri4 Date: Sun, 18 Nov 2018 23:39:14 +0200 Subject: [PATCH 3/4] fix logout --- app/controllers/sessions_controller.rb | 2 +- config/routes.rb | 5 +++-- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/app/controllers/sessions_controller.rb b/app/controllers/sessions_controller.rb index 1bc02ec..6420346 100644 --- a/app/controllers/sessions_controller.rb +++ b/app/controllers/sessions_controller.rb @@ -13,7 +13,7 @@ def create end def destroy - User.find(session[:user_id]).destroy + User.find(session[:user_id]) session[:user_id] = nil redirect_to :root end diff --git a/config/routes.rb b/config/routes.rb index 24dafbd..4cd314a 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -4,8 +4,9 @@ post 'users/login', to: 'users#create' get 'users/registration', to: 'users#registration' get 'index', to: 'welcome#index' - delete '/logout' => 'sessions#destroy' - get '/logout' => 'sessions#destroy' + get 'login' => 'sessions#new' + post 'login' => 'sessions#create' + delete 'logout' => 'sessions#destroy' root 'tasks#index' # For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html resource :session From 20f9f931ace7ebca104e210ead55bd67448b23b7 Mon Sep 17 00:00:00 2001 From: sandri4 Date: Mon, 19 Nov 2018 00:06:09 +0200 Subject: [PATCH 4/4] Remove new task form from index page and create with bootsrap popup with form --- app/assets/javascripts/application.js | 3 ++ app/assets/stylesheets/application.scss | 2 +- app/views/tasks/_form.html.erb | 54 ++++++++++++++----------- app/views/tasks/index.html.erb | 14 +++---- 4 files changed, 41 insertions(+), 32 deletions(-) diff --git a/app/assets/javascripts/application.js b/app/assets/javascripts/application.js index a75bcec..7548ef3 100644 --- a/app/assets/javascripts/application.js +++ b/app/assets/javascripts/application.js @@ -17,3 +17,6 @@ //= require activestorage //= require turbolinks //= require_tree . +$('#myModal').on('shown.bs.modal', function () { + $('#myInput').trigger('focus') +}) diff --git a/app/assets/stylesheets/application.scss b/app/assets/stylesheets/application.scss index f1784e0..ef36dc1 100644 --- a/app/assets/stylesheets/application.scss +++ b/app/assets/stylesheets/application.scss @@ -13,4 +13,4 @@ *= require_tree . *= require_self */ -@import "bootstrap"; \ No newline at end of file +@import "bootstrap"; diff --git a/app/views/tasks/_form.html.erb b/app/views/tasks/_form.html.erb index 8bea6c3..b4ed89c 100644 --- a/app/views/tasks/_form.html.erb +++ b/app/views/tasks/_form.html.erb @@ -1,25 +1,33 @@ -<%= form_for @task, remote: true do |f| %> -
-
- Title -
- <%= f.text_field :title, value: @task.title, class: 'form-control', 'aria-label' => 'Small', 'aria-describedby' => 'inputGroup-sizing-sm'%> - <%= @task.errors[:title].first %> -
-
- - <%= f.text_area :description, value: @task.description, id: 'exampleFormControlTextarea1', class: 'form-control', rows: 3 %> - <%= @task.errors[:description].first %> -
- <%= f.date_field :expire_at, value: @task.expire_at %> - <%= @task.errors[:expire_at].first %> - <%= f.button :OK, class: 'btn btn-outline-primary' %> -<% end %> +