From 758ec1fc77ccd658f3b3ad44fa02b3a0db385c2b Mon Sep 17 00:00:00 2001 From: Yago Santos Date: Wed, 13 Oct 2021 21:48:23 -0400 Subject: [PATCH 1/9] refactor: Add a handle column to events table --- db/migrate/20211014013357_add_handle_to_events.rb | 8 ++++++++ db/schema.rb | 3 ++- 2 files changed, 10 insertions(+), 1 deletion(-) create mode 100644 db/migrate/20211014013357_add_handle_to_events.rb diff --git a/db/migrate/20211014013357_add_handle_to_events.rb b/db/migrate/20211014013357_add_handle_to_events.rb new file mode 100644 index 0000000..336293c --- /dev/null +++ b/db/migrate/20211014013357_add_handle_to_events.rb @@ -0,0 +1,8 @@ +# frozen_string_literal: true + +# Add a handle column to events table +class AddHandleToEvents < ActiveRecord::Migration[6.1] + def change + add_column :events, :handle, :string + end +end diff --git a/db/schema.rb b/db/schema.rb index 80479dc..284d75b 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,7 +10,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 2021_10_06_022040) do +ActiveRecord::Schema.define(version: 2021_10_14_013357) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" @@ -31,6 +31,7 @@ t.datetime "end_at", null: false t.datetime "created_at", precision: 6, null: false t.datetime "updated_at", precision: 6, null: false + t.string "handle" end create_table "friendships", force: :cascade do |t| From 249155da094d3529b455daa0a0272303f044c210 Mon Sep 17 00:00:00 2001 From: Yago Santos Date: Wed, 13 Oct 2021 21:49:33 -0400 Subject: [PATCH 2/9] feature: Add presence validation to handle column --- app/models/event.rb | 1 + 1 file changed, 1 insertion(+) diff --git a/app/models/event.rb b/app/models/event.rb index 11bed9a..eafc434 100644 --- a/app/models/event.rb +++ b/app/models/event.rb @@ -6,6 +6,7 @@ class Event < ApplicationRecord has_many :attendees, through: :event_attendees, source: :profile validates :start_at, :end_at, presence: true + validates :handle, presence: true scope :ongoing_or_upcoming, -> { where("end_at >= ?", Time.zone.now) } From ce1f292d065e9cf8ae7747069d100b4d58fcfc79 Mon Sep 17 00:00:00 2001 From: Yago Santos Date: Wed, 13 Oct 2021 21:52:06 -0400 Subject: [PATCH 3/9] refactor: Update event factory to include handle --- spec/factories/events.rb | 3 +++ 1 file changed, 3 insertions(+) diff --git a/spec/factories/events.rb b/spec/factories/events.rb index f7d29f5..3016a3e 100644 --- a/spec/factories/events.rb +++ b/spec/factories/events.rb @@ -3,18 +3,21 @@ FactoryBot.define do factory :event do name { "RubyConf" } + handle { "RubyConf" } description { "[RubyConf 2020](https://rubyconf.org/) will be held in `Denver`." } start_at { "2021-11-08 00:00:00" } end_at { "2021-11-10 00:00:00" } trait :one_day do name { "HexDevs Open-Source" } + handle { "HexDevs" } start_at { "2021-08-05 21:00:00" } end_at { "2021-08-05 23:59:00" } end trait :past_event do name { "PastConf" } + handle { "PastConf" } description { "The conference you always miss by a few days." } start_at { 3.days.ago } end_at { 1.day.ago } From 0024cdced4753082422098f23067b8250b3ed47f Mon Sep 17 00:00:00 2001 From: Yago Santos Date: Wed, 13 Oct 2021 21:53:35 -0400 Subject: [PATCH 4/9] refactor: Add handle to list of trusted event params --- app/controllers/events_controller.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/controllers/events_controller.rb b/app/controllers/events_controller.rb index 9ada4d4..bdce02c 100644 --- a/app/controllers/events_controller.rb +++ b/app/controllers/events_controller.rb @@ -84,6 +84,6 @@ def set_event # Only allow a list of trusted parameters through. def event_params - params.require(:event).permit(:name, :description, :start_at, :end_at) + params.require(:event).permit(:name, :handle, :description, :start_at, :end_at) end end From 446b753c064250cf9baec53be3777edfd43bd0c6 Mon Sep 17 00:00:00 2001 From: Yago Santos Date: Thu, 14 Oct 2021 22:10:57 -0400 Subject: [PATCH 5/9] refactor: Add handle to form partial --- app/views/events/_form.html.erb | 1 + 1 file changed, 1 insertion(+) diff --git a/app/views/events/_form.html.erb b/app/views/events/_form.html.erb index 11393da..b1b8b46 100644 --- a/app/views/events/_form.html.erb +++ b/app/views/events/_form.html.erb @@ -3,6 +3,7 @@ <%= render "layouts/errors", resource: @event %> <%= form.labelled_text_field :name %> + <%= form.labelled_text_field :handle %> <%= form.labelled_text_area :description %>
From f307cb0e4ac9616562219dbd92393b0f1e8de8f7 Mon Sep 17 00:00:00 2001 From: Yago Santos Date: Thu, 14 Oct 2021 22:12:49 -0400 Subject: [PATCH 6/9] refactor: Add index and unique to handle attribute in the events table --- db/migrate/20211015020446_change_handle_to_events.rb | 8 ++++++++ db/schema.rb | 3 ++- 2 files changed, 10 insertions(+), 1 deletion(-) create mode 100644 db/migrate/20211015020446_change_handle_to_events.rb diff --git a/db/migrate/20211015020446_change_handle_to_events.rb b/db/migrate/20211015020446_change_handle_to_events.rb new file mode 100644 index 0000000..13fe3fb --- /dev/null +++ b/db/migrate/20211015020446_change_handle_to_events.rb @@ -0,0 +1,8 @@ +# frozen_string_literal: true + +# Add index and unique to handle attribute in the events table +class ChangeHandleToEvents < ActiveRecord::Migration[6.1] + def change + add_index :events, :handle, unique: true + end +end diff --git a/db/schema.rb b/db/schema.rb index 284d75b..f86b49e 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,7 +10,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 2021_10_14_013357) do +ActiveRecord::Schema.define(version: 2021_10_15_020446) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" @@ -32,6 +32,7 @@ t.datetime "created_at", precision: 6, null: false t.datetime "updated_at", precision: 6, null: false t.string "handle" + t.index ["handle"], name: "index_events_on_handle", unique: true end create_table "friendships", force: :cascade do |t| From ff9964197ef51267d524d6b348c43ead35ca86e5 Mon Sep 17 00:00:00 2001 From: Yago Santos Date: Thu, 14 Oct 2021 22:16:59 -0400 Subject: [PATCH 7/9] refactor: Add uniqueness and url format validation to handle --- app/models/event.rb | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/app/models/event.rb b/app/models/event.rb index eafc434..9f73cda 100644 --- a/app/models/event.rb +++ b/app/models/event.rb @@ -6,7 +6,9 @@ class Event < ApplicationRecord has_many :attendees, through: :event_attendees, source: :profile validates :start_at, :end_at, presence: true - validates :handle, presence: true + validates :handle, presence: true, + format: { with: /\A[a-zA-Z0-9]+\z/, message: "Only letters and numbers are allowed" }, + uniqueness: { case_sensitive: true } scope :ongoing_or_upcoming, -> { where("end_at >= ?", Time.zone.now) } From 750616b2937de4f78f0cfc613a17196f663fbbad Mon Sep 17 00:00:00 2001 From: Yago Santos Date: Thu, 14 Oct 2021 22:18:12 -0400 Subject: [PATCH 8/9] refactor: Update handle attribute in Event factory This will account for the fact that handles must be unique and match the viable url format. --- spec/factories/events.rb | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/spec/factories/events.rb b/spec/factories/events.rb index 3016a3e..8e7f32f 100644 --- a/spec/factories/events.rb +++ b/spec/factories/events.rb @@ -3,21 +3,21 @@ FactoryBot.define do factory :event do name { "RubyConf" } - handle { "RubyConf" } + sequence(:handle) { |n| "RubyConf#{n}" } description { "[RubyConf 2020](https://rubyconf.org/) will be held in `Denver`." } start_at { "2021-11-08 00:00:00" } end_at { "2021-11-10 00:00:00" } trait :one_day do name { "HexDevs Open-Source" } - handle { "HexDevs" } + sequence(:handle) { |n| "HexDevs#{n}" } start_at { "2021-08-05 21:00:00" } end_at { "2021-08-05 23:59:00" } end trait :past_event do name { "PastConf" } - handle { "PastConf" } + sequence(:handle) { |n| "PastConf#{n}" } description { "The conference you always miss by a few days." } start_at { 3.days.ago } end_at { 1.day.ago } From 3f8fe6b3a51874c307d14d5f938df642a51c1788 Mon Sep 17 00:00:00 2001 From: Yago Santos Date: Sat, 16 Oct 2021 20:58:14 -0400 Subject: [PATCH 9/9] refactor: Add handle column to events, along with index and unique We've removed both the AddHandleToEvents and ChangeHandleToEvents migrations, because it's possible to complete both changes in a single migration (AddIndexToEvents). --- db/migrate/20211014013357_add_handle_to_events.rb | 8 -------- db/migrate/20211015020446_change_handle_to_events.rb | 8 -------- db/migrate/20211017005416_add_index_to_events.rb | 10 ++++++++++ db/schema.rb | 2 +- 4 files changed, 11 insertions(+), 17 deletions(-) delete mode 100644 db/migrate/20211014013357_add_handle_to_events.rb delete mode 100644 db/migrate/20211015020446_change_handle_to_events.rb create mode 100644 db/migrate/20211017005416_add_index_to_events.rb diff --git a/db/migrate/20211014013357_add_handle_to_events.rb b/db/migrate/20211014013357_add_handle_to_events.rb deleted file mode 100644 index 336293c..0000000 --- a/db/migrate/20211014013357_add_handle_to_events.rb +++ /dev/null @@ -1,8 +0,0 @@ -# frozen_string_literal: true - -# Add a handle column to events table -class AddHandleToEvents < ActiveRecord::Migration[6.1] - def change - add_column :events, :handle, :string - end -end diff --git a/db/migrate/20211015020446_change_handle_to_events.rb b/db/migrate/20211015020446_change_handle_to_events.rb deleted file mode 100644 index 13fe3fb..0000000 --- a/db/migrate/20211015020446_change_handle_to_events.rb +++ /dev/null @@ -1,8 +0,0 @@ -# frozen_string_literal: true - -# Add index and unique to handle attribute in the events table -class ChangeHandleToEvents < ActiveRecord::Migration[6.1] - def change - add_index :events, :handle, unique: true - end -end diff --git a/db/migrate/20211017005416_add_index_to_events.rb b/db/migrate/20211017005416_add_index_to_events.rb new file mode 100644 index 0000000..f23af42 --- /dev/null +++ b/db/migrate/20211017005416_add_index_to_events.rb @@ -0,0 +1,10 @@ +# frozen_string_literal: true + +# Add a handle column to events table. +# Add index and unique to handle attribute in the events table. +class AddIndexToEvents < ActiveRecord::Migration[6.1] + def change + add_column :events, :handle, :string + add_index :events, :handle, unique: true + end +end diff --git a/db/schema.rb b/db/schema.rb index f86b49e..bfded4b 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,7 +10,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 2021_10_15_020446) do +ActiveRecord::Schema.define(version: 2021_10_17_005416) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql"