From 718fcf4bf9148d278ba994fbc743b19e273df0ef Mon Sep 17 00:00:00 2001 From: dmacinto Date: Sat, 21 Jan 2023 17:53:59 +0000 Subject: [PATCH 01/26] devise install --- Gemfile | 2 + Gemfile.lock | 16 +- config/environments/development.rb | 1 + config/initializers/devise.rb | 311 +++++++++++++++++++++++++++++ config/locales/devise.en.yml | 65 ++++++ config/routes.rb | 1 + db/schema.rb | 18 ++ 7 files changed, 412 insertions(+), 2 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..4064bfa 100644 --- a/Gemfile +++ b/Gemfile @@ -3,6 +3,8 @@ git_source(:github) { |repo| "https://github.com/#{repo}.git" } ruby '3.0.3' +gem 'devise' + # Bundle edge Rails instead: gem 'rails', github: 'rails/rails', branch: 'main' gem 'rails', '~> 6.1.3', '>= 6.1.3.1' # Use postgresql as the database for Active Record diff --git a/Gemfile.lock b/Gemfile.lock index dc44cfd..34f032f 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 htmlbeautifier jbuilder (~> 2.7) listen (~> 3.3) diff --git a/config/environments/development.rb b/config/environments/development.rb index a711738..b24d295 100644 --- a/config/environments/development.rb +++ b/config/environments/development.rb @@ -1,6 +1,7 @@ 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 diff --git a/config/initializers/devise.rb b/config/initializers/devise.rb new file mode 100644 index 0000000..f89aa77 --- /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 = 'b1ae179db8ce321c1729d3fde6496f8ea6211f0d663efb29cb77732bda81dfc08d65d043613f6d09d98b671b29d642c8b56e0bedc07be1107b94338cd21c19d9' + + # ==> 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 = 'ba9b265f4ab28fe38041ff6178e5c527f361576025fae2cbad7e65518b3df9cfce257018a2a2d2dd14fd84576913fbc7c793c209bf8a9134d1010199c38e0c99' + + # 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..83b8ffa 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 to: "home#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 d8b6da761491c7a32f639251f53d7ac9840304c4 Mon Sep 17 00:00:00 2001 From: dmacinto Date: Sat, 21 Jan 2023 17:54:30 +0000 Subject: [PATCH 02/26] devise generate --- app/models/user.rb | 6 +++ config/routes.rb | 1 + .../20230121175413_devise_create_users.rb | 48 +++++++++++++++++++ db/schema.rb | 18 ++++++- test/fixtures/users.yml | 11 +++++ test/models/user_test.rb | 7 +++ 6 files changed, 90 insertions(+), 1 deletion(-) create mode 100644 app/models/user.rb create mode 100644 db/migrate/20230121175413_devise_create_users.rb create mode 100644 test/fixtures/users.yml create mode 100644 test/models/user_test.rb 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 83b8ffa..3de8099 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 to: "home#index" end diff --git a/db/migrate/20230121175413_devise_create_users.rb b/db/migrate/20230121175413_devise_create_users.rb new file mode 100644 index 0000000..d6d80c0 --- /dev/null +++ b/db/migrate/20230121175413_devise_create_users.rb @@ -0,0 +1,48 @@ +# frozen_string_literal: true + +class DeviseCreateUsers < ActiveRecord::Migration[6.1] + def change + 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.string :username + t.boolean :private + t.integer :likes_count + t.integer :comments_count + + t.timestamps null: false + end + + add_index :users, :email, unique: true + add_index :users, :reset_password_token, unique: true + # add_index :users, :confirmation_token, unique: true + # add_index :users, :unlock_token, unique: true + end +end diff --git a/db/schema.rb b/db/schema.rb index 4603022..41edf51 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,9 +10,25 @@ # # 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_21_175413) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" + 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.string "username" + t.boolean "private" + t.integer "likes_count" + t.integer "comments_count" + 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 + 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 0adaa3d4fa05a5f1d6e8722edce11ff62ae789bc Mon Sep 17 00:00:00 2001 From: dmacinto Date: Sat, 21 Jan 2023 18:07:15 +0000 Subject: [PATCH 03/26] fixed migratation data --- db/migrate/20230121175413_devise_create_users.rb | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/db/migrate/20230121175413_devise_create_users.rb b/db/migrate/20230121175413_devise_create_users.rb index d6d80c0..6ada586 100644 --- a/db/migrate/20230121175413_devise_create_users.rb +++ b/db/migrate/20230121175413_devise_create_users.rb @@ -1,10 +1,12 @@ # frozen_string_literal: true class DeviseCreateUsers < ActiveRecord::Migration[6.1] + enable_extension("citext") + def change create_table :users do |t| ## Database authenticatable - t.string :email, null: false, default: "" + t.citext :email, null: false, default: "" t.string :encrypted_password, null: false, default: "" ## Recoverable @@ -32,16 +34,17 @@ def change # t.string :unlock_token # Only if unlock strategy is :email or :both # t.datetime :locked_at - t.string :username - t.boolean :private - t.integer :likes_count - t.integer :comments_count + t.citext :username + t.boolean :private, default: true + t.integer :likes_count, default: 0 + t.integer :comments_count, default: 0 t.timestamps null: false end add_index :users, :email, unique: true add_index :users, :reset_password_token, unique: true + add_index :user, :username, unique: true # add_index :users, :confirmation_token, unique: true # add_index :users, :unlock_token, unique: true end From b6e908846e356a2cad8bef2083ac873e918aacda Mon Sep 17 00:00:00 2001 From: dmacinto Date: Sat, 21 Jan 2023 18:14:47 +0000 Subject: [PATCH 04/26] scaffold and association accessors added --- app/assets/stylesheets/comments.scss | 3 + app/assets/stylesheets/follow_requests.scss | 3 + app/assets/stylesheets/likes.scss | 3 + app/assets/stylesheets/photos.scss | 3 + app/assets/stylesheets/scaffolds.scss | 65 +++++++++++++++++ app/controllers/comments_controller.rb | 70 +++++++++++++++++++ app/controllers/follow_requests_controller.rb | 70 +++++++++++++++++++ app/controllers/likes_controller.rb | 70 +++++++++++++++++++ app/controllers/photos_controller.rb | 70 +++++++++++++++++++ app/helpers/comments_helper.rb | 2 + app/helpers/follow_requests_helper.rb | 2 + app/helpers/likes_helper.rb | 2 + app/helpers/photos_helper.rb | 2 + app/models/comment.rb | 6 ++ app/models/follow_request.rb | 13 ++++ app/models/like.rb | 6 ++ app/models/photo.rb | 17 +++++ app/models/user.rb | 26 +++++++ app/views/comments/_comment.json.jbuilder | 2 + app/views/comments/_form.html.erb | 32 +++++++++ app/views/comments/edit.html.erb | 6 ++ app/views/comments/index.html.erb | 31 ++++++++ app/views/comments/index.json.jbuilder | 1 + app/views/comments/new.html.erb | 5 ++ app/views/comments/show.html.erb | 19 +++++ app/views/comments/show.json.jbuilder | 1 + .../_follow_request.json.jbuilder | 2 + app/views/follow_requests/_form.html.erb | 32 +++++++++ app/views/follow_requests/edit.html.erb | 6 ++ app/views/follow_requests/index.html.erb | 31 ++++++++ app/views/follow_requests/index.json.jbuilder | 1 + app/views/follow_requests/new.html.erb | 5 ++ app/views/follow_requests/show.html.erb | 19 +++++ app/views/follow_requests/show.json.jbuilder | 1 + app/views/likes/_form.html.erb | 27 +++++++ app/views/likes/_like.json.jbuilder | 2 + app/views/likes/edit.html.erb | 6 ++ app/views/likes/index.html.erb | 29 ++++++++ app/views/likes/index.json.jbuilder | 1 + app/views/likes/new.html.erb | 5 ++ app/views/likes/show.html.erb | 14 ++++ app/views/likes/show.json.jbuilder | 1 + app/views/photos/_form.html.erb | 42 +++++++++++ app/views/photos/_photo.json.jbuilder | 2 + app/views/photos/edit.html.erb | 6 ++ app/views/photos/index.html.erb | 35 ++++++++++ app/views/photos/index.json.jbuilder | 1 + app/views/photos/new.html.erb | 5 ++ app/views/photos/show.html.erb | 29 ++++++++ app/views/photos/show.json.jbuilder | 1 + config/routes.rb | 4 ++ db/migrate/20230121180727_create_photos.rb | 13 ++++ db/migrate/20230121181109_create_comments.rb | 11 +++ .../20230121181118_create_follow_requests.rb | 11 +++ db/migrate/20230121181127_create_likes.rb | 10 +++ db/schema.rb | 49 ++++++++++++- test/controllers/comments_controller_test.rb | 48 +++++++++++++ .../follow_requests_controller_test.rb | 48 +++++++++++++ test/controllers/likes_controller_test.rb | 48 +++++++++++++ test/controllers/photos_controller_test.rb | 48 +++++++++++++ test/fixtures/comments.yml | 11 +++ test/fixtures/follow_requests.yml | 11 +++ test/fixtures/likes.yml | 9 +++ test/fixtures/photos.yml | 15 ++++ test/models/comment_test.rb | 7 ++ test/models/follow_request_test.rb | 7 ++ test/models/like_test.rb | 7 ++ test/models/photo_test.rb | 7 ++ test/system/comments_test.rb | 47 +++++++++++++ test/system/follow_requests_test.rb | 47 +++++++++++++ test/system/likes_test.rb | 45 ++++++++++++ test/system/photos_test.rb | 51 ++++++++++++++ 72 files changed, 1386 insertions(+), 1 deletion(-) create mode 100644 app/assets/stylesheets/comments.scss create mode 100644 app/assets/stylesheets/follow_requests.scss create mode 100644 app/assets/stylesheets/likes.scss create mode 100644 app/assets/stylesheets/photos.scss create mode 100644 app/assets/stylesheets/scaffolds.scss create mode 100644 app/controllers/comments_controller.rb create mode 100644 app/controllers/follow_requests_controller.rb create mode 100644 app/controllers/likes_controller.rb create mode 100644 app/controllers/photos_controller.rb create mode 100644 app/helpers/comments_helper.rb create mode 100644 app/helpers/follow_requests_helper.rb create mode 100644 app/helpers/likes_helper.rb create mode 100644 app/helpers/photos_helper.rb create mode 100644 app/models/comment.rb create mode 100644 app/models/follow_request.rb create mode 100644 app/models/like.rb create mode 100644 app/models/photo.rb create mode 100644 app/views/comments/_comment.json.jbuilder create mode 100644 app/views/comments/_form.html.erb create mode 100644 app/views/comments/edit.html.erb create mode 100644 app/views/comments/index.html.erb create mode 100644 app/views/comments/index.json.jbuilder create mode 100644 app/views/comments/new.html.erb create mode 100644 app/views/comments/show.html.erb create mode 100644 app/views/comments/show.json.jbuilder create mode 100644 app/views/follow_requests/_follow_request.json.jbuilder create mode 100644 app/views/follow_requests/_form.html.erb create mode 100644 app/views/follow_requests/edit.html.erb create mode 100644 app/views/follow_requests/index.html.erb create mode 100644 app/views/follow_requests/index.json.jbuilder create mode 100644 app/views/follow_requests/new.html.erb create mode 100644 app/views/follow_requests/show.html.erb create mode 100644 app/views/follow_requests/show.json.jbuilder create mode 100644 app/views/likes/_form.html.erb create mode 100644 app/views/likes/_like.json.jbuilder create mode 100644 app/views/likes/edit.html.erb create mode 100644 app/views/likes/index.html.erb create mode 100644 app/views/likes/index.json.jbuilder create mode 100644 app/views/likes/new.html.erb create mode 100644 app/views/likes/show.html.erb create mode 100644 app/views/likes/show.json.jbuilder create mode 100644 app/views/photos/_form.html.erb create mode 100644 app/views/photos/_photo.json.jbuilder create mode 100644 app/views/photos/edit.html.erb create mode 100644 app/views/photos/index.html.erb create mode 100644 app/views/photos/index.json.jbuilder create mode 100644 app/views/photos/new.html.erb create mode 100644 app/views/photos/show.html.erb create mode 100644 app/views/photos/show.json.jbuilder create mode 100644 db/migrate/20230121180727_create_photos.rb create mode 100644 db/migrate/20230121181109_create_comments.rb create mode 100644 db/migrate/20230121181118_create_follow_requests.rb create mode 100644 db/migrate/20230121181127_create_likes.rb create mode 100644 test/controllers/comments_controller_test.rb create mode 100644 test/controllers/follow_requests_controller_test.rb create mode 100644 test/controllers/likes_controller_test.rb create mode 100644 test/controllers/photos_controller_test.rb create mode 100644 test/fixtures/comments.yml create mode 100644 test/fixtures/follow_requests.yml create mode 100644 test/fixtures/likes.yml create mode 100644 test/fixtures/photos.yml create mode 100644 test/models/comment_test.rb create mode 100644 test/models/follow_request_test.rb create mode 100644 test/models/like_test.rb create mode 100644 test/models/photo_test.rb create mode 100644 test/system/comments_test.rb create mode 100644 test/system/follow_requests_test.rb create mode 100644 test/system/likes_test.rb create mode 100644 test/system/photos_test.rb diff --git a/app/assets/stylesheets/comments.scss b/app/assets/stylesheets/comments.scss new file mode 100644 index 0000000..67e9dc5 --- /dev/null +++ b/app/assets/stylesheets/comments.scss @@ -0,0 +1,3 @@ +// Place all the styles related to the comments controller here. +// They will automatically be included in application.css. +// You can use Sass (SCSS) here: https://sass-lang.com/ diff --git a/app/assets/stylesheets/follow_requests.scss b/app/assets/stylesheets/follow_requests.scss new file mode 100644 index 0000000..9bc340c --- /dev/null +++ b/app/assets/stylesheets/follow_requests.scss @@ -0,0 +1,3 @@ +// Place all the styles related to the follow_requests controller here. +// They will automatically be included in application.css. +// You can use Sass (SCSS) here: https://sass-lang.com/ diff --git a/app/assets/stylesheets/likes.scss b/app/assets/stylesheets/likes.scss new file mode 100644 index 0000000..bfb01ae --- /dev/null +++ b/app/assets/stylesheets/likes.scss @@ -0,0 +1,3 @@ +// Place all the styles related to the likes controller here. +// They will automatically be included in application.css. +// You can use Sass (SCSS) here: https://sass-lang.com/ diff --git a/app/assets/stylesheets/photos.scss b/app/assets/stylesheets/photos.scss new file mode 100644 index 0000000..2a65da5 --- /dev/null +++ b/app/assets/stylesheets/photos.scss @@ -0,0 +1,3 @@ +// Place all the styles related to the photos controller here. +// They will automatically be included in application.css. +// You can use Sass (SCSS) here: https://sass-lang.com/ 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/controllers/comments_controller.rb b/app/controllers/comments_controller.rb new file mode 100644 index 0000000..cd08b60 --- /dev/null +++ b/app/controllers/comments_controller.rb @@ -0,0 +1,70 @@ +class CommentsController < ApplicationController + before_action :set_comment, only: %i[ show edit update destroy ] + + # GET /comments or /comments.json + def index + @comments = Comment.all + end + + # GET /comments/1 or /comments/1.json + def show + end + + # GET /comments/new + def new + @comment = Comment.new + end + + # GET /comments/1/edit + def edit + end + + # POST /comments or /comments.json + def create + @comment = Comment.new(comment_params) + + respond_to do |format| + if @comment.save + format.html { redirect_to comment_url(@comment), notice: "Comment was successfully created." } + format.json { render :show, status: :created, location: @comment } + else + format.html { render :new, status: :unprocessable_entity } + format.json { render json: @comment.errors, status: :unprocessable_entity } + end + end + end + + # PATCH/PUT /comments/1 or /comments/1.json + def update + respond_to do |format| + if @comment.update(comment_params) + format.html { redirect_to comment_url(@comment), notice: "Comment was successfully updated." } + format.json { render :show, status: :ok, location: @comment } + else + format.html { render :edit, status: :unprocessable_entity } + format.json { render json: @comment.errors, status: :unprocessable_entity } + end + end + end + + # DELETE /comments/1 or /comments/1.json + def destroy + @comment.destroy + + respond_to do |format| + format.html { redirect_to comments_url, notice: "Comment was successfully destroyed." } + format.json { head :no_content } + end + end + + private + # Use callbacks to share common setup or constraints between actions. + def set_comment + @comment = Comment.find(params[:id]) + end + + # Only allow a list of trusted parameters through. + def comment_params + params.require(:comment).permit(:author_id, :photo_id, :body) + end +end diff --git a/app/controllers/follow_requests_controller.rb b/app/controllers/follow_requests_controller.rb new file mode 100644 index 0000000..47074cf --- /dev/null +++ b/app/controllers/follow_requests_controller.rb @@ -0,0 +1,70 @@ +class FollowRequestsController < ApplicationController + before_action :set_follow_request, only: %i[ show edit update destroy ] + + # GET /follow_requests or /follow_requests.json + def index + @follow_requests = FollowRequest.all + end + + # GET /follow_requests/1 or /follow_requests/1.json + def show + end + + # GET /follow_requests/new + def new + @follow_request = FollowRequest.new + end + + # GET /follow_requests/1/edit + def edit + end + + # POST /follow_requests or /follow_requests.json + def create + @follow_request = FollowRequest.new(follow_request_params) + + respond_to do |format| + if @follow_request.save + format.html { redirect_to follow_request_url(@follow_request), notice: "Follow request was successfully created." } + format.json { render :show, status: :created, location: @follow_request } + else + format.html { render :new, status: :unprocessable_entity } + format.json { render json: @follow_request.errors, status: :unprocessable_entity } + end + end + end + + # PATCH/PUT /follow_requests/1 or /follow_requests/1.json + def update + respond_to do |format| + if @follow_request.update(follow_request_params) + format.html { redirect_to follow_request_url(@follow_request), notice: "Follow request was successfully updated." } + format.json { render :show, status: :ok, location: @follow_request } + else + format.html { render :edit, status: :unprocessable_entity } + format.json { render json: @follow_request.errors, status: :unprocessable_entity } + end + end + end + + # DELETE /follow_requests/1 or /follow_requests/1.json + def destroy + @follow_request.destroy + + respond_to do |format| + format.html { redirect_to follow_requests_url, notice: "Follow request was successfully destroyed." } + format.json { head :no_content } + end + end + + private + # Use callbacks to share common setup or constraints between actions. + def set_follow_request + @follow_request = FollowRequest.find(params[:id]) + end + + # Only allow a list of trusted parameters through. + def follow_request_params + params.require(:follow_request).permit(:recipient_id, :sender_id, :status) + end +end diff --git a/app/controllers/likes_controller.rb b/app/controllers/likes_controller.rb new file mode 100644 index 0000000..f38a1c6 --- /dev/null +++ b/app/controllers/likes_controller.rb @@ -0,0 +1,70 @@ +class LikesController < ApplicationController + before_action :set_like, only: %i[ show edit update destroy ] + + # GET /likes or /likes.json + def index + @likes = Like.all + end + + # GET /likes/1 or /likes/1.json + def show + end + + # GET /likes/new + def new + @like = Like.new + end + + # GET /likes/1/edit + def edit + end + + # POST /likes or /likes.json + def create + @like = Like.new(like_params) + + respond_to do |format| + if @like.save + format.html { redirect_to like_url(@like), notice: "Like was successfully created." } + format.json { render :show, status: :created, location: @like } + else + format.html { render :new, status: :unprocessable_entity } + format.json { render json: @like.errors, status: :unprocessable_entity } + end + end + end + + # PATCH/PUT /likes/1 or /likes/1.json + def update + respond_to do |format| + if @like.update(like_params) + format.html { redirect_to like_url(@like), notice: "Like was successfully updated." } + format.json { render :show, status: :ok, location: @like } + else + format.html { render :edit, status: :unprocessable_entity } + format.json { render json: @like.errors, status: :unprocessable_entity } + end + end + end + + # DELETE /likes/1 or /likes/1.json + def destroy + @like.destroy + + respond_to do |format| + format.html { redirect_to likes_url, notice: "Like was successfully destroyed." } + format.json { head :no_content } + end + end + + private + # Use callbacks to share common setup or constraints between actions. + def set_like + @like = Like.find(params[:id]) + end + + # Only allow a list of trusted parameters through. + def like_params + params.require(:like).permit(:fan_id, :photo_id) + end +end diff --git a/app/controllers/photos_controller.rb b/app/controllers/photos_controller.rb new file mode 100644 index 0000000..86ba2de --- /dev/null +++ b/app/controllers/photos_controller.rb @@ -0,0 +1,70 @@ +class PhotosController < ApplicationController + before_action :set_photo, only: %i[ show edit update destroy ] + + # GET /photos or /photos.json + def index + @photos = Photo.all + end + + # GET /photos/1 or /photos/1.json + def show + end + + # GET /photos/new + def new + @photo = Photo.new + end + + # GET /photos/1/edit + def edit + end + + # POST /photos or /photos.json + def create + @photo = Photo.new(photo_params) + + respond_to do |format| + if @photo.save + format.html { redirect_to photo_url(@photo), notice: "Photo was successfully created." } + format.json { render :show, status: :created, location: @photo } + else + format.html { render :new, status: :unprocessable_entity } + format.json { render json: @photo.errors, status: :unprocessable_entity } + end + end + end + + # PATCH/PUT /photos/1 or /photos/1.json + def update + respond_to do |format| + if @photo.update(photo_params) + format.html { redirect_to photo_url(@photo), notice: "Photo was successfully updated." } + format.json { render :show, status: :ok, location: @photo } + else + format.html { render :edit, status: :unprocessable_entity } + format.json { render json: @photo.errors, status: :unprocessable_entity } + end + end + end + + # DELETE /photos/1 or /photos/1.json + def destroy + @photo.destroy + + respond_to do |format| + format.html { redirect_to photos_url, notice: "Photo was successfully destroyed." } + format.json { head :no_content } + end + end + + private + # Use callbacks to share common setup or constraints between actions. + def set_photo + @photo = Photo.find(params[:id]) + end + + # Only allow a list of trusted parameters through. + def photo_params + params.require(:photo).permit(:image, :comments_count, :likes_count, :caption, :owner_id) + end +end diff --git a/app/helpers/comments_helper.rb b/app/helpers/comments_helper.rb new file mode 100644 index 0000000..0ec9ca5 --- /dev/null +++ b/app/helpers/comments_helper.rb @@ -0,0 +1,2 @@ +module CommentsHelper +end diff --git a/app/helpers/follow_requests_helper.rb b/app/helpers/follow_requests_helper.rb new file mode 100644 index 0000000..cfd350e --- /dev/null +++ b/app/helpers/follow_requests_helper.rb @@ -0,0 +1,2 @@ +module FollowRequestsHelper +end diff --git a/app/helpers/likes_helper.rb b/app/helpers/likes_helper.rb new file mode 100644 index 0000000..a78a759 --- /dev/null +++ b/app/helpers/likes_helper.rb @@ -0,0 +1,2 @@ +module LikesHelper +end diff --git a/app/helpers/photos_helper.rb b/app/helpers/photos_helper.rb new file mode 100644 index 0000000..0a10d47 --- /dev/null +++ b/app/helpers/photos_helper.rb @@ -0,0 +1,2 @@ +module PhotosHelper +end diff --git a/app/models/comment.rb b/app/models/comment.rb new file mode 100644 index 0000000..5a4e7dd --- /dev/null +++ b/app/models/comment.rb @@ -0,0 +1,6 @@ +class Comment < ApplicationRecord + belongs_to :author, class_name: "User", counter_cache: true + belongs_to :photo, counter_cache: true + + validates :body, presence: true +end diff --git a/app/models/follow_request.rb b/app/models/follow_request.rb new file mode 100644 index 0000000..bbb9cc7 --- /dev/null +++ b/app/models/follow_request.rb @@ -0,0 +1,13 @@ +class FollowRequest < ApplicationRecord + belongs_to :recipient, class_name: "User" + belongs_to :sender, class_name: "User" + + enum status: { pending: "pending", rejected: "rejected", accepted: "accepted" } + + scope :accepted, -> { where(status: "accepted" ) } + scope :not_accepted, -> { where.not(status: "accepted" ) } + + validates :user_id, uniqueness: { scope: :sender_id, message: "has already sent a follow request" } + + validates :user_id, uniqueness: { scope: :recipient_id, message: "has already received a follow request" } +end diff --git a/app/models/like.rb b/app/models/like.rb new file mode 100644 index 0000000..28a7284 --- /dev/null +++ b/app/models/like.rb @@ -0,0 +1,6 @@ +class Like < ApplicationRecord + belongs_to :fan, class_name: "User", counter_cache: true + belongs_to :photo, counter_cache: true + + validates :user_id, uniqueness: { scope: :photo_id, message: "has already liked this photo" } +end diff --git a/app/models/photo.rb b/app/models/photo.rb new file mode 100644 index 0000000..fe1e699 --- /dev/null +++ b/app/models/photo.rb @@ -0,0 +1,17 @@ +class Photo < ApplicationRecord + belongs_to :owner, class_name: "User", counter_cache: true + + has_many :comments + + has_many :likes + + has_many :fans, through: :likes + + validates :caption, presence: true + + validates :image, presence: true + + scope :past_week, -> { where(created_at: 1.week.ago...) } + + scope :by_likes, -> { order(likes_count: :desc) } +end diff --git a/app/models/user.rb b/app/models/user.rb index 4756799..5b1fdd5 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -3,4 +3,30 @@ class User < ApplicationRecord # :confirmable, :lockable, :timeoutable, :trackable and :omniauthable devise :database_authenticatable, :registerable, :recoverable, :rememberable, :validatable + + has_many :comments, foreign_key: :author_id + + has_many :sent_follow_requests, foreign_key: :sender_id, class_name: "FollowRequest" + + has_many :accepted_sent_follow_requests, -> { accepted }, foreign_key: :sender_id, class_name: "FollowRequest" + + has_many :received_follow_requests, foreign_key: :recipient_id, class_name: "FollowRequest" + + has_many :accepted_received_follow_requests, ->{ accepted }, foreign_key: :recipient_id, class_name: "FollowRequest" + + has_many :likes, foreign_key: :fan_id + + has_many :own_photos, foreign_key: :owner_id, class_name: "Photo" + + has_many :liked_photos, through: :likes, source: :photo + + has_many :leaders, through: :accepted_sent_follow_requests, source: :recipient + + has_many :followers, through: :accepted_received_follow_requests, source: :sender + + has_many :feed, through: :leaders, source: :own_photos + + has_many :discover, through: :leaders, source: :liked_photos + + validates :username, presence: true, uniqueness: true end diff --git a/app/views/comments/_comment.json.jbuilder b/app/views/comments/_comment.json.jbuilder new file mode 100644 index 0000000..64674ae --- /dev/null +++ b/app/views/comments/_comment.json.jbuilder @@ -0,0 +1,2 @@ +json.extract! comment, :id, :author_id, :photo_id, :body, :created_at, :updated_at +json.url comment_url(comment, format: :json) diff --git a/app/views/comments/_form.html.erb b/app/views/comments/_form.html.erb new file mode 100644 index 0000000..3a569ea --- /dev/null +++ b/app/views/comments/_form.html.erb @@ -0,0 +1,32 @@ +<%= form_with(model: comment) do |form| %> + <% if comment.errors.any? %> +
+

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

+ +
    + <% comment.errors.each do |error| %> +
  • <%= error.full_message %>
  • + <% end %> +
+
+ <% end %> + +
+ <%= form.label :author_id %> + <%= form.text_field :author_id %> +
+ +
+ <%= form.label :photo_id %> + <%= form.text_field :photo_id %> +
+ +
+ <%= form.label :body %> + <%= form.text_area :body %> +
+ +
+ <%= form.submit %> +
+<% end %> diff --git a/app/views/comments/edit.html.erb b/app/views/comments/edit.html.erb new file mode 100644 index 0000000..a20be1d --- /dev/null +++ b/app/views/comments/edit.html.erb @@ -0,0 +1,6 @@ +

Editing Comment

+ +<%= render 'form', comment: @comment %> + +<%= link_to 'Show', @comment %> | +<%= link_to 'Back', comments_path %> diff --git a/app/views/comments/index.html.erb b/app/views/comments/index.html.erb new file mode 100644 index 0000000..1f167e4 --- /dev/null +++ b/app/views/comments/index.html.erb @@ -0,0 +1,31 @@ +

<%= notice %>

+ +

Comments

+ + + + + + + + + + + + + <% @comments.each do |comment| %> + + + + + + + + + <% end %> + +
AuthorPhotoBody
<%= comment.author_id %><%= comment.photo_id %><%= comment.body %><%= link_to 'Show', comment %><%= link_to 'Edit', edit_comment_path(comment) %><%= link_to 'Destroy', comment, method: :delete, data: { confirm: 'Are you sure?' } %>
+ +
+ +<%= link_to 'New Comment', new_comment_path %> diff --git a/app/views/comments/index.json.jbuilder b/app/views/comments/index.json.jbuilder new file mode 100644 index 0000000..e3322af --- /dev/null +++ b/app/views/comments/index.json.jbuilder @@ -0,0 +1 @@ +json.array! @comments, partial: "comments/comment", as: :comment diff --git a/app/views/comments/new.html.erb b/app/views/comments/new.html.erb new file mode 100644 index 0000000..f8b43b6 --- /dev/null +++ b/app/views/comments/new.html.erb @@ -0,0 +1,5 @@ +

New Comment

+ +<%= render 'form', comment: @comment %> + +<%= link_to 'Back', comments_path %> diff --git a/app/views/comments/show.html.erb b/app/views/comments/show.html.erb new file mode 100644 index 0000000..58fbc00 --- /dev/null +++ b/app/views/comments/show.html.erb @@ -0,0 +1,19 @@ +

<%= notice %>

+ +

+ Author: + <%= @comment.author_id %> +

+ +

+ Photo: + <%= @comment.photo_id %> +

+ +

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

+ +<%= link_to 'Edit', edit_comment_path(@comment) %> | +<%= link_to 'Back', comments_path %> diff --git a/app/views/comments/show.json.jbuilder b/app/views/comments/show.json.jbuilder new file mode 100644 index 0000000..78a9099 --- /dev/null +++ b/app/views/comments/show.json.jbuilder @@ -0,0 +1 @@ +json.partial! "comments/comment", comment: @comment diff --git a/app/views/follow_requests/_follow_request.json.jbuilder b/app/views/follow_requests/_follow_request.json.jbuilder new file mode 100644 index 0000000..d7dd54c --- /dev/null +++ b/app/views/follow_requests/_follow_request.json.jbuilder @@ -0,0 +1,2 @@ +json.extract! follow_request, :id, :recipient_id, :sender_id, :status, :created_at, :updated_at +json.url follow_request_url(follow_request, format: :json) diff --git a/app/views/follow_requests/_form.html.erb b/app/views/follow_requests/_form.html.erb new file mode 100644 index 0000000..323c21c --- /dev/null +++ b/app/views/follow_requests/_form.html.erb @@ -0,0 +1,32 @@ +<%= form_with(model: follow_request) do |form| %> + <% if follow_request.errors.any? %> +
+

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

+ +
    + <% follow_request.errors.each do |error| %> +
  • <%= error.full_message %>
  • + <% end %> +
+
+ <% end %> + +
+ <%= form.label :recipient_id %> + <%= form.text_field :recipient_id %> +
+ +
+ <%= form.label :sender_id %> + <%= form.text_field :sender_id %> +
+ +
+ <%= form.label :status %> + <%= form.text_field :status %> +
+ +
+ <%= form.submit %> +
+<% end %> diff --git a/app/views/follow_requests/edit.html.erb b/app/views/follow_requests/edit.html.erb new file mode 100644 index 0000000..54c3b9c --- /dev/null +++ b/app/views/follow_requests/edit.html.erb @@ -0,0 +1,6 @@ +

Editing Follow Request

+ +<%= render 'form', follow_request: @follow_request %> + +<%= link_to 'Show', @follow_request %> | +<%= link_to 'Back', follow_requests_path %> diff --git a/app/views/follow_requests/index.html.erb b/app/views/follow_requests/index.html.erb new file mode 100644 index 0000000..dc14b41 --- /dev/null +++ b/app/views/follow_requests/index.html.erb @@ -0,0 +1,31 @@ +

<%= notice %>

+ +

Follow Requests

+ + + + + + + + + + + + + <% @follow_requests.each do |follow_request| %> + + + + + + + + + <% end %> + +
RecipientSenderStatus
<%= follow_request.recipient_id %><%= follow_request.sender_id %><%= follow_request.status %><%= link_to 'Show', follow_request %><%= link_to 'Edit', edit_follow_request_path(follow_request) %><%= link_to 'Destroy', follow_request, method: :delete, data: { confirm: 'Are you sure?' } %>
+ +
+ +<%= link_to 'New Follow Request', new_follow_request_path %> diff --git a/app/views/follow_requests/index.json.jbuilder b/app/views/follow_requests/index.json.jbuilder new file mode 100644 index 0000000..b45255d --- /dev/null +++ b/app/views/follow_requests/index.json.jbuilder @@ -0,0 +1 @@ +json.array! @follow_requests, partial: "follow_requests/follow_request", as: :follow_request diff --git a/app/views/follow_requests/new.html.erb b/app/views/follow_requests/new.html.erb new file mode 100644 index 0000000..b06ca67 --- /dev/null +++ b/app/views/follow_requests/new.html.erb @@ -0,0 +1,5 @@ +

New Follow Request

+ +<%= render 'form', follow_request: @follow_request %> + +<%= link_to 'Back', follow_requests_path %> diff --git a/app/views/follow_requests/show.html.erb b/app/views/follow_requests/show.html.erb new file mode 100644 index 0000000..7d94d12 --- /dev/null +++ b/app/views/follow_requests/show.html.erb @@ -0,0 +1,19 @@ +

<%= notice %>

+ +

+ Recipient: + <%= @follow_request.recipient_id %> +

+ +

+ Sender: + <%= @follow_request.sender_id %> +

+ +

+ Status: + <%= @follow_request.status %> +

+ +<%= link_to 'Edit', edit_follow_request_path(@follow_request) %> | +<%= link_to 'Back', follow_requests_path %> diff --git a/app/views/follow_requests/show.json.jbuilder b/app/views/follow_requests/show.json.jbuilder new file mode 100644 index 0000000..3fb15c1 --- /dev/null +++ b/app/views/follow_requests/show.json.jbuilder @@ -0,0 +1 @@ +json.partial! "follow_requests/follow_request", follow_request: @follow_request diff --git a/app/views/likes/_form.html.erb b/app/views/likes/_form.html.erb new file mode 100644 index 0000000..aa444ea --- /dev/null +++ b/app/views/likes/_form.html.erb @@ -0,0 +1,27 @@ +<%= form_with(model: like) do |form| %> + <% if like.errors.any? %> +
+

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

+ +
    + <% like.errors.each do |error| %> +
  • <%= error.full_message %>
  • + <% end %> +
+
+ <% end %> + +
+ <%= form.label :fan_id %> + <%= form.text_field :fan_id %> +
+ +
+ <%= form.label :photo_id %> + <%= form.text_field :photo_id %> +
+ +
+ <%= form.submit %> +
+<% end %> diff --git a/app/views/likes/_like.json.jbuilder b/app/views/likes/_like.json.jbuilder new file mode 100644 index 0000000..f89aea4 --- /dev/null +++ b/app/views/likes/_like.json.jbuilder @@ -0,0 +1,2 @@ +json.extract! like, :id, :fan_id, :photo_id, :created_at, :updated_at +json.url like_url(like, format: :json) diff --git a/app/views/likes/edit.html.erb b/app/views/likes/edit.html.erb new file mode 100644 index 0000000..622df3d --- /dev/null +++ b/app/views/likes/edit.html.erb @@ -0,0 +1,6 @@ +

Editing Like

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

<%= notice %>

+ +

Likes

+ + + + + + + + + + + + <% @likes.each do |like| %> + + + + + + + + <% end %> + +
FanPhoto
<%= like.fan_id %><%= like.photo_id %><%= link_to 'Show', like %><%= link_to 'Edit', edit_like_path(like) %><%= link_to 'Destroy', like, method: :delete, data: { confirm: 'Are you sure?' } %>
+ +
+ +<%= link_to 'New Like', new_like_path %> diff --git a/app/views/likes/index.json.jbuilder b/app/views/likes/index.json.jbuilder new file mode 100644 index 0000000..721d476 --- /dev/null +++ b/app/views/likes/index.json.jbuilder @@ -0,0 +1 @@ +json.array! @likes, partial: "likes/like", as: :like diff --git a/app/views/likes/new.html.erb b/app/views/likes/new.html.erb new file mode 100644 index 0000000..0643826 --- /dev/null +++ b/app/views/likes/new.html.erb @@ -0,0 +1,5 @@ +

New Like

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

<%= notice %>

+ +

+ Fan: + <%= @like.fan_id %> +

+ +

+ Photo: + <%= @like.photo_id %> +

+ +<%= link_to 'Edit', edit_like_path(@like) %> | +<%= link_to 'Back', likes_path %> diff --git a/app/views/likes/show.json.jbuilder b/app/views/likes/show.json.jbuilder new file mode 100644 index 0000000..587277f --- /dev/null +++ b/app/views/likes/show.json.jbuilder @@ -0,0 +1 @@ +json.partial! "likes/like", like: @like diff --git a/app/views/photos/_form.html.erb b/app/views/photos/_form.html.erb new file mode 100644 index 0000000..ea4c5e6 --- /dev/null +++ b/app/views/photos/_form.html.erb @@ -0,0 +1,42 @@ +<%= form_with(model: photo) do |form| %> + <% if photo.errors.any? %> +
+

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

+ +
    + <% photo.errors.each do |error| %> +
  • <%= error.full_message %>
  • + <% end %> +
+
+ <% end %> + +
+ <%= form.label :image %> + <%= form.text_field :image %> +
+ +
+ <%= form.label :comments_count %> + <%= form.number_field :comments_count %> +
+ +
+ <%= form.label :likes_count %> + <%= form.number_field :likes_count %> +
+ +
+ <%= form.label :caption %> + <%= form.text_area :caption %> +
+ +
+ <%= form.label :owner_id %> + <%= form.text_field :owner_id %> +
+ +
+ <%= form.submit %> +
+<% end %> diff --git a/app/views/photos/_photo.json.jbuilder b/app/views/photos/_photo.json.jbuilder new file mode 100644 index 0000000..fad93a0 --- /dev/null +++ b/app/views/photos/_photo.json.jbuilder @@ -0,0 +1,2 @@ +json.extract! photo, :id, :image, :comments_count, :likes_count, :caption, :owner_id, :created_at, :updated_at +json.url photo_url(photo, format: :json) diff --git a/app/views/photos/edit.html.erb b/app/views/photos/edit.html.erb new file mode 100644 index 0000000..688c99c --- /dev/null +++ b/app/views/photos/edit.html.erb @@ -0,0 +1,6 @@ +

Editing Photo

+ +<%= render 'form', photo: @photo %> + +<%= link_to 'Show', @photo %> | +<%= link_to 'Back', photos_path %> diff --git a/app/views/photos/index.html.erb b/app/views/photos/index.html.erb new file mode 100644 index 0000000..a8e955c --- /dev/null +++ b/app/views/photos/index.html.erb @@ -0,0 +1,35 @@ +

<%= notice %>

+ +

Photos

+ + + + + + + + + + + + + + + <% @photos.each do |photo| %> + + + + + + + + + + + <% end %> + +
ImageComments countLikes countCaptionOwner
<%= photo.image %><%= photo.comments_count %><%= photo.likes_count %><%= photo.caption %><%= photo.owner_id %><%= link_to 'Show', photo %><%= link_to 'Edit', edit_photo_path(photo) %><%= link_to 'Destroy', photo, method: :delete, data: { confirm: 'Are you sure?' } %>
+ +
+ +<%= link_to 'New Photo', new_photo_path %> diff --git a/app/views/photos/index.json.jbuilder b/app/views/photos/index.json.jbuilder new file mode 100644 index 0000000..24d84b8 --- /dev/null +++ b/app/views/photos/index.json.jbuilder @@ -0,0 +1 @@ +json.array! @photos, partial: "photos/photo", as: :photo diff --git a/app/views/photos/new.html.erb b/app/views/photos/new.html.erb new file mode 100644 index 0000000..b9a62c0 --- /dev/null +++ b/app/views/photos/new.html.erb @@ -0,0 +1,5 @@ +

New Photo

+ +<%= render 'form', photo: @photo %> + +<%= link_to 'Back', photos_path %> diff --git a/app/views/photos/show.html.erb b/app/views/photos/show.html.erb new file mode 100644 index 0000000..30bece6 --- /dev/null +++ b/app/views/photos/show.html.erb @@ -0,0 +1,29 @@ +

<%= notice %>

+ +

+ Image: + <%= @photo.image %> +

+ +

+ Comments count: + <%= @photo.comments_count %> +

+ +

+ Likes count: + <%= @photo.likes_count %> +

+ +

+ Caption: + <%= @photo.caption %> +

+ +

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

+ +<%= link_to 'Edit', edit_photo_path(@photo) %> | +<%= link_to 'Back', photos_path %> diff --git a/app/views/photos/show.json.jbuilder b/app/views/photos/show.json.jbuilder new file mode 100644 index 0000000..5c0f6ff --- /dev/null +++ b/app/views/photos/show.json.jbuilder @@ -0,0 +1 @@ +json.partial! "photos/photo", photo: @photo diff --git a/config/routes.rb b/config/routes.rb index 3de8099..6e10146 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -1,4 +1,8 @@ Rails.application.routes.draw do + resources :likes + resources :follow_requests + resources :comments + resources :photos devise_for :users # For details on the DSL available within this file, see https://guides.rubyonrails.org/routing.html root to: "home#index" diff --git a/db/migrate/20230121180727_create_photos.rb b/db/migrate/20230121180727_create_photos.rb new file mode 100644 index 0000000..48bbef4 --- /dev/null +++ b/db/migrate/20230121180727_create_photos.rb @@ -0,0 +1,13 @@ +class CreatePhotos < ActiveRecord::Migration[6.1] + def change + create_table :photos do |t| + t.string :image + t.integer :comments_count + t.integer :likes_count + t.text :caption + t.references :owner, null: false, foreign_key: { to_table: :users}, index: true + + t.timestamps + end + end +end diff --git a/db/migrate/20230121181109_create_comments.rb b/db/migrate/20230121181109_create_comments.rb new file mode 100644 index 0000000..2e703da --- /dev/null +++ b/db/migrate/20230121181109_create_comments.rb @@ -0,0 +1,11 @@ +class CreateComments < ActiveRecord::Migration[6.1] + def change + create_table :comments do |t| + t.references :author, null: false, foreign_key: { to_table: :users} + t.references :photo, null: false, foreign_key: true + t.text :body + + t.timestamps + end + end +end diff --git a/db/migrate/20230121181118_create_follow_requests.rb b/db/migrate/20230121181118_create_follow_requests.rb new file mode 100644 index 0000000..75743ba --- /dev/null +++ b/db/migrate/20230121181118_create_follow_requests.rb @@ -0,0 +1,11 @@ +class CreateFollowRequests < ActiveRecord::Migration[6.1] + def change + create_table :follow_requests do |t| + t.references :recipient, null: false, foreign_key: { to_table: :users} + t.references :sender, null: false, foreign_key: { to_table: :users} + t.string :status + + t.timestamps + end + end +end diff --git a/db/migrate/20230121181127_create_likes.rb b/db/migrate/20230121181127_create_likes.rb new file mode 100644 index 0000000..f10df11 --- /dev/null +++ b/db/migrate/20230121181127_create_likes.rb @@ -0,0 +1,10 @@ +class CreateLikes < ActiveRecord::Migration[6.1] + def change + create_table :likes do |t| + t.references :fan, null: false, foreign_key: { to_table: :users} + t.references :photo, null: false, foreign_key: true + + t.timestamps + end + end +end diff --git a/db/schema.rb b/db/schema.rb index 41edf51..59f7bff 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,11 +10,51 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 2023_01_21_175413) do +ActiveRecord::Schema.define(version: 2023_01_21_181127) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" + create_table "comments", force: :cascade do |t| + t.bigint "author_id", null: false + t.bigint "photo_id", null: false + t.text "body" + t.datetime "created_at", precision: 6, null: false + t.datetime "updated_at", precision: 6, null: false + t.index ["author_id"], name: "index_comments_on_author_id" + t.index ["photo_id"], name: "index_comments_on_photo_id" + end + + create_table "follow_requests", force: :cascade do |t| + t.bigint "recipient_id", null: false + t.bigint "sender_id", null: false + t.string "status" + t.datetime "created_at", precision: 6, null: false + t.datetime "updated_at", precision: 6, null: false + t.index ["recipient_id"], name: "index_follow_requests_on_recipient_id" + t.index ["sender_id"], name: "index_follow_requests_on_sender_id" + end + + create_table "likes", force: :cascade do |t| + t.bigint "fan_id", null: false + t.bigint "photo_id", null: false + t.datetime "created_at", precision: 6, null: false + t.datetime "updated_at", precision: 6, null: false + t.index ["fan_id"], name: "index_likes_on_fan_id" + t.index ["photo_id"], name: "index_likes_on_photo_id" + end + + create_table "photos", force: :cascade do |t| + t.string "image" + t.integer "comments_count" + t.integer "likes_count" + t.text "caption" + t.bigint "owner_id", null: false + t.datetime "created_at", precision: 6, null: false + t.datetime "updated_at", precision: 6, null: false + t.index ["owner_id"], name: "index_photos_on_owner_id" + end + create_table "users", force: :cascade do |t| t.string "email", default: "", null: false t.string "encrypted_password", default: "", null: false @@ -31,4 +71,11 @@ t.index ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true end + add_foreign_key "comments", "photos" + add_foreign_key "comments", "users", column: "author_id" + add_foreign_key "follow_requests", "users", column: "recipient_id" + add_foreign_key "follow_requests", "users", column: "sender_id" + add_foreign_key "likes", "photos" + add_foreign_key "likes", "users", column: "fan_id" + add_foreign_key "photos", "users", column: "owner_id" end diff --git a/test/controllers/comments_controller_test.rb b/test/controllers/comments_controller_test.rb new file mode 100644 index 0000000..d26febb --- /dev/null +++ b/test/controllers/comments_controller_test.rb @@ -0,0 +1,48 @@ +require "test_helper" + +class CommentsControllerTest < ActionDispatch::IntegrationTest + setup do + @comment = comments(:one) + end + + test "should get index" do + get comments_url + assert_response :success + end + + test "should get new" do + get new_comment_url + assert_response :success + end + + test "should create comment" do + assert_difference('Comment.count') do + post comments_url, params: { comment: { author_id: @comment.author_id, body: @comment.body, photo_id: @comment.photo_id } } + end + + assert_redirected_to comment_url(Comment.last) + end + + test "should show comment" do + get comment_url(@comment) + assert_response :success + end + + test "should get edit" do + get edit_comment_url(@comment) + assert_response :success + end + + test "should update comment" do + patch comment_url(@comment), params: { comment: { author_id: @comment.author_id, body: @comment.body, photo_id: @comment.photo_id } } + assert_redirected_to comment_url(@comment) + end + + test "should destroy comment" do + assert_difference('Comment.count', -1) do + delete comment_url(@comment) + end + + assert_redirected_to comments_url + end +end diff --git a/test/controllers/follow_requests_controller_test.rb b/test/controllers/follow_requests_controller_test.rb new file mode 100644 index 0000000..ab9e0a1 --- /dev/null +++ b/test/controllers/follow_requests_controller_test.rb @@ -0,0 +1,48 @@ +require "test_helper" + +class FollowRequestsControllerTest < ActionDispatch::IntegrationTest + setup do + @follow_request = follow_requests(:one) + end + + test "should get index" do + get follow_requests_url + assert_response :success + end + + test "should get new" do + get new_follow_request_url + assert_response :success + end + + test "should create follow_request" do + assert_difference('FollowRequest.count') do + post follow_requests_url, params: { follow_request: { recipient_id: @follow_request.recipient_id, sender_id: @follow_request.sender_id, status: @follow_request.status } } + end + + assert_redirected_to follow_request_url(FollowRequest.last) + end + + test "should show follow_request" do + get follow_request_url(@follow_request) + assert_response :success + end + + test "should get edit" do + get edit_follow_request_url(@follow_request) + assert_response :success + end + + test "should update follow_request" do + patch follow_request_url(@follow_request), params: { follow_request: { recipient_id: @follow_request.recipient_id, sender_id: @follow_request.sender_id, status: @follow_request.status } } + assert_redirected_to follow_request_url(@follow_request) + end + + test "should destroy follow_request" do + assert_difference('FollowRequest.count', -1) do + delete follow_request_url(@follow_request) + end + + assert_redirected_to follow_requests_url + end +end diff --git a/test/controllers/likes_controller_test.rb b/test/controllers/likes_controller_test.rb new file mode 100644 index 0000000..fc00f57 --- /dev/null +++ b/test/controllers/likes_controller_test.rb @@ -0,0 +1,48 @@ +require "test_helper" + +class LikesControllerTest < ActionDispatch::IntegrationTest + setup do + @like = likes(:one) + end + + test "should get index" do + get likes_url + assert_response :success + end + + test "should get new" do + get new_like_url + assert_response :success + end + + test "should create like" do + assert_difference('Like.count') do + post likes_url, params: { like: { fan_id: @like.fan_id, photo_id: @like.photo_id } } + end + + assert_redirected_to like_url(Like.last) + end + + test "should show like" do + get like_url(@like) + assert_response :success + end + + test "should get edit" do + get edit_like_url(@like) + assert_response :success + end + + test "should update like" do + patch like_url(@like), params: { like: { fan_id: @like.fan_id, photo_id: @like.photo_id } } + assert_redirected_to like_url(@like) + end + + test "should destroy like" do + assert_difference('Like.count', -1) do + delete like_url(@like) + end + + assert_redirected_to likes_url + end +end diff --git a/test/controllers/photos_controller_test.rb b/test/controllers/photos_controller_test.rb new file mode 100644 index 0000000..e228104 --- /dev/null +++ b/test/controllers/photos_controller_test.rb @@ -0,0 +1,48 @@ +require "test_helper" + +class PhotosControllerTest < ActionDispatch::IntegrationTest + setup do + @photo = photos(:one) + end + + test "should get index" do + get photos_url + assert_response :success + end + + test "should get new" do + get new_photo_url + assert_response :success + end + + test "should create photo" do + assert_difference('Photo.count') do + post photos_url, params: { photo: { caption: @photo.caption, comments_count: @photo.comments_count, image: @photo.image, likes_count: @photo.likes_count, owner_id: @photo.owner_id } } + end + + assert_redirected_to photo_url(Photo.last) + end + + test "should show photo" do + get photo_url(@photo) + assert_response :success + end + + test "should get edit" do + get edit_photo_url(@photo) + assert_response :success + end + + test "should update photo" do + patch photo_url(@photo), params: { photo: { caption: @photo.caption, comments_count: @photo.comments_count, image: @photo.image, likes_count: @photo.likes_count, owner_id: @photo.owner_id } } + assert_redirected_to photo_url(@photo) + end + + test "should destroy photo" do + assert_difference('Photo.count', -1) do + delete photo_url(@photo) + end + + assert_redirected_to photos_url + end +end diff --git a/test/fixtures/comments.yml b/test/fixtures/comments.yml new file mode 100644 index 0000000..f488a61 --- /dev/null +++ b/test/fixtures/comments.yml @@ -0,0 +1,11 @@ +# Read about fixtures at https://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html + +one: + author: one + photo: one + body: MyText + +two: + author: two + photo: two + body: MyText diff --git a/test/fixtures/follow_requests.yml b/test/fixtures/follow_requests.yml new file mode 100644 index 0000000..db2ea72 --- /dev/null +++ b/test/fixtures/follow_requests.yml @@ -0,0 +1,11 @@ +# Read about fixtures at https://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html + +one: + recipient: one + sender: one + status: MyString + +two: + recipient: two + sender: two + status: MyString diff --git a/test/fixtures/likes.yml b/test/fixtures/likes.yml new file mode 100644 index 0000000..1dbab99 --- /dev/null +++ b/test/fixtures/likes.yml @@ -0,0 +1,9 @@ +# Read about fixtures at https://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html + +one: + fan: one + photo: one + +two: + fan: two + photo: two diff --git a/test/fixtures/photos.yml b/test/fixtures/photos.yml new file mode 100644 index 0000000..d731231 --- /dev/null +++ b/test/fixtures/photos.yml @@ -0,0 +1,15 @@ +# Read about fixtures at https://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html + +one: + image: MyString + comments_count: 1 + likes_count: 1 + caption: MyText + owner: one + +two: + image: MyString + comments_count: 1 + likes_count: 1 + caption: MyText + owner: two diff --git a/test/models/comment_test.rb b/test/models/comment_test.rb new file mode 100644 index 0000000..5a6feda --- /dev/null +++ b/test/models/comment_test.rb @@ -0,0 +1,7 @@ +require "test_helper" + +class CommentTest < ActiveSupport::TestCase + # test "the truth" do + # assert true + # end +end diff --git a/test/models/follow_request_test.rb b/test/models/follow_request_test.rb new file mode 100644 index 0000000..23e7bda --- /dev/null +++ b/test/models/follow_request_test.rb @@ -0,0 +1,7 @@ +require "test_helper" + +class FollowRequestTest < ActiveSupport::TestCase + # test "the truth" do + # assert true + # end +end diff --git a/test/models/like_test.rb b/test/models/like_test.rb new file mode 100644 index 0000000..72d8d6c --- /dev/null +++ b/test/models/like_test.rb @@ -0,0 +1,7 @@ +require "test_helper" + +class LikeTest < ActiveSupport::TestCase + # test "the truth" do + # assert true + # end +end diff --git a/test/models/photo_test.rb b/test/models/photo_test.rb new file mode 100644 index 0000000..a7830c7 --- /dev/null +++ b/test/models/photo_test.rb @@ -0,0 +1,7 @@ +require "test_helper" + +class PhotoTest < ActiveSupport::TestCase + # test "the truth" do + # assert true + # end +end diff --git a/test/system/comments_test.rb b/test/system/comments_test.rb new file mode 100644 index 0000000..5f82906 --- /dev/null +++ b/test/system/comments_test.rb @@ -0,0 +1,47 @@ +require "application_system_test_case" + +class CommentsTest < ApplicationSystemTestCase + setup do + @comment = comments(:one) + end + + test "visiting the index" do + visit comments_url + assert_selector "h1", text: "Comments" + end + + test "creating a Comment" do + visit comments_url + click_on "New Comment" + + fill_in "Author", with: @comment.author_id + fill_in "Body", with: @comment.body + fill_in "Photo", with: @comment.photo_id + click_on "Create Comment" + + assert_text "Comment was successfully created" + click_on "Back" + end + + test "updating a Comment" do + visit comments_url + click_on "Edit", match: :first + + fill_in "Author", with: @comment.author_id + fill_in "Body", with: @comment.body + fill_in "Photo", with: @comment.photo_id + click_on "Update Comment" + + assert_text "Comment was successfully updated" + click_on "Back" + end + + test "destroying a Comment" do + visit comments_url + page.accept_confirm do + click_on "Destroy", match: :first + end + + assert_text "Comment was successfully destroyed" + end +end diff --git a/test/system/follow_requests_test.rb b/test/system/follow_requests_test.rb new file mode 100644 index 0000000..2485f7e --- /dev/null +++ b/test/system/follow_requests_test.rb @@ -0,0 +1,47 @@ +require "application_system_test_case" + +class FollowRequestsTest < ApplicationSystemTestCase + setup do + @follow_request = follow_requests(:one) + end + + test "visiting the index" do + visit follow_requests_url + assert_selector "h1", text: "Follow Requests" + end + + test "creating a Follow request" do + visit follow_requests_url + click_on "New Follow Request" + + fill_in "Recipient", with: @follow_request.recipient_id + fill_in "Sender", with: @follow_request.sender_id + fill_in "Status", with: @follow_request.status + click_on "Create Follow request" + + assert_text "Follow request was successfully created" + click_on "Back" + end + + test "updating a Follow request" do + visit follow_requests_url + click_on "Edit", match: :first + + fill_in "Recipient", with: @follow_request.recipient_id + fill_in "Sender", with: @follow_request.sender_id + fill_in "Status", with: @follow_request.status + click_on "Update Follow request" + + assert_text "Follow request was successfully updated" + click_on "Back" + end + + test "destroying a Follow request" do + visit follow_requests_url + page.accept_confirm do + click_on "Destroy", match: :first + end + + assert_text "Follow request was successfully destroyed" + end +end diff --git a/test/system/likes_test.rb b/test/system/likes_test.rb new file mode 100644 index 0000000..ad40f2a --- /dev/null +++ b/test/system/likes_test.rb @@ -0,0 +1,45 @@ +require "application_system_test_case" + +class LikesTest < ApplicationSystemTestCase + setup do + @like = likes(:one) + end + + test "visiting the index" do + visit likes_url + assert_selector "h1", text: "Likes" + end + + test "creating a Like" do + visit likes_url + click_on "New Like" + + fill_in "Fan", with: @like.fan_id + fill_in "Photo", with: @like.photo_id + click_on "Create Like" + + assert_text "Like was successfully created" + click_on "Back" + end + + test "updating a Like" do + visit likes_url + click_on "Edit", match: :first + + fill_in "Fan", with: @like.fan_id + fill_in "Photo", with: @like.photo_id + click_on "Update Like" + + assert_text "Like was successfully updated" + click_on "Back" + end + + test "destroying a Like" do + visit likes_url + page.accept_confirm do + click_on "Destroy", match: :first + end + + assert_text "Like was successfully destroyed" + end +end diff --git a/test/system/photos_test.rb b/test/system/photos_test.rb new file mode 100644 index 0000000..22ef121 --- /dev/null +++ b/test/system/photos_test.rb @@ -0,0 +1,51 @@ +require "application_system_test_case" + +class PhotosTest < ApplicationSystemTestCase + setup do + @photo = photos(:one) + end + + test "visiting the index" do + visit photos_url + assert_selector "h1", text: "Photos" + end + + test "creating a Photo" do + visit photos_url + click_on "New Photo" + + fill_in "Caption", with: @photo.caption + fill_in "Comments count", with: @photo.comments_count + fill_in "Image", with: @photo.image + fill_in "Likes count", with: @photo.likes_count + fill_in "Owner", with: @photo.owner_id + click_on "Create Photo" + + assert_text "Photo was successfully created" + click_on "Back" + end + + test "updating a Photo" do + visit photos_url + click_on "Edit", match: :first + + fill_in "Caption", with: @photo.caption + fill_in "Comments count", with: @photo.comments_count + fill_in "Image", with: @photo.image + fill_in "Likes count", with: @photo.likes_count + fill_in "Owner", with: @photo.owner_id + click_on "Update Photo" + + assert_text "Photo was successfully updated" + click_on "Back" + end + + test "destroying a Photo" do + visit photos_url + page.accept_confirm do + click_on "Destroy", match: :first + end + + assert_text "Photo was successfully destroyed" + end +end From 4fff75b375f734ac308406a840112a96769390df Mon Sep 17 00:00:00 2001 From: dmacinto Date: Sat, 21 Jan 2023 18:24:31 +0000 Subject: [PATCH 05/26] annotated models --- app/models/comment.rb | 11 +++++++++++ app/models/follow_request.rb | 11 +++++++++++ app/models/like.rb | 10 ++++++++++ app/models/photo.rb | 13 +++++++++++++ app/models/user.rb | 17 +++++++++++++++++ config/routes.rb | 12 +++++++++++- test/fixtures/comments.yml | 12 +++++++++++- test/fixtures/follow_requests.yml | 12 +++++++++++- test/fixtures/likes.yml | 11 ++++++++++- test/fixtures/photos.yml | 14 +++++++++++++- test/fixtures/users.yml | 18 +++++++++++++++++- test/models/comment_test.rb | 11 +++++++++++ test/models/follow_request_test.rb | 11 +++++++++++ test/models/like_test.rb | 10 ++++++++++ test/models/photo_test.rb | 13 +++++++++++++ test/models/user_test.rb | 17 +++++++++++++++++ 16 files changed, 197 insertions(+), 6 deletions(-) diff --git a/app/models/comment.rb b/app/models/comment.rb index 5a4e7dd..7f261fe 100644 --- a/app/models/comment.rb +++ b/app/models/comment.rb @@ -1,3 +1,14 @@ +# == Schema Information +# +# Table name: comments +# +# id :bigint not null, primary key +# author_id :bigint not null +# photo_id :bigint not null +# body :text +# created_at :datetime not null +# updated_at :datetime not null +# class Comment < ApplicationRecord belongs_to :author, class_name: "User", counter_cache: true belongs_to :photo, counter_cache: true diff --git a/app/models/follow_request.rb b/app/models/follow_request.rb index bbb9cc7..0cb70d5 100644 --- a/app/models/follow_request.rb +++ b/app/models/follow_request.rb @@ -1,3 +1,14 @@ +# == Schema Information +# +# Table name: follow_requests +# +# id :bigint not null, primary key +# recipient_id :bigint not null +# sender_id :bigint not null +# status :string +# created_at :datetime not null +# updated_at :datetime not null +# class FollowRequest < ApplicationRecord belongs_to :recipient, class_name: "User" belongs_to :sender, class_name: "User" diff --git a/app/models/like.rb b/app/models/like.rb index 28a7284..ca9cbf4 100644 --- a/app/models/like.rb +++ b/app/models/like.rb @@ -1,3 +1,13 @@ +# == Schema Information +# +# Table name: likes +# +# id :bigint not null, primary key +# fan_id :bigint not null +# photo_id :bigint not null +# created_at :datetime not null +# updated_at :datetime not null +# class Like < ApplicationRecord belongs_to :fan, class_name: "User", counter_cache: true belongs_to :photo, counter_cache: true diff --git a/app/models/photo.rb b/app/models/photo.rb index fe1e699..f3d6a48 100644 --- a/app/models/photo.rb +++ b/app/models/photo.rb @@ -1,3 +1,16 @@ +# == Schema Information +# +# Table name: photos +# +# id :bigint not null, primary key +# image :string +# comments_count :integer +# likes_count :integer +# caption :text +# owner_id :bigint not null +# created_at :datetime not null +# updated_at :datetime not null +# class Photo < ApplicationRecord belongs_to :owner, class_name: "User", counter_cache: true diff --git a/app/models/user.rb b/app/models/user.rb index 5b1fdd5..1323632 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -1,3 +1,20 @@ +# == Schema Information +# +# Table name: users +# +# id :bigint not null, primary key +# email :string default(""), not null +# encrypted_password :string default(""), not null +# reset_password_token :string +# reset_password_sent_at :datetime +# remember_created_at :datetime +# username :string +# private :boolean +# likes_count :integer +# comments_count :integer +# created_at :datetime not null +# updated_at :datetime not null +# class User < ApplicationRecord # Include default devise modules. Others available are: # :confirmable, :lockable, :timeoutable, :trackable and :omniauthable diff --git a/config/routes.rb b/config/routes.rb index 6e10146..12abb2c 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -1,9 +1,19 @@ +# == Route Map + Rails.application.routes.draw do + root "photos#index" + resources :likes resources :follow_requests resources :comments resources :photos devise_for :users # For details on the DSL available within this file, see https://guides.rubyonrails.org/routing.html - root to: "home#index" + + get ":username/liked" => "photos#liked", as: :liked_photos + get ":username/feed" => "users#feed", as: :user_feed + get ":username/followers" => "users#followers", as: :user_followers + get ":username/following" => "photos#following", as: :photo_following + + get "/:username" => "users#show", as: :user end diff --git a/test/fixtures/comments.yml b/test/fixtures/comments.yml index f488a61..751168a 100644 --- a/test/fixtures/comments.yml +++ b/test/fixtures/comments.yml @@ -1,4 +1,14 @@ -# Read about fixtures at https://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html +# == Schema Information +# +# Table name: comments +# +# id :bigint not null, primary key +# author_id :bigint not null +# photo_id :bigint not null +# body :text +# created_at :datetime not null +# updated_at :datetime not null +# one: author: one diff --git a/test/fixtures/follow_requests.yml b/test/fixtures/follow_requests.yml index db2ea72..8a73792 100644 --- a/test/fixtures/follow_requests.yml +++ b/test/fixtures/follow_requests.yml @@ -1,4 +1,14 @@ -# Read about fixtures at https://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html +# == Schema Information +# +# Table name: follow_requests +# +# id :bigint not null, primary key +# recipient_id :bigint not null +# sender_id :bigint not null +# status :string +# created_at :datetime not null +# updated_at :datetime not null +# one: recipient: one diff --git a/test/fixtures/likes.yml b/test/fixtures/likes.yml index 1dbab99..1545ee5 100644 --- a/test/fixtures/likes.yml +++ b/test/fixtures/likes.yml @@ -1,4 +1,13 @@ -# Read about fixtures at https://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html +# == Schema Information +# +# Table name: likes +# +# id :bigint not null, primary key +# fan_id :bigint not null +# photo_id :bigint not null +# created_at :datetime not null +# updated_at :datetime not null +# one: fan: one diff --git a/test/fixtures/photos.yml b/test/fixtures/photos.yml index d731231..8f3486d 100644 --- a/test/fixtures/photos.yml +++ b/test/fixtures/photos.yml @@ -1,4 +1,16 @@ -# Read about fixtures at https://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html +# == Schema Information +# +# Table name: photos +# +# id :bigint not null, primary key +# image :string +# comments_count :integer +# likes_count :integer +# caption :text +# owner_id :bigint not null +# created_at :datetime not null +# updated_at :datetime not null +# one: image: MyString diff --git a/test/fixtures/users.yml b/test/fixtures/users.yml index 5181636..05df2f4 100644 --- a/test/fixtures/users.yml +++ b/test/fixtures/users.yml @@ -1,4 +1,20 @@ -# 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 +# reset_password_token :string +# reset_password_sent_at :datetime +# remember_created_at :datetime +# username :string +# private :boolean +# likes_count :integer +# comments_count :integer +# created_at :datetime not null +# updated_at :datetime not null +# # 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/comment_test.rb b/test/models/comment_test.rb index 5a6feda..1331920 100644 --- a/test/models/comment_test.rb +++ b/test/models/comment_test.rb @@ -1,3 +1,14 @@ +# == Schema Information +# +# Table name: comments +# +# id :bigint not null, primary key +# author_id :bigint not null +# photo_id :bigint not null +# body :text +# created_at :datetime not null +# updated_at :datetime not null +# require "test_helper" class CommentTest < ActiveSupport::TestCase diff --git a/test/models/follow_request_test.rb b/test/models/follow_request_test.rb index 23e7bda..8264314 100644 --- a/test/models/follow_request_test.rb +++ b/test/models/follow_request_test.rb @@ -1,3 +1,14 @@ +# == Schema Information +# +# Table name: follow_requests +# +# id :bigint not null, primary key +# recipient_id :bigint not null +# sender_id :bigint not null +# status :string +# created_at :datetime not null +# updated_at :datetime not null +# require "test_helper" class FollowRequestTest < ActiveSupport::TestCase diff --git a/test/models/like_test.rb b/test/models/like_test.rb index 72d8d6c..5c49c4f 100644 --- a/test/models/like_test.rb +++ b/test/models/like_test.rb @@ -1,3 +1,13 @@ +# == Schema Information +# +# Table name: likes +# +# id :bigint not null, primary key +# fan_id :bigint not null +# photo_id :bigint not null +# created_at :datetime not null +# updated_at :datetime not null +# require "test_helper" class LikeTest < ActiveSupport::TestCase diff --git a/test/models/photo_test.rb b/test/models/photo_test.rb index a7830c7..7fb832f 100644 --- a/test/models/photo_test.rb +++ b/test/models/photo_test.rb @@ -1,3 +1,16 @@ +# == Schema Information +# +# Table name: photos +# +# id :bigint not null, primary key +# image :string +# comments_count :integer +# likes_count :integer +# caption :text +# owner_id :bigint not null +# created_at :datetime not null +# updated_at :datetime not null +# require "test_helper" class PhotoTest < ActiveSupport::TestCase diff --git a/test/models/user_test.rb b/test/models/user_test.rb index 5c07f49..bdc2f5b 100644 --- a/test/models/user_test.rb +++ b/test/models/user_test.rb @@ -1,3 +1,20 @@ +# == Schema Information +# +# Table name: users +# +# id :bigint not null, primary key +# email :string default(""), not null +# encrypted_password :string default(""), not null +# reset_password_token :string +# reset_password_sent_at :datetime +# remember_created_at :datetime +# username :string +# private :boolean +# likes_count :integer +# comments_count :integer +# created_at :datetime not null +# updated_at :datetime not null +# require "test_helper" class UserTest < ActiveSupport::TestCase From ee85483f5ae493898ce164718c936669eb8732c3 Mon Sep 17 00:00:00 2001 From: dmacinto Date: Sat, 21 Jan 2023 18:44:41 +0000 Subject: [PATCH 06/26] removed photogram elements --- app/models/comment.rb | 17 ---- app/models/follow_request.rb | 24 ----- app/models/like.rb | 16 --- app/models/photo.rb | 30 ------ app/views/comments/_comment.json.jbuilder | 2 - app/views/comments/_form.html.erb | 32 ------ app/views/comments/edit.html.erb | 6 -- app/views/comments/index.html.erb | 31 ------ app/views/comments/index.json.jbuilder | 1 - app/views/comments/new.html.erb | 5 - app/views/comments/show.html.erb | 19 ---- app/views/comments/show.json.jbuilder | 1 - .../_follow_request.json.jbuilder | 2 - app/views/follow_requests/_form.html.erb | 32 ------ app/views/follow_requests/edit.html.erb | 6 -- app/views/follow_requests/index.html.erb | 31 ------ app/views/follow_requests/index.json.jbuilder | 1 - app/views/follow_requests/new.html.erb | 5 - app/views/follow_requests/show.html.erb | 19 ---- app/views/follow_requests/show.json.jbuilder | 1 - app/views/likes/_form.html.erb | 27 ----- app/views/likes/_like.json.jbuilder | 2 - app/views/likes/edit.html.erb | 6 -- app/views/likes/index.html.erb | 29 ------ app/views/likes/index.json.jbuilder | 1 - app/views/likes/new.html.erb | 5 - app/views/likes/show.html.erb | 14 --- app/views/likes/show.json.jbuilder | 1 - app/views/photos/_form.html.erb | 42 -------- app/views/photos/_photo.json.jbuilder | 2 - app/views/photos/edit.html.erb | 6 -- app/views/photos/index.html.erb | 35 ------- app/views/photos/index.json.jbuilder | 1 - app/views/photos/new.html.erb | 5 - app/views/photos/show.html.erb | 29 ------ app/views/photos/show.json.jbuilder | 1 - config/routes.rb | 10 +- db/migrate/20230121180727_create_photos.rb | 13 --- db/migrate/20230121181109_create_comments.rb | 11 --- .../20230121181118_create_follow_requests.rb | 11 --- db/migrate/20230121181127_create_likes.rb | 10 -- db/schema.rb | 47 --------- lib/tasks/dev.rake | 99 +++++++++++++++++++ 43 files changed, 100 insertions(+), 588 deletions(-) delete mode 100644 app/models/comment.rb delete mode 100644 app/models/follow_request.rb delete mode 100644 app/models/like.rb delete mode 100644 app/models/photo.rb delete mode 100644 app/views/comments/_comment.json.jbuilder delete mode 100644 app/views/comments/_form.html.erb delete mode 100644 app/views/comments/edit.html.erb delete mode 100644 app/views/comments/index.html.erb delete mode 100644 app/views/comments/index.json.jbuilder delete mode 100644 app/views/comments/new.html.erb delete mode 100644 app/views/comments/show.html.erb delete mode 100644 app/views/comments/show.json.jbuilder delete mode 100644 app/views/follow_requests/_follow_request.json.jbuilder delete mode 100644 app/views/follow_requests/_form.html.erb delete mode 100644 app/views/follow_requests/edit.html.erb delete mode 100644 app/views/follow_requests/index.html.erb delete mode 100644 app/views/follow_requests/index.json.jbuilder delete mode 100644 app/views/follow_requests/new.html.erb delete mode 100644 app/views/follow_requests/show.html.erb delete mode 100644 app/views/follow_requests/show.json.jbuilder delete mode 100644 app/views/likes/_form.html.erb delete mode 100644 app/views/likes/_like.json.jbuilder delete mode 100644 app/views/likes/edit.html.erb delete mode 100644 app/views/likes/index.html.erb delete mode 100644 app/views/likes/index.json.jbuilder delete mode 100644 app/views/likes/new.html.erb delete mode 100644 app/views/likes/show.html.erb delete mode 100644 app/views/likes/show.json.jbuilder delete mode 100644 app/views/photos/_form.html.erb delete mode 100644 app/views/photos/_photo.json.jbuilder delete mode 100644 app/views/photos/edit.html.erb delete mode 100644 app/views/photos/index.html.erb delete mode 100644 app/views/photos/index.json.jbuilder delete mode 100644 app/views/photos/new.html.erb delete mode 100644 app/views/photos/show.html.erb delete mode 100644 app/views/photos/show.json.jbuilder delete mode 100644 db/migrate/20230121180727_create_photos.rb delete mode 100644 db/migrate/20230121181109_create_comments.rb delete mode 100644 db/migrate/20230121181118_create_follow_requests.rb delete mode 100644 db/migrate/20230121181127_create_likes.rb create mode 100644 lib/tasks/dev.rake diff --git a/app/models/comment.rb b/app/models/comment.rb deleted file mode 100644 index 7f261fe..0000000 --- a/app/models/comment.rb +++ /dev/null @@ -1,17 +0,0 @@ -# == Schema Information -# -# Table name: comments -# -# id :bigint not null, primary key -# author_id :bigint not null -# photo_id :bigint not null -# body :text -# created_at :datetime not null -# updated_at :datetime not null -# -class Comment < ApplicationRecord - belongs_to :author, class_name: "User", counter_cache: true - belongs_to :photo, counter_cache: true - - validates :body, presence: true -end diff --git a/app/models/follow_request.rb b/app/models/follow_request.rb deleted file mode 100644 index 0cb70d5..0000000 --- a/app/models/follow_request.rb +++ /dev/null @@ -1,24 +0,0 @@ -# == Schema Information -# -# Table name: follow_requests -# -# id :bigint not null, primary key -# recipient_id :bigint not null -# sender_id :bigint not null -# status :string -# created_at :datetime not null -# updated_at :datetime not null -# -class FollowRequest < ApplicationRecord - belongs_to :recipient, class_name: "User" - belongs_to :sender, class_name: "User" - - enum status: { pending: "pending", rejected: "rejected", accepted: "accepted" } - - scope :accepted, -> { where(status: "accepted" ) } - scope :not_accepted, -> { where.not(status: "accepted" ) } - - validates :user_id, uniqueness: { scope: :sender_id, message: "has already sent a follow request" } - - validates :user_id, uniqueness: { scope: :recipient_id, message: "has already received a follow request" } -end diff --git a/app/models/like.rb b/app/models/like.rb deleted file mode 100644 index ca9cbf4..0000000 --- a/app/models/like.rb +++ /dev/null @@ -1,16 +0,0 @@ -# == Schema Information -# -# Table name: likes -# -# id :bigint not null, primary key -# fan_id :bigint not null -# photo_id :bigint not null -# created_at :datetime not null -# updated_at :datetime not null -# -class Like < ApplicationRecord - belongs_to :fan, class_name: "User", counter_cache: true - belongs_to :photo, counter_cache: true - - validates :user_id, uniqueness: { scope: :photo_id, message: "has already liked this photo" } -end diff --git a/app/models/photo.rb b/app/models/photo.rb deleted file mode 100644 index f3d6a48..0000000 --- a/app/models/photo.rb +++ /dev/null @@ -1,30 +0,0 @@ -# == Schema Information -# -# Table name: photos -# -# id :bigint not null, primary key -# image :string -# comments_count :integer -# likes_count :integer -# caption :text -# owner_id :bigint not null -# created_at :datetime not null -# updated_at :datetime not null -# -class Photo < ApplicationRecord - belongs_to :owner, class_name: "User", counter_cache: true - - has_many :comments - - has_many :likes - - has_many :fans, through: :likes - - validates :caption, presence: true - - validates :image, presence: true - - scope :past_week, -> { where(created_at: 1.week.ago...) } - - scope :by_likes, -> { order(likes_count: :desc) } -end diff --git a/app/views/comments/_comment.json.jbuilder b/app/views/comments/_comment.json.jbuilder deleted file mode 100644 index 64674ae..0000000 --- a/app/views/comments/_comment.json.jbuilder +++ /dev/null @@ -1,2 +0,0 @@ -json.extract! comment, :id, :author_id, :photo_id, :body, :created_at, :updated_at -json.url comment_url(comment, format: :json) diff --git a/app/views/comments/_form.html.erb b/app/views/comments/_form.html.erb deleted file mode 100644 index 3a569ea..0000000 --- a/app/views/comments/_form.html.erb +++ /dev/null @@ -1,32 +0,0 @@ -<%= form_with(model: comment) do |form| %> - <% if comment.errors.any? %> -
-

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

