From 0a71df14acff2797573971e8bbfd41a9527bacc8 Mon Sep 17 00:00:00 2001 From: uchicagopriya Date: Sun, 22 Jan 2023 01:46:08 +0000 Subject: [PATCH 01/25] Added Devise Gem and root route and environment port --- Gemfile | 2 + Gemfile.lock | 16 +- app/views/layouts/application.html.erb | 2 + config/environments/development.rb | 9 +- config/initializers/devise.rb | 311 +++++++++++++++++++++++++ config/locales/devise.en.yml | 65 ++++++ config/routes.rb | 1 + db/schema.rb | 18 ++ 8 files changed, 418 insertions(+), 6 deletions(-) create mode 100644 config/initializers/devise.rb create mode 100644 config/locales/devise.en.yml create mode 100644 db/schema.rb diff --git a/Gemfile b/Gemfile index 64dae54..7baba8f 100644 --- a/Gemfile +++ b/Gemfile @@ -21,6 +21,8 @@ gem 'jbuilder', '~> 2.7' # gem 'redis', '~> 4.0' # Use Active Model has_secure_password # gem 'bcrypt', '~> 3.1.7' +gem 'devise', '~> 4.8', '>= 4.8.1' + # Use Active Storage variant # gem 'image_processing', '~> 1.2' diff --git a/Gemfile.lock b/Gemfile.lock index dc44cfd..97ba9f7 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -81,6 +81,7 @@ GEM ast (2.4.2) awesome_print (1.9.2) backport (1.2.0) + bcrypt (3.1.18) benchmark (0.2.0) better_errors (2.9.1) coderay (>= 1.0.0) @@ -111,6 +112,12 @@ GEM concurrent-ruby (1.1.10) crass (1.0.6) debug_inspector (1.1.0) + devise (4.8.1) + bcrypt (~> 3.0) + orm_adapter (~> 0.1) + railties (>= 4.1.0) + responders + warden (~> 1.2.3) diff-lcs (1.5.0) diffy (3.4.2) e2mmap (0.1.0) @@ -153,8 +160,7 @@ GEM nokogiri (1.13.9) mini_portile2 (~> 2.8.0) racc (~> 1.4) - nokogiri (1.13.9-x86_64-darwin) - racc (~> 1.4) + orm_adapter (0.5.0) parallel (1.22.1) parser (3.1.1.0) ast (~> 2.4.1) @@ -218,6 +224,9 @@ GEM ffi (~> 1.0) rchardet (1.8.0) regexp_parser (2.2.1) + responders (3.0.1) + actionpack (>= 5.0) + railties (>= 5.0) reverse_markdown (2.1.1) nokogiri rexml (3.2.5) @@ -307,6 +316,8 @@ GEM tzinfo (>= 1.0.0) unicode-display_width (2.1.0) uniform_notifier (1.16.0) + warden (1.2.9) + rack (>= 2.0.9) web-console (4.2.0) actionview (>= 6.0.0) activemodel (>= 6.0.0) @@ -344,6 +355,7 @@ DEPENDENCIES bullet byebug capybara (>= 3.26) + devise (~> 4.8, >= 4.8.1) htmlbeautifier jbuilder (~> 2.7) listen (~> 3.3) diff --git a/app/views/layouts/application.html.erb b/app/views/layouts/application.html.erb index 9917f83..dc2fe69 100644 --- a/app/views/layouts/application.html.erb +++ b/app/views/layouts/application.html.erb @@ -11,6 +11,8 @@ +

<%= notice %>

+

<%= alert %>

<%= yield %> diff --git a/config/environments/development.rb b/config/environments/development.rb index a711738..5956d1e 100644 --- a/config/environments/development.rb +++ b/config/environments/development.rb @@ -1,11 +1,12 @@ require "active_support/core_ext/integer/time" Rails.application.configure do + config.action_mailer.default_url_options = { host: "localhost", port: 3000 } # Settings specified here will take precedence over those in config/application.rb. config.hosts.clear # Allow all Gitpod IPs to render the BetterErrors page - config.web_console.whitelisted_ips = '0.0.0.0/0.0.0.0' - BetterErrors::Middleware.allow_ip! '0.0.0.0/0.0.0.0' + config.web_console.whitelisted_ips = "0.0.0.0/0.0.0.0" + BetterErrors::Middleware.allow_ip! "0.0.0.0/0.0.0.0" # In the development environment your application's code is reloaded any time # it changes. This slows down response time but is perfect for development # since you don't have to restart the web server when you make code changes. @@ -19,13 +20,13 @@ # Enable/disable caching. By default caching is disabled. # Run rails dev:cache to toggle caching. - if Rails.root.join('tmp', 'caching-dev.txt').exist? + if Rails.root.join("tmp", "caching-dev.txt").exist? config.action_controller.perform_caching = true config.action_controller.enable_fragment_cache_logging = true config.cache_store = :memory_store config.public_file_server.headers = { - 'Cache-Control' => "public, max-age=#{2.days.to_i}" + "Cache-Control" => "public, max-age=#{2.days.to_i}", } else config.action_controller.perform_caching = false diff --git a/config/initializers/devise.rb b/config/initializers/devise.rb new file mode 100644 index 0000000..2ed5596 --- /dev/null +++ b/config/initializers/devise.rb @@ -0,0 +1,311 @@ +# frozen_string_literal: true + +# Assuming you have not yet modified this file, each configuration option below +# is set to its default value. Note that some are commented out while others +# are not: uncommented lines are intended to protect your configuration from +# breaking changes in upgrades (i.e., in the event that future versions of +# Devise change the default values for those options). +# +# Use this hook to configure devise mailer, warden hooks and so forth. +# Many of these configuration options can be set straight in your model. +Devise.setup do |config| + # The secret key used by Devise. Devise uses this key to generate + # random tokens. Changing this key will render invalid all existing + # confirmation, reset password and unlock tokens in the database. + # Devise will use the `secret_key_base` as its `secret_key` + # by default. You can change it below and use your own secret key. + # config.secret_key = 'd747a8fe0f5da3761acb7a78b71232f16bc0d22196ec268865c427d972dd3926d17d5d85a40d6191c8de1e093f142041d32f070514a7240197380aa734037af9' + + # ==> Controller configuration + # Configure the parent class to the devise controllers. + # config.parent_controller = 'DeviseController' + + # ==> Mailer Configuration + # Configure the e-mail address which will be shown in Devise::Mailer, + # note that it will be overwritten if you use your own mailer class + # with default "from" parameter. + config.mailer_sender = 'please-change-me-at-config-initializers-devise@example.com' + + # Configure the class responsible to send e-mails. + # config.mailer = 'Devise::Mailer' + + # Configure the parent class responsible to send e-mails. + # config.parent_mailer = 'ActionMailer::Base' + + # ==> ORM configuration + # Load and configure the ORM. Supports :active_record (default) and + # :mongoid (bson_ext recommended) by default. Other ORMs may be + # available as additional gems. + require 'devise/orm/active_record' + + # ==> Configuration for any authentication mechanism + # Configure which keys are used when authenticating a user. The default is + # just :email. You can configure it to use [:username, :subdomain], so for + # authenticating a user, both parameters are required. Remember that those + # parameters are used only when authenticating and not when retrieving from + # session. If you need permissions, you should implement that in a before filter. + # You can also supply a hash where the value is a boolean determining whether + # or not authentication should be aborted when the value is not present. + # config.authentication_keys = [:email] + + # Configure parameters from the request object used for authentication. Each entry + # given should be a request method and it will automatically be passed to the + # find_for_authentication method and considered in your model lookup. For instance, + # if you set :request_keys to [:subdomain], :subdomain will be used on authentication. + # The same considerations mentioned for authentication_keys also apply to request_keys. + # config.request_keys = [] + + # Configure which authentication keys should be case-insensitive. + # These keys will be downcased upon creating or modifying a user and when used + # to authenticate or find a user. Default is :email. + config.case_insensitive_keys = [:email] + + # Configure which authentication keys should have whitespace stripped. + # These keys will have whitespace before and after removed upon creating or + # modifying a user and when used to authenticate or find a user. Default is :email. + config.strip_whitespace_keys = [:email] + + # Tell if authentication through request.params is enabled. True by default. + # It can be set to an array that will enable params authentication only for the + # given strategies, for example, `config.params_authenticatable = [:database]` will + # enable it only for database (email + password) authentication. + # config.params_authenticatable = true + + # Tell if authentication through HTTP Auth is enabled. False by default. + # It can be set to an array that will enable http authentication only for the + # given strategies, for example, `config.http_authenticatable = [:database]` will + # enable it only for database authentication. + # For API-only applications to support authentication "out-of-the-box", you will likely want to + # enable this with :database unless you are using a custom strategy. + # The supported strategies are: + # :database = Support basic authentication with authentication key + password + # config.http_authenticatable = false + + # If 401 status code should be returned for AJAX requests. True by default. + # config.http_authenticatable_on_xhr = true + + # The realm used in Http Basic Authentication. 'Application' by default. + # config.http_authentication_realm = 'Application' + + # It will change confirmation, password recovery and other workflows + # to behave the same regardless if the e-mail provided was right or wrong. + # Does not affect registerable. + # config.paranoid = true + + # By default Devise will store the user in session. You can skip storage for + # particular strategies by setting this option. + # Notice that if you are skipping storage for all authentication paths, you + # may want to disable generating routes to Devise's sessions controller by + # passing skip: :sessions to `devise_for` in your config/routes.rb + config.skip_session_storage = [:http_auth] + + # By default, Devise cleans up the CSRF token on authentication to + # avoid CSRF token fixation attacks. This means that, when using AJAX + # requests for sign in and sign up, you need to get a new CSRF token + # from the server. You can disable this option at your own risk. + # config.clean_up_csrf_token_on_authentication = true + + # When false, Devise will not attempt to reload routes on eager load. + # This can reduce the time taken to boot the app but if your application + # requires the Devise mappings to be loaded during boot time the application + # won't boot properly. + # config.reload_routes = true + + # ==> Configuration for :database_authenticatable + # For bcrypt, this is the cost for hashing the password and defaults to 12. If + # using other algorithms, it sets how many times you want the password to be hashed. + # The number of stretches used for generating the hashed password are stored + # with the hashed password. This allows you to change the stretches without + # invalidating existing passwords. + # + # Limiting the stretches to just one in testing will increase the performance of + # your test suite dramatically. However, it is STRONGLY RECOMMENDED to not use + # a value less than 10 in other environments. Note that, for bcrypt (the default + # algorithm), the cost increases exponentially with the number of stretches (e.g. + # a value of 20 is already extremely slow: approx. 60 seconds for 1 calculation). + config.stretches = Rails.env.test? ? 1 : 12 + + # Set up a pepper to generate the hashed password. + # config.pepper = 'ca40c7426d591addd65575d5fa4100750b5d0d2a98d3f9a35056b358c1327c3650ba9d3732c827841fe4df00c59117627663a0e8237682c7050e458092a69ee8' + + # Send a notification to the original email when the user's email is changed. + # config.send_email_changed_notification = false + + # Send a notification email when the user's password is changed. + # config.send_password_change_notification = false + + # ==> Configuration for :confirmable + # A period that the user is allowed to access the website even without + # confirming their account. For instance, if set to 2.days, the user will be + # able to access the website for two days without confirming their account, + # access will be blocked just in the third day. + # You can also set it to nil, which will allow the user to access the website + # without confirming their account. + # Default is 0.days, meaning the user cannot access the website without + # confirming their account. + # config.allow_unconfirmed_access_for = 2.days + + # A period that the user is allowed to confirm their account before their + # token becomes invalid. For example, if set to 3.days, the user can confirm + # their account within 3 days after the mail was sent, but on the fourth day + # their account can't be confirmed with the token any more. + # Default is nil, meaning there is no restriction on how long a user can take + # before confirming their account. + # config.confirm_within = 3.days + + # If true, requires any email changes to be confirmed (exactly the same way as + # initial account confirmation) to be applied. Requires additional unconfirmed_email + # db field (see migrations). Until confirmed, new email is stored in + # unconfirmed_email column, and copied to email column on successful confirmation. + config.reconfirmable = true + + # Defines which key will be used when confirming an account + # config.confirmation_keys = [:email] + + # ==> Configuration for :rememberable + # The time the user will be remembered without asking for credentials again. + # config.remember_for = 2.weeks + + # Invalidates all the remember me tokens when the user signs out. + config.expire_all_remember_me_on_sign_out = true + + # If true, extends the user's remember period when remembered via cookie. + # config.extend_remember_period = false + + # Options to be passed to the created cookie. For instance, you can set + # secure: true in order to force SSL only cookies. + # config.rememberable_options = {} + + # ==> Configuration for :validatable + # Range for password length. + config.password_length = 6..128 + + # Email regex used to validate email formats. It simply asserts that + # one (and only one) @ exists in the given string. This is mainly + # to give user feedback and not to assert the e-mail validity. + config.email_regexp = /\A[^@\s]+@[^@\s]+\z/ + + # ==> Configuration for :timeoutable + # The time you want to timeout the user session without activity. After this + # time the user will be asked for credentials again. Default is 30 minutes. + # config.timeout_in = 30.minutes + + # ==> Configuration for :lockable + # Defines which strategy will be used to lock an account. + # :failed_attempts = Locks an account after a number of failed attempts to sign in. + # :none = No lock strategy. You should handle locking by yourself. + # config.lock_strategy = :failed_attempts + + # Defines which key will be used when locking and unlocking an account + # config.unlock_keys = [:email] + + # Defines which strategy will be used to unlock an account. + # :email = Sends an unlock link to the user email + # :time = Re-enables login after a certain amount of time (see :unlock_in below) + # :both = Enables both strategies + # :none = No unlock strategy. You should handle unlocking by yourself. + # config.unlock_strategy = :both + + # Number of authentication tries before locking an account if lock_strategy + # is failed attempts. + # config.maximum_attempts = 20 + + # Time interval to unlock the account if :time is enabled as unlock_strategy. + # config.unlock_in = 1.hour + + # Warn on the last attempt before the account is locked. + # config.last_attempt_warning = true + + # ==> Configuration for :recoverable + # + # Defines which key will be used when recovering the password for an account + # config.reset_password_keys = [:email] + + # Time interval you can reset your password with a reset password key. + # Don't put a too small interval or your users won't have the time to + # change their passwords. + config.reset_password_within = 6.hours + + # When set to false, does not sign a user in automatically after their password is + # reset. Defaults to true, so a user is signed in automatically after a reset. + # config.sign_in_after_reset_password = true + + # ==> Configuration for :encryptable + # Allow you to use another hashing or encryption algorithm besides bcrypt (default). + # You can use :sha1, :sha512 or algorithms from others authentication tools as + # :clearance_sha1, :authlogic_sha512 (then you should set stretches above to 20 + # for default behavior) and :restful_authentication_sha1 (then you should set + # stretches to 10, and copy REST_AUTH_SITE_KEY to pepper). + # + # Require the `devise-encryptable` gem when using anything other than bcrypt + # config.encryptor = :sha512 + + # ==> Scopes configuration + # Turn scoped views on. Before rendering "sessions/new", it will first check for + # "users/sessions/new". It's turned off by default because it's slower if you + # are using only default views. + # config.scoped_views = false + + # Configure the default scope given to Warden. By default it's the first + # devise role declared in your routes (usually :user). + # config.default_scope = :user + + # Set this configuration to false if you want /users/sign_out to sign out + # only the current scope. By default, Devise signs out all scopes. + # config.sign_out_all_scopes = true + + # ==> Navigation configuration + # Lists the formats that should be treated as navigational. Formats like + # :html, should redirect to the sign in page when the user does not have + # access, but formats like :xml or :json, should return 401. + # + # If you have any extra navigational formats, like :iphone or :mobile, you + # should add them to the navigational formats lists. + # + # The "*/*" below is required to match Internet Explorer requests. + # config.navigational_formats = ['*/*', :html] + + # The default HTTP method used to sign out a resource. Default is :delete. + config.sign_out_via = :delete + + # ==> OmniAuth + # Add a new OmniAuth provider. Check the wiki for more information on setting + # up on your models and hooks. + # config.omniauth :github, 'APP_ID', 'APP_SECRET', scope: 'user,public_repo' + + # ==> Warden configuration + # If you want to use other strategies, that are not supported by Devise, or + # change the failure app, you can configure them inside the config.warden block. + # + # config.warden do |manager| + # manager.intercept_401 = false + # manager.default_strategies(scope: :user).unshift :some_external_strategy + # end + + # ==> Mountable engine configurations + # When using Devise inside an engine, let's call it `MyEngine`, and this engine + # is mountable, there are some extra configurations to be taken into account. + # The following options are available, assuming the engine is mounted as: + # + # mount MyEngine, at: '/my_engine' + # + # The router that invoked `devise_for`, in the example above, would be: + # config.router_name = :my_engine + # + # When using OmniAuth, Devise cannot automatically set OmniAuth path, + # so you need to do it manually. For the users scope, it would be: + # config.omniauth_path_prefix = '/my_engine/users/auth' + + # ==> Turbolinks configuration + # If your app is using Turbolinks, Turbolinks::Controller needs to be included to make redirection work correctly: + # + # ActiveSupport.on_load(:devise_failure_app) do + # include Turbolinks::Controller + # end + + # ==> Configuration for :registerable + + # When set to false, does not sign a user in automatically after their password is + # changed. Defaults to true, so a user is signed in automatically after changing a password. + # config.sign_in_after_change_password = true +end diff --git a/config/locales/devise.en.yml b/config/locales/devise.en.yml new file mode 100644 index 0000000..260e1c4 --- /dev/null +++ b/config/locales/devise.en.yml @@ -0,0 +1,65 @@ +# Additional translations at https://github.com/heartcombo/devise/wiki/I18n + +en: + devise: + confirmations: + confirmed: "Your email address has been successfully confirmed." + send_instructions: "You will receive an email with instructions for how to confirm your email address in a few minutes." + send_paranoid_instructions: "If your email address exists in our database, you will receive an email with instructions for how to confirm your email address in a few minutes." + failure: + already_authenticated: "You are already signed in." + inactive: "Your account is not activated yet." + invalid: "Invalid %{authentication_keys} or password." + locked: "Your account is locked." + last_attempt: "You have one more attempt before your account is locked." + not_found_in_database: "Invalid %{authentication_keys} or password." + timeout: "Your session expired. Please sign in again to continue." + unauthenticated: "You need to sign in or sign up before continuing." + unconfirmed: "You have to confirm your email address before continuing." + mailer: + confirmation_instructions: + subject: "Confirmation instructions" + reset_password_instructions: + subject: "Reset password instructions" + unlock_instructions: + subject: "Unlock instructions" + email_changed: + subject: "Email Changed" + password_change: + subject: "Password Changed" + omniauth_callbacks: + failure: "Could not authenticate you from %{kind} because \"%{reason}\"." + success: "Successfully authenticated from %{kind} account." + passwords: + no_token: "You can't access this page without coming from a password reset email. If you do come from a password reset email, please make sure you used the full URL provided." + send_instructions: "You will receive an email with instructions on how to reset your password in a few minutes." + send_paranoid_instructions: "If your email address exists in our database, you will receive a password recovery link at your email address in a few minutes." + updated: "Your password has been changed successfully. You are now signed in." + updated_not_active: "Your password has been changed successfully." + registrations: + destroyed: "Bye! Your account has been successfully cancelled. We hope to see you again soon." + signed_up: "Welcome! You have signed up successfully." + signed_up_but_inactive: "You have signed up successfully. However, we could not sign you in because your account is not yet activated." + signed_up_but_locked: "You have signed up successfully. However, we could not sign you in because your account is locked." + signed_up_but_unconfirmed: "A message with a confirmation link has been sent to your email address. Please follow the link to activate your account." + update_needs_confirmation: "You updated your account successfully, but we need to verify your new email address. Please check your email and follow the confirmation link to confirm your new email address." + updated: "Your account has been updated successfully." + updated_but_not_signed_in: "Your account has been updated successfully, but since your password was changed, you need to sign in again." + sessions: + signed_in: "Signed in successfully." + signed_out: "Signed out successfully." + already_signed_out: "Signed out successfully." + unlocks: + send_instructions: "You will receive an email with instructions for how to unlock your account in a few minutes." + send_paranoid_instructions: "If your account exists, you will receive an email with instructions for how to unlock it in a few minutes." + unlocked: "Your account has been unlocked successfully. Please sign in to continue." + errors: + messages: + already_confirmed: "was already confirmed, please try signing in" + confirmation_period_expired: "needs to be confirmed within %{period}, please request a new one" + expired: "has expired, please request a new one" + not_found: "not found" + not_locked: "was not locked" + not_saved: + one: "1 error prohibited this %{resource} from being saved:" + other: "%{count} errors prohibited this %{resource} from being saved:" diff --git a/config/routes.rb b/config/routes.rb index c06383a..f181ccf 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -1,3 +1,4 @@ Rails.application.routes.draw do # For details on the DSL available within this file, see https://guides.rubyonrails.org/routing.html + root "tasks#index" end diff --git a/db/schema.rb b/db/schema.rb new file mode 100644 index 0000000..4603022 --- /dev/null +++ b/db/schema.rb @@ -0,0 +1,18 @@ +# 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. +# +# This file is the source Rails uses to define your schema when running `bin/rails +# db:schema:load`. When creating a new database, `bin/rails db:schema:load` tends to +# be faster and is potentially less error prone than running all of your +# migrations from scratch. Old migrations may fail to apply correctly if those +# migrations use external dependencies or application code. +# +# It's strongly recommended that you check this file into your version control system. + +ActiveRecord::Schema.define(version: 0) do + + # These are extensions that must be enabled in order to support this database + enable_extension "plpgsql" + +end From 2ff2129c49e54f73dc29f8abe6f18b95bb7f0eae Mon Sep 17 00:00:00 2001 From: uchicagopriya Date: Sun, 22 Jan 2023 01:53:34 +0000 Subject: [PATCH 02/25] Added citext and uniqueness to username and migrated user db --- Gemfile | 76 +++++++++---------- app/models/user.rb | 6 ++ config/routes.rb | 1 + .../20230122014735_devise_create_users.rb | 47 ++++++++++++ test/fixtures/users.yml | 11 +++ test/models/user_test.rb | 7 ++ 6 files changed, 110 insertions(+), 38 deletions(-) create mode 100644 app/models/user.rb create mode 100644 db/migrate/20230122014735_devise_create_users.rb create mode 100644 test/fixtures/users.yml create mode 100644 test/models/user_test.rb diff --git a/Gemfile b/Gemfile index 7baba8f..76f5a39 100644 --- a/Gemfile +++ b/Gemfile @@ -1,79 +1,79 @@ -source 'https://rubygems.org' +source "https://rubygems.org" git_source(:github) { |repo| "https://github.com/#{repo}.git" } -ruby '3.0.3' +ruby "3.0.3" # Bundle edge Rails instead: gem 'rails', github: 'rails/rails', branch: 'main' -gem 'rails', '~> 6.1.3', '>= 6.1.3.1' +gem "rails", "~> 6.1.3", ">= 6.1.3.1" # Use postgresql as the database for Active Record -gem 'pg', '~> 1.1' +gem "pg", "~> 1.1" # Use Puma as the app server -gem 'puma', '~> 5.0' +gem "puma", "~> 5.0" # Use SCSS for stylesheets -gem 'sass-rails', '>= 6' +gem "sass-rails", ">= 6" # Transpile app-like JavaScript. Read more: https://github.com/rails/webpacker -gem 'webpacker', '~> 5.0' +gem "webpacker", "~> 5.0" # Turbolinks makes navigating your web application faster. Read more: https://github.com/turbolinks/turbolinks -gem 'turbolinks', '~> 5' +gem "turbolinks", "~> 5" # Build JSON APIs with ease. Read more: https://github.com/rails/jbuilder -gem 'jbuilder', '~> 2.7' +gem "jbuilder", "~> 2.7" # Use Redis adapter to run Action Cable in production # gem 'redis', '~> 4.0' # Use Active Model has_secure_password # gem 'bcrypt', '~> 3.1.7' -gem 'devise', '~> 4.8', '>= 4.8.1' - +gem "devise", "~> 4.8", ">= 4.8.1" +gem "annotate", "~> 3.2" # Use Active Storage variant # gem 'image_processing', '~> 1.2' # Reduces boot times through caching; required in config/boot.rb -gem 'bootsnap', '>= 1.4.4', require: false +gem "bootsnap", ">= 1.4.4", require: false group :development, :test do # Call 'byebug' anywhere in the code to stop execution and get a debugger console - gem 'byebug', platforms: [:mri, :mingw, :x64_mingw] + gem "byebug", platforms: [:mri, :mingw, :x64_mingw] end group :development do # Access an interactive console on exception pages or by calling 'console' anywhere in the code. - gem 'web-console', '>= 4.1.0' + gem "web-console", ">= 4.1.0" # Display performance information such as SQL time and flame graphs for each request in your browser. # Can be configured to work on production as well see: https://github.com/MiniProfiler/rack-mini-profiler/blob/master/README.md - gem 'rack-mini-profiler', '~> 2.0' - gem 'listen', '~> 3.3' + gem "rack-mini-profiler", "~> 2.0" + gem "listen", "~> 3.3" # Spring speeds up development by keeping your application running in the background. Read more: https://github.com/rails/spring - gem 'spring' + gem "spring" end group :test do # Adds support for Capybara system testing and selenium driver - gem 'capybara', '>= 3.26' - gem 'selenium-webdriver' + gem "capybara", ">= 3.26" + gem "selenium-webdriver" # Easy installation and use of web drivers to run system tests with browsers - gem 'webdrivers' + gem "webdrivers" end # Windows does not include zoneinfo files, so bundle the tzinfo-data gem -gem 'tzinfo-data', platforms: [:mingw, :mswin, :x64_mingw, :jruby] +gem "tzinfo-data", platforms: [:mingw, :mswin, :x64_mingw, :jruby] # appdev Gems group :development do - gem 'annotate' - gem 'awesome_print' - gem 'better_errors' - gem 'binding_of_caller' - gem 'bullet' - gem 'htmlbeautifier' - gem 'pry-rails' - gem 'rack-canonical-host' - gem 'rails-erd' - gem 'rollbar' - gem 'rufo' - gem 'standard', require: false - gem 'solargraph' - gem 'rubocop-rails', require: false - gem 'rubocop-rspec', require: false - gem 'skylight' - gem 'web_git', github: 'firstdraft/web_git' + gem "annotate" + gem "awesome_print" + gem "better_errors" + gem "binding_of_caller" + gem "bullet" + gem "htmlbeautifier" + gem "pry-rails" + gem "rack-canonical-host" + gem "rails-erd" + gem "rollbar" + gem "rufo" + gem "standard", require: false + gem "solargraph" + gem "rubocop-rails", require: false + gem "rubocop-rspec", require: false + gem "skylight" + gem "web_git", github: "firstdraft/web_git" end diff --git a/app/models/user.rb b/app/models/user.rb new file mode 100644 index 0000000..4756799 --- /dev/null +++ b/app/models/user.rb @@ -0,0 +1,6 @@ +class User < ApplicationRecord + # Include default devise modules. Others available are: + # :confirmable, :lockable, :timeoutable, :trackable and :omniauthable + devise :database_authenticatable, :registerable, + :recoverable, :rememberable, :validatable +end diff --git a/config/routes.rb b/config/routes.rb index f181ccf..6b55b17 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -1,4 +1,5 @@ Rails.application.routes.draw do + devise_for :users # For details on the DSL available within this file, see https://guides.rubyonrails.org/routing.html root "tasks#index" end diff --git a/db/migrate/20230122014735_devise_create_users.rb b/db/migrate/20230122014735_devise_create_users.rb new file mode 100644 index 0000000..bd2bb97 --- /dev/null +++ b/db/migrate/20230122014735_devise_create_users.rb @@ -0,0 +1,47 @@ +# frozen_string_literal: true + +class DeviseCreateUsers < ActiveRecord::Migration[6.1] + def change + enable_extension("citext") + create_table :users do |t| + ## Database authenticatable + t.string :email, null: false, default: "" + t.string :encrypted_password, null: false, default: "" + + ## Recoverable + t.string :reset_password_token + t.datetime :reset_password_sent_at + + ## Rememberable + t.datetime :remember_created_at + + ## Trackable + # t.integer :sign_in_count, default: 0, null: false + # t.datetime :current_sign_in_at + # t.datetime :last_sign_in_at + # t.string :current_sign_in_ip + # t.string :last_sign_in_ip + + ## Confirmable + # t.string :confirmation_token + # t.datetime :confirmed_at + # t.datetime :confirmation_sent_at + # t.string :unconfirmed_email # Only if using reconfirmable + + ## Lockable + # t.integer :failed_attempts, default: 0, null: false # Only if lock strategy is :failed_attempts + # t.string :unlock_token # Only if unlock strategy is :email or :both + # t.datetime :locked_at + + t.citext :username + + t.timestamps null: false + end + + add_index :users, :email, unique: true + add_index :users, :reset_password_token, unique: true + add_index :users, :username, unique: true + # add_index :users, :confirmation_token, unique: true + # add_index :users, :unlock_token, unique: true + end +end diff --git a/test/fixtures/users.yml b/test/fixtures/users.yml new file mode 100644 index 0000000..5181636 --- /dev/null +++ b/test/fixtures/users.yml @@ -0,0 +1,11 @@ +# Read about fixtures at https://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html + +# This model initially had no columns defined. If you add columns to the +# model remove the '{}' from the fixture names and add the columns immediately +# below each fixture, per the syntax in the comments below +# +one: {} +# column: value +# +two: {} +# column: value diff --git a/test/models/user_test.rb b/test/models/user_test.rb new file mode 100644 index 0000000..5c07f49 --- /dev/null +++ b/test/models/user_test.rb @@ -0,0 +1,7 @@ +require "test_helper" + +class UserTest < ActiveSupport::TestCase + # test "the truth" do + # assert true + # end +end From acccdce290724097e3ecb49daec70e4a4ec6d538 Mon Sep 17 00:00:00 2001 From: uchicagopriya Date: Sun, 22 Jan 2023 02:03:16 +0000 Subject: [PATCH 03/25] Scaffolded tasks and migrated db --- Gemfile | 5 +- Gemfile.lock | 2 +- app/assets/stylesheets/scaffolds.scss | 65 +++++++++++++++++++++ app/assets/stylesheets/tasks.scss | 3 + app/controllers/tasks_controller.rb | 70 +++++++++++++++++++++++ app/helpers/tasks_helper.rb | 2 + app/models/task.rb | 3 + app/views/tasks/_form.html.erb | 27 +++++++++ app/views/tasks/_task.json.jbuilder | 2 + app/views/tasks/edit.html.erb | 6 ++ app/views/tasks/index.html.erb | 29 ++++++++++ app/views/tasks/index.json.jbuilder | 1 + app/views/tasks/new.html.erb | 5 ++ app/views/tasks/show.html.erb | 14 +++++ app/views/tasks/show.json.jbuilder | 1 + config/routes.rb | 1 + db/migrate/20230122015942_create_tasks.rb | 9 +++ db/schema.rb | 26 ++++++++- test/controllers/tasks_controller_test.rb | 48 ++++++++++++++++ test/fixtures/tasks.yml | 9 +++ test/models/task_test.rb | 7 +++ test/system/tasks_test.rb | 45 +++++++++++++++ 22 files changed, 375 insertions(+), 5 deletions(-) create mode 100644 app/assets/stylesheets/scaffolds.scss create mode 100644 app/assets/stylesheets/tasks.scss create mode 100644 app/controllers/tasks_controller.rb create mode 100644 app/helpers/tasks_helper.rb create mode 100644 app/models/task.rb create mode 100644 app/views/tasks/_form.html.erb create mode 100644 app/views/tasks/_task.json.jbuilder create mode 100644 app/views/tasks/edit.html.erb create mode 100644 app/views/tasks/index.html.erb create mode 100644 app/views/tasks/index.json.jbuilder create mode 100644 app/views/tasks/new.html.erb create mode 100644 app/views/tasks/show.html.erb create mode 100644 app/views/tasks/show.json.jbuilder create mode 100644 db/migrate/20230122015942_create_tasks.rb create mode 100644 test/controllers/tasks_controller_test.rb create mode 100644 test/fixtures/tasks.yml create mode 100644 test/models/task_test.rb create mode 100644 test/system/tasks_test.rb diff --git a/Gemfile b/Gemfile index 76f5a39..5caa086 100644 --- a/Gemfile +++ b/Gemfile @@ -21,8 +21,6 @@ gem "jbuilder", "~> 2.7" # gem 'redis', '~> 4.0' # Use Active Model has_secure_password # gem 'bcrypt', '~> 3.1.7' -gem "devise", "~> 4.8", ">= 4.8.1" -gem "annotate", "~> 3.2" # Use Active Storage variant # gem 'image_processing', '~> 1.2' @@ -59,7 +57,8 @@ gem "tzinfo-data", platforms: [:mingw, :mswin, :x64_mingw, :jruby] # appdev Gems group :development do - gem "annotate" + gem "devise", "~> 4.8", ">= 4.8.1" + gem "annotate", "~> 3.2" gem "awesome_print" gem "better_errors" gem "binding_of_caller" diff --git a/Gemfile.lock b/Gemfile.lock index 97ba9f7..b83b4bc 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -347,7 +347,7 @@ PLATFORMS x86_64-darwin-19 DEPENDENCIES - annotate + annotate (~> 3.2) awesome_print better_errors binding_of_caller diff --git a/app/assets/stylesheets/scaffolds.scss b/app/assets/stylesheets/scaffolds.scss new file mode 100644 index 0000000..bb2597f --- /dev/null +++ b/app/assets/stylesheets/scaffolds.scss @@ -0,0 +1,65 @@ +body { + background-color: #fff; + color: #333; + margin: 33px; } + +body, p, ol, ul, td { + font-family: verdana, arial, helvetica, sans-serif; + font-size: 13px; + line-height: 18px; } + +pre { + background-color: #eee; + padding: 10px; + font-size: 11px; } + +a { + color: #000; } + +a:visited { + color: #666; } + +a:hover { + color: #fff; + background-color: #000; } + +th { + padding-bottom: 5px; } + +td { + padding: 0 5px 7px; } + +div.field, +div.actions { + margin-bottom: 10px; } + +#notice { + color: green; } + +.field_with_errors { + padding: 2px; + background-color: red; + display: table; } + +#error_explanation { + width: 450px; + border: 2px solid red; + padding: 7px 7px 0; + margin-bottom: 20px; + background-color: #f0f0f0; } + +#error_explanation h2 { + text-align: left; + font-weight: bold; + padding: 5px 5px 5px 15px; + font-size: 12px; + margin: -7px -7px 0; + background-color: #c00; + color: #fff; } + +#error_explanation ul li { + font-size: 12px; + list-style: square; } + +label { + display: block; } diff --git a/app/assets/stylesheets/tasks.scss b/app/assets/stylesheets/tasks.scss new file mode 100644 index 0000000..44f19ff --- /dev/null +++ b/app/assets/stylesheets/tasks.scss @@ -0,0 +1,3 @@ +// Place all the styles related to the tasks controller here. +// They will automatically be included in application.css. +// You can use Sass (SCSS) here: https://sass-lang.com/ diff --git a/app/controllers/tasks_controller.rb b/app/controllers/tasks_controller.rb new file mode 100644 index 0000000..034cd1b --- /dev/null +++ b/app/controllers/tasks_controller.rb @@ -0,0 +1,70 @@ +class TasksController < ApplicationController + before_action :set_task, only: %i[ show edit update destroy ] + + # GET /tasks or /tasks.json + def index + @tasks = Task.all + end + + # GET /tasks/1 or /tasks/1.json + def show + end + + # GET /tasks/new + def new + @task = Task.new + end + + # GET /tasks/1/edit + def edit + end + + # POST /tasks or /tasks.json + def create + @task = Task.new(task_params) + + respond_to do |format| + if @task.save + format.html { redirect_to task_url(@task), notice: "Task was successfully created." } + format.json { render :show, status: :created, location: @task } + else + format.html { render :new, status: :unprocessable_entity } + format.json { render json: @task.errors, status: :unprocessable_entity } + end + end + end + + # PATCH/PUT /tasks/1 or /tasks/1.json + def update + respond_to do |format| + if @task.update(task_params) + format.html { redirect_to task_url(@task), notice: "Task was successfully updated." } + format.json { render :show, status: :ok, location: @task } + else + format.html { render :edit, status: :unprocessable_entity } + format.json { render json: @task.errors, status: :unprocessable_entity } + end + end + end + + # DELETE /tasks/1 or /tasks/1.json + def destroy + @task.destroy + + respond_to do |format| + format.html { redirect_to tasks_url, notice: "Task was successfully destroyed." } + format.json { head :no_content } + end + end + + private + # Use callbacks to share common setup or constraints between actions. + def set_task + @task = Task.find(params[:id]) + end + + # Only allow a list of trusted parameters through. + def task_params + params.require(:task).permit(:owner_id, :body) + end +end diff --git a/app/helpers/tasks_helper.rb b/app/helpers/tasks_helper.rb new file mode 100644 index 0000000..ce894d0 --- /dev/null +++ b/app/helpers/tasks_helper.rb @@ -0,0 +1,2 @@ +module TasksHelper +end diff --git a/app/models/task.rb b/app/models/task.rb new file mode 100644 index 0000000..50af7f4 --- /dev/null +++ b/app/models/task.rb @@ -0,0 +1,3 @@ +class Task < ApplicationRecord + belongs_to :owner, class_name: "User" +end diff --git a/app/views/tasks/_form.html.erb b/app/views/tasks/_form.html.erb new file mode 100644 index 0000000..acbce4e --- /dev/null +++ b/app/views/tasks/_form.html.erb @@ -0,0 +1,27 @@ +<%= form_with(model: task) do |form| %> + <% if task.errors.any? %> +
+

<%= pluralize(task.errors.count, "error") %> prohibited this task from being saved:

+ +
    + <% task.errors.each do |error| %> +
  • <%= error.full_message %>
  • + <% end %> +
+
+ <% end %> + +
+ <%= form.label :owner_id %> + <%= form.text_field :owner_id %> +
+ +
+ <%= form.label :body %> + <%= form.text_area :body %> +
+ +
+ <%= form.submit %> +
+<% end %> diff --git a/app/views/tasks/_task.json.jbuilder b/app/views/tasks/_task.json.jbuilder new file mode 100644 index 0000000..3b4dbd0 --- /dev/null +++ b/app/views/tasks/_task.json.jbuilder @@ -0,0 +1,2 @@ +json.extract! task, :id, :owner_id, :body, :created_at, :updated_at +json.url task_url(task, format: :json) diff --git a/app/views/tasks/edit.html.erb b/app/views/tasks/edit.html.erb new file mode 100644 index 0000000..7b088fb --- /dev/null +++ b/app/views/tasks/edit.html.erb @@ -0,0 +1,6 @@ +

Editing Task

+ +<%= render 'form', task: @task %> + +<%= link_to 'Show', @task %> | +<%= link_to 'Back', tasks_path %> diff --git a/app/views/tasks/index.html.erb b/app/views/tasks/index.html.erb new file mode 100644 index 0000000..fffc5ba --- /dev/null +++ b/app/views/tasks/index.html.erb @@ -0,0 +1,29 @@ +

<%= notice %>

+ +

Tasks

+ + + + + + + + + + + + <% @tasks.each do |task| %> + + + + + + + + <% end %> + +
OwnerBody
<%= task.owner_id %><%= task.body %><%= link_to 'Show', task %><%= link_to 'Edit', edit_task_path(task) %><%= link_to 'Destroy', task, method: :delete, data: { confirm: 'Are you sure?' } %>
+ +
+ +<%= link_to 'New Task', new_task_path %> diff --git a/app/views/tasks/index.json.jbuilder b/app/views/tasks/index.json.jbuilder new file mode 100644 index 0000000..7481994 --- /dev/null +++ b/app/views/tasks/index.json.jbuilder @@ -0,0 +1 @@ +json.array! @tasks, partial: "tasks/task", as: :task diff --git a/app/views/tasks/new.html.erb b/app/views/tasks/new.html.erb new file mode 100644 index 0000000..3b6b4ba --- /dev/null +++ b/app/views/tasks/new.html.erb @@ -0,0 +1,5 @@ +

New Task

+ +<%= render 'form', task: @task %> + +<%= link_to 'Back', tasks_path %> diff --git a/app/views/tasks/show.html.erb b/app/views/tasks/show.html.erb new file mode 100644 index 0000000..e2486fe --- /dev/null +++ b/app/views/tasks/show.html.erb @@ -0,0 +1,14 @@ +

<%= notice %>

+ +

+ Owner: + <%= @task.owner_id %> +

+ +

+ Body: + <%= @task.body %> +

+ +<%= link_to 'Edit', edit_task_path(@task) %> | +<%= link_to 'Back', tasks_path %> diff --git a/app/views/tasks/show.json.jbuilder b/app/views/tasks/show.json.jbuilder new file mode 100644 index 0000000..fd8232f --- /dev/null +++ b/app/views/tasks/show.json.jbuilder @@ -0,0 +1 @@ +json.partial! "tasks/task", task: @task diff --git a/config/routes.rb b/config/routes.rb index 6b55b17..5abab18 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -1,4 +1,5 @@ Rails.application.routes.draw do + resources :tasks devise_for :users # For details on the DSL available within this file, see https://guides.rubyonrails.org/routing.html root "tasks#index" diff --git a/db/migrate/20230122015942_create_tasks.rb b/db/migrate/20230122015942_create_tasks.rb new file mode 100644 index 0000000..89c4ad6 --- /dev/null +++ b/db/migrate/20230122015942_create_tasks.rb @@ -0,0 +1,9 @@ +class CreateTasks < ActiveRecord::Migration[6.1] + def change + create_table :tasks do |t| + t.belongs_to :owner, null: false, foreign_key: { to_table: :users } + t.text :body + t.timestamps + end + end +end diff --git a/db/schema.rb b/db/schema.rb index 4603022..340da75 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,9 +10,33 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 0) do +ActiveRecord::Schema.define(version: 2023_01_22_015942) do # These are extensions that must be enabled in order to support this database + enable_extension "citext" enable_extension "plpgsql" + create_table "tasks", force: :cascade do |t| + t.bigint "owner_id", null: false + t.text "body" + t.datetime "created_at", precision: 6, null: false + t.datetime "updated_at", precision: 6, null: false + t.index ["owner_id"], name: "index_tasks_on_owner_id" + end + + create_table "users", force: :cascade do |t| + t.string "email", default: "", null: false + t.string "encrypted_password", default: "", null: false + t.string "reset_password_token" + t.datetime "reset_password_sent_at" + t.datetime "remember_created_at" + t.citext "username" + t.datetime "created_at", precision: 6, null: false + t.datetime "updated_at", precision: 6, null: false + t.index ["email"], name: "index_users_on_email", unique: true + t.index ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true + t.index ["username"], name: "index_users_on_username", unique: true + end + + add_foreign_key "tasks", "users", column: "owner_id" end diff --git a/test/controllers/tasks_controller_test.rb b/test/controllers/tasks_controller_test.rb new file mode 100644 index 0000000..8059c1a --- /dev/null +++ b/test/controllers/tasks_controller_test.rb @@ -0,0 +1,48 @@ +require "test_helper" + +class TasksControllerTest < ActionDispatch::IntegrationTest + setup do + @task = tasks(:one) + end + + test "should get index" do + get tasks_url + assert_response :success + end + + test "should get new" do + get new_task_url + assert_response :success + end + + test "should create task" do + assert_difference('Task.count') do + post tasks_url, params: { task: { body: @task.body, owner_id: @task.owner_id } } + end + + assert_redirected_to task_url(Task.last) + end + + test "should show task" do + get task_url(@task) + assert_response :success + end + + test "should get edit" do + get edit_task_url(@task) + assert_response :success + end + + test "should update task" do + patch task_url(@task), params: { task: { body: @task.body, owner_id: @task.owner_id } } + assert_redirected_to task_url(@task) + end + + test "should destroy task" do + assert_difference('Task.count', -1) do + delete task_url(@task) + end + + assert_redirected_to tasks_url + end +end diff --git a/test/fixtures/tasks.yml b/test/fixtures/tasks.yml new file mode 100644 index 0000000..0b2ee31 --- /dev/null +++ b/test/fixtures/tasks.yml @@ -0,0 +1,9 @@ +# Read about fixtures at https://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html + +one: + owner: one + body: MyText + +two: + owner: two + body: MyText diff --git a/test/models/task_test.rb b/test/models/task_test.rb new file mode 100644 index 0000000..29982eb --- /dev/null +++ b/test/models/task_test.rb @@ -0,0 +1,7 @@ +require "test_helper" + +class TaskTest < ActiveSupport::TestCase + # test "the truth" do + # assert true + # end +end diff --git a/test/system/tasks_test.rb b/test/system/tasks_test.rb new file mode 100644 index 0000000..62aaea3 --- /dev/null +++ b/test/system/tasks_test.rb @@ -0,0 +1,45 @@ +require "application_system_test_case" + +class TasksTest < ApplicationSystemTestCase + setup do + @task = tasks(:one) + end + + test "visiting the index" do + visit tasks_url + assert_selector "h1", text: "Tasks" + end + + test "creating a Task" do + visit tasks_url + click_on "New Task" + + fill_in "Body", with: @task.body + fill_in "Owner", with: @task.owner_id + click_on "Create Task" + + assert_text "Task was successfully created" + click_on "Back" + end + + test "updating a Task" do + visit tasks_url + click_on "Edit", match: :first + + fill_in "Body", with: @task.body + fill_in "Owner", with: @task.owner_id + click_on "Update Task" + + assert_text "Task was successfully updated" + click_on "Back" + end + + test "destroying a Task" do + visit tasks_url + page.accept_confirm do + click_on "Destroy", match: :first + end + + assert_text "Task was successfully destroyed" + end +end From f2eda0279ed6ffd29173f8e0bd1be461880c09d7 Mon Sep 17 00:00:00 2001 From: uchicagopriya Date: Sun, 22 Jan 2023 02:40:37 +0000 Subject: [PATCH 04/25] Added default to status column in tasks --- app/models/task.rb | 20 +++++++ app/models/user.rb | 21 +++++++ .../20230122022537_add_status_to_tasks.rb | 5 ++ ...22023948_change_default_status_of_tasks.rb | 5 ++ db/schema.rb | 3 +- lib/tasks/auto_annotate_models.rake | 59 +++++++++++++++++++ test/fixtures/tasks.yml | 20 ++++++- test/fixtures/users.yml | 21 ++++++- test/models/task_test.rb | 19 ++++++ test/models/user_test.rb | 20 +++++++ 10 files changed, 190 insertions(+), 3 deletions(-) create mode 100644 db/migrate/20230122022537_add_status_to_tasks.rb create mode 100644 db/migrate/20230122023948_change_default_status_of_tasks.rb create mode 100644 lib/tasks/auto_annotate_models.rake diff --git a/app/models/task.rb b/app/models/task.rb index 50af7f4..489ab91 100644 --- a/app/models/task.rb +++ b/app/models/task.rb @@ -1,3 +1,23 @@ +# == Schema Information +# +# Table name: tasks +# +# id :bigint not null, primary key +# body :text +# status :string default("new") +# created_at :datetime not null +# updated_at :datetime not null +# owner_id :bigint not null +# +# Indexes +# +# index_tasks_on_owner_id (owner_id) +# +# Foreign Keys +# +# fk_rails_... (owner_id => users.id) +# class Task < ApplicationRecord belongs_to :owner, class_name: "User" + enum status: { new: "new", pending: "pending", complete: "complete" } end diff --git a/app/models/user.rb b/app/models/user.rb index 4756799..f5a6eb4 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -1,6 +1,27 @@ +# == Schema Information +# +# Table name: users +# +# id :bigint not null, primary key +# email :string default(""), not null +# encrypted_password :string default(""), not null +# remember_created_at :datetime +# reset_password_sent_at :datetime +# reset_password_token :string +# username :citext +# created_at :datetime not null +# updated_at :datetime not null +# +# Indexes +# +# index_users_on_email (email) UNIQUE +# index_users_on_reset_password_token (reset_password_token) UNIQUE +# index_users_on_username (username) UNIQUE +# class User < ApplicationRecord # Include default devise modules. Others available are: # :confirmable, :lockable, :timeoutable, :trackable and :omniauthable devise :database_authenticatable, :registerable, :recoverable, :rememberable, :validatable + has_many :tasks end diff --git a/db/migrate/20230122022537_add_status_to_tasks.rb b/db/migrate/20230122022537_add_status_to_tasks.rb new file mode 100644 index 0000000..c695c9e --- /dev/null +++ b/db/migrate/20230122022537_add_status_to_tasks.rb @@ -0,0 +1,5 @@ +class AddStatusToTasks < ActiveRecord::Migration[6.1] + def change + add_column :tasks, :status, :string + end +end diff --git a/db/migrate/20230122023948_change_default_status_of_tasks.rb b/db/migrate/20230122023948_change_default_status_of_tasks.rb new file mode 100644 index 0000000..b42fc30 --- /dev/null +++ b/db/migrate/20230122023948_change_default_status_of_tasks.rb @@ -0,0 +1,5 @@ +class ChangeDefaultStatusOfTasks < ActiveRecord::Migration[6.1] + def change + change_column_default :tasks, :status, "new" + end +end diff --git a/db/schema.rb b/db/schema.rb index 340da75..a8dbd89 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,7 +10,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 2023_01_22_015942) do +ActiveRecord::Schema.define(version: 2023_01_22_023948) do # These are extensions that must be enabled in order to support this database enable_extension "citext" @@ -21,6 +21,7 @@ t.text "body" t.datetime "created_at", precision: 6, null: false t.datetime "updated_at", precision: 6, null: false + t.string "status", default: "new" t.index ["owner_id"], name: "index_tasks_on_owner_id" end diff --git a/lib/tasks/auto_annotate_models.rake b/lib/tasks/auto_annotate_models.rake new file mode 100644 index 0000000..e96283e --- /dev/null +++ b/lib/tasks/auto_annotate_models.rake @@ -0,0 +1,59 @@ +# NOTE: only doing this in development as some production environments (Heroku) +# NOTE: are sensitive to local FS writes, and besides -- it's just not proper +# NOTE: to have a dev-mode tool do its thing in production. +if Rails.env.development? + require 'annotate' + task :set_annotation_options do + # You can override any of these by setting an environment variable of the + # same name. + Annotate.set_defaults( + 'active_admin' => 'false', + 'additional_file_patterns' => [], + 'routes' => 'false', + 'models' => 'true', + 'position_in_routes' => 'before', + 'position_in_class' => 'before', + 'position_in_test' => 'before', + 'position_in_fixture' => 'before', + 'position_in_factory' => 'before', + 'position_in_serializer' => 'before', + 'show_foreign_keys' => 'true', + 'show_complete_foreign_keys' => 'false', + 'show_indexes' => 'true', + 'simple_indexes' => 'false', + 'model_dir' => 'app/models', + 'root_dir' => '', + 'include_version' => 'false', + 'require' => '', + 'exclude_tests' => 'false', + 'exclude_fixtures' => 'false', + 'exclude_factories' => 'false', + 'exclude_serializers' => 'false', + 'exclude_scaffolds' => 'true', + 'exclude_controllers' => 'true', + 'exclude_helpers' => 'true', + 'exclude_sti_subclasses' => 'false', + 'ignore_model_sub_dir' => 'false', + 'ignore_columns' => nil, + 'ignore_routes' => nil, + 'ignore_unknown_models' => 'false', + 'hide_limit_column_types' => 'integer,bigint,boolean', + 'hide_default_column_types' => 'json,jsonb,hstore', + 'skip_on_db_migrate' => 'false', + 'format_bare' => 'true', + 'format_rdoc' => 'false', + 'format_yard' => 'false', + 'format_markdown' => 'false', + 'sort' => 'false', + 'force' => 'false', + 'frozen' => 'false', + 'classified_sort' => 'true', + 'trace' => 'false', + 'wrapper_open' => nil, + 'wrapper_close' => nil, + 'with_comment' => 'true' + ) + end + + Annotate.load_tasks +end diff --git a/test/fixtures/tasks.yml b/test/fixtures/tasks.yml index 0b2ee31..2912af4 100644 --- a/test/fixtures/tasks.yml +++ b/test/fixtures/tasks.yml @@ -1,4 +1,22 @@ -# Read about fixtures at https://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html +# == Schema Information +# +# Table name: tasks +# +# id :bigint not null, primary key +# body :text +# status :string default("new") +# created_at :datetime not null +# updated_at :datetime not null +# owner_id :bigint not null +# +# Indexes +# +# index_tasks_on_owner_id (owner_id) +# +# Foreign Keys +# +# fk_rails_... (owner_id => users.id) +# one: owner: one diff --git a/test/fixtures/users.yml b/test/fixtures/users.yml index 5181636..8cf19fc 100644 --- a/test/fixtures/users.yml +++ b/test/fixtures/users.yml @@ -1,4 +1,23 @@ -# Read about fixtures at https://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html +# == Schema Information +# +# Table name: users +# +# id :bigint not null, primary key +# email :string default(""), not null +# encrypted_password :string default(""), not null +# remember_created_at :datetime +# reset_password_sent_at :datetime +# reset_password_token :string +# username :citext +# created_at :datetime not null +# updated_at :datetime not null +# +# Indexes +# +# index_users_on_email (email) UNIQUE +# index_users_on_reset_password_token (reset_password_token) UNIQUE +# index_users_on_username (username) UNIQUE +# # This model initially had no columns defined. If you add columns to the # model remove the '{}' from the fixture names and add the columns immediately diff --git a/test/models/task_test.rb b/test/models/task_test.rb index 29982eb..8a4ef96 100644 --- a/test/models/task_test.rb +++ b/test/models/task_test.rb @@ -1,3 +1,22 @@ +# == Schema Information +# +# Table name: tasks +# +# id :bigint not null, primary key +# body :text +# status :string default("new") +# created_at :datetime not null +# updated_at :datetime not null +# owner_id :bigint not null +# +# Indexes +# +# index_tasks_on_owner_id (owner_id) +# +# Foreign Keys +# +# fk_rails_... (owner_id => users.id) +# require "test_helper" class TaskTest < ActiveSupport::TestCase diff --git a/test/models/user_test.rb b/test/models/user_test.rb index 5c07f49..d542a7c 100644 --- a/test/models/user_test.rb +++ b/test/models/user_test.rb @@ -1,3 +1,23 @@ +# == Schema Information +# +# Table name: users +# +# id :bigint not null, primary key +# email :string default(""), not null +# encrypted_password :string default(""), not null +# remember_created_at :datetime +# reset_password_sent_at :datetime +# reset_password_token :string +# username :citext +# created_at :datetime not null +# updated_at :datetime not null +# +# Indexes +# +# index_users_on_email (email) UNIQUE +# index_users_on_reset_password_token (reset_password_token) UNIQUE +# index_users_on_username (username) UNIQUE +# require "test_helper" class UserTest < ActiveSupport::TestCase From aeda0f26e94089994891b8de7fe849e535318f26 Mon Sep 17 00:00:00 2001 From: uchicagopriya Date: Sun, 22 Jan 2023 02:43:37 +0000 Subject: [PATCH 05/25] Fixed the default on the status column to notstarted --- app/models/task.rb | 4 ++-- db/migrate/20230122024241_change_default_status_on_tasks.rb | 5 +++++ db/schema.rb | 4 ++-- test/fixtures/tasks.yml | 2 +- test/models/task_test.rb | 2 +- 5 files changed, 11 insertions(+), 6 deletions(-) create mode 100644 db/migrate/20230122024241_change_default_status_on_tasks.rb diff --git a/app/models/task.rb b/app/models/task.rb index 489ab91..c15ddbc 100644 --- a/app/models/task.rb +++ b/app/models/task.rb @@ -4,7 +4,7 @@ # # id :bigint not null, primary key # body :text -# status :string default("new") +# status :string default("notstarted") # created_at :datetime not null # updated_at :datetime not null # owner_id :bigint not null @@ -19,5 +19,5 @@ # class Task < ApplicationRecord belongs_to :owner, class_name: "User" - enum status: { new: "new", pending: "pending", complete: "complete" } + enum status: { notstarted: "notstarted", pending: "pending", complete: "complete" } end diff --git a/db/migrate/20230122024241_change_default_status_on_tasks.rb b/db/migrate/20230122024241_change_default_status_on_tasks.rb new file mode 100644 index 0000000..bbbe4aa --- /dev/null +++ b/db/migrate/20230122024241_change_default_status_on_tasks.rb @@ -0,0 +1,5 @@ +class ChangeDefaultStatusOnTasks < ActiveRecord::Migration[6.1] + def change + change_column_default :tasks, :status, "notstarted" + end +end diff --git a/db/schema.rb b/db/schema.rb index a8dbd89..8320154 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,7 +10,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 2023_01_22_023948) do +ActiveRecord::Schema.define(version: 2023_01_22_024241) do # These are extensions that must be enabled in order to support this database enable_extension "citext" @@ -21,7 +21,7 @@ t.text "body" t.datetime "created_at", precision: 6, null: false t.datetime "updated_at", precision: 6, null: false - t.string "status", default: "new" + t.string "status", default: "notstarted" t.index ["owner_id"], name: "index_tasks_on_owner_id" end diff --git a/test/fixtures/tasks.yml b/test/fixtures/tasks.yml index 2912af4..c41ee43 100644 --- a/test/fixtures/tasks.yml +++ b/test/fixtures/tasks.yml @@ -4,7 +4,7 @@ # # id :bigint not null, primary key # body :text -# status :string default("new") +# status :string default("notstarted") # created_at :datetime not null # updated_at :datetime not null # owner_id :bigint not null diff --git a/test/models/task_test.rb b/test/models/task_test.rb index 8a4ef96..65a97f5 100644 --- a/test/models/task_test.rb +++ b/test/models/task_test.rb @@ -4,7 +4,7 @@ # # id :bigint not null, primary key # body :text -# status :string default("new") +# status :string default("notstarted") # created_at :datetime not null # updated_at :datetime not null # owner_id :bigint not null From 1915a2bd2a7dfdf84d66e9c1d34e4a03164d8c6a Mon Sep 17 00:00:00 2001 From: uchicagopriya Date: Sun, 22 Jan 2023 02:49:56 +0000 Subject: [PATCH 06/25] Added bootstrap and basic navbar --- app/views/layouts/application.html.erb | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/app/views/layouts/application.html.erb b/app/views/layouts/application.html.erb index dc2fe69..ee62e94 100644 --- a/app/views/layouts/application.html.erb +++ b/app/views/layouts/application.html.erb @@ -5,12 +5,29 @@ <%= csrf_meta_tags %> <%= csp_meta_tag %> - + <%= stylesheet_link_tag 'application', media: 'all', 'data-turbolinks-track': 'reload' %> <%= javascript_pack_tag 'application', 'data-turbolinks-track': 'reload' %> + +