- -
    - <% comment.errors.each do |error| %> -
  • <%= error.full_message %>
  • - <% end %> -
-
- <% end %> - -
- <%= form.label :author_id %> - <%= form.text_field :author_id %> -
- -
- <%= form.label :photo_id %> - <%= form.text_field :photo_id %> -
- -
- <%= form.label :body %> - <%= form.text_area :body %> -
- -
- <%= form.submit %> -
-<% end %> diff --git a/app/views/comments/edit.html.erb b/app/views/comments/edit.html.erb deleted file mode 100644 index a20be1d..0000000 --- a/app/views/comments/edit.html.erb +++ /dev/null @@ -1,6 +0,0 @@ -

Editing Comment

- -<%= render 'form', comment: @comment %> - -<%= link_to 'Show', @comment %> | -<%= link_to 'Back', comments_path %> diff --git a/app/views/comments/index.html.erb b/app/views/comments/index.html.erb deleted file mode 100644 index 1f167e4..0000000 --- a/app/views/comments/index.html.erb +++ /dev/null @@ -1,31 +0,0 @@ -

<%= notice %>

- -

Comments

- - - - - - - - - - - - - <% @comments.each do |comment| %> - - - - - - - - - <% end %> - -
AuthorPhotoBody
<%= comment.author_id %><%= comment.photo_id %><%= comment.body %><%= link_to 'Show', comment %><%= link_to 'Edit', edit_comment_path(comment) %><%= link_to 'Destroy', comment, method: :delete, data: { confirm: 'Are you sure?' } %>
- -
- -<%= link_to 'New Comment', new_comment_path %> diff --git a/app/views/comments/index.json.jbuilder b/app/views/comments/index.json.jbuilder deleted file mode 100644 index e3322af..0000000 --- a/app/views/comments/index.json.jbuilder +++ /dev/null @@ -1 +0,0 @@ -json.array! @comments, partial: "comments/comment", as: :comment diff --git a/app/views/comments/new.html.erb b/app/views/comments/new.html.erb deleted file mode 100644 index f8b43b6..0000000 --- a/app/views/comments/new.html.erb +++ /dev/null @@ -1,5 +0,0 @@ -

New Comment

- -<%= render 'form', comment: @comment %> - -<%= link_to 'Back', comments_path %> diff --git a/app/views/comments/show.html.erb b/app/views/comments/show.html.erb deleted file mode 100644 index 58fbc00..0000000 --- a/app/views/comments/show.html.erb +++ /dev/null @@ -1,19 +0,0 @@ -

<%= notice %>

- -

- Author: - <%= @comment.author_id %> -

- -

- Photo: - <%= @comment.photo_id %> -

- -

- Body: - <%= @comment.body %> -

- -<%= link_to 'Edit', edit_comment_path(@comment) %> | -<%= link_to 'Back', comments_path %> diff --git a/app/views/comments/show.json.jbuilder b/app/views/comments/show.json.jbuilder deleted file mode 100644 index 78a9099..0000000 --- a/app/views/comments/show.json.jbuilder +++ /dev/null @@ -1 +0,0 @@ -json.partial! "comments/comment", comment: @comment diff --git a/app/views/follow_requests/_follow_request.json.jbuilder b/app/views/follow_requests/_follow_request.json.jbuilder deleted file mode 100644 index d7dd54c..0000000 --- a/app/views/follow_requests/_follow_request.json.jbuilder +++ /dev/null @@ -1,2 +0,0 @@ -json.extract! follow_request, :id, :recipient_id, :sender_id, :status, :created_at, :updated_at -json.url follow_request_url(follow_request, format: :json) diff --git a/app/views/follow_requests/_form.html.erb b/app/views/follow_requests/_form.html.erb deleted file mode 100644 index 323c21c..0000000 --- a/app/views/follow_requests/_form.html.erb +++ /dev/null @@ -1,32 +0,0 @@ -<%= form_with(model: follow_request) do |form| %> - <% if follow_request.errors.any? %> -
-

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

- -
    - <% follow_request.errors.each do |error| %> -
  • <%= error.full_message %>
  • - <% end %> -
-
- <% end %> - -
- <%= form.label :recipient_id %> - <%= form.text_field :recipient_id %> -
- -
- <%= form.label :sender_id %> - <%= form.text_field :sender_id %> -
- -
- <%= form.label :status %> - <%= form.text_field :status %> -
- -
- <%= form.submit %> -
-<% end %> diff --git a/app/views/follow_requests/edit.html.erb b/app/views/follow_requests/edit.html.erb deleted file mode 100644 index 54c3b9c..0000000 --- a/app/views/follow_requests/edit.html.erb +++ /dev/null @@ -1,6 +0,0 @@ -

Editing Follow Request

- -<%= render 'form', follow_request: @follow_request %> - -<%= link_to 'Show', @follow_request %> | -<%= link_to 'Back', follow_requests_path %> diff --git a/app/views/follow_requests/index.html.erb b/app/views/follow_requests/index.html.erb deleted file mode 100644 index dc14b41..0000000 --- a/app/views/follow_requests/index.html.erb +++ /dev/null @@ -1,31 +0,0 @@ -

<%= notice %>

- -

Follow Requests

- - - - - - - - - - - - - <% @follow_requests.each do |follow_request| %> - - - - - - - - - <% end %> - -
RecipientSenderStatus
<%= follow_request.recipient_id %><%= follow_request.sender_id %><%= follow_request.status %><%= link_to 'Show', follow_request %><%= link_to 'Edit', edit_follow_request_path(follow_request) %><%= link_to 'Destroy', follow_request, method: :delete, data: { confirm: 'Are you sure?' } %>
- -
- -<%= link_to 'New Follow Request', new_follow_request_path %> diff --git a/app/views/follow_requests/index.json.jbuilder b/app/views/follow_requests/index.json.jbuilder deleted file mode 100644 index b45255d..0000000 --- a/app/views/follow_requests/index.json.jbuilder +++ /dev/null @@ -1 +0,0 @@ -json.array! @follow_requests, partial: "follow_requests/follow_request", as: :follow_request diff --git a/app/views/follow_requests/new.html.erb b/app/views/follow_requests/new.html.erb deleted file mode 100644 index b06ca67..0000000 --- a/app/views/follow_requests/new.html.erb +++ /dev/null @@ -1,5 +0,0 @@ -

New Follow Request

- -<%= render 'form', follow_request: @follow_request %> - -<%= link_to 'Back', follow_requests_path %> diff --git a/app/views/follow_requests/show.html.erb b/app/views/follow_requests/show.html.erb deleted file mode 100644 index 7d94d12..0000000 --- a/app/views/follow_requests/show.html.erb +++ /dev/null @@ -1,19 +0,0 @@ -

<%= notice %>

- -

- Recipient: - <%= @follow_request.recipient_id %> -

- -

- Sender: - <%= @follow_request.sender_id %> -

- -

- Status: - <%= @follow_request.status %> -

- -<%= link_to 'Edit', edit_follow_request_path(@follow_request) %> | -<%= link_to 'Back', follow_requests_path %> diff --git a/app/views/follow_requests/show.json.jbuilder b/app/views/follow_requests/show.json.jbuilder deleted file mode 100644 index 3fb15c1..0000000 --- a/app/views/follow_requests/show.json.jbuilder +++ /dev/null @@ -1 +0,0 @@ -json.partial! "follow_requests/follow_request", follow_request: @follow_request diff --git a/app/views/likes/_form.html.erb b/app/views/likes/_form.html.erb deleted file mode 100644 index aa444ea..0000000 --- a/app/views/likes/_form.html.erb +++ /dev/null @@ -1,27 +0,0 @@ -<%= form_with(model: like) do |form| %> - <% if like.errors.any? %> -
-

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

- -
    - <% like.errors.each do |error| %> -
  • <%= error.full_message %>
  • - <% end %> -
-
- <% end %> - -
- <%= form.label :fan_id %> - <%= form.text_field :fan_id %> -
- -
- <%= form.label :photo_id %> - <%= form.text_field :photo_id %> -
- -
- <%= form.submit %> -
-<% end %> diff --git a/app/views/likes/_like.json.jbuilder b/app/views/likes/_like.json.jbuilder deleted file mode 100644 index f89aea4..0000000 --- a/app/views/likes/_like.json.jbuilder +++ /dev/null @@ -1,2 +0,0 @@ -json.extract! like, :id, :fan_id, :photo_id, :created_at, :updated_at -json.url like_url(like, format: :json) diff --git a/app/views/likes/edit.html.erb b/app/views/likes/edit.html.erb deleted file mode 100644 index 622df3d..0000000 --- a/app/views/likes/edit.html.erb +++ /dev/null @@ -1,6 +0,0 @@ -

Editing Like

- -<%= render 'form', like: @like %> - -<%= link_to 'Show', @like %> | -<%= link_to 'Back', likes_path %> diff --git a/app/views/likes/index.html.erb b/app/views/likes/index.html.erb deleted file mode 100644 index 3be87f7..0000000 --- a/app/views/likes/index.html.erb +++ /dev/null @@ -1,29 +0,0 @@ -

<%= notice %>

- -

Likes

- - - - - - - - - - - - <% @likes.each do |like| %> - - - - - - - - <% end %> - -
FanPhoto
<%= like.fan_id %><%= like.photo_id %><%= link_to 'Show', like %><%= link_to 'Edit', edit_like_path(like) %><%= link_to 'Destroy', like, method: :delete, data: { confirm: 'Are you sure?' } %>
- -
- -<%= link_to 'New Like', new_like_path %> diff --git a/app/views/likes/index.json.jbuilder b/app/views/likes/index.json.jbuilder deleted file mode 100644 index 721d476..0000000 --- a/app/views/likes/index.json.jbuilder +++ /dev/null @@ -1 +0,0 @@ -json.array! @likes, partial: "likes/like", as: :like diff --git a/app/views/likes/new.html.erb b/app/views/likes/new.html.erb deleted file mode 100644 index 0643826..0000000 --- a/app/views/likes/new.html.erb +++ /dev/null @@ -1,5 +0,0 @@ -

New Like

- -<%= render 'form', like: @like %> - -<%= link_to 'Back', likes_path %> diff --git a/app/views/likes/show.html.erb b/app/views/likes/show.html.erb deleted file mode 100644 index 9c4752e..0000000 --- a/app/views/likes/show.html.erb +++ /dev/null @@ -1,14 +0,0 @@ -

<%= notice %>

- -

- Fan: - <%= @like.fan_id %> -

- -

- Photo: - <%= @like.photo_id %> -

- -<%= link_to 'Edit', edit_like_path(@like) %> | -<%= link_to 'Back', likes_path %> diff --git a/app/views/likes/show.json.jbuilder b/app/views/likes/show.json.jbuilder deleted file mode 100644 index 587277f..0000000 --- a/app/views/likes/show.json.jbuilder +++ /dev/null @@ -1 +0,0 @@ -json.partial! "likes/like", like: @like diff --git a/app/views/photos/_form.html.erb b/app/views/photos/_form.html.erb deleted file mode 100644 index ea4c5e6..0000000 --- a/app/views/photos/_form.html.erb +++ /dev/null @@ -1,42 +0,0 @@ -<%= form_with(model: photo) do |form| %> - <% if photo.errors.any? %> -
-

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

- -
    - <% photo.errors.each do |error| %> -
  • <%= error.full_message %>
  • - <% end %> -
-
- <% end %> - -
- <%= form.label :image %> - <%= form.text_field :image %> -
- -
- <%= form.label :comments_count %> - <%= form.number_field :comments_count %> -
- -
- <%= form.label :likes_count %> - <%= form.number_field :likes_count %> -
- -
- <%= form.label :caption %> - <%= form.text_area :caption %> -
- -
- <%= form.label :owner_id %> - <%= form.text_field :owner_id %> -
- -
- <%= form.submit %> -
-<% end %> diff --git a/app/views/photos/_photo.json.jbuilder b/app/views/photos/_photo.json.jbuilder deleted file mode 100644 index fad93a0..0000000 --- a/app/views/photos/_photo.json.jbuilder +++ /dev/null @@ -1,2 +0,0 @@ -json.extract! photo, :id, :image, :comments_count, :likes_count, :caption, :owner_id, :created_at, :updated_at -json.url photo_url(photo, format: :json) diff --git a/app/views/photos/edit.html.erb b/app/views/photos/edit.html.erb deleted file mode 100644 index 688c99c..0000000 --- a/app/views/photos/edit.html.erb +++ /dev/null @@ -1,6 +0,0 @@ -

Editing Photo

- -<%= render 'form', photo: @photo %> - -<%= link_to 'Show', @photo %> | -<%= link_to 'Back', photos_path %> diff --git a/app/views/photos/index.html.erb b/app/views/photos/index.html.erb deleted file mode 100644 index a8e955c..0000000 --- a/app/views/photos/index.html.erb +++ /dev/null @@ -1,35 +0,0 @@ -

<%= notice %>

- -

Photos

- - - - - - - - - - - - - - - <% @photos.each do |photo| %> - - - - - - - - - - - <% end %> - -
ImageComments countLikes countCaptionOwner
<%= photo.image %><%= photo.comments_count %><%= photo.likes_count %><%= photo.caption %><%= photo.owner_id %><%= link_to 'Show', photo %><%= link_to 'Edit', edit_photo_path(photo) %><%= link_to 'Destroy', photo, method: :delete, data: { confirm: 'Are you sure?' } %>
- -
- -<%= link_to 'New Photo', new_photo_path %> diff --git a/app/views/photos/index.json.jbuilder b/app/views/photos/index.json.jbuilder deleted file mode 100644 index 24d84b8..0000000 --- a/app/views/photos/index.json.jbuilder +++ /dev/null @@ -1 +0,0 @@ -json.array! @photos, partial: "photos/photo", as: :photo diff --git a/app/views/photos/new.html.erb b/app/views/photos/new.html.erb deleted file mode 100644 index b9a62c0..0000000 --- a/app/views/photos/new.html.erb +++ /dev/null @@ -1,5 +0,0 @@ -

New Photo

- -<%= render 'form', photo: @photo %> - -<%= link_to 'Back', photos_path %> diff --git a/app/views/photos/show.html.erb b/app/views/photos/show.html.erb deleted file mode 100644 index 30bece6..0000000 --- a/app/views/photos/show.html.erb +++ /dev/null @@ -1,29 +0,0 @@ -

<%= notice %>

- -

- Image: - <%= @photo.image %> -

- -

- Comments count: - <%= @photo.comments_count %> -

- -

- Likes count: - <%= @photo.likes_count %> -

- -

- Caption: - <%= @photo.caption %> -

- -

- Owner: - <%= @photo.owner_id %> -

- -<%= link_to 'Edit', edit_photo_path(@photo) %> | -<%= link_to 'Back', photos_path %> diff --git a/app/views/photos/show.json.jbuilder b/app/views/photos/show.json.jbuilder deleted file mode 100644 index 5c0f6ff..0000000 --- a/app/views/photos/show.json.jbuilder +++ /dev/null @@ -1 +0,0 @@ -json.partial! "photos/photo", photo: @photo diff --git a/config/routes.rb b/config/routes.rb index 12abb2c..bb53cbf 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -1,19 +1,11 @@ # == Route Map Rails.application.routes.draw do - root "photos#index" + root "tasks#index" - resources :likes - resources :follow_requests - resources :comments - resources :photos devise_for :users # For details on the DSL available within this file, see https://guides.rubyonrails.org/routing.html - get ":username/liked" => "photos#liked", as: :liked_photos - get ":username/feed" => "users#feed", as: :user_feed - get ":username/followers" => "users#followers", as: :user_followers - get ":username/following" => "photos#following", as: :photo_following get "/:username" => "users#show", as: :user end diff --git a/db/migrate/20230121180727_create_photos.rb b/db/migrate/20230121180727_create_photos.rb deleted file mode 100644 index 48bbef4..0000000 --- a/db/migrate/20230121180727_create_photos.rb +++ /dev/null @@ -1,13 +0,0 @@ -class CreatePhotos < ActiveRecord::Migration[6.1] - def change - create_table :photos do |t| - t.string :image - t.integer :comments_count - t.integer :likes_count - t.text :caption - t.references :owner, null: false, foreign_key: { to_table: :users}, index: true - - t.timestamps - end - end -end diff --git a/db/migrate/20230121181109_create_comments.rb b/db/migrate/20230121181109_create_comments.rb deleted file mode 100644 index 2e703da..0000000 --- a/db/migrate/20230121181109_create_comments.rb +++ /dev/null @@ -1,11 +0,0 @@ -class CreateComments < ActiveRecord::Migration[6.1] - def change - create_table :comments do |t| - t.references :author, null: false, foreign_key: { to_table: :users} - t.references :photo, null: false, foreign_key: true - t.text :body - - t.timestamps - end - end -end diff --git a/db/migrate/20230121181118_create_follow_requests.rb b/db/migrate/20230121181118_create_follow_requests.rb deleted file mode 100644 index 75743ba..0000000 --- a/db/migrate/20230121181118_create_follow_requests.rb +++ /dev/null @@ -1,11 +0,0 @@ -class CreateFollowRequests < ActiveRecord::Migration[6.1] - def change - create_table :follow_requests do |t| - t.references :recipient, null: false, foreign_key: { to_table: :users} - t.references :sender, null: false, foreign_key: { to_table: :users} - t.string :status - - t.timestamps - end - end -end diff --git a/db/migrate/20230121181127_create_likes.rb b/db/migrate/20230121181127_create_likes.rb deleted file mode 100644 index f10df11..0000000 --- a/db/migrate/20230121181127_create_likes.rb +++ /dev/null @@ -1,10 +0,0 @@ -class CreateLikes < ActiveRecord::Migration[6.1] - def change - create_table :likes do |t| - t.references :fan, null: false, foreign_key: { to_table: :users} - t.references :photo, null: false, foreign_key: true - - t.timestamps - end - end -end diff --git a/db/schema.rb b/db/schema.rb index 59f7bff..f956060 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -15,46 +15,6 @@ # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" - create_table "comments", force: :cascade do |t| - t.bigint "author_id", null: false - t.bigint "photo_id", null: false - t.text "body" - t.datetime "created_at", precision: 6, null: false - t.datetime "updated_at", precision: 6, null: false - t.index ["author_id"], name: "index_comments_on_author_id" - t.index ["photo_id"], name: "index_comments_on_photo_id" - end - - create_table "follow_requests", force: :cascade do |t| - t.bigint "recipient_id", null: false - t.bigint "sender_id", null: false - t.string "status" - t.datetime "created_at", precision: 6, null: false - t.datetime "updated_at", precision: 6, null: false - t.index ["recipient_id"], name: "index_follow_requests_on_recipient_id" - t.index ["sender_id"], name: "index_follow_requests_on_sender_id" - end - - create_table "likes", force: :cascade do |t| - t.bigint "fan_id", null: false - t.bigint "photo_id", null: false - t.datetime "created_at", precision: 6, null: false - t.datetime "updated_at", precision: 6, null: false - t.index ["fan_id"], name: "index_likes_on_fan_id" - t.index ["photo_id"], name: "index_likes_on_photo_id" - end - - create_table "photos", force: :cascade do |t| - t.string "image" - t.integer "comments_count" - t.integer "likes_count" - t.text "caption" - t.bigint "owner_id", null: false - t.datetime "created_at", precision: 6, null: false - t.datetime "updated_at", precision: 6, null: false - t.index ["owner_id"], name: "index_photos_on_owner_id" - end - create_table "users", force: :cascade do |t| t.string "email", default: "", null: false t.string "encrypted_password", default: "", null: false @@ -71,11 +31,4 @@ t.index ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true end - add_foreign_key "comments", "photos" - add_foreign_key "comments", "users", column: "author_id" - add_foreign_key "follow_requests", "users", column: "recipient_id" - add_foreign_key "follow_requests", "users", column: "sender_id" - add_foreign_key "likes", "photos" - add_foreign_key "likes", "users", column: "fan_id" - add_foreign_key "photos", "users", column: "owner_id" end diff --git a/lib/tasks/dev.rake b/lib/tasks/dev.rake new file mode 100644 index 0000000..dbd8d08 --- /dev/null +++ b/lib/tasks/dev.rake @@ -0,0 +1,99 @@ +task sample_data: :environment do + p "Creating sample data" + + if Rails.env.development? + FollowRequest.delete_all + Comment.delete_all + Like.delete_all + Photo.delete_all + User.delete_all + end + + + usernames = Array.new { Faker::Name.first_name } + + usernames << "alice" + usernames << "bob" + usernames << "charlie" + usernames << "doug" + usernames << "eric" + usernames << "frank" + usernames << "george" + + + usernames.each do |username| + User.create( + email: "#{username}@example.com", + password: "password", + username: username.downcase, + private: [true, false].sample, + + + ) + end + + + +# 10.times do +# name = Faker::Name.first_name +# User.create( +# email: "#{name}@example.com", +# password: "password", +# username: name.downcase, +# private: [true, false].sample, +# ) + #p u.errors.full_messages +# end +# p "#{User.count} users have been created." + + + users = User.all + + users.each do |first_user| + users.each do |second_user| + if rand < 0.75 + first_user.sent_follow_requests.create( + recipient: second_user, + status: FollowRequest.statuses.keys.sample + ) + end + + if rand < 0.75 + second_user.sent_follow_requests.create( + recipient: first_user, + status: FollowRequest.statuses.keys.sample + ) + end + end + end + + users.each do |user| + rand(15).times do + photo = user.own_photos.create( + caption: Faker::Quote.jack_handey, + image: "https://robohash.org/#{rand(9999)}" + ) + + user.followers.each do |follower| + if rand < 0.5 + photo.fans << follower + end + + if rand < 0.25 + photo.comments.create( + body: Faker::Quote.jack_handey, + author: follower + ) + end + end + 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 #{FollowRequest.count} follow requests." + p "There are now #{Photo.count} photos." + p "There are now #{Like.count} likes." + p "There are now #{Comment.count} comments." +end From f6a03ad3edc2513d61a2a8cb56ed39563ed29069 Mon Sep 17 00:00:00 2001 From: dmacinto Date: Sat, 21 Jan 2023 19:03:28 +0000 Subject: [PATCH 07/26] built tasks erased photogram --- app/assets/stylesheets/comments.scss | 3 - app/assets/stylesheets/follow_requests.scss | 3 - app/assets/stylesheets/photos.scss | 3 - app/assets/stylesheets/scaffolds.scss | 65 ----------------- .../stylesheets/{likes.scss => tasks.scss} | 2 +- app/controllers/comments_controller.rb | 70 ------------------- app/controllers/follow_requests_controller.rb | 70 ------------------- app/controllers/likes_controller.rb | 70 ------------------- app/controllers/photos_controller.rb | 70 ------------------- app/controllers/tasks_controller.rb | 70 +++++++++++++++++++ app/helpers/tasks_helper.rb | 2 + app/models/task.rb | 3 + app/views/tasks/_form.html.erb | 37 ++++++++++ app/views/tasks/_task.json.jbuilder | 2 + app/views/tasks/edit.html.erb | 6 ++ app/views/tasks/index.html.erb | 33 +++++++++ app/views/tasks/index.json.jbuilder | 1 + app/views/tasks/new.html.erb | 5 ++ app/views/tasks/show.html.erb | 24 +++++++ app/views/tasks/show.json.jbuilder | 1 + config/routes.rb | 1 + .../20230121175413_devise_create_users.rb | 9 ++- db/migrate/20230121185324_create_tasks.rb | 12 ++++ db/schema.rb | 16 ++++- test/controllers/comments_controller_test.rb | 48 ------------- .../follow_requests_controller_test.rb | 48 ------------- test/controllers/likes_controller_test.rb | 48 ------------- test/controllers/photos_controller_test.rb | 48 ------------- test/controllers/tasks_controller_test.rb | 48 +++++++++++++ test/fixtures/comments.yml | 21 ------ test/fixtures/follow_requests.yml | 21 ------ test/fixtures/likes.yml | 18 ----- test/fixtures/photos.yml | 27 ------- test/fixtures/tasks.yml | 13 ++++ test/models/comment_test.rb | 18 ----- test/models/follow_request_test.rb | 18 ----- test/models/like_test.rb | 17 ----- test/models/photo_test.rb | 20 ------ test/models/task_test.rb | 7 ++ test/system/comments_test.rb | 47 ------------- test/system/follow_requests_test.rb | 47 ------------- test/system/likes_test.rb | 45 ------------ test/system/photos_test.rb | 51 -------------- test/system/tasks_test.rb | 49 +++++++++++++ 44 files changed, 332 insertions(+), 905 deletions(-) delete mode 100644 app/assets/stylesheets/comments.scss delete mode 100644 app/assets/stylesheets/follow_requests.scss delete mode 100644 app/assets/stylesheets/photos.scss delete mode 100644 app/assets/stylesheets/scaffolds.scss rename app/assets/stylesheets/{likes.scss => tasks.scss} (64%) delete mode 100644 app/controllers/comments_controller.rb delete mode 100644 app/controllers/follow_requests_controller.rb delete mode 100644 app/controllers/likes_controller.rb delete mode 100644 app/controllers/photos_controller.rb 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/20230121185324_create_tasks.rb delete mode 100644 test/controllers/comments_controller_test.rb delete mode 100644 test/controllers/follow_requests_controller_test.rb delete mode 100644 test/controllers/likes_controller_test.rb delete mode 100644 test/controllers/photos_controller_test.rb create mode 100644 test/controllers/tasks_controller_test.rb delete mode 100644 test/fixtures/comments.yml delete mode 100644 test/fixtures/follow_requests.yml delete mode 100644 test/fixtures/likes.yml delete mode 100644 test/fixtures/photos.yml create mode 100644 test/fixtures/tasks.yml delete mode 100644 test/models/comment_test.rb delete mode 100644 test/models/follow_request_test.rb delete mode 100644 test/models/like_test.rb delete mode 100644 test/models/photo_test.rb create mode 100644 test/models/task_test.rb delete mode 100644 test/system/comments_test.rb delete mode 100644 test/system/follow_requests_test.rb delete mode 100644 test/system/likes_test.rb delete mode 100644 test/system/photos_test.rb create mode 100644 test/system/tasks_test.rb diff --git a/app/assets/stylesheets/comments.scss b/app/assets/stylesheets/comments.scss deleted file mode 100644 index 67e9dc5..0000000 --- a/app/assets/stylesheets/comments.scss +++ /dev/null @@ -1,3 +0,0 @@ -// Place all the styles related to the comments controller here. -// They will automatically be included in application.css. -// You can use Sass (SCSS) here: https://sass-lang.com/ diff --git a/app/assets/stylesheets/follow_requests.scss b/app/assets/stylesheets/follow_requests.scss deleted file mode 100644 index 9bc340c..0000000 --- a/app/assets/stylesheets/follow_requests.scss +++ /dev/null @@ -1,3 +0,0 @@ -// Place all the styles related to the follow_requests controller here. -// They will automatically be included in application.css. -// You can use Sass (SCSS) here: https://sass-lang.com/ diff --git a/app/assets/stylesheets/photos.scss b/app/assets/stylesheets/photos.scss deleted file mode 100644 index 2a65da5..0000000 --- a/app/assets/stylesheets/photos.scss +++ /dev/null @@ -1,3 +0,0 @@ -// Place all the styles related to the photos controller here. -// They will automatically be included in application.css. -// You can use Sass (SCSS) here: https://sass-lang.com/ diff --git a/app/assets/stylesheets/scaffolds.scss b/app/assets/stylesheets/scaffolds.scss deleted file mode 100644 index bb2597f..0000000 --- a/app/assets/stylesheets/scaffolds.scss +++ /dev/null @@ -1,65 +0,0 @@ -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/likes.scss b/app/assets/stylesheets/tasks.scss similarity index 64% rename from app/assets/stylesheets/likes.scss rename to app/assets/stylesheets/tasks.scss index bfb01ae..44f19ff 100644 --- a/app/assets/stylesheets/likes.scss +++ b/app/assets/stylesheets/tasks.scss @@ -1,3 +1,3 @@ -// Place all the styles related to the likes controller here. +// 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/comments_controller.rb b/app/controllers/comments_controller.rb deleted file mode 100644 index cd08b60..0000000 --- a/app/controllers/comments_controller.rb +++ /dev/null @@ -1,70 +0,0 @@ -class CommentsController < ApplicationController - before_action :set_comment, only: %i[ show edit update destroy ] - - # GET /comments or /comments.json - def index - @comments = Comment.all - end - - # GET /comments/1 or /comments/1.json - def show - end - - # GET /comments/new - def new - @comment = Comment.new - end - - # GET /comments/1/edit - def edit - end - - # POST /comments or /comments.json - def create - @comment = Comment.new(comment_params) - - respond_to do |format| - if @comment.save - format.html { redirect_to comment_url(@comment), notice: "Comment was successfully created." } - format.json { render :show, status: :created, location: @comment } - else - format.html { render :new, status: :unprocessable_entity } - format.json { render json: @comment.errors, status: :unprocessable_entity } - end - end - end - - # PATCH/PUT /comments/1 or /comments/1.json - def update - respond_to do |format| - if @comment.update(comment_params) - format.html { redirect_to comment_url(@comment), notice: "Comment was successfully updated." } - format.json { render :show, status: :ok, location: @comment } - else - format.html { render :edit, status: :unprocessable_entity } - format.json { render json: @comment.errors, status: :unprocessable_entity } - end - end - end - - # DELETE /comments/1 or /comments/1.json - def destroy - @comment.destroy - - respond_to do |format| - format.html { redirect_to comments_url, notice: "Comment was successfully destroyed." } - format.json { head :no_content } - end - end - - private - # Use callbacks to share common setup or constraints between actions. - def set_comment - @comment = Comment.find(params[:id]) - end - - # Only allow a list of trusted parameters through. - def comment_params - params.require(:comment).permit(:author_id, :photo_id, :body) - end -end diff --git a/app/controllers/follow_requests_controller.rb b/app/controllers/follow_requests_controller.rb deleted file mode 100644 index 47074cf..0000000 --- a/app/controllers/follow_requests_controller.rb +++ /dev/null @@ -1,70 +0,0 @@ -class FollowRequestsController < ApplicationController - before_action :set_follow_request, only: %i[ show edit update destroy ] - - # GET /follow_requests or /follow_requests.json - def index - @follow_requests = FollowRequest.all - end - - # GET /follow_requests/1 or /follow_requests/1.json - def show - end - - # GET /follow_requests/new - def new - @follow_request = FollowRequest.new - end - - # GET /follow_requests/1/edit - def edit - end - - # POST /follow_requests or /follow_requests.json - def create - @follow_request = FollowRequest.new(follow_request_params) - - respond_to do |format| - if @follow_request.save - format.html { redirect_to follow_request_url(@follow_request), notice: "Follow request was successfully created." } - format.json { render :show, status: :created, location: @follow_request } - else - format.html { render :new, status: :unprocessable_entity } - format.json { render json: @follow_request.errors, status: :unprocessable_entity } - end - end - end - - # PATCH/PUT /follow_requests/1 or /follow_requests/1.json - def update - respond_to do |format| - if @follow_request.update(follow_request_params) - format.html { redirect_to follow_request_url(@follow_request), notice: "Follow request was successfully updated." } - format.json { render :show, status: :ok, location: @follow_request } - else - format.html { render :edit, status: :unprocessable_entity } - format.json { render json: @follow_request.errors, status: :unprocessable_entity } - end - end - end - - # DELETE /follow_requests/1 or /follow_requests/1.json - def destroy - @follow_request.destroy - - respond_to do |format| - format.html { redirect_to follow_requests_url, notice: "Follow request was successfully destroyed." } - format.json { head :no_content } - end - end - - private - # Use callbacks to share common setup or constraints between actions. - def set_follow_request - @follow_request = FollowRequest.find(params[:id]) - end - - # Only allow a list of trusted parameters through. - def follow_request_params - params.require(:follow_request).permit(:recipient_id, :sender_id, :status) - end -end diff --git a/app/controllers/likes_controller.rb b/app/controllers/likes_controller.rb deleted file mode 100644 index f38a1c6..0000000 --- a/app/controllers/likes_controller.rb +++ /dev/null @@ -1,70 +0,0 @@ -class LikesController < ApplicationController - before_action :set_like, only: %i[ show edit update destroy ] - - # GET /likes or /likes.json - def index - @likes = Like.all - end - - # GET /likes/1 or /likes/1.json - def show - end - - # GET /likes/new - def new - @like = Like.new - end - - # GET /likes/1/edit - def edit - end - - # POST /likes or /likes.json - def create - @like = Like.new(like_params) - - respond_to do |format| - if @like.save - format.html { redirect_to like_url(@like), notice: "Like was successfully created." } - format.json { render :show, status: :created, location: @like } - else - format.html { render :new, status: :unprocessable_entity } - format.json { render json: @like.errors, status: :unprocessable_entity } - end - end - end - - # PATCH/PUT /likes/1 or /likes/1.json - def update - respond_to do |format| - if @like.update(like_params) - format.html { redirect_to like_url(@like), notice: "Like was successfully updated." } - format.json { render :show, status: :ok, location: @like } - else - format.html { render :edit, status: :unprocessable_entity } - format.json { render json: @like.errors, status: :unprocessable_entity } - end - end - end - - # DELETE /likes/1 or /likes/1.json - def destroy - @like.destroy - - respond_to do |format| - format.html { redirect_to likes_url, notice: "Like was successfully destroyed." } - format.json { head :no_content } - end - end - - private - # Use callbacks to share common setup or constraints between actions. - def set_like - @like = Like.find(params[:id]) - end - - # Only allow a list of trusted parameters through. - def like_params - params.require(:like).permit(:fan_id, :photo_id) - end -end diff --git a/app/controllers/photos_controller.rb b/app/controllers/photos_controller.rb deleted file mode 100644 index 86ba2de..0000000 --- a/app/controllers/photos_controller.rb +++ /dev/null @@ -1,70 +0,0 @@ -class PhotosController < ApplicationController - before_action :set_photo, only: %i[ show edit update destroy ] - - # GET /photos or /photos.json - def index - @photos = Photo.all - end - - # GET /photos/1 or /photos/1.json - def show - end - - # GET /photos/new - def new - @photo = Photo.new - end - - # GET /photos/1/edit - def edit - end - - # POST /photos or /photos.json - def create - @photo = Photo.new(photo_params) - - respond_to do |format| - if @photo.save - format.html { redirect_to photo_url(@photo), notice: "Photo was successfully created." } - format.json { render :show, status: :created, location: @photo } - else - format.html { render :new, status: :unprocessable_entity } - format.json { render json: @photo.errors, status: :unprocessable_entity } - end - end - end - - # PATCH/PUT /photos/1 or /photos/1.json - def update - respond_to do |format| - if @photo.update(photo_params) - format.html { redirect_to photo_url(@photo), notice: "Photo was successfully updated." } - format.json { render :show, status: :ok, location: @photo } - else - format.html { render :edit, status: :unprocessable_entity } - format.json { render json: @photo.errors, status: :unprocessable_entity } - end - end - end - - # DELETE /photos/1 or /photos/1.json - def destroy - @photo.destroy - - respond_to do |format| - format.html { redirect_to photos_url, notice: "Photo was successfully destroyed." } - format.json { head :no_content } - end - end - - private - # Use callbacks to share common setup or constraints between actions. - def set_photo - @photo = Photo.find(params[:id]) - end - - # Only allow a list of trusted parameters through. - def photo_params - params.require(:photo).permit(:image, :comments_count, :likes_count, :caption, :owner_id) - end -end diff --git a/app/controllers/tasks_controller.rb b/app/controllers/tasks_controller.rb new file mode 100644 index 0000000..9f27b98 --- /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(:tasks, :tasks_count, :task_caption, :owner_id) + 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..b3e9761 --- /dev/null +++ b/app/models/task.rb @@ -0,0 +1,3 @@ +class Task < ApplicationRecord + belongs_to :owner +end diff --git a/app/views/tasks/_form.html.erb b/app/views/tasks/_form.html.erb new file mode 100644 index 0000000..b26e728 --- /dev/null +++ b/app/views/tasks/_form.html.erb @@ -0,0 +1,37 @@ +<%= 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 :tasks %> + <%= form.text_field :tasks %> +
+ +
+ <%= form.label :tasks_count %> + <%= form.number_field :tasks_count %> +
+ +
+ <%= form.label :task_caption %> + <%= form.text_area :task_caption %> +
+ +
+ <%= form.label :owner_id %> + <%= form.text_field :owner_id %> +
+ +
+ <%= 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..bb5cdd8 --- /dev/null +++ b/app/views/tasks/_task.json.jbuilder @@ -0,0 +1,2 @@ +json.extract! task, :id, :tasks, :tasks_count, :task_caption, :owner_id, :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..e3a8c11 --- /dev/null +++ b/app/views/tasks/index.html.erb @@ -0,0 +1,33 @@ +

<%= notice %>

+ +

Tasks