<%= notice %>

<%= alert %>

<%= yield %> From 20f1ef139001059ab250bd0e37a87cddafb371ef Mon Sep 17 00:00:00 2001 From: uchicagopriya Date: Sun, 22 Jan 2023 21:50:55 +0000 Subject: [PATCH 07/25] Added sample data dev.rake task --- Gemfile | 1 + Gemfile.lock | 3 +++ app/models/task.rb | 3 +++ app/models/user.rb | 2 +- app/views/tasks/index.html.erb | 15 ++++++++++- lib/tasks/dev.rake | 48 ++++++++++++++++++++++++++++++++++ 6 files changed, 70 insertions(+), 2 deletions(-) create mode 100644 lib/tasks/dev.rake diff --git a/Gemfile b/Gemfile index 5caa086..f1cc7aa 100644 --- a/Gemfile +++ b/Gemfile @@ -57,6 +57,7 @@ gem "tzinfo-data", platforms: [:mingw, :mswin, :x64_mingw, :jruby] # appdev Gems group :development do + gem "faker", "~> 3.1" gem "devise", "~> 4.8", ">= 4.8.1" gem "annotate", "~> 3.2" gem "awesome_print" diff --git a/Gemfile.lock b/Gemfile.lock index b83b4bc..1ccf6cf 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -122,6 +122,8 @@ GEM diffy (3.4.2) e2mmap (0.1.0) erubi (1.11.0) + faker (3.1.0) + i18n (>= 1.8.11, < 2) ffi (1.15.5) git (1.12.0) addressable (~> 2.8) @@ -356,6 +358,7 @@ DEPENDENCIES byebug capybara (>= 3.26) devise (~> 4.8, >= 4.8.1) + faker (~> 3.1) htmlbeautifier jbuilder (~> 2.7) listen (~> 3.3) diff --git a/app/models/task.rb b/app/models/task.rb index c15ddbc..354b83d 100644 --- a/app/models/task.rb +++ b/app/models/task.rb @@ -20,4 +20,7 @@ class Task < ApplicationRecord belongs_to :owner, class_name: "User" enum status: { notstarted: "notstarted", pending: "pending", complete: "complete" } + scope :notstarted, -> { where(status: "notstarted") } + scope :pending, -> { where(status: "pending") } + scope :complete, -> { where(status: "complete") } end diff --git a/app/models/user.rb b/app/models/user.rb index f5a6eb4..eda69a3 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -23,5 +23,5 @@ class User < ApplicationRecord # :confirmable, :lockable, :timeoutable, :trackable and :omniauthable devise :database_authenticatable, :registerable, :recoverable, :rememberable, :validatable - has_many :tasks + has_many :tasks, foreign_key: :owner_id end diff --git a/app/views/tasks/index.html.erb b/app/views/tasks/index.html.erb index fffc5ba..153a5b0 100644 --- a/app/views/tasks/index.html.erb +++ b/app/views/tasks/index.html.erb @@ -1,7 +1,20 @@

<%= notice %>

-

Tasks

+
+
+
+ Not yet started +
+
+ In progress +
+
+ Completed +
+
+
+ diff --git a/lib/tasks/dev.rake b/lib/tasks/dev.rake new file mode 100644 index 0000000..1d2e600 --- /dev/null +++ b/lib/tasks/dev.rake @@ -0,0 +1,48 @@ +desc "Fill the database tables with some sample data" +task sample_data: :environment do + starting = Time.now + + Task.delete_all + User.delete_all + + people = Array.new(10) do + { + first_name: Faker::Name.first_name, + last_name: Faker::Name.last_name, + } + end + + people << { first_name: "Alice", last_name: "Smith" } + people << { first_name: "Bob", last_name: "Smith" } + people << { first_name: "Carol", last_name: "Smith" } + people << { first_name: "Doug", last_name: "Smith" } + + people.each do |person| + username = person.fetch(:first_name).downcase + + user = User.create( + email: "#{username}@example.com", + password: "password", + username: username.downcase, + ) + + p user.errors.full_messages + end + + users = User.all + + users.each do |user| + rand(15).times do + a_task = user.tasks.create( + body: Faker::Quote.jack_handey, + status: ["notstarted", "pending", "complete"].sample, + ) + p a_task.errors.full_messages + end + end + + ending = Time.now + p "It took #{(ending - starting).to_i} seconds to create sample data." + p "There are now #{User.count} users." + p "There are now #{Task.count} tasks." +end From 333796b9ae430c99b6d477beef0d893b758310ff Mon Sep 17 00:00:00 2001 From: uchicagopriya Date: Sun, 22 Jan 2023 21:52:53 +0000 Subject: [PATCH 08/25] Force sign-in --- app/controllers/application_controller.rb | 1 + 1 file changed, 1 insertion(+) diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 09705d1..6b4dcfa 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -1,2 +1,3 @@ class ApplicationController < ActionController::Base + before_action :authenticate_user! end From d0d15a65a5b0159981ecc13ca7d848422f62abb3 Mon Sep 17 00:00:00 2001 From: uchicagopriya Date: Sun, 22 Jan 2023 22:02:18 +0000 Subject: [PATCH 09/25] Only show tasks owned by current user on index --- app/controllers/tasks_controller.rb | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/app/controllers/tasks_controller.rb b/app/controllers/tasks_controller.rb index 034cd1b..0c12f5c 100644 --- a/app/controllers/tasks_controller.rb +++ b/app/controllers/tasks_controller.rb @@ -3,7 +3,7 @@ class TasksController < ApplicationController # GET /tasks or /tasks.json def index - @tasks = Task.all + @tasks = current_user.tasks end # GET /tasks/1 or /tasks/1.json @@ -58,13 +58,14 @@ def destroy end private - # Use callbacks to share common setup or constraints between actions. - def set_task - @task = Task.find(params[:id]) - end - # Only allow a list of trusted parameters through. - def task_params - params.require(:task).permit(:owner_id, :body) - end + # Use callbacks to share common setup or constraints between actions. + def set_task + @task = Task.find(params[:id]) + end + + # Only allow a list of trusted parameters through. + def task_params + params.require(:task).permit(:owner_id, :body) + end end From 42c3fae4d71f4d801282255df338cfe94708a56b Mon Sep 17 00:00:00 2001 From: uchicagopriya Date: Sun, 22 Jan 2023 22:07:12 +0000 Subject: [PATCH 10/25] Updated the nav bar --- app/views/layouts/application.html.erb | 20 ++++++++------ app/views/tasks/index.html.erb | 36 ++++++++++---------------- 2 files changed, 25 insertions(+), 31 deletions(-) diff --git a/app/views/layouts/application.html.erb b/app/views/layouts/application.html.erb index ee62e94..ad1d8e7 100644 --- a/app/views/layouts/application.html.erb +++ b/app/views/layouts/application.html.erb @@ -10,20 +10,24 @@ <%= javascript_pack_tag 'application', 'data-turbolinks-track': 'reload' %> - -
- - - - - - - - - - <% @tasks.each do |task| %> - - - - - - - - <% end %> - -
OwnerBody
<%= task.owner_id %><%= task.body %><%= link_to 'Show', task %><%= link_to 'Edit', edit_task_path(task) %><%= link_to 'Destroy', task, method: :delete, data: { confirm: 'Are you sure?' } %>
+ + + <% @tasks.each do |task| %> + + + + + + + <% end %> + +
<%= task.body %><%= link_to 'Show', task %><%= link_to 'Edit', edit_task_path(task) %><%= link_to 'Destroy', task, method: :delete, data: { confirm: 'Are you sure?' } %>
+
<%= link_to 'New Task', new_task_path %> From f98826fb6ad184db2d151de2837791cde4b342c6 Mon Sep 17 00:00:00 2001 From: uchicagopriya Date: Sun, 22 Jan 2023 22:43:19 +0000 Subject: [PATCH 11/25] Set up index page with three columns --- app/views/tasks/_task.html.erb | 3 ++ app/views/tasks/index.html.erb | 85 +++++++++++++++++++++++++++------- 2 files changed, 70 insertions(+), 18 deletions(-) create mode 100644 app/views/tasks/_task.html.erb diff --git a/app/views/tasks/_task.html.erb b/app/views/tasks/_task.html.erb new file mode 100644 index 0000000..340e86d --- /dev/null +++ b/app/views/tasks/_task.html.erb @@ -0,0 +1,3 @@ +

+this is a task +

diff --git a/app/views/tasks/index.html.erb b/app/views/tasks/index.html.erb index c46ca2f..a5b6608 100644 --- a/app/views/tasks/index.html.erb +++ b/app/views/tasks/index.html.erb @@ -1,32 +1,81 @@ -

<%= notice %>

-

Tasks

+<%= link_to 'New Task', new_task_path %> +
+
+
+
+ Not yet started +
+
    + <% @tasks.notstarted.each do |task| %> + <%= render "tasks/task", task: task %> + <% end %> +
+
+
+ +
+
+
+ In progress +
+
    + <% @tasks.notstarted.each do |task| %> + <%= render "tasks/task", task: task %> + <% end %> +
+
+
+ +
+
+
+ Completed +
+
    + <% @tasks.notstarted.each do |task| %> + <%= render "tasks/task", task: task %> + <% end %> +
+
+
+
+#
+ #
+ #
- Not yet started -
+ # Not yet started + #
+ #
- In progress -
+ # In progress + #
+ #
- Completed -
- + # Completed + # + # + # + # - <% @tasks.each do |task| %> + # <% @tasks.each do |task| %> + # + # + # + # + # - - <% end %> - -
<%= task.body %><%= link_to 'Show', task %><%= link_to 'Edit', edit_task_path(task) %><%= link_to 'Destroy', task, method: :delete, data: { confirm: 'Are you sure?' } %>
- -
- -<%= link_to 'New Task', new_task_path %> + # + # <% end %> + # + # + # +#
From b00dce6d1b2e7c078d6d2757a8243c506c6834a6 Mon Sep 17 00:00:00 2001 From: uchicagopriya Date: Sun, 22 Jan 2023 22:48:30 +0000 Subject: [PATCH 12/25] Set up three task partials --- app/views/tasks/_completedtask.html.erb | 4 +++ app/views/tasks/_newtask.html.erb | 4 +++ app/views/tasks/_pendingtask.html.erb | 4 +++ app/views/tasks/_task.html.erb | 3 -- app/views/tasks/index.html.erb | 48 +++---------------------- 5 files changed, 16 insertions(+), 47 deletions(-) create mode 100644 app/views/tasks/_completedtask.html.erb create mode 100644 app/views/tasks/_newtask.html.erb create mode 100644 app/views/tasks/_pendingtask.html.erb delete mode 100644 app/views/tasks/_task.html.erb diff --git a/app/views/tasks/_completedtask.html.erb b/app/views/tasks/_completedtask.html.erb new file mode 100644 index 0000000..6161485 --- /dev/null +++ b/app/views/tasks/_completedtask.html.erb @@ -0,0 +1,4 @@ +<%= task.body %> +<%= link_to 'Show', task %> +<%= link_to 'Edit', edit_task_path(task) %> +<%= link_to 'Destroy', task, method: :delete, data: { confirm: 'Are you sure?' } %> diff --git a/app/views/tasks/_newtask.html.erb b/app/views/tasks/_newtask.html.erb new file mode 100644 index 0000000..6161485 --- /dev/null +++ b/app/views/tasks/_newtask.html.erb @@ -0,0 +1,4 @@ +<%= task.body %> +<%= link_to 'Show', task %> +<%= link_to 'Edit', edit_task_path(task) %> +<%= link_to 'Destroy', task, method: :delete, data: { confirm: 'Are you sure?' } %> diff --git a/app/views/tasks/_pendingtask.html.erb b/app/views/tasks/_pendingtask.html.erb new file mode 100644 index 0000000..6161485 --- /dev/null +++ b/app/views/tasks/_pendingtask.html.erb @@ -0,0 +1,4 @@ +<%= task.body %> +<%= link_to 'Show', task %> +<%= link_to 'Edit', edit_task_path(task) %> +<%= link_to 'Destroy', task, method: :delete, data: { confirm: 'Are you sure?' } %> diff --git a/app/views/tasks/_task.html.erb b/app/views/tasks/_task.html.erb deleted file mode 100644 index 340e86d..0000000 --- a/app/views/tasks/_task.html.erb +++ /dev/null @@ -1,3 +0,0 @@ -

-this is a task -

diff --git a/app/views/tasks/index.html.erb b/app/views/tasks/index.html.erb index a5b6608..6191de8 100644 --- a/app/views/tasks/index.html.erb +++ b/app/views/tasks/index.html.erb @@ -1,14 +1,13 @@ -<%= link_to 'New Task', new_task_path %> -
Not yet started
+ <%= link_to 'New Task', new_task_path %>
    <% @tasks.notstarted.each do |task| %> - <%= render "tasks/task", task: task %> + <%= render "tasks/newtask", task: task %> <% end %>
@@ -21,7 +20,7 @@
    <% @tasks.notstarted.each do |task| %> - <%= render "tasks/task", task: task %> + <%= render "tasks/pendingtask", task: task %> <% end %>
@@ -34,48 +33,9 @@
    <% @tasks.notstarted.each do |task| %> - <%= render "tasks/task", task: task %> + <%= render "tasks/completedtask", task: task %> <% end %>
-# -
- # -
- # -
- # Not yet started - #
- # -
- # In progress - #
- # -
- # Completed - #
- #
- # - - - # - - # <% @tasks.each do |task| %> - # - - # - - # - - # - - # - - # - # <% end %> - # - #
<%= task.body %><%= link_to 'Show', task %><%= link_to 'Edit', edit_task_path(task) %><%= link_to 'Destroy', task, method: :delete, data: { confirm: 'Are you sure?' } %>
- #
-#
From a75cf1714b664511f4914914b67f3b4273b97d7c Mon Sep 17 00:00:00 2001 From: uchicagopriya Date: Sun, 22 Jan 2023 23:38:22 +0000 Subject: [PATCH 13/25] Updated formatting of actions --- app/controllers/tasks_controller.rb | 3 +++ app/views/tasks/_completedtask.html.erb | 4 ---- app/views/tasks/_newtask.html.erb | 4 ---- app/views/tasks/_pendingtask.html.erb | 4 ---- app/views/tasks/_task.html.erb | 20 ++++++++++++++++++++ app/views/tasks/edit.html.erb | 1 - app/views/tasks/forward.html.erb | 0 app/views/tasks/index.html.erb | 10 +++++----- 8 files changed, 28 insertions(+), 18 deletions(-) delete mode 100644 app/views/tasks/_completedtask.html.erb delete mode 100644 app/views/tasks/_newtask.html.erb delete mode 100644 app/views/tasks/_pendingtask.html.erb create mode 100644 app/views/tasks/_task.html.erb create mode 100644 app/views/tasks/forward.html.erb diff --git a/app/controllers/tasks_controller.rb b/app/controllers/tasks_controller.rb index 0c12f5c..558def3 100644 --- a/app/controllers/tasks_controller.rb +++ b/app/controllers/tasks_controller.rb @@ -10,6 +10,9 @@ def index def show end + def forward + end + # GET /tasks/new def new @task = Task.new diff --git a/app/views/tasks/_completedtask.html.erb b/app/views/tasks/_completedtask.html.erb deleted file mode 100644 index 6161485..0000000 --- a/app/views/tasks/_completedtask.html.erb +++ /dev/null @@ -1,4 +0,0 @@ -<%= task.body %> -<%= link_to 'Show', task %> -<%= link_to 'Edit', edit_task_path(task) %> -<%= link_to 'Destroy', task, method: :delete, data: { confirm: 'Are you sure?' } %> diff --git a/app/views/tasks/_newtask.html.erb b/app/views/tasks/_newtask.html.erb deleted file mode 100644 index 6161485..0000000 --- a/app/views/tasks/_newtask.html.erb +++ /dev/null @@ -1,4 +0,0 @@ -<%= task.body %> -<%= link_to 'Show', task %> -<%= link_to 'Edit', edit_task_path(task) %> -<%= link_to 'Destroy', task, method: :delete, data: { confirm: 'Are you sure?' } %> diff --git a/app/views/tasks/_pendingtask.html.erb b/app/views/tasks/_pendingtask.html.erb deleted file mode 100644 index 6161485..0000000 --- a/app/views/tasks/_pendingtask.html.erb +++ /dev/null @@ -1,4 +0,0 @@ -<%= task.body %> -<%= link_to 'Show', task %> -<%= link_to 'Edit', edit_task_path(task) %> -<%= link_to 'Destroy', task, method: :delete, data: { confirm: 'Are you sure?' } %> diff --git a/app/views/tasks/_task.html.erb b/app/views/tasks/_task.html.erb new file mode 100644 index 0000000..a098bf9 --- /dev/null +++ b/app/views/tasks/_task.html.erb @@ -0,0 +1,20 @@ +<%= task.body %> +
+
+
+ <%= link_to 'Show', task %> +
+
+
+
+ <%= link_to edit_task_path(task), class: "btn btn-link btn-sm text-muted" do %> + + <% end %> +
+
+
+
+ <%= link_to 'Destroy', task, method: :delete, data: { confirm: 'Are you sure?' } %> +
+
+
diff --git a/app/views/tasks/edit.html.erb b/app/views/tasks/edit.html.erb index 7b088fb..a16fbdc 100644 --- a/app/views/tasks/edit.html.erb +++ b/app/views/tasks/edit.html.erb @@ -1,6 +1,5 @@

Editing Task

<%= render 'form', task: @task %> - <%= link_to 'Show', @task %> | <%= link_to 'Back', tasks_path %> diff --git a/app/views/tasks/forward.html.erb b/app/views/tasks/forward.html.erb new file mode 100644 index 0000000..e69de29 diff --git a/app/views/tasks/index.html.erb b/app/views/tasks/index.html.erb index 6191de8..6b2781d 100644 --- a/app/views/tasks/index.html.erb +++ b/app/views/tasks/index.html.erb @@ -7,7 +7,7 @@ <%= link_to 'New Task', new_task_path %>
    <% @tasks.notstarted.each do |task| %> - <%= render "tasks/newtask", task: task %> + <%= render "tasks/task", task: task %> <% end %>
@@ -19,8 +19,8 @@ In progress
    - <% @tasks.notstarted.each do |task| %> - <%= render "tasks/pendingtask", task: task %> + <% @tasks.pending.each do |task| %> + <%= render "tasks/task", task: task %> <% end %>
@@ -32,8 +32,8 @@ Completed
    - <% @tasks.notstarted.each do |task| %> - <%= render "tasks/completedtask", task: task %> + <% @tasks.complete.each do |task| %> + <%= render "tasks/task", task: task %> <% end %>