+ + + + + + + + + + + + + + <% @tasks.each do |task| %> + + + + + + + + + + <% end %> + +
TasksTasks countTask captionOwner
<%= task.tasks %><%= task.tasks_count %><%= task.task_caption %><%= task.owner_id %><%= 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..1136417 --- /dev/null +++ b/app/views/tasks/show.html.erb @@ -0,0 +1,24 @@ +

<%= notice %>

+ +

+ Tasks: + <%= @task.tasks %> +

+ +

+ Tasks count: + <%= @task.tasks_count %> +

+ +

+ Task caption: + <%= @task.task_caption %> +

+ +

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

+ +<%= 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 bb53cbf..9ff63dc 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -1,6 +1,7 @@ # == Route Map Rails.application.routes.draw do + resources :tasks root "tasks#index" devise_for :users diff --git a/db/migrate/20230121175413_devise_create_users.rb b/db/migrate/20230121175413_devise_create_users.rb index 6ada586..b988221 100644 --- a/db/migrate/20230121175413_devise_create_users.rb +++ b/db/migrate/20230121175413_devise_create_users.rb @@ -1,8 +1,9 @@ # frozen_string_literal: true class DeviseCreateUsers < ActiveRecord::Migration[6.1] - enable_extension("citext") - + enable_extension "citext" + enable_extension "plpgsql" + def change create_table :users do |t| ## Database authenticatable @@ -35,9 +36,7 @@ def change # t.datetime :locked_at t.citext :username - t.boolean :private, default: true - t.integer :likes_count, default: 0 - t.integer :comments_count, default: 0 + t.integer :task_count, default: 0 t.timestamps null: false end diff --git a/db/migrate/20230121185324_create_tasks.rb b/db/migrate/20230121185324_create_tasks.rb new file mode 100644 index 0000000..a69ca6e --- /dev/null +++ b/db/migrate/20230121185324_create_tasks.rb @@ -0,0 +1,12 @@ +class CreateTasks < ActiveRecord::Migration[6.1] + def change + create_table :tasks do |t| + t.string :tasks + t.integer :tasks_count + t.text :task_caption + t.references :owner, null: false, foreign_key: { to_table: :users} + + t.timestamps + end + end +end diff --git a/db/schema.rb b/db/schema.rb index f956060..b8d0cdd 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,11 +10,21 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 2023_01_21_181127) do +ActiveRecord::Schema.define(version: 2023_01_21_185324) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" + create_table "tasks", force: :cascade do |t| + t.string "tasks" + t.integer "tasks_count" + t.text "task_caption" + t.bigint "owner_id", null: false + 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 @@ -23,12 +33,12 @@ t.datetime "remember_created_at" t.string "username" t.boolean "private" - t.integer "likes_count" - t.integer "comments_count" + t.integer "task_count" 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 end + add_foreign_key "tasks", "users", column: "owner_id" end diff --git a/test/controllers/comments_controller_test.rb b/test/controllers/comments_controller_test.rb deleted file mode 100644 index d26febb..0000000 --- a/test/controllers/comments_controller_test.rb +++ /dev/null @@ -1,48 +0,0 @@ -require "test_helper" - -class CommentsControllerTest < ActionDispatch::IntegrationTest - setup do - @comment = comments(:one) - end - - test "should get index" do - get comments_url - assert_response :success - end - - test "should get new" do - get new_comment_url - assert_response :success - end - - test "should create comment" do - assert_difference('Comment.count') do - post comments_url, params: { comment: { author_id: @comment.author_id, body: @comment.body, photo_id: @comment.photo_id } } - end - - assert_redirected_to comment_url(Comment.last) - end - - test "should show comment" do - get comment_url(@comment) - assert_response :success - end - - test "should get edit" do - get edit_comment_url(@comment) - assert_response :success - end - - test "should update comment" do - patch comment_url(@comment), params: { comment: { author_id: @comment.author_id, body: @comment.body, photo_id: @comment.photo_id } } - assert_redirected_to comment_url(@comment) - end - - test "should destroy comment" do - assert_difference('Comment.count', -1) do - delete comment_url(@comment) - end - - assert_redirected_to comments_url - end -end diff --git a/test/controllers/follow_requests_controller_test.rb b/test/controllers/follow_requests_controller_test.rb deleted file mode 100644 index ab9e0a1..0000000 --- a/test/controllers/follow_requests_controller_test.rb +++ /dev/null @@ -1,48 +0,0 @@ -require "test_helper" - -class FollowRequestsControllerTest < ActionDispatch::IntegrationTest - setup do - @follow_request = follow_requests(:one) - end - - test "should get index" do - get follow_requests_url - assert_response :success - end - - test "should get new" do - get new_follow_request_url - assert_response :success - end - - test "should create follow_request" do - assert_difference('FollowRequest.count') do - post follow_requests_url, params: { follow_request: { recipient_id: @follow_request.recipient_id, sender_id: @follow_request.sender_id, status: @follow_request.status } } - end - - assert_redirected_to follow_request_url(FollowRequest.last) - end - - test "should show follow_request" do - get follow_request_url(@follow_request) - assert_response :success - end - - test "should get edit" do - get edit_follow_request_url(@follow_request) - assert_response :success - end - - test "should update follow_request" do - patch follow_request_url(@follow_request), params: { follow_request: { recipient_id: @follow_request.recipient_id, sender_id: @follow_request.sender_id, status: @follow_request.status } } - assert_redirected_to follow_request_url(@follow_request) - end - - test "should destroy follow_request" do - assert_difference('FollowRequest.count', -1) do - delete follow_request_url(@follow_request) - end - - assert_redirected_to follow_requests_url - end -end diff --git a/test/controllers/likes_controller_test.rb b/test/controllers/likes_controller_test.rb deleted file mode 100644 index fc00f57..0000000 --- a/test/controllers/likes_controller_test.rb +++ /dev/null @@ -1,48 +0,0 @@ -require "test_helper" - -class LikesControllerTest < ActionDispatch::IntegrationTest - setup do - @like = likes(:one) - end - - test "should get index" do - get likes_url - assert_response :success - end - - test "should get new" do - get new_like_url - assert_response :success - end - - test "should create like" do - assert_difference('Like.count') do - post likes_url, params: { like: { fan_id: @like.fan_id, photo_id: @like.photo_id } } - end - - assert_redirected_to like_url(Like.last) - end - - test "should show like" do - get like_url(@like) - assert_response :success - end - - test "should get edit" do - get edit_like_url(@like) - assert_response :success - end - - test "should update like" do - patch like_url(@like), params: { like: { fan_id: @like.fan_id, photo_id: @like.photo_id } } - assert_redirected_to like_url(@like) - end - - test "should destroy like" do - assert_difference('Like.count', -1) do - delete like_url(@like) - end - - assert_redirected_to likes_url - end -end diff --git a/test/controllers/photos_controller_test.rb b/test/controllers/photos_controller_test.rb deleted file mode 100644 index e228104..0000000 --- a/test/controllers/photos_controller_test.rb +++ /dev/null @@ -1,48 +0,0 @@ -require "test_helper" - -class PhotosControllerTest < ActionDispatch::IntegrationTest - setup do - @photo = photos(:one) - end - - test "should get index" do - get photos_url - assert_response :success - end - - test "should get new" do - get new_photo_url - assert_response :success - end - - test "should create photo" do - assert_difference('Photo.count') do - post photos_url, params: { photo: { caption: @photo.caption, comments_count: @photo.comments_count, image: @photo.image, likes_count: @photo.likes_count, owner_id: @photo.owner_id } } - end - - assert_redirected_to photo_url(Photo.last) - end - - test "should show photo" do - get photo_url(@photo) - assert_response :success - end - - test "should get edit" do - get edit_photo_url(@photo) - assert_response :success - end - - test "should update photo" do - patch photo_url(@photo), params: { photo: { caption: @photo.caption, comments_count: @photo.comments_count, image: @photo.image, likes_count: @photo.likes_count, owner_id: @photo.owner_id } } - assert_redirected_to photo_url(@photo) - end - - test "should destroy photo" do - assert_difference('Photo.count', -1) do - delete photo_url(@photo) - end - - assert_redirected_to photos_url - end -end diff --git a/test/controllers/tasks_controller_test.rb b/test/controllers/tasks_controller_test.rb new file mode 100644 index 0000000..86df144 --- /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: { owner_id: @task.owner_id, task_caption: @task.task_caption, tasks: @task.tasks, tasks_count: @task.tasks_count } } + 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: { owner_id: @task.owner_id, task_caption: @task.task_caption, tasks: @task.tasks, tasks_count: @task.tasks_count } } + 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/comments.yml b/test/fixtures/comments.yml deleted file mode 100644 index 751168a..0000000 --- a/test/fixtures/comments.yml +++ /dev/null @@ -1,21 +0,0 @@ -# == Schema Information -# -# Table name: comments -# -# id :bigint not null, primary key -# author_id :bigint not null -# photo_id :bigint not null -# body :text -# created_at :datetime not null -# updated_at :datetime not null -# - -one: - author: one - photo: one - body: MyText - -two: - author: two - photo: two - body: MyText diff --git a/test/fixtures/follow_requests.yml b/test/fixtures/follow_requests.yml deleted file mode 100644 index 8a73792..0000000 --- a/test/fixtures/follow_requests.yml +++ /dev/null @@ -1,21 +0,0 @@ -# == Schema Information -# -# Table name: follow_requests -# -# id :bigint not null, primary key -# recipient_id :bigint not null -# sender_id :bigint not null -# status :string -# created_at :datetime not null -# updated_at :datetime not null -# - -one: - recipient: one - sender: one - status: MyString - -two: - recipient: two - sender: two - status: MyString diff --git a/test/fixtures/likes.yml b/test/fixtures/likes.yml deleted file mode 100644 index 1545ee5..0000000 --- a/test/fixtures/likes.yml +++ /dev/null @@ -1,18 +0,0 @@ -# == Schema Information -# -# Table name: likes -# -# id :bigint not null, primary key -# fan_id :bigint not null -# photo_id :bigint not null -# created_at :datetime not null -# updated_at :datetime not null -# - -one: - fan: one - photo: one - -two: - fan: two - photo: two diff --git a/test/fixtures/photos.yml b/test/fixtures/photos.yml deleted file mode 100644 index 8f3486d..0000000 --- a/test/fixtures/photos.yml +++ /dev/null @@ -1,27 +0,0 @@ -# == Schema Information -# -# Table name: photos -# -# id :bigint not null, primary key -# image :string -# comments_count :integer -# likes_count :integer -# caption :text -# owner_id :bigint not null -# created_at :datetime not null -# updated_at :datetime not null -# - -one: - image: MyString - comments_count: 1 - likes_count: 1 - caption: MyText - owner: one - -two: - image: MyString - comments_count: 1 - likes_count: 1 - caption: MyText - owner: two diff --git a/test/fixtures/tasks.yml b/test/fixtures/tasks.yml new file mode 100644 index 0000000..7e2a48d --- /dev/null +++ b/test/fixtures/tasks.yml @@ -0,0 +1,13 @@ +# Read about fixtures at https://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html + +one: + tasks: MyString + tasks_count: 1 + task_caption: MyText + owner: one + +two: + tasks: MyString + tasks_count: 1 + task_caption: MyText + owner: two diff --git a/test/models/comment_test.rb b/test/models/comment_test.rb deleted file mode 100644 index 1331920..0000000 --- a/test/models/comment_test.rb +++ /dev/null @@ -1,18 +0,0 @@ -# == Schema Information -# -# Table name: comments -# -# id :bigint not null, primary key -# author_id :bigint not null -# photo_id :bigint not null -# body :text -# created_at :datetime not null -# updated_at :datetime not null -# -require "test_helper" - -class CommentTest < ActiveSupport::TestCase - # test "the truth" do - # assert true - # end -end diff --git a/test/models/follow_request_test.rb b/test/models/follow_request_test.rb deleted file mode 100644 index 8264314..0000000 --- a/test/models/follow_request_test.rb +++ /dev/null @@ -1,18 +0,0 @@ -# == Schema Information -# -# Table name: follow_requests -# -# id :bigint not null, primary key -# recipient_id :bigint not null -# sender_id :bigint not null -# status :string -# created_at :datetime not null -# updated_at :datetime not null -# -require "test_helper" - -class FollowRequestTest < ActiveSupport::TestCase - # test "the truth" do - # assert true - # end -end diff --git a/test/models/like_test.rb b/test/models/like_test.rb deleted file mode 100644 index 5c49c4f..0000000 --- a/test/models/like_test.rb +++ /dev/null @@ -1,17 +0,0 @@ -# == Schema Information -# -# Table name: likes -# -# id :bigint not null, primary key -# fan_id :bigint not null -# photo_id :bigint not null -# created_at :datetime not null -# updated_at :datetime not null -# -require "test_helper" - -class LikeTest < ActiveSupport::TestCase - # test "the truth" do - # assert true - # end -end diff --git a/test/models/photo_test.rb b/test/models/photo_test.rb deleted file mode 100644 index 7fb832f..0000000 --- a/test/models/photo_test.rb +++ /dev/null @@ -1,20 +0,0 @@ -# == Schema Information -# -# Table name: photos -# -# id :bigint not null, primary key -# image :string -# comments_count :integer -# likes_count :integer -# caption :text -# owner_id :bigint not null -# created_at :datetime not null -# updated_at :datetime not null -# -require "test_helper" - -class PhotoTest < ActiveSupport::TestCase - # test "the truth" do - # assert true - # end -end 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/comments_test.rb b/test/system/comments_test.rb deleted file mode 100644 index 5f82906..0000000 --- a/test/system/comments_test.rb +++ /dev/null @@ -1,47 +0,0 @@ -require "application_system_test_case" - -class CommentsTest < ApplicationSystemTestCase - setup do - @comment = comments(:one) - end - - test "visiting the index" do - visit comments_url - assert_selector "h1", text: "Comments" - end - - test "creating a Comment" do - visit comments_url - click_on "New Comment" - - fill_in "Author", with: @comment.author_id - fill_in "Body", with: @comment.body - fill_in "Photo", with: @comment.photo_id - click_on "Create Comment" - - assert_text "Comment was successfully created" - click_on "Back" - end - - test "updating a Comment" do - visit comments_url - click_on "Edit", match: :first - - fill_in "Author", with: @comment.author_id - fill_in "Body", with: @comment.body - fill_in "Photo", with: @comment.photo_id - click_on "Update Comment" - - assert_text "Comment was successfully updated" - click_on "Back" - end - - test "destroying a Comment" do - visit comments_url - page.accept_confirm do - click_on "Destroy", match: :first - end - - assert_text "Comment was successfully destroyed" - end -end diff --git a/test/system/follow_requests_test.rb b/test/system/follow_requests_test.rb deleted file mode 100644 index 2485f7e..0000000 --- a/test/system/follow_requests_test.rb +++ /dev/null @@ -1,47 +0,0 @@ -require "application_system_test_case" - -class FollowRequestsTest < ApplicationSystemTestCase - setup do - @follow_request = follow_requests(:one) - end - - test "visiting the index" do - visit follow_requests_url - assert_selector "h1", text: "Follow Requests" - end - - test "creating a Follow request" do - visit follow_requests_url - click_on "New Follow Request" - - fill_in "Recipient", with: @follow_request.recipient_id - fill_in "Sender", with: @follow_request.sender_id - fill_in "Status", with: @follow_request.status - click_on "Create Follow request" - - assert_text "Follow request was successfully created" - click_on "Back" - end - - test "updating a Follow request" do - visit follow_requests_url - click_on "Edit", match: :first - - fill_in "Recipient", with: @follow_request.recipient_id - fill_in "Sender", with: @follow_request.sender_id - fill_in "Status", with: @follow_request.status - click_on "Update Follow request" - - assert_text "Follow request was successfully updated" - click_on "Back" - end - - test "destroying a Follow request" do - visit follow_requests_url - page.accept_confirm do - click_on "Destroy", match: :first - end - - assert_text "Follow request was successfully destroyed" - end -end diff --git a/test/system/likes_test.rb b/test/system/likes_test.rb deleted file mode 100644 index ad40f2a..0000000 --- a/test/system/likes_test.rb +++ /dev/null @@ -1,45 +0,0 @@ -require "application_system_test_case" - -class LikesTest < ApplicationSystemTestCase - setup do - @like = likes(:one) - end - - test "visiting the index" do - visit likes_url - assert_selector "h1", text: "Likes" - end - - test "creating a Like" do - visit likes_url - click_on "New Like" - - fill_in "Fan", with: @like.fan_id - fill_in "Photo", with: @like.photo_id - click_on "Create Like" - - assert_text "Like was successfully created" - click_on "Back" - end - - test "updating a Like" do - visit likes_url - click_on "Edit", match: :first - - fill_in "Fan", with: @like.fan_id - fill_in "Photo", with: @like.photo_id - click_on "Update Like" - - assert_text "Like was successfully updated" - click_on "Back" - end - - test "destroying a Like" do - visit likes_url - page.accept_confirm do - click_on "Destroy", match: :first - end - - assert_text "Like was successfully destroyed" - end -end diff --git a/test/system/photos_test.rb b/test/system/photos_test.rb deleted file mode 100644 index 22ef121..0000000 --- a/test/system/photos_test.rb +++ /dev/null @@ -1,51 +0,0 @@ -require "application_system_test_case" - -class PhotosTest < ApplicationSystemTestCase - setup do - @photo = photos(:one) - end - - test "visiting the index" do - visit photos_url - assert_selector "h1", text: "Photos" - end - - test "creating a Photo" do - visit photos_url - click_on "New Photo" - - fill_in "Caption", with: @photo.caption - fill_in "Comments count", with: @photo.comments_count - fill_in "Image", with: @photo.image - fill_in "Likes count", with: @photo.likes_count - fill_in "Owner", with: @photo.owner_id - click_on "Create Photo" - - assert_text "Photo was successfully created" - click_on "Back" - end - - test "updating a Photo" do - visit photos_url - click_on "Edit", match: :first - - fill_in "Caption", with: @photo.caption - fill_in "Comments count", with: @photo.comments_count - fill_in "Image", with: @photo.image - fill_in "Likes count", with: @photo.likes_count - fill_in "Owner", with: @photo.owner_id - click_on "Update Photo" - - assert_text "Photo was successfully updated" - click_on "Back" - end - - test "destroying a Photo" do - visit photos_url - page.accept_confirm do - click_on "Destroy", match: :first - end - - assert_text "Photo was successfully destroyed" - end -end diff --git a/test/system/tasks_test.rb b/test/system/tasks_test.rb new file mode 100644 index 0000000..f41e922 --- /dev/null +++ b/test/system/tasks_test.rb @@ -0,0 +1,49 @@ +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 "Owner", with: @task.owner_id + fill_in "Task caption", with: @task.task_caption + fill_in "Tasks", with: @task.tasks + fill_in "Tasks count", with: @task.tasks_count + 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 "Owner", with: @task.owner_id + fill_in "Task caption", with: @task.task_caption + fill_in "Tasks", with: @task.tasks + fill_in "Tasks count", with: @task.tasks_count + 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 609fa9ef8c8ff3658e23841ab56cb29844ddb694 Mon Sep 17 00:00:00 2001 From: dmacinto Date: Sat, 21 Jan 2023 19:28:05 +0000 Subject: [PATCH 08/26] cleaned up tasks migration file and deleted the remaining old code --- app/models/task.rb | 14 ++++- app/models/user.rb | 24 +-------- .../20230121175413_devise_create_users.rb | 2 +- db/migrate/20230121185324_create_tasks.rb | 7 ++- db/schema.rb | 20 +++---- lib/tasks/dev.rake | 52 ++----------------- test/fixtures/tasks.yml | 13 ++++- test/fixtures/users.yml | 3 +- test/models/task_test.rb | 12 +++++ test/models/user_test.rb | 3 +- 10 files changed, 57 insertions(+), 93 deletions(-) diff --git a/app/models/task.rb b/app/models/task.rb index b3e9761..83e9260 100644 --- a/app/models/task.rb +++ b/app/models/task.rb @@ -1,3 +1,15 @@ +# == Schema Information +# +# Table name: tasks +# +# id :bigint not null, primary key +# content :string +# tasks_count :integer +# task_caption :text +# owner_id :bigint not null +# created_at :datetime not null +# updated_at :datetime not null +# class Task < ApplicationRecord - belongs_to :owner + belongs_to :user end diff --git a/app/models/user.rb b/app/models/user.rb index 1323632..fa24195 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -10,8 +10,7 @@ # remember_created_at :datetime # username :string # private :boolean -# likes_count :integer -# comments_count :integer +# task_count :integer # created_at :datetime not null # updated_at :datetime not null # @@ -21,29 +20,8 @@ class User < ApplicationRecord devise :database_authenticatable, :registerable, :recoverable, :rememberable, :validatable - has_many :comments, foreign_key: :author_id - has_many :sent_follow_requests, foreign_key: :sender_id, class_name: "FollowRequest" - has_many :accepted_sent_follow_requests, -> { accepted }, foreign_key: :sender_id, class_name: "FollowRequest" - - has_many :received_follow_requests, foreign_key: :recipient_id, class_name: "FollowRequest" - - has_many :accepted_received_follow_requests, ->{ accepted }, foreign_key: :recipient_id, class_name: "FollowRequest" - - has_many :likes, foreign_key: :fan_id - - has_many :own_photos, foreign_key: :owner_id, class_name: "Photo" - - has_many :liked_photos, through: :likes, source: :photo - - has_many :leaders, through: :accepted_sent_follow_requests, source: :recipient - - has_many :followers, through: :accepted_received_follow_requests, source: :sender - - has_many :feed, through: :leaders, source: :own_photos - has_many :discover, through: :leaders, source: :liked_photos - validates :username, presence: true, uniqueness: true end diff --git a/db/migrate/20230121175413_devise_create_users.rb b/db/migrate/20230121175413_devise_create_users.rb index b988221..f036b47 100644 --- a/db/migrate/20230121175413_devise_create_users.rb +++ b/db/migrate/20230121175413_devise_create_users.rb @@ -43,7 +43,7 @@ def change add_index :users, :email, unique: true add_index :users, :reset_password_token, unique: true - add_index :user, :username, unique: true + add_index :users, :username, unique: true # add_index :users, :confirmation_token, unique: true # add_index :users, :unlock_token, unique: true end diff --git a/db/migrate/20230121185324_create_tasks.rb b/db/migrate/20230121185324_create_tasks.rb index a69ca6e..5b38510 100644 --- a/db/migrate/20230121185324_create_tasks.rb +++ b/db/migrate/20230121185324_create_tasks.rb @@ -1,10 +1,9 @@ class CreateTasks < ActiveRecord::Migration[6.1] def change create_table :tasks do |t| - t.string :tasks - t.integer :tasks_count - t.text :task_caption - t.references :owner, null: false, foreign_key: { to_table: :users} + t.text :content, null: false + t.references :user, null: false, foreign_key: { to_table: :users} + t.string :status, default: "not_yet_started", null: false t.timestamps end diff --git a/db/schema.rb b/db/schema.rb index b8d0cdd..2b4dad9 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -13,32 +13,32 @@ ActiveRecord::Schema.define(version: 2023_01_21_185324) 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.string "tasks" - t.integer "tasks_count" - t.text "task_caption" - t.bigint "owner_id", null: false + t.text "content", null: false + t.bigint "user_id", null: false + t.string "status", default: "not_yet_started", null: false 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" + t.index ["user_id"], name: "index_tasks_on_user_id" end create_table "users", force: :cascade do |t| - t.string "email", default: "", null: false + t.citext "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.string "username" - t.boolean "private" - t.integer "task_count" + t.citext "username" + t.integer "task_count", default: 0 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" + add_foreign_key "tasks", "users" end diff --git a/lib/tasks/dev.rake b/lib/tasks/dev.rake index dbd8d08..3c97bd4 100644 --- a/lib/tasks/dev.rake +++ b/lib/tasks/dev.rake @@ -2,10 +2,7 @@ task sample_data: :environment do p "Creating sample data" if Rails.env.development? - FollowRequest.delete_all - Comment.delete_all - Like.delete_all - Photo.delete_all + Task.delete_all User.delete_all end @@ -27,53 +24,13 @@ task sample_data: :environment do password: "password", username: username.downcase, private: [true, false].sample, - - ) end - - - -# 10.times do -# name = Faker::Name.first_name -# User.create( -# email: "#{name}@example.com", -# password: "password", -# username: name.downcase, -# private: [true, false].sample, -# ) - #p u.errors.full_messages -# end -# p "#{User.count} users have been created." - users = User.all - users.each do |first_user| - users.each do |second_user| - if rand < 0.75 - first_user.sent_follow_requests.create( - recipient: second_user, - status: FollowRequest.statuses.keys.sample - ) - end - - if rand < 0.75 - second_user.sent_follow_requests.create( - recipient: first_user, - status: FollowRequest.statuses.keys.sample - ) - end - end - end - users.each do |user| - rand(15).times do - photo = user.own_photos.create( - caption: Faker::Quote.jack_handey, - image: "https://robohash.org/#{rand(9999)}" - ) - + rand(20).times do user.followers.each do |follower| if rand < 0.5 photo.fans << follower @@ -92,8 +49,5 @@ task sample_data: :environment do 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 #{FollowRequest.count} follow requests." - p "There are now #{Photo.count} photos." - p "There are now #{Like.count} likes." - p "There are now #{Comment.count} comments." + p "There are now #{Task.count} tasks." end diff --git a/test/fixtures/tasks.yml b/test/fixtures/tasks.yml index 7e2a48d..b4808e8 100644 --- a/test/fixtures/tasks.yml +++ b/test/fixtures/tasks.yml @@ -1,4 +1,15 @@ -# Read about fixtures at https://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html +# == Schema Information +# +# Table name: tasks +# +# id :bigint not null, primary key +# tasks :string +# tasks_count :integer +# task_caption :text +# owner_id :bigint not null +# created_at :datetime not null +# updated_at :datetime not null +# one: tasks: MyString diff --git a/test/fixtures/users.yml b/test/fixtures/users.yml index 05df2f4..c538727 100644 --- a/test/fixtures/users.yml +++ b/test/fixtures/users.yml @@ -10,8 +10,7 @@ # remember_created_at :datetime # username :string # private :boolean -# likes_count :integer -# comments_count :integer +# task_count :integer # created_at :datetime not null # updated_at :datetime not null # diff --git a/test/models/task_test.rb b/test/models/task_test.rb index 29982eb..71d1e7e 100644 --- a/test/models/task_test.rb +++ b/test/models/task_test.rb @@ -1,3 +1,15 @@ +# == Schema Information +# +# Table name: tasks +# +# id :bigint not null, primary key +# tasks :string +# tasks_count :integer +# task_caption :text +# owner_id :bigint not null +# created_at :datetime not null +# updated_at :datetime not null +# require "test_helper" class TaskTest < ActiveSupport::TestCase diff --git a/test/models/user_test.rb b/test/models/user_test.rb index bdc2f5b..a747482 100644 --- a/test/models/user_test.rb +++ b/test/models/user_test.rb @@ -10,8 +10,7 @@ # remember_created_at :datetime # username :string # private :boolean -# likes_count :integer -# comments_count :integer +# task_count :integer # created_at :datetime not null # updated_at :datetime not null # From 42bc05ba644c89074b55b050907f037accd0da94 Mon Sep 17 00:00:00 2001 From: dmacinto Date: Sat, 21 Jan 2023 19:31:45 +0000 Subject: [PATCH 09/26] reset annotate notes on models --- app/models/task.rb | 13 ++++++------- app/models/user.rb | 12 ++++-------- test/fixtures/tasks.yml | 14 ++++++-------- test/fixtures/users.yml | 12 +++--------- test/models/task_test.rb | 13 ++++++------- test/models/user_test.rb | 7 +++---- 6 files changed, 28 insertions(+), 43 deletions(-) diff --git a/app/models/task.rb b/app/models/task.rb index 83e9260..ecfa8ac 100644 --- a/app/models/task.rb +++ b/app/models/task.rb @@ -2,13 +2,12 @@ # # Table name: tasks # -# id :bigint not null, primary key -# content :string -# tasks_count :integer -# task_caption :text -# owner_id :bigint not null -# created_at :datetime not null -# updated_at :datetime not null +# id :bigint not null, primary key +# content :text not null +# user_id :bigint not null +# status :string default("not_yet_started"), not null +# created_at :datetime not null +# updated_at :datetime not null # class Task < ApplicationRecord belongs_to :user diff --git a/app/models/user.rb b/app/models/user.rb index fa24195..9e4e14a 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -3,14 +3,13 @@ # Table name: users # # id :bigint not null, primary key -# email :string default(""), not null +# email :citext default(""), not null # encrypted_password :string default(""), not null # reset_password_token :string # reset_password_sent_at :datetime # remember_created_at :datetime -# username :string -# private :boolean -# task_count :integer +# username :citext +# task_count :integer default(0) # created_at :datetime not null # updated_at :datetime not null # @@ -19,9 +18,6 @@ class User < ApplicationRecord # :confirmable, :lockable, :timeoutable, :trackable and :omniauthable devise :database_authenticatable, :registerable, :recoverable, :rememberable, :validatable - - - - + validates :username, presence: true, uniqueness: true end diff --git a/test/fixtures/tasks.yml b/test/fixtures/tasks.yml index b4808e8..8b32e01 100644 --- a/test/fixtures/tasks.yml +++ b/test/fixtures/tasks.yml @@ -2,15 +2,13 @@ # # Table name: tasks # -# id :bigint not null, primary key -# tasks :string -# tasks_count :integer -# task_caption :text -# owner_id :bigint not null -# created_at :datetime not null -# updated_at :datetime not null +# id :bigint not null, primary key +# content :text not null +# user_id :bigint not null +# status :string default("not_yet_started"), not null +# created_at :datetime not null +# updated_at :datetime not null # - one: tasks: MyString tasks_count: 1 diff --git a/test/fixtures/users.yml b/test/fixtures/users.yml index c538727..69d51f7 100644 --- a/test/fixtures/users.yml +++ b/test/fixtures/users.yml @@ -3,22 +3,16 @@ # Table name: users # # id :bigint not null, primary key -# email :string default(""), not null +# email :citext default(""), not null # encrypted_password :string default(""), not null # reset_password_token :string # reset_password_sent_at :datetime # remember_created_at :datetime -# username :string -# private :boolean -# task_count :integer +# username :citext +# task_count :integer default(0) # created_at :datetime not null # updated_at :datetime not null # - -# 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 # diff --git a/test/models/task_test.rb b/test/models/task_test.rb index 71d1e7e..65a947a 100644 --- a/test/models/task_test.rb +++ b/test/models/task_test.rb @@ -2,13 +2,12 @@ # # Table name: tasks # -# id :bigint not null, primary key -# tasks :string -# tasks_count :integer -# task_caption :text -# owner_id :bigint not null -# created_at :datetime not null -# updated_at :datetime not null +# id :bigint not null, primary key +# content :text not null +# user_id :bigint not null +# status :string default("not_yet_started"), not null +# created_at :datetime not null +# updated_at :datetime not null # require "test_helper" diff --git a/test/models/user_test.rb b/test/models/user_test.rb index a747482..e907716 100644 --- a/test/models/user_test.rb +++ b/test/models/user_test.rb @@ -3,14 +3,13 @@ # Table name: users # # id :bigint not null, primary key -# email :string default(""), not null +# email :citext default(""), not null # encrypted_password :string default(""), not null # reset_password_token :string # reset_password_sent_at :datetime # remember_created_at :datetime -# username :string -# private :boolean -# task_count :integer +# username :citext +# task_count :integer default(0) # created_at :datetime not null # updated_at :datetime not null # From 93c893a242aac1274817f76d601ddce14ae3bab6 Mon Sep 17 00:00:00 2001 From: dmacinto Date: Sat, 21 Jan 2023 19:41:31 +0000 Subject: [PATCH 10/26] built sample data and authenticate users --- Gemfile | 2 ++ Gemfile.lock | 3 +++ app/controllers/application_controller.rb | 1 + app/models/task.rb | 7 +++++++ app/models/user.rb | 1 + latest_specs.4.8 | Bin 0 -> 4 bytes latest_specs.4.8.gz | Bin 0 -> 24 bytes lib/tasks/dev.rake | 23 ++++++++-------------- prerelease_specs.4.8 | Bin 0 -> 4 bytes prerelease_specs.4.8.gz | Bin 0 -> 24 bytes specs.4.8 | Bin 0 -> 4 bytes specs.4.8.gz | Bin 0 -> 24 bytes 12 files changed, 22 insertions(+), 15 deletions(-) create mode 100644 latest_specs.4.8 create mode 100644 latest_specs.4.8.gz create mode 100644 prerelease_specs.4.8 create mode 100644 prerelease_specs.4.8.gz create mode 100644 specs.4.8 create mode 100644 specs.4.8.gz diff --git a/Gemfile b/Gemfile index 4064bfa..0ec6f4e 100644 --- a/Gemfile +++ b/Gemfile @@ -5,6 +5,8 @@ ruby '3.0.3' gem 'devise' +gem 'faker' + # Bundle edge Rails instead: gem 'rails', github: 'rails/rails', branch: 'main' gem 'rails', '~> 6.1.3', '>= 6.1.3.1' # Use postgresql as the database for Active Record diff --git a/Gemfile.lock b/Gemfile.lock index 34f032f..2a53427 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 + faker htmlbeautifier jbuilder (~> 2.7) listen (~> 3.3) 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 diff --git a/app/models/task.rb b/app/models/task.rb index ecfa8ac..8a2e59c 100644 --- a/app/models/task.rb +++ b/app/models/task.rb @@ -11,4 +11,11 @@ # class Task < ApplicationRecord belongs_to :user + + enum status: { + not_yet_started: "not_yet_started", + in_progress: "in_progress", + completed: "completed" + } + end diff --git a/app/models/user.rb b/app/models/user.rb index 9e4e14a..b358a1a 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -19,5 +19,6 @@ class User < ApplicationRecord devise :database_authenticatable, :registerable, :recoverable, :rememberable, :validatable + has_many :tasks, dependent: :destroy validates :username, presence: true, uniqueness: true end diff --git a/latest_specs.4.8 b/latest_specs.4.8 new file mode 100644 index 0000000000000000000000000000000000000000..0ba94359df472e358cfe3fa92192fc82832db5b6 GIT binary patch literal 4 LcmZSKh-Lr)0O9~> literal 0 HcmV?d00001 diff --git a/latest_specs.4.8.gz b/latest_specs.4.8.gz new file mode 100644 index 0000000000000000000000000000000000000000..5603c08bd5cc5e8c83dec4761e96272d3a82ed97 GIT binary patch literal 24 fcmb2|=3tm@cP5#EIr(8n8UsVl+^U}}3=9kaVF(9s literal 0 HcmV?d00001 diff --git a/lib/tasks/dev.rake b/lib/tasks/dev.rake index 3c97bd4..c8ccfc3 100644 --- a/lib/tasks/dev.rake +++ b/lib/tasks/dev.rake @@ -23,29 +23,22 @@ task sample_data: :environment do email: "#{username}@example.com", password: "password", username: username.downcase, - private: [true, false].sample, ) end users = User.all - users.each do |user| - rand(20).times do - user.followers.each do |follower| - if rand < 0.5 - photo.fans << follower - end - - if rand < 0.25 - photo.comments.create( - body: Faker::Quote.jack_handey, - author: follower - ) - end - end + + Task.statuses.values.each do |status| + rand(3..10).times do + user.tasks.create( + content: Faker::Hipster.sentence, + status: status + ) end end + ending = Time.now #p "It took #{(ending - starting).to_i} seconds to create sample data." p "There are now #{User.count} users." diff --git a/prerelease_specs.4.8 b/prerelease_specs.4.8 new file mode 100644 index 0000000000000000000000000000000000000000..0ba94359df472e358cfe3fa92192fc82832db5b6 GIT binary patch literal 4 LcmZSKh-Lr)0O9~> literal 0 HcmV?d00001 diff --git a/prerelease_specs.4.8.gz b/prerelease_specs.4.8.gz new file mode 100644 index 0000000000000000000000000000000000000000..5603c08bd5cc5e8c83dec4761e96272d3a82ed97 GIT binary patch literal 24 fcmb2|=3tm@cP5#EIr(8n8UsVl+^U}}3=9kaVF(9s literal 0 HcmV?d00001 diff --git a/specs.4.8 b/specs.4.8 new file mode 100644 index 0000000000000000000000000000000000000000..0ba94359df472e358cfe3fa92192fc82832db5b6 GIT binary patch literal 4 LcmZSKh-Lr)0O9~> literal 0 HcmV?d00001 diff --git a/specs.4.8.gz b/specs.4.8.gz new file mode 100644 index 0000000000000000000000000000000000000000..5603c08bd5cc5e8c83dec4761e96272d3a82ed97 GIT binary patch literal 24 fcmb2|=3tm@cP5#EIr(8n8UsVl+^U}}3=9kaVF(9s literal 0 HcmV?d00001 From 606a5c12c250f0033a58a1c1b2e7486a6a428432 Mon Sep 17 00:00:00 2001 From: dmacinto Date: Sat, 21 Jan 2023 19:50:21 +0000 Subject: [PATCH 11/26] added bootstrap and flash messages --- app/assets/stylesheets/tasks.scss | 3 --- app/views/layouts/application.html.erb | 9 ++++++++- app/views/shared/_cdn_assets.html.erb | 5 +++++ app/views/shared/_flash_messages.html.erb | 10 ++++++++++ 4 files changed, 23 insertions(+), 4 deletions(-) delete mode 100644 app/assets/stylesheets/tasks.scss create mode 100644 app/views/shared/_cdn_assets.html.erb create mode 100644 app/views/shared/_flash_messages.html.erb diff --git a/app/assets/stylesheets/tasks.scss b/app/assets/stylesheets/tasks.scss deleted file mode 100644 index 44f19ff..0000000 --- a/app/assets/stylesheets/tasks.scss +++ /dev/null @@ -1,3 +0,0 @@ -// 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/views/layouts/application.html.erb b/app/views/layouts/application.html.erb index 9917f83..73a6043 100644 --- a/app/views/layouts/application.html.erb +++ b/app/views/layouts/application.html.erb @@ -1,16 +1,23 @@ + Vanilla Rails <%= csrf_meta_tags %> <%= csp_meta_tag %> + <%= render "shared/cdn_assets" %> + <%= stylesheet_link_tag 'application', media: 'all', 'data-turbolinks-track': 'reload' %> <%= javascript_pack_tag 'application', 'data-turbolinks-track': 'reload' %> - <%= yield %> +
+ <%= render "shared/flash_messages" %> + + <%= yield %> +
diff --git a/app/views/shared/_cdn_assets.html.erb b/app/views/shared/_cdn_assets.html.erb new file mode 100644 index 0000000..aa16d0d --- /dev/null +++ b/app/views/shared/_cdn_assets.html.erb @@ -0,0 +1,5 @@ + + + + + diff --git a/app/views/shared/_flash_messages.html.erb b/app/views/shared/_flash_messages.html.erb new file mode 100644 index 0000000..f161055 --- /dev/null +++ b/app/views/shared/_flash_messages.html.erb @@ -0,0 +1,10 @@ +<% if notice.present? %> + +<% end %> +<% if alert.present? %> + +<% end %> From d018474a98ef41101e28585247ff07525219d256 Mon Sep 17 00:00:00 2001 From: dmacinto Date: Sat, 21 Jan 2023 20:05:37 +0000 Subject: [PATCH 12/26] commented out tasks in sample data generation --- app/views/layouts/application.html.erb | 4 +++- app/views/shared/_navbar.html.erb | 29 ++++++++++++++++++++++++++ app/views/tasks/index.html.erb | 9 ++++++-- config/routes.rb | 3 --- lib/tasks/dev.rake | 18 ++++++++-------- 5 files changed, 48 insertions(+), 15 deletions(-) create mode 100644 app/views/shared/_navbar.html.erb diff --git a/app/views/layouts/application.html.erb b/app/views/layouts/application.html.erb index 73a6043..38351d1 100644 --- a/app/views/layouts/application.html.erb +++ b/app/views/layouts/application.html.erb @@ -2,7 +2,7 @@ - Vanilla Rails + Tasks <%= csrf_meta_tags %> <%= csp_meta_tag %> @@ -14,6 +14,8 @@ + <% render "shared/navbar" %> +
<%= render "shared/flash_messages" %> diff --git a/app/views/shared/_navbar.html.erb b/app/views/shared/_navbar.html.erb new file mode 100644 index 0000000..3136f12 --- /dev/null +++ b/app/views/shared/_navbar.html.erb @@ -0,0 +1,29 @@ + diff --git a/app/views/tasks/index.html.erb b/app/views/tasks/index.html.erb index e3a8c11..5aa216c 100644 --- a/app/views/tasks/index.html.erb +++ b/app/views/tasks/index.html.erb @@ -1,7 +1,12 @@ -