From c0304963a53a8ca24e83dfdc0f8fbc9bc461ea9a Mon Sep 17 00:00:00 2001 From: uchicagopriya Date: Mon, 23 Jan 2023 04:37:21 +0000 Subject: [PATCH 14/25] Added FontAwesome and some data validations --- app/models/task.rb | 1 + app/models/user.rb | 2 +- app/views/layouts/application.html.erb | 17 ++++++++++++++--- app/views/tasks/_task.html.erb | 4 +--- 4 files changed, 17 insertions(+), 7 deletions(-) diff --git a/app/models/task.rb b/app/models/task.rb index 354b83d..cc7c2f0 100644 --- a/app/models/task.rb +++ b/app/models/task.rb @@ -23,4 +23,5 @@ class Task < ApplicationRecord scope :notstarted, -> { where(status: "notstarted") } scope :pending, -> { where(status: "pending") } scope :complete, -> { where(status: "complete") } + validates :body, presence: true end diff --git a/app/models/user.rb b/app/models/user.rb index eda69a3..2454637 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -23,5 +23,5 @@ class User < ApplicationRecord # :confirmable, :lockable, :timeoutable, :trackable and :omniauthable devise :database_authenticatable, :registerable, :recoverable, :rememberable, :validatable - has_many :tasks, foreign_key: :owner_id + has_many :tasks, foreign_key: :owner_id, dependent: :destroy end diff --git a/app/views/layouts/application.html.erb b/app/views/layouts/application.html.erb index ad1d8e7..b52e2a7 100644 --- a/app/views/layouts/application.html.erb +++ b/app/views/layouts/application.html.erb @@ -1,11 +1,14 @@ - Vanilla Rails + + My Tasks <%= csrf_meta_tags %> <%= csp_meta_tag %> + + <%= stylesheet_link_tag 'application', media: 'all', 'data-turbolinks-track': 'reload' %> <%= javascript_pack_tag 'application', 'data-turbolinks-track': 'reload' %> @@ -32,8 +35,16 @@ -

<%= notice %>

-

<%= alert %>

+ <% if notice.present? %> + + <% end %> + <% if alert.present? %> + + <% end %> <%= yield %> diff --git a/app/views/tasks/_task.html.erb b/app/views/tasks/_task.html.erb index a098bf9..53413eb 100644 --- a/app/views/tasks/_task.html.erb +++ b/app/views/tasks/_task.html.erb @@ -7,9 +7,7 @@
- <%= link_to edit_task_path(task), class: "btn btn-link btn-sm text-muted" do %> - - <% end %> + <%= link_to 'Edit', edit_task_path(task) %>
From 44a306526a9d1e6526936de243414cfcb0f81881 Mon Sep 17 00:00:00 2001 From: uchicagopriya Date: Mon, 23 Jan 2023 04:44:25 +0000 Subject: [PATCH 15/25] Updated sample data dev.rake task --- lib/tasks/dev.rake | 30 +++++++++++++++++------------- 1 file changed, 17 insertions(+), 13 deletions(-) diff --git a/lib/tasks/dev.rake b/lib/tasks/dev.rake index 1d2e600..b517eed 100644 --- a/lib/tasks/dev.rake +++ b/lib/tasks/dev.rake @@ -5,13 +5,7 @@ task sample_data: :environment do Task.delete_all User.delete_all - people = Array.new(10) do - { - first_name: Faker::Name.first_name, - last_name: Faker::Name.last_name, - } - end - + people = Array.new people << { first_name: "Alice", last_name: "Smith" } people << { first_name: "Bob", last_name: "Smith" } people << { first_name: "Carol", last_name: "Smith" } @@ -31,13 +25,23 @@ task sample_data: :environment do users = User.all + # users.each do |user| + # rand(15).times do + # a_task = user.tasks.create( + # body: Faker::Quote.jack_handey, + # status: ["notstarted", "pending", "complete"].sample, + # ) + # p a_task.errors.full_messages + # end + # end users.each do |user| - rand(15).times do - a_task = user.tasks.create( - body: Faker::Quote.jack_handey, - status: ["notstarted", "pending", "complete"].sample, - ) - p a_task.errors.full_messages + Task.statuses.values.each do |status| + 10.times do + user.tasks.create( + body: Faker::Quote.jack_handey, + status: status, + ) + end end end From 38974d73d4cd2ebe95aead4fa714fee5a9cde9b3 Mon Sep 17 00:00:00 2001 From: uchicagopriya Date: Mon, 23 Jan 2023 04:53:33 +0000 Subject: [PATCH 16/25] Changed edit link to icon --- app/views/tasks/_task.html.erb | 35 ++++++++++++++++++++-------------- app/views/tasks/index.html.erb | 4 ++-- 2 files changed, 23 insertions(+), 16 deletions(-) diff --git a/app/views/tasks/_task.html.erb b/app/views/tasks/_task.html.erb index 53413eb..406d38a 100644 --- a/app/views/tasks/_task.html.erb +++ b/app/views/tasks/_task.html.erb @@ -1,18 +1,25 @@ -<%= task.body %> -
-
-
- <%= link_to 'Show', task %> +
  • + <%= task.body %> + +
    +
    +
    + <%= link_to 'Show', task %> +
    -
    -
    -
    - <%= link_to 'Edit', edit_task_path(task) %> + +
    +
    + <%= link_to edit_task_path(task), class: "btn btn-link" do %> + + <% end %> +
    -
    -
    -
    - <%= link_to 'Destroy', task, method: :delete, data: { confirm: 'Are you sure?' } %> + +
    +
    + <%= link_to 'Destroy', task, method: :delete, data: { confirm: 'Are you sure?' } %> +
    -
    +
  • diff --git a/app/views/tasks/index.html.erb b/app/views/tasks/index.html.erb index 6b2781d..5d4b74d 100644 --- a/app/views/tasks/index.html.erb +++ b/app/views/tasks/index.html.erb @@ -5,7 +5,7 @@ Not yet started
    <%= link_to 'New Task', new_task_path %> -
      +
        <% @tasks.notstarted.each do |task| %> <%= render "tasks/task", task: task %> <% end %> @@ -31,7 +31,7 @@
        Completed
        -
          +
            <% @tasks.complete.each do |task| %> <%= render "tasks/task", task: task %> <% end %> From f323e9e2e0af36472907d86c15dcdd8b05675a06 Mon Sep 17 00:00:00 2001 From: uchicagopriya Date: Mon, 23 Jan 2023 04:59:33 +0000 Subject: [PATCH 17/25] Changed links on task partial to icons in link_to --- app/views/tasks/_task.html.erb | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/app/views/tasks/_task.html.erb b/app/views/tasks/_task.html.erb index 406d38a..4035601 100644 --- a/app/views/tasks/_task.html.erb +++ b/app/views/tasks/_task.html.erb @@ -4,7 +4,9 @@
            - <%= link_to 'Show', task %> + <%= link_to task_path(task), class: "btn btn-link" do %> + + <% end %>
            @@ -18,7 +20,9 @@
            - <%= link_to 'Destroy', task, method: :delete, data: { confirm: 'Are you sure?' } %> + <%= link_to task, method: :delete, data: { confirm: 'Delete?'}, class: "btn btn-link" do %> + + <% end %>
            From a5f0665282467a0145e7e74003fa1d3f62a4472f Mon Sep 17 00:00:00 2001 From: uchicagopriya Date: Mon, 23 Jan 2023 05:29:27 +0000 Subject: [PATCH 18/25] Added move route and action --- app/controllers/tasks_controller.rb | 19 +++++++++++++++---- app/views/tasks/_task.html.erb | 10 ++++++++-- config/routes.rb | 5 ++++- 3 files changed, 27 insertions(+), 7 deletions(-) diff --git a/app/controllers/tasks_controller.rb b/app/controllers/tasks_controller.rb index 558def3..3c395ed 100644 --- a/app/controllers/tasks_controller.rb +++ b/app/controllers/tasks_controller.rb @@ -1,5 +1,19 @@ class TasksController < ApplicationController - before_action :set_task, only: %i[ show edit update destroy ] + before_action :set_task, only: %i[ show edit update destroy move ] + + def move + if @task.notstarted? + @task.pending! + elsif @task.pending? + @task.complete! + else + @task.pending! + end + + respond_to do |format| + format.html { redirect_to tasks_url, notice: "Task status updated" } + end + end # GET /tasks or /tasks.json def index @@ -10,9 +24,6 @@ def index def show end - def forward - end - # GET /tasks/new def new @task = Task.new diff --git a/app/views/tasks/_task.html.erb b/app/views/tasks/_task.html.erb index 4035601..468adeb 100644 --- a/app/views/tasks/_task.html.erb +++ b/app/views/tasks/_task.html.erb @@ -4,8 +4,14 @@
            - <%= link_to task_path(task), class: "btn btn-link" do %> - + <%= link_to move_task_path(task), class: "btn btn-link", method: :patch, remote: true do %> + <% if task.notstarted? %> + + <% elsif task.pending? %> + + <% else %> + + <% end %> <% end %>
            diff --git a/config/routes.rb b/config/routes.rb index 5abab18..de18035 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -1,5 +1,8 @@ Rails.application.routes.draw do - resources :tasks + resources :tasks do + patch "move", on: :member + end + devise_for :users # For details on the DSL available within this file, see https://guides.rubyonrails.org/routing.html root "tasks#index" From 45a4f27c8750c8c6c3fa7055efc02bd6bf9741e9 Mon Sep 17 00:00:00 2001 From: uchicagopriya Date: Mon, 23 Jan 2023 05:33:43 +0000 Subject: [PATCH 19/25] Added format.js to controller actions --- app/controllers/tasks_controller.rb | 6 ++++++ app/views/layouts/application.html.erb | 2 ++ 2 files changed, 8 insertions(+) diff --git a/app/controllers/tasks_controller.rb b/app/controllers/tasks_controller.rb index 3c395ed..341b734 100644 --- a/app/controllers/tasks_controller.rb +++ b/app/controllers/tasks_controller.rb @@ -12,6 +12,7 @@ def move respond_to do |format| format.html { redirect_to tasks_url, notice: "Task status updated" } + format.js end end @@ -41,9 +42,11 @@ def create if @task.save format.html { redirect_to task_url(@task), notice: "Task was successfully created." } format.json { render :show, status: :created, location: @task } + format.js else format.html { render :new, status: :unprocessable_entity } format.json { render json: @task.errors, status: :unprocessable_entity } + format.js end end end @@ -54,9 +57,11 @@ def update if @task.update(task_params) format.html { redirect_to task_url(@task), notice: "Task was successfully updated." } format.json { render :show, status: :ok, location: @task } + format.js else format.html { render :edit, status: :unprocessable_entity } format.json { render json: @task.errors, status: :unprocessable_entity } + format.js end end end @@ -68,6 +73,7 @@ def destroy respond_to do |format| format.html { redirect_to tasks_url, notice: "Task was successfully destroyed." } format.json { head :no_content } + format.js end end diff --git a/app/views/layouts/application.html.erb b/app/views/layouts/application.html.erb index b52e2a7..a4b16b1 100644 --- a/app/views/layouts/application.html.erb +++ b/app/views/layouts/application.html.erb @@ -9,8 +9,10 @@ + <%= stylesheet_link_tag 'application', media: 'all', 'data-turbolinks-track': 'reload' %> <%= javascript_pack_tag 'application', 'data-turbolinks-track': 'reload' %> + From d4093abccd4b757937c6cf8f3d4e06ddced9313b Mon Sep 17 00:00:00 2001 From: uchicagopriya Date: Tue, 24 Jan 2023 00:24:36 +0000 Subject: [PATCH 20/25] Ajax-ified delete task route --- app/controllers/tasks_controller.rb | 4 +++- app/views/tasks/_task.html.erb | 2 +- app/views/tasks/destroy.js.erb | 3 +++ config/routes.rb | 2 +- 4 files changed, 8 insertions(+), 3 deletions(-) create mode 100644 app/views/tasks/destroy.js.erb diff --git a/app/controllers/tasks_controller.rb b/app/controllers/tasks_controller.rb index 341b734..71782c5 100644 --- a/app/controllers/tasks_controller.rb +++ b/app/controllers/tasks_controller.rb @@ -73,7 +73,9 @@ def destroy respond_to do |format| format.html { redirect_to tasks_url, notice: "Task was successfully destroyed." } format.json { head :no_content } - format.js + format.js do + render template: "tasks/destroy.js.erb" + end end end diff --git a/app/views/tasks/_task.html.erb b/app/views/tasks/_task.html.erb index 468adeb..c0c3e79 100644 --- a/app/views/tasks/_task.html.erb +++ b/app/views/tasks/_task.html.erb @@ -26,7 +26,7 @@
            - <%= link_to task, method: :delete, data: { confirm: 'Delete?'}, class: "btn btn-link" do %> + <%= link_to task, method: :delete, data: { confirm: 'Delete?'}, class: "btn btn-link", remote: true do %> <% end %>
            diff --git a/app/views/tasks/destroy.js.erb b/app/views/tasks/destroy.js.erb new file mode 100644 index 0000000..27912af --- /dev/null +++ b/app/views/tasks/destroy.js.erb @@ -0,0 +1,3 @@ +$("#<%= dom_id(@task) %>").fadeOut(50, function() { +$(this).remove(); +}); diff --git a/config/routes.rb b/config/routes.rb index de18035..b948579 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -2,7 +2,7 @@ resources :tasks do patch "move", on: :member end - + devise_for :users # For details on the DSL available within this file, see https://guides.rubyonrails.org/routing.html root "tasks#index" From 69be1714d278a0a24a6316ed74a94b7369fc74ee Mon Sep 17 00:00:00 2001 From: uchicagopriya Date: Tue, 24 Jan 2023 00:52:19 +0000 Subject: [PATCH 21/25] Ajax-ified edit and update task routes --- app/controllers/tasks_controller.rb | 10 ++++++++- app/views/tasks/_form.html.erb | 34 +++++++++-------------------- app/views/tasks/_task.html.erb | 2 +- app/views/tasks/edit.js.erb | 1 + app/views/tasks/update.js.erb | 1 + 5 files changed, 22 insertions(+), 26 deletions(-) create mode 100644 app/views/tasks/edit.js.erb create mode 100644 app/views/tasks/update.js.erb diff --git a/app/controllers/tasks_controller.rb b/app/controllers/tasks_controller.rb index 71782c5..a1bcf4e 100644 --- a/app/controllers/tasks_controller.rb +++ b/app/controllers/tasks_controller.rb @@ -32,6 +32,12 @@ def new # GET /tasks/1/edit def edit + respond_to do |format| + format.html + format.js do + render template: "tasks/edit.js.erb" + end + end end # POST /tasks or /tasks.json @@ -57,7 +63,9 @@ def update if @task.update(task_params) format.html { redirect_to task_url(@task), notice: "Task was successfully updated." } format.json { render :show, status: :ok, location: @task } - format.js + format.js do + render template: "tasks/update.js.erb" + end else format.html { render :edit, status: :unprocessable_entity } format.json { render json: @task.errors, status: :unprocessable_entity } diff --git a/app/views/tasks/_form.html.erb b/app/views/tasks/_form.html.erb index acbce4e..50ef7df 100644 --- a/app/views/tasks/_form.html.erb +++ b/app/views/tasks/_form.html.erb @@ -1,27 +1,13 @@ -<%= form_with(model: task) do |form| %> - <% if task.errors.any? %> -
            -

            <%= pluralize(task.errors.count, "error") %> prohibited this task from being saved:

            - -
              - <% task.errors.each do |error| %> -
            • <%= error.full_message %>
            • - <% end %> -
            +
          • + <%= form_with(model: task, local: false) do |form| %> +
            + <%= form.label :body, class: "visually-hidden" %> + <%= form.text_field :body, class: "form-control" %>
            - <% end %> - -
            - <%= form.label :owner_id %> - <%= form.text_field :owner_id %> -
            -
            - <%= form.label :body %> - <%= form.text_area :body %> -
            +
            + <%= form.submit class: "btn btn-outline-warning" %> +
            -
            - <%= form.submit %> -
            -<% end %> + <% end %> +
          • diff --git a/app/views/tasks/_task.html.erb b/app/views/tasks/_task.html.erb index c0c3e79..d389271 100644 --- a/app/views/tasks/_task.html.erb +++ b/app/views/tasks/_task.html.erb @@ -18,7 +18,7 @@
            - <%= link_to edit_task_path(task), class: "btn btn-link" do %> + <%= link_to edit_task_path(task), class: "btn btn-link", remote: true do %> <% end %>
            diff --git a/app/views/tasks/edit.js.erb b/app/views/tasks/edit.js.erb new file mode 100644 index 0000000..50f083e --- /dev/null +++ b/app/views/tasks/edit.js.erb @@ -0,0 +1 @@ +$("#<%= dom_id @task %>").replaceWith("<%= j(render "tasks/form", task: @task)%>"); diff --git a/app/views/tasks/update.js.erb b/app/views/tasks/update.js.erb new file mode 100644 index 0000000..fea4e90 --- /dev/null +++ b/app/views/tasks/update.js.erb @@ -0,0 +1 @@ +$("#<%= dom_id @task, :edit %>").replaceWith("<%= j(render @task)%>"); From ccb8c2dac6f6731e478f226bbbeace97abcf8e21 Mon Sep 17 00:00:00 2001 From: uchicagopriya Date: Tue, 24 Jan 2023 00:58:50 +0000 Subject: [PATCH 22/25] Ajax-ified move task route --- app/controllers/tasks_controller.rb | 4 +++- app/views/tasks/move.js.erb | 3 +++ 2 files changed, 6 insertions(+), 1 deletion(-) create mode 100644 app/views/tasks/move.js.erb diff --git a/app/controllers/tasks_controller.rb b/app/controllers/tasks_controller.rb index a1bcf4e..9a13d3e 100644 --- a/app/controllers/tasks_controller.rb +++ b/app/controllers/tasks_controller.rb @@ -12,7 +12,9 @@ def move respond_to do |format| format.html { redirect_to tasks_url, notice: "Task status updated" } - format.js + format.js do + render template: "tasks/move.js.erb" + end end end diff --git a/app/views/tasks/move.js.erb b/app/views/tasks/move.js.erb new file mode 100644 index 0000000..96ac7d9 --- /dev/null +++ b/app/views/tasks/move.js.erb @@ -0,0 +1,3 @@ +$("#<%= dom_id @task %>").remove(); +var moved_task = $("<%= j(render @task) %>"); +$("#<%= @task.status%>_list").prepend(moved_task); From 87de66685ce1757919a5cbbabfb09282babdbce2 Mon Sep 17 00:00:00 2001 From: uchicagopriya Date: Tue, 24 Jan 2023 01:12:10 +0000 Subject: [PATCH 23/25] Somewhat ajax-ified create task route --- app/controllers/tasks_controller.rb | 10 ++++++---- app/views/tasks/_form.html.erb | 2 +- app/views/tasks/create.js.erb | 6 ++++++ app/views/tasks/index.html.erb | 3 ++- app/views/tasks/update.js.erb | 2 +- 5 files changed, 16 insertions(+), 7 deletions(-) create mode 100644 app/views/tasks/create.js.erb diff --git a/app/controllers/tasks_controller.rb b/app/controllers/tasks_controller.rb index 9a13d3e..a8a7409 100644 --- a/app/controllers/tasks_controller.rb +++ b/app/controllers/tasks_controller.rb @@ -6,7 +6,7 @@ def move @task.pending! elsif @task.pending? @task.complete! - else + elsif @task.complete? @task.pending! end @@ -44,13 +44,15 @@ def edit # POST /tasks or /tasks.json def create - @task = Task.new(task_params) + @task = current_user.tasks.build(task_params) respond_to do |format| if @task.save format.html { redirect_to task_url(@task), notice: "Task was successfully created." } format.json { render :show, status: :created, location: @task } - format.js + format.js do + render template: "tasks/create.js.erb" + end else format.html { render :new, status: :unprocessable_entity } format.json { render json: @task.errors, status: :unprocessable_entity } @@ -98,6 +100,6 @@ def set_task # Only allow a list of trusted parameters through. def task_params - params.require(:task).permit(:owner_id, :body) + params.require(:task).permit(:body, :status) end end diff --git a/app/views/tasks/_form.html.erb b/app/views/tasks/_form.html.erb index 50ef7df..46bcf4e 100644 --- a/app/views/tasks/_form.html.erb +++ b/app/views/tasks/_form.html.erb @@ -1,4 +1,4 @@ -
          • +
          • <%= form_with(model: task, local: false) do |form| %>
            <%= form.label :body, class: "visually-hidden" %> diff --git a/app/views/tasks/create.js.erb b/app/views/tasks/create.js.erb new file mode 100644 index 0000000..5ea9aab --- /dev/null +++ b/app/views/tasks/create.js.erb @@ -0,0 +1,6 @@ +var new_task = $("<%= j(render @task) %>"); +new_task.hide(); +$("#form_task").before(new_task); +new_task.slideDown(); +$("#form_task").before(new_task); +$("#form_task #task_body").val(""); diff --git a/app/views/tasks/index.html.erb b/app/views/tasks/index.html.erb index 5d4b74d..83b4783 100644 --- a/app/views/tasks/index.html.erb +++ b/app/views/tasks/index.html.erb @@ -4,8 +4,9 @@
            Not yet started
            - <%= link_to 'New Task', new_task_path %> +
              + <%= render "tasks/form", task: current_user.tasks.build %> <% @tasks.notstarted.each do |task| %> <%= render "tasks/task", task: task %> <% end %> diff --git a/app/views/tasks/update.js.erb b/app/views/tasks/update.js.erb index fea4e90..c613862 100644 --- a/app/views/tasks/update.js.erb +++ b/app/views/tasks/update.js.erb @@ -1 +1 @@ -$("#<%= dom_id @task, :edit %>").replaceWith("<%= j(render @task)%>"); +$("#<%= dom_id @task, :form %>").replaceWith("<%= j(render @task)%>"); From 107f304bb6caf776f22783710beb579263213b58 Mon Sep 17 00:00:00 2001 From: uchicagopriya Date: Tue, 24 Jan 2023 04:21:49 +0000 Subject: [PATCH 24/25] Fixed create Ajax --- app/controllers/tasks_controller.rb | 3 +-- app/views/tasks/_form.html.erb | 2 +- app/views/tasks/_task.html.erb | 2 +- app/views/tasks/create.js.erb | 3 +-- app/views/tasks/index.html.erb | 2 +- 5 files changed, 5 insertions(+), 7 deletions(-) diff --git a/app/controllers/tasks_controller.rb b/app/controllers/tasks_controller.rb index a8a7409..dc384d8 100644 --- a/app/controllers/tasks_controller.rb +++ b/app/controllers/tasks_controller.rb @@ -11,7 +11,6 @@ def move end respond_to do |format| - format.html { redirect_to tasks_url, notice: "Task status updated" } format.js do render template: "tasks/move.js.erb" end @@ -44,7 +43,7 @@ def edit # POST /tasks or /tasks.json def create - @task = current_user.tasks.build(task_params) + @task = current_user.tasks.new(task_params) respond_to do |format| if @task.save diff --git a/app/views/tasks/_form.html.erb b/app/views/tasks/_form.html.erb index 46bcf4e..07ad8bd 100644 --- a/app/views/tasks/_form.html.erb +++ b/app/views/tasks/_form.html.erb @@ -6,7 +6,7 @@
            - <%= form.submit class: "btn btn-outline-warning" %> + <%= form.submit class: "btn btn-outline-primary" %>
            <% end %> diff --git a/app/views/tasks/_task.html.erb b/app/views/tasks/_task.html.erb index d389271..7f3da6e 100644 --- a/app/views/tasks/_task.html.erb +++ b/app/views/tasks/_task.html.erb @@ -6,7 +6,7 @@
            <%= link_to move_task_path(task), class: "btn btn-link", method: :patch, remote: true do %> <% if task.notstarted? %> - + <% elsif task.pending? %> <% else %> diff --git a/app/views/tasks/create.js.erb b/app/views/tasks/create.js.erb index 5ea9aab..8dc5fa3 100644 --- a/app/views/tasks/create.js.erb +++ b/app/views/tasks/create.js.erb @@ -1,6 +1,5 @@ var new_task = $("<%= j(render @task) %>"); new_task.hide(); -$("#form_task").before(new_task); +$("#form_task").after(new_task); new_task.slideDown(); -$("#form_task").before(new_task); $("#form_task #task_body").val(""); diff --git a/app/views/tasks/index.html.erb b/app/views/tasks/index.html.erb index 83b4783..a9c1afc 100644 --- a/app/views/tasks/index.html.erb +++ b/app/views/tasks/index.html.erb @@ -6,7 +6,7 @@
              - <%= render "tasks/form", task: current_user.tasks.build %> + <%= render "tasks/form", task: current_user.tasks.new %> <% @tasks.notstarted.each do |task| %> <%= render "tasks/task", task: task %> <% end %> From 07eef5da88f959dc3446f9ffd79a84b3c4777d7a Mon Sep 17 00:00:00 2001 From: uchicagopriya Date: Tue, 24 Jan 2023 21:25:59 +0000 Subject: [PATCH 25/25] Moved unused files to not_in_use folder and tested --- app/views/{tasks => not_in_use}/edit.html.erb | 0 app/views/{tasks => not_in_use}/forward.html.erb | 0 app/views/{tasks => not_in_use}/new.html.erb | 0 app/views/{tasks => not_in_use}/show.html.erb | 0 app/views/tasks/index.html.erb | 8 ++++---- 5 files changed, 4 insertions(+), 4 deletions(-) rename app/views/{tasks => not_in_use}/edit.html.erb (100%) rename app/views/{tasks => not_in_use}/forward.html.erb (100%) rename app/views/{tasks => not_in_use}/new.html.erb (100%) rename app/views/{tasks => not_in_use}/show.html.erb (100%) diff --git a/app/views/tasks/edit.html.erb b/app/views/not_in_use/edit.html.erb similarity index 100% rename from app/views/tasks/edit.html.erb rename to app/views/not_in_use/edit.html.erb diff --git a/app/views/tasks/forward.html.erb b/app/views/not_in_use/forward.html.erb similarity index 100% rename from app/views/tasks/forward.html.erb rename to app/views/not_in_use/forward.html.erb diff --git a/app/views/tasks/new.html.erb b/app/views/not_in_use/new.html.erb similarity index 100% rename from app/views/tasks/new.html.erb rename to app/views/not_in_use/new.html.erb diff --git a/app/views/tasks/show.html.erb b/app/views/not_in_use/show.html.erb similarity index 100% rename from app/views/tasks/show.html.erb rename to app/views/not_in_use/show.html.erb diff --git a/app/views/tasks/index.html.erb b/app/views/tasks/index.html.erb index a9c1afc..dc494a3 100644 --- a/app/views/tasks/index.html.erb +++ b/app/views/tasks/index.html.erb @@ -1,7 +1,7 @@
              -
              -
              +
              +
              Not yet started
              @@ -15,7 +15,7 @@
              -
              +
              In progress
              @@ -28,7 +28,7 @@
              -
              +
              Completed