<%= notice %>

-

Tasks

+ +
+
+

Not yet started

+
+
+ diff --git a/config/routes.rb b/config/routes.rb index 9ff63dc..eaf2bb8 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -6,7 +6,4 @@ devise_for :users # For details on the DSL available within this file, see https://guides.rubyonrails.org/routing.html - - - get "/:username" => "users#show", as: :user end diff --git a/lib/tasks/dev.rake b/lib/tasks/dev.rake index c8ccfc3..aed6e17 100644 --- a/lib/tasks/dev.rake +++ b/lib/tasks/dev.rake @@ -26,17 +26,17 @@ task sample_data: :environment do ) end - users = User.all + user = User.all - Task.statuses.values.each do |status| - rand(3..10).times do - user.tasks.create( - content: Faker::Hipster.sentence, - status: status - ) - end - end +# Task.statuses.values.each do |status| +# rand(3..10).times do +# user.tasks.create( +# content: Faker::Hipster.sentence, +# status: status +# ) +# end +# end ending = Time.now From adcb3cf5a71e6a23ba6a64a8716832a37ac561ce Mon Sep 17 00:00:00 2001 From: dmacinto Date: Sat, 21 Jan 2023 20:18:59 +0000 Subject: [PATCH 13/26] fixed navbar --- app/views/layouts/application.html.erb | 4 ++-- app/views/shared/_cdn_assets.html.erb | 8 ++++++-- app/views/tasks/index.html.erb | 21 +++++++++++---------- lib/tasks/dev.rake | 24 ++++++++++++++++++++++++ 4 files changed, 43 insertions(+), 14 deletions(-) diff --git a/app/views/layouts/application.html.erb b/app/views/layouts/application.html.erb index 38351d1..e4c5056 100644 --- a/app/views/layouts/application.html.erb +++ b/app/views/layouts/application.html.erb @@ -9,12 +9,12 @@ <%= render "shared/cdn_assets" %> - <%= stylesheet_link_tag 'application', media: 'all', 'data-turbolinks-track': 'reload' %> + <%= stylesheet_link_tag 'application', media: 'all','data-turbolinks-track': 'reload' %> <%= javascript_pack_tag 'application', 'data-turbolinks-track': 'reload' %> - <% render "shared/navbar" %> + <%= render "shared/navbar" %>
<%= render "shared/flash_messages" %> diff --git a/app/views/shared/_cdn_assets.html.erb b/app/views/shared/_cdn_assets.html.erb index aa16d0d..be7dbba 100644 --- a/app/views/shared/_cdn_assets.html.erb +++ b/app/views/shared/_cdn_assets.html.erb @@ -1,5 +1,9 @@ - + - + + + + + diff --git a/app/views/tasks/index.html.erb b/app/views/tasks/index.html.erb index 5aa216c..6bc748b 100644 --- a/app/views/tasks/index.html.erb +++ b/app/views/tasks/index.html.erb @@ -4,19 +4,20 @@

Not yet started

+
    + <% current_user.tasks.not_yet_started.each do |task| %> +
  • + <%= task.content %> +
  • + <% end %> +
-
- - - - - - - - - + + + + <% @tasks.each do |task| %> diff --git a/lib/tasks/dev.rake b/lib/tasks/dev.rake index aed6e17..18e431c 100644 --- a/lib/tasks/dev.rake +++ b/lib/tasks/dev.rake @@ -38,6 +38,30 @@ task sample_data: :environment do # end # end +#namespace :dev do +# desc "Hydrate database with fake sample data" +# task prime: :environment do +# user = User.find_or_create_by(email: "alice@example.com") do |user| +# user.password = "password" +# end + +# user.tasks.destroy_all + +# Task.statuses.values.each do |status| +# rand(3..10).times do +# user.tasks.create( +# content: Faker::Hacker.say_something_smart, +# status: status +# ) +# end +# end +# end + +#end + + + + ending = Time.now #p "It took #{(ending - starting).to_i} seconds to create sample data." From 0be0f0faccd184ed90d608c589746779843e5ba1 Mon Sep 17 00:00:00 2001 From: dmacinto Date: Sat, 21 Jan 2023 21:12:20 +0000 Subject: [PATCH 14/26] added move action --- app/controllers/tasks_controller.rb | 15 +++++++++ app/views/tasks/_task.html.erb | 39 +++++++++++++++++++++++ app/views/tasks/index.html.erb | 48 ++++++++++++----------------- 3 files changed, 73 insertions(+), 29 deletions(-) create mode 100644 app/views/tasks/_task.html.erb diff --git a/app/controllers/tasks_controller.rb b/app/controllers/tasks_controller.rb index 9f27b98..b8153df 100644 --- a/app/controllers/tasks_controller.rb +++ b/app/controllers/tasks_controller.rb @@ -1,6 +1,21 @@ class TasksController < ApplicationController before_action :set_task, only: %i[ show edit update destroy ] + def move + if @task.not_yet_started? + @task.in_progress! + elsif @task.in_progress? + @task.completed! + else + @task.in_progress! + end + + respond_to do |format| + format.html { redirect_to task_url, notice: "Task updated"} + end + end + + # GET /tasks or /tasks.json def index @tasks = Task.all diff --git a/app/views/tasks/_task.html.erb b/app/views/tasks/_task.html.erb new file mode 100644 index 0000000..28a38d2 --- /dev/null +++ b/app/views/tasks/_task.html.erb @@ -0,0 +1,39 @@ +
  • +
    + <%= task.content %> +
    + +
    +
    +
    + <%= link_to move_task_path(task), class: "btn btn-link" do %> + <% case task.status %> + <% when "not_yet_started" %> + + <% when "in_progress" %> + + <% else %> + + <% end %> + + <% end %> +
    +
    + +
    +
    + <%= link_to edit_task_path(task), class: "btn btn-link" do %> + + <% end %> +
    +
    + +
    +
    + <%= link_to task, method: :delete, class: "btn btn-link" do %> + + <% end %> +
    +
    +
    +
  • diff --git a/app/views/tasks/index.html.erb b/app/views/tasks/index.html.erb index 6bc748b..72a7525 100644 --- a/app/views/tasks/index.html.erb +++ b/app/views/tasks/index.html.erb @@ -1,39 +1,29 @@

    Tasks

    -
    -
    -

    Not yet started

    -
      - <% current_user.tasks.not_yet_started.each do |task| %> -
    • - <%= task.content %> -
    • - <% end %> +
      +
      +

      Not yet started

      + <%= link_to 'New Task', new_task_path %> +
        + <%= render current_user.tasks.not_yet_started.order(created_at: :asc) %>
      -
      - - - +
      +

      In progress

      +
        + <%= render current_user.tasks.in_progress.order(updated_at: :desc) %> +
      +
      -
    - <% @tasks.each do |task| %> - - - - - - - - - - <% end %> - -
    TasksTasks countTask captionOwner
    <%= task.tasks %><%= task.tasks_count %><%= task.task_caption %><%= task.owner_id %><%= link_to 'Show', task %><%= link_to 'Edit', edit_task_path(task) %><%= link_to 'Destroy', task, method: :delete, data: { confirm: 'Are you sure?' } %>
    +
    +

    Completed

    +
      + <%= render current_user.tasks.completed.order(updated_at: :desc) %> +
    +
    -
    -<%= link_to 'New Task', new_task_path %> +
    From df96eb740efa7e419eec6adcc1379b6f39990caf Mon Sep 17 00:00:00 2001 From: dmacinto Date: Sat, 21 Jan 2023 22:49:57 +0000 Subject: [PATCH 15/26] updated navbar for bootstrap 4.6 --- app/views/shared/_cdn_assets.html.erb | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/app/views/shared/_cdn_assets.html.erb b/app/views/shared/_cdn_assets.html.erb index be7dbba..e7eb80f 100644 --- a/app/views/shared/_cdn_assets.html.erb +++ b/app/views/shared/_cdn_assets.html.erb @@ -1,9 +1,7 @@ - + - + - - - + From 101f923e056f2a0771ec5eeef863c4f424f9d46e Mon Sep 17 00:00:00 2001 From: dmacinto Date: Sun, 22 Jan 2023 00:26:02 +0000 Subject: [PATCH 16/26] figuring out sample data stuff --- app/controllers/tasks_controller.rb | 3 + app/models/task.rb | 12 +++- app/models/user.rb | 12 +++- app/views/tasks/create.js.erb | 1 + app/views/tasks/destroy.js.erb | 1 + lib/tasks/auto_annotate_models.rake | 59 +++++++++++++++ lib/tasks/dev.rake | 84 +++++----------------- lib/tasks/test_dev.rake | 107 ++++++++++++++++++++++++++++ test/fixtures/tasks.yml | 10 ++- test/fixtures/users.yml | 12 +++- test/models/task_test.rb | 10 ++- test/models/user_test.rb | 12 +++- 12 files changed, 244 insertions(+), 79 deletions(-) create mode 100644 app/views/tasks/create.js.erb create mode 100644 app/views/tasks/destroy.js.erb create mode 100644 lib/tasks/auto_annotate_models.rake create mode 100644 lib/tasks/test_dev.rake diff --git a/app/controllers/tasks_controller.rb b/app/controllers/tasks_controller.rb index b8153df..cbc99c3 100644 --- a/app/controllers/tasks_controller.rb +++ b/app/controllers/tasks_controller.rb @@ -42,6 +42,7 @@ 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 } @@ -55,6 +56,7 @@ 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 } @@ -69,6 +71,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/models/task.rb b/app/models/task.rb index 8a2e59c..33658b8 100644 --- a/app/models/task.rb +++ b/app/models/task.rb @@ -4,14 +4,24 @@ # # id :bigint not null, primary key # content :text not null -# user_id :bigint not null # status :string default("not_yet_started"), not null # created_at :datetime not null # updated_at :datetime not null +# user_id :bigint not null +# +# Indexes +# +# index_tasks_on_user_id (user_id) +# +# Foreign Keys +# +# fk_rails_... (user_id => users.id) # class Task < ApplicationRecord belongs_to :user + validates :content, presence: true + enum status: { not_yet_started: "not_yet_started", in_progress: "in_progress", diff --git a/app/models/user.rb b/app/models/user.rb index b358a1a..0c77940 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -5,14 +5,20 @@ # id :bigint not null, primary key # email :citext default(""), not null # encrypted_password :string default(""), not null -# reset_password_token :string -# reset_password_sent_at :datetime # remember_created_at :datetime -# username :citext +# reset_password_sent_at :datetime +# reset_password_token :string # task_count :integer default(0) +# 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 diff --git a/app/views/tasks/create.js.erb b/app/views/tasks/create.js.erb new file mode 100644 index 0000000..07e0344 --- /dev/null +++ b/app/views/tasks/create.js.erb @@ -0,0 +1 @@ +fasdfasdfasdfas diff --git a/app/views/tasks/destroy.js.erb b/app/views/tasks/destroy.js.erb new file mode 100644 index 0000000..7b2c9bc --- /dev/null +++ b/app/views/tasks/destroy.js.erb @@ -0,0 +1 @@ +sdfafdsafdsfa 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/lib/tasks/dev.rake b/lib/tasks/dev.rake index 18e431c..8d01d9b 100644 --- a/lib/tasks/dev.rake +++ b/lib/tasks/dev.rake @@ -1,70 +1,20 @@ -task sample_data: :environment do - p "Creating sample data" - - if Rails.env.development? - Task.delete_all - User.delete_all - end - - - usernames = Array.new { Faker::Name.first_name } - - usernames << "alice" - usernames << "bob" - usernames << "charlie" - usernames << "doug" - usernames << "eric" - usernames << "frank" - usernames << "george" - - - usernames.each do |username| - User.create( - email: "#{username}@example.com", - password: "password", - username: username.downcase, - ) +namespace :dev do + desc "Hydrate database with fake sample data" + task prime: :environment do + user = User.find_or_create_by(email: "alice@example.com") do |user| + user.password = "password" + end + + user.tasks.destroy_all + + Task.statuses.values.each do |status| + rand(3..10).times do + user.tasks.create( + content: Faker::Hacker.say_something_smart, + status: status + ) + end + end end - - user = User.all - - -# Task.statuses.values.each do |status| -# rand(3..10).times do -# user.tasks.create( -# content: Faker::Hipster.sentence, -# status: status -# ) -# end -# end - -#namespace :dev do -# desc "Hydrate database with fake sample data" -# task prime: :environment do -# user = User.find_or_create_by(email: "alice@example.com") do |user| -# user.password = "password" -# end - -# user.tasks.destroy_all - -# Task.statuses.values.each do |status| -# rand(3..10).times do -# user.tasks.create( -# content: Faker::Hacker.say_something_smart, -# status: status -# ) -# end -# end -# 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 diff --git a/lib/tasks/test_dev.rake b/lib/tasks/test_dev.rake new file mode 100644 index 0000000..d023028 --- /dev/null +++ b/lib/tasks/test_dev.rake @@ -0,0 +1,107 @@ +task sample_data: :environment do + p "Creating sample data" + + if Rails.env.development? + Task.delete_all + User.delete_all + end + + + #usernames = Array.new { Faker::Name.first_name } + + #usernames << "alice" + #usernames << "bob" + #usernames << "charlie" + #usernames << "doug" + #usernames << "eric" + #usernames << "frank" + #usernames << "george" + + + #usernames.each do |username| + # User.create( + # email: "#{username}@example.com", + # password: "password", + # username: username.downcase, + # ) + #end + + #user = User.all + + + + + + + #Task.statuses.values.each do |status| + # rand(3..10).times do + # user.tasks.create( + # content: Faker::Hipster.sentence, + # status: status + # ) + # end + #end + + + + + + + + + + + #users.each do |user| + # rand(15).times do + # photo = user.own_photos.create( + # caption: Faker::Quote.jack_handey, + # image: "https://robohash.org/#{rand(9999)}" + # ) + + # user.followers.each do |follower| + # if rand < 0.5 + # photo.fans << follower + # end + + # if rand < 0.25 + # photo.comments.create( + # body: Faker::Quote.jack_handey, + # author: follower + # ) + # end + # end + # end + #end + + + + + + + + + + task prime: :environment do + user = User.find_or_create_by(email: "alice@example.com") do |user| + user.password = "password" + end + + user.tasks.destroy_all + + Task.statuses.values.each do |status| + rand(3..10).times do + user.tasks.create( + content: Faker::Hipster.sentence, + status: status + ) + end + 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 diff --git a/test/fixtures/tasks.yml b/test/fixtures/tasks.yml index 8b32e01..cbc19c5 100644 --- a/test/fixtures/tasks.yml +++ b/test/fixtures/tasks.yml @@ -4,10 +4,18 @@ # # id :bigint not null, primary key # content :text not null -# user_id :bigint not null # status :string default("not_yet_started"), not null # created_at :datetime not null # updated_at :datetime not null +# user_id :bigint not null +# +# Indexes +# +# index_tasks_on_user_id (user_id) +# +# Foreign Keys +# +# fk_rails_... (user_id => users.id) # one: tasks: MyString diff --git a/test/fixtures/users.yml b/test/fixtures/users.yml index 69d51f7..a67a848 100644 --- a/test/fixtures/users.yml +++ b/test/fixtures/users.yml @@ -5,14 +5,20 @@ # id :bigint not null, primary key # email :citext default(""), not null # encrypted_password :string default(""), not null -# reset_password_token :string -# reset_password_sent_at :datetime # remember_created_at :datetime -# username :citext +# reset_password_sent_at :datetime +# reset_password_token :string # task_count :integer default(0) +# 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 +# one: {} # column: value # diff --git a/test/models/task_test.rb b/test/models/task_test.rb index 65a947a..9d33ebd 100644 --- a/test/models/task_test.rb +++ b/test/models/task_test.rb @@ -4,10 +4,18 @@ # # id :bigint not null, primary key # content :text not null -# user_id :bigint not null # status :string default("not_yet_started"), not null # created_at :datetime not null # updated_at :datetime not null +# user_id :bigint not null +# +# Indexes +# +# index_tasks_on_user_id (user_id) +# +# Foreign Keys +# +# fk_rails_... (user_id => users.id) # require "test_helper" diff --git a/test/models/user_test.rb b/test/models/user_test.rb index e907716..3116cc3 100644 --- a/test/models/user_test.rb +++ b/test/models/user_test.rb @@ -5,14 +5,20 @@ # id :bigint not null, primary key # email :citext default(""), not null # encrypted_password :string default(""), not null -# reset_password_token :string -# reset_password_sent_at :datetime # remember_created_at :datetime -# username :citext +# reset_password_sent_at :datetime +# reset_password_token :string # task_count :integer default(0) +# 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 58528c0ac86d6f6820818a21128d15e3327095da Mon Sep 17 00:00:00 2001 From: dmacinto Date: Sun, 22 Jan 2023 00:33:03 +0000 Subject: [PATCH 17/26] sample data actually runs now --- app/models/user.rb | 2 +- lib/tasks/dev.rake | 2 +- lib/tasks/test_dev.rake | 6 +++--- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/app/models/user.rb b/app/models/user.rb index 0c77940..22d6876 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -26,5 +26,5 @@ class User < ApplicationRecord :recoverable, :rememberable, :validatable has_many :tasks, dependent: :destroy - validates :username, presence: true, uniqueness: true +# validates :username, presence: true, uniqueness: true end diff --git a/lib/tasks/dev.rake b/lib/tasks/dev.rake index 8d01d9b..0b50d9e 100644 --- a/lib/tasks/dev.rake +++ b/lib/tasks/dev.rake @@ -1,7 +1,7 @@ namespace :dev do desc "Hydrate database with fake sample data" task prime: :environment do - user = User.find_or_create_by(email: "alice@example.com") do |user| + user = User.find_or_create_by!(email: "alice@example.com") do |user| user.password = "password" end diff --git a/lib/tasks/test_dev.rake b/lib/tasks/test_dev.rake index d023028..6843238 100644 --- a/lib/tasks/test_dev.rake +++ b/lib/tasks/test_dev.rake @@ -81,12 +81,12 @@ task sample_data: :environment do - task prime: :environment do + user = User.find_or_create_by(email: "alice@example.com") do |user| user.password = "password" end - user.tasks.destroy_all + #user.tasks.destroy_all Task.statuses.values.each do |status| rand(3..10).times do @@ -96,7 +96,7 @@ task sample_data: :environment do ) end end - end + From edcdd9e2beec16cec84d088a255c124d6b78feae Mon Sep 17 00:00:00 2001 From: dmacinto Date: Tue, 24 Jan 2023 15:07:20 +0000 Subject: [PATCH 18/26] added move action --- app/controllers/tasks_controller.rb | 2 +- app/views/tasks/_task.html.erb | 5 +- config/routes.rb | 7 +- lib/tasks/test_dev.rake | 105 ++++------------------------ 4 files changed, 21 insertions(+), 98 deletions(-) diff --git a/app/controllers/tasks_controller.rb b/app/controllers/tasks_controller.rb index cbc99c3..1684506 100644 --- a/app/controllers/tasks_controller.rb +++ b/app/controllers/tasks_controller.rb @@ -1,5 +1,5 @@ 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.not_yet_started? diff --git a/app/views/tasks/_task.html.erb b/app/views/tasks/_task.html.erb index 28a38d2..64b8a9b 100644 --- a/app/views/tasks/_task.html.erb +++ b/app/views/tasks/_task.html.erb @@ -6,16 +6,15 @@
    - <%= link_to move_task_path(task), class: "btn btn-link" do %> + <%= link_to move_task_path(task), class: "btn btn-link", method: :patch, remote: true do %> <% case task.status %> <% when "not_yet_started" %> - <% when "in_progress" %> + <% elsif task.in_progress %> <% else %> <% end %> - <% end %>
    diff --git a/config/routes.rb b/config/routes.rb index eaf2bb8..1ba3e92 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -1,9 +1,14 @@ # == Route Map Rails.application.routes.draw do - resources :tasks + resources :tasks do + patch "move", on: :member + end + root "tasks#index" devise_for :users + + # For details on the DSL available within this file, see https://guides.rubyonrails.org/routing.html end diff --git a/lib/tasks/test_dev.rake b/lib/tasks/test_dev.rake index 6843238..134e604 100644 --- a/lib/tasks/test_dev.rake +++ b/lib/tasks/test_dev.rake @@ -6,102 +6,21 @@ task sample_data: :environment do User.delete_all end - - #usernames = Array.new { Faker::Name.first_name } - - #usernames << "alice" - #usernames << "bob" - #usernames << "charlie" - #usernames << "doug" - #usernames << "eric" - #usernames << "frank" - #usernames << "george" - - - #usernames.each do |username| - # User.create( - # email: "#{username}@example.com", - # password: "password", - # username: username.downcase, - # ) - #end - - #user = User.all - - - - - - - #Task.statuses.values.each do |status| - # rand(3..10).times do - # user.tasks.create( - # content: Faker::Hipster.sentence, - # status: status - # ) - # end - #end - - - - - - - - - - - #users.each do |user| - # rand(15).times do - # photo = user.own_photos.create( - # caption: Faker::Quote.jack_handey, - # image: "https://robohash.org/#{rand(9999)}" - # ) - - # user.followers.each do |follower| - # if rand < 0.5 - # photo.fans << follower - # end - - # if rand < 0.25 - # photo.comments.create( - # body: Faker::Quote.jack_handey, - # author: follower - # ) - # end - # end - # end - #end - - - - - - - - - - - user = User.find_or_create_by(email: "alice@example.com") do |user| - user.password = "password" - end - - #user.tasks.destroy_all - - Task.statuses.values.each do |status| - rand(3..10).times do - user.tasks.create( - content: Faker::Hipster.sentence, - status: status - ) - end + user = User.find_or_create_by(email: "alice@example.com") do |user| + user.password = "password" + end + + Task.statuses.values.each do |status| + rand(3..10).times do + user.tasks.create( + content: Faker::Hipster.sentence, + status: status + ) 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 3f0b94d28a9ea583c5f29cee1dfd8e9ecd908a63 Mon Sep 17 00:00:00 2001 From: dmacinto Date: Tue, 24 Jan 2023 15:13:10 +0000 Subject: [PATCH 19/26] fixed _task.html.erb typo --- app/views/tasks/_task.html.erb | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/app/views/tasks/_task.html.erb b/app/views/tasks/_task.html.erb index 64b8a9b..fde2db4 100644 --- a/app/views/tasks/_task.html.erb +++ b/app/views/tasks/_task.html.erb @@ -1,3 +1,4 @@ +
  • <%= task.content %> @@ -7,10 +8,9 @@
    <%= link_to move_task_path(task), class: "btn btn-link", method: :patch, remote: true do %> - <% case task.status %> - <% when "not_yet_started" %> + <% if task.not_yet_started? %> - <% elsif task.in_progress %> + <% elsif task.in_progress? %> <% else %> From ffd6ceb525c9ba27c99d02ca736138f5b12936e2 Mon Sep 17 00:00:00 2001 From: dmacinto Date: Tue, 24 Jan 2023 15:23:47 +0000 Subject: [PATCH 20/26] destroy edited --- app/controllers/tasks_controller.rb | 2 ++ app/views/shared/_cdn_assets.html.erb | 2 ++ app/views/tasks/_task.html.erb | 2 +- app/views/tasks/destroy.js.erb | 2 +- app/views/tasks/index.html.erb | 37 +++++++++++++++++---------- app/views/tasks/move.js.erb | 5 ++++ 6 files changed, 35 insertions(+), 15 deletions(-) create mode 100644 app/views/tasks/move.js.erb diff --git a/app/controllers/tasks_controller.rb b/app/controllers/tasks_controller.rb index 1684506..32900f1 100644 --- a/app/controllers/tasks_controller.rb +++ b/app/controllers/tasks_controller.rb @@ -12,6 +12,8 @@ def move respond_to do |format| format.html { redirect_to task_url, notice: "Task updated"} + + format.js end end diff --git a/app/views/shared/_cdn_assets.html.erb b/app/views/shared/_cdn_assets.html.erb index e7eb80f..f282cde 100644 --- a/app/views/shared/_cdn_assets.html.erb +++ b/app/views/shared/_cdn_assets.html.erb @@ -5,3 +5,5 @@ + + diff --git a/app/views/tasks/_task.html.erb b/app/views/tasks/_task.html.erb index fde2db4..c8e734f 100644 --- a/app/views/tasks/_task.html.erb +++ b/app/views/tasks/_task.html.erb @@ -29,7 +29,7 @@
    - <%= link_to task, method: :delete, class: "btn btn-link" do %> + <%= link_to task, method: :delete, remote: true, class: "btn btn-link" do %> <% end %>
    diff --git a/app/views/tasks/destroy.js.erb b/app/views/tasks/destroy.js.erb index 7b2c9bc..85eb1df 100644 --- a/app/views/tasks/destroy.js.erb +++ b/app/views/tasks/destroy.js.erb @@ -1 +1 @@ -sdfafdsafdsfa +$("#<%= dom_id @task %>").fadeOut(500); diff --git a/app/views/tasks/index.html.erb b/app/views/tasks/index.html.erb index 72a7525..d72ae9e 100644 --- a/app/views/tasks/index.html.erb +++ b/app/views/tasks/index.html.erb @@ -3,27 +3,38 @@
    -

    Not yet started

    - <%= link_to 'New Task', new_task_path %> -
      +
      +
      + Not yet started +
      + +
        <%= render current_user.tasks.not_yet_started.order(created_at: :asc) %>
      +
    -
    -

    In progress

    -
      +
      +
      + In progress +
      + +
        <%= render current_user.tasks.in_progress.order(updated_at: :desc) %> -
      +
    +
    -

    Completed

    -
      - <%= render current_user.tasks.completed.order(updated_at: :desc) %> -
    +
    +
    + Completed +
    + +
      + <%= render current_user.tasks.completed.order(updated_at: :desc) %> +
    +
    - -
    diff --git a/app/views/tasks/move.js.erb b/app/views/tasks/move.js.erb new file mode 100644 index 0000000..52fd7b6 --- /dev/null +++ b/app/views/tasks/move.js.erb @@ -0,0 +1,5 @@ +$("#<%= dom_id @task %>").remove(); + +var updated_task = $("<%= j(render @task) %>"); + +$("#<%= @task.status %>_list").prepend(updated_task); From 2b7f26e3dd693a2aa67a6dc208959a57a4750f43 Mon Sep 17 00:00:00 2001 From: dmacinto Date: Tue, 24 Jan 2023 15:33:20 +0000 Subject: [PATCH 21/26] update and some clean up --- app/controllers/tasks_controller.rb | 7 ++++- app/views/shared/_navbar.html.erb | 2 +- app/views/tasks/_form.html.erb | 45 +++++++---------------------- app/views/tasks/_task.html.erb | 2 +- app/views/tasks/edit.js.erb | 1 + app/views/tasks/update.js.erb | 1 + 6 files changed, 21 insertions(+), 37 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 32900f1..280ee08 100644 --- a/app/controllers/tasks_controller.rb +++ b/app/controllers/tasks_controller.rb @@ -12,7 +12,6 @@ def move respond_to do |format| format.html { redirect_to task_url, notice: "Task updated"} - format.js end end @@ -34,6 +33,10 @@ def new # GET /tasks/1/edit def edit + respond_to do |format| + format.html + format.js + end end # POST /tasks or /tasks.json @@ -48,6 +51,7 @@ def create else format.html { render :new, status: :unprocessable_entity } format.json { render json: @task.errors, status: :unprocessable_entity } + format.js end end end @@ -62,6 +66,7 @@ def update else format.html { render :edit, status: :unprocessable_entity } format.json { render json: @task.errors, status: :unprocessable_entity } + format.js end end end diff --git a/app/views/shared/_navbar.html.erb b/app/views/shared/_navbar.html.erb index 3136f12..8667d7f 100644 --- a/app/views/shared/_navbar.html.erb +++ b/app/views/shared/_navbar.html.erb @@ -1,6 +1,6 @@