From 5c15b07dccbf995bff0f60b1f4bb6f6617be95e6 Mon Sep 17 00:00:00 2001 From: Aakash Nepal Date: Mon, 13 May 2024 21:44:51 +0545 Subject: [PATCH 01/31] feat: add migration and model for custom links --- app/models/custom_link.rb | 3 +++ db/migrate/20240513155246_create_custom_links.rb | 10 ++++++++++ 2 files changed, 13 insertions(+) create mode 100644 app/models/custom_link.rb create mode 100644 db/migrate/20240513155246_create_custom_links.rb diff --git a/app/models/custom_link.rb b/app/models/custom_link.rb new file mode 100644 index 0000000000..e6cba7499b --- /dev/null +++ b/app/models/custom_link.rb @@ -0,0 +1,3 @@ +class CustomLink < ApplicationRecord + belongs_to :casa_org +end diff --git a/db/migrate/20240513155246_create_custom_links.rb b/db/migrate/20240513155246_create_custom_links.rb new file mode 100644 index 0000000000..b1ed1d4f9b --- /dev/null +++ b/db/migrate/20240513155246_create_custom_links.rb @@ -0,0 +1,10 @@ +class CreateCustomLinks < ActiveRecord::Migration[7.1] + def change + create_table :custom_links do |t| + t.string :text + t.text :url + t.references :casa_org, null: false, foreign_key: true + t.timestamps + end + end +end From 10e78b9970a6d11957f374702c730e1777e759f5 Mon Sep 17 00:00:00 2001 From: Aakash Nepal Date: Mon, 13 May 2024 21:50:58 +0545 Subject: [PATCH 02/31] feat: update the schema information --- app/models/custom_link.rb | 20 ++++++++++++++++++++ app/models/learning_hour.rb | 1 - db/schema.rb | 13 +++++++++++-- 3 files changed, 31 insertions(+), 3 deletions(-) diff --git a/app/models/custom_link.rb b/app/models/custom_link.rb index e6cba7499b..ac97064f0e 100644 --- a/app/models/custom_link.rb +++ b/app/models/custom_link.rb @@ -1,3 +1,23 @@ class CustomLink < ApplicationRecord belongs_to :casa_org end + +# == Schema Information +# +# Table name: custom_links +# +# id :bigint not null, primary key +# text :string +# url :text +# created_at :datetime not null +# updated_at :datetime not null +# casa_org_id :bigint not null +# +# Indexes +# +# index_custom_links_on_casa_org_id (casa_org_id) +# +# Foreign Keys +# +# fk_rails_... (casa_org_id => casa_orgs.id) +# diff --git a/app/models/learning_hour.rb b/app/models/learning_hour.rb index a3405354c1..e22242693f 100644 --- a/app/models/learning_hour.rb +++ b/app/models/learning_hour.rb @@ -52,7 +52,6 @@ def user_org_learning_topic_enable? # id :bigint not null, primary key # duration_hours :integer not null # duration_minutes :integer not null -# learning_type :integer default(5) # name :string not null # occurred_at :datetime not null # created_at :datetime not null diff --git a/db/schema.rb b/db/schema.rb index a618e59f84..7c9f1cfb94 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[7.1].define(version: 2024_05_07_022441) do +ActiveRecord::Schema[7.1].define(version: 2024_05_13_155246) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" @@ -298,6 +298,15 @@ t.index ["judge_id"], name: "index_court_dates_on_judge_id" end + create_table "custom_links", force: :cascade do |t| + t.string "text" + t.text "url" + t.bigint "casa_org_id", null: false + t.datetime "created_at", null: false + t.datetime "updated_at", null: false + t.index ["casa_org_id"], name: "index_custom_links_on_casa_org_id" + end + create_table "delayed_jobs", force: :cascade do |t| t.integer "priority", default: 0, null: false t.integer "attempts", default: 0, null: false @@ -420,7 +429,6 @@ create_table "learning_hours", force: :cascade do |t| t.bigint "user_id", null: false - t.integer "learning_type", default: 5 t.string "name", null: false t.integer "duration_minutes", null: false t.integer "duration_hours", null: false @@ -684,6 +692,7 @@ add_foreign_key "contact_topic_answers", "contact_topics" add_foreign_key "contact_topics", "casa_orgs" add_foreign_key "court_dates", "casa_cases" + add_foreign_key "custom_links", "casa_orgs" add_foreign_key "emancipation_options", "emancipation_categories" add_foreign_key "followups", "users", column: "creator_id" add_foreign_key "judges", "casa_orgs" From 5910151574add8a18f0df1ec8fd6a951b891a518 Mon Sep 17 00:00:00 2001 From: Aakash Nepal Date: Mon, 13 May 2024 21:51:21 +0545 Subject: [PATCH 03/31] feat: add factory and spec file --- spec/factories/custom_links.rb | 5 +++++ spec/models/custom_link_spec.rb | 5 +++++ 2 files changed, 10 insertions(+) create mode 100644 spec/factories/custom_links.rb create mode 100644 spec/models/custom_link_spec.rb diff --git a/spec/factories/custom_links.rb b/spec/factories/custom_links.rb new file mode 100644 index 0000000000..e780483cdb --- /dev/null +++ b/spec/factories/custom_links.rb @@ -0,0 +1,5 @@ +FactoryBot.define do + factory :custom_link do + + end +end diff --git a/spec/models/custom_link_spec.rb b/spec/models/custom_link_spec.rb new file mode 100644 index 0000000000..b02c93c919 --- /dev/null +++ b/spec/models/custom_link_spec.rb @@ -0,0 +1,5 @@ +require 'rails_helper' + +RSpec.describe CustomLink, type: :model do + pending "add some examples to (or delete) #{__FILE__}" +end From 22fbe5b1f91ae750ed2691d0784765e06a6b6aca Mon Sep 17 00:00:00 2001 From: Aakash Nepal Date: Sun, 23 Jun 2024 22:36:31 +0545 Subject: [PATCH 04/31] feat: add setter for custom url --- app/controllers/casa_org_controller.rb | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/app/controllers/casa_org_controller.rb b/app/controllers/casa_org_controller.rb index 77392b4ee1..c859b3cdd6 100644 --- a/app/controllers/casa_org_controller.rb +++ b/app/controllers/casa_org_controller.rb @@ -90,6 +90,10 @@ def set_contact_topics @contact_topics = @casa_org.contact_topics.where(soft_delete: false) end + def set_custom_url + @custom_links = @casa_org.custom_links.where(soft_delete: false) + end + def set_active_storage_url_options ActiveStorage::Current.url_options = {host: request.base_url} end From e04602070d1f58195ebf1302ed101c35eb9ac1ee Mon Sep 17 00:00:00 2001 From: Aakash Nepal Date: Sun, 23 Jun 2024 22:37:30 +0545 Subject: [PATCH 05/31] feat: add custom link section in edit organization page --- app/views/casa_org/edit.html.erb | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/app/views/casa_org/edit.html.erb b/app/views/casa_org/edit.html.erb index 7805b8250f..a0bbaad2c7 100644 --- a/app/views/casa_org/edit.html.erb +++ b/app/views/casa_org/edit.html.erb @@ -163,3 +163,17 @@
<%= render "contact_topics" %>
+
+
+
+
+

+ Manage Custom Links +

+
+
+
+
+
+ <%= render "custom_links" %> +
\ No newline at end of file From a697e1082cfac880e83682b63ba18d71d1c44235 Mon Sep 17 00:00:00 2001 From: Aakash Nepal Date: Sun, 23 Jun 2024 22:40:15 +0545 Subject: [PATCH 06/31] fet: add before action and update setter name --- app/controllers/casa_org_controller.rb | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/app/controllers/casa_org_controller.rb b/app/controllers/casa_org_controller.rb index c859b3cdd6..807ca6fc04 100644 --- a/app/controllers/casa_org_controller.rb +++ b/app/controllers/casa_org_controller.rb @@ -7,6 +7,7 @@ class CasaOrgController < ApplicationController before_action :set_learning_hour_topics, only: %i[edit update] before_action :set_sent_emails, only: %i[edit update] before_action :set_contact_topics, only: %i[edit update] + before_action :set_custom_links, only: %i[edit update] before_action :require_organization! after_action :verify_authorized before_action :set_active_storage_url_options, only: %i[edit update] @@ -90,7 +91,7 @@ def set_contact_topics @contact_topics = @casa_org.contact_topics.where(soft_delete: false) end - def set_custom_url + def set_custom_links @custom_links = @casa_org.custom_links.where(soft_delete: false) end From c25ba65d7f18ef7eca374186540c920f65117804 Mon Sep 17 00:00:00 2001 From: Aakash Nepal Date: Sun, 23 Jun 2024 22:43:04 +0545 Subject: [PATCH 07/31] feat: add soft delete column to custom links table --- db/migrate/20240513155246_create_custom_links.rb | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/db/migrate/20240513155246_create_custom_links.rb b/db/migrate/20240513155246_create_custom_links.rb index b1ed1d4f9b..dc20604c9f 100644 --- a/db/migrate/20240513155246_create_custom_links.rb +++ b/db/migrate/20240513155246_create_custom_links.rb @@ -1,9 +1,13 @@ +# frozen_string_literal: true + +# Migration file to create custom_links table class CreateCustomLinks < ActiveRecord::Migration[7.1] def change create_table :custom_links do |t| t.string :text t.text :url t.references :casa_org, null: false, foreign_key: true + t.boolean :soft_delete, null: false, default: false t.timestamps end end From 53b95c204c588de499f504e714445463a30b2532 Mon Sep 17 00:00:00 2001 From: Aakash Nepal Date: Sun, 23 Jun 2024 22:50:00 +0545 Subject: [PATCH 08/31] feat: add active scope to custom link class --- app/models/custom_link.rb | 2 ++ 1 file changed, 2 insertions(+) diff --git a/app/models/custom_link.rb b/app/models/custom_link.rb index ac97064f0e..6c9503c91d 100644 --- a/app/models/custom_link.rb +++ b/app/models/custom_link.rb @@ -1,5 +1,6 @@ class CustomLink < ApplicationRecord belongs_to :casa_org + scope :active, -> { where(active: true, soft_delete: false) } end # == Schema Information @@ -7,6 +8,7 @@ class CustomLink < ApplicationRecord # Table name: custom_links # # id :bigint not null, primary key +# soft_delete :boolean default(FALSE), not null # text :string # url :text # created_at :datetime not null From 33a0d41a5e23d9752fe5ddc795e0361645a5bf11 Mon Sep 17 00:00:00 2001 From: Aakash Nepal Date: Sun, 23 Jun 2024 23:07:20 +0545 Subject: [PATCH 09/31] feat: add active column to custom links table --- db/migrate/20240513155246_create_custom_links.rb | 1 + 1 file changed, 1 insertion(+) diff --git a/db/migrate/20240513155246_create_custom_links.rb b/db/migrate/20240513155246_create_custom_links.rb index dc20604c9f..c838fdbdbf 100644 --- a/db/migrate/20240513155246_create_custom_links.rb +++ b/db/migrate/20240513155246_create_custom_links.rb @@ -8,6 +8,7 @@ def change t.text :url t.references :casa_org, null: false, foreign_key: true t.boolean :soft_delete, null: false, default: false + t.boolean :active, null: false, default: true t.timestamps end end From ebbb863cc48932eeae7060b89a6996a031829181 Mon Sep 17 00:00:00 2001 From: Aakash Nepal Date: Sun, 23 Jun 2024 23:24:11 +0545 Subject: [PATCH 10/31] feat: update destroy popup text --- app/views/casa_org/_custom_links.html.erb | 64 +++++++++++++++++++++++ 1 file changed, 64 insertions(+) create mode 100644 app/views/casa_org/_custom_links.html.erb diff --git a/app/views/casa_org/_custom_links.html.erb b/app/views/casa_org/_custom_links.html.erb new file mode 100644 index 0000000000..f8ef91b64f --- /dev/null +++ b/app/views/casa_org/_custom_links.html.erb @@ -0,0 +1,64 @@ +
+
+
+
+
+

Custom Link

+
+
+ +
+
+
+ + + + + + + + + + <% @custom_links.each do |custom_link| %> + <% id = "custom_link-#{custom_link.id}" %> + + + + + + + <%= render(Modal::GroupComponent.new(id: id)) do |component| %> + <% component.with_header(text: "Delete Custom Link?", id: id) %> + <% component.with_body(text: [ + "This custom link will be deleted and will no longer be visible in profile dropdown."]) %> + <% component.with_footer do %> + <%= link_to soft_delete_custom_link_path(custom_link), method: :delete, + class: "btn-sm main-btn danger-btn btn-hover ms-auto" do %> + + Delete Custom Link + <% end %> + <% end %> + <% end %> + <% end %> + +
Display TextURLActive?
+ <%= custom_link.text %> + <%= custom_link.url %> + <%= custom_link.active ? "Yes" : "No" %> + + <%= render(DropdownMenuComponent.new(menu_title: "Actions Menu", hide_label: true)) do %> +
  • <%= link_to "Edit", edit_custom_link_path(custom_link), class: "dropdown-item" %>
  • +
  • <%= render(Modal::OpenLinkComponent.new(text: "Delete", target: id, klass: "dropdown-item")) %>
  • + <% end %> +
    +
    +
    +
    +
    From ab5454a52d6bfb4fbedf54c73e50288bbae9bbc7 Mon Sep 17 00:00:00 2001 From: Aakash Nepal Date: Sun, 23 Jun 2024 23:24:31 +0545 Subject: [PATCH 11/31] feat: add custom link policy --- app/policies/custom_link_policy.rb | 8 ++++++++ 1 file changed, 8 insertions(+) create mode 100644 app/policies/custom_link_policy.rb diff --git a/app/policies/custom_link_policy.rb b/app/policies/custom_link_policy.rb new file mode 100644 index 0000000000..416e65094b --- /dev/null +++ b/app/policies/custom_link_policy.rb @@ -0,0 +1,8 @@ +class CustomLinkPolicy < ApplicationPolicy + alias_method :create?, :is_admin_same_org? + alias_method :edit?, :is_admin_same_org? + alias_method :new?, :is_admin_same_org? + alias_method :show?, :is_admin_same_org? + alias_method :update?, :is_admin_same_org? + alias_method :soft_delete?, :is_admin_same_org? +end From a7b39b3a8c9a860b27bce4edc86bcc03ed999ac1 Mon Sep 17 00:00:00 2001 From: Aakash Nepal Date: Sun, 23 Jun 2024 23:25:07 +0545 Subject: [PATCH 12/31] feat: add route and controller for custom link policy --- app/controllers/custom_links_controller.rb | 72 ++++++++++++++++++++++ config/routes.rb | 4 ++ 2 files changed, 76 insertions(+) create mode 100644 app/controllers/custom_links_controller.rb diff --git a/app/controllers/custom_links_controller.rb b/app/controllers/custom_links_controller.rb new file mode 100644 index 0000000000..5a9dcbf311 --- /dev/null +++ b/app/controllers/custom_links_controller.rb @@ -0,0 +1,72 @@ +class CustomLinksController < ApplicationController + before_action :set_custom_link, only: %i[edit update soft_delete] + + # GET /custom_links + def index + byebug + @custom_links = CustomLink.all + end + + # GET /custom_links/1 + def show + end + + # GET /custom_links/new + def new + authorize CustomLink + custom_link = CustomLink.new(casa_org_id: current_user.casa_org_id) + @custom_link = custom_link + end + + # GET /custom_links/1/edit + def edit + authorize @custom_link + end + + # POST /custom_links + def create + authorize CustomLink + + @custom_link = CustomLink.new(custom_link_params) + + if @custom_link.save + redirect_to edit_casa_org_path(current_organization), notice: 'Custom link was successfully created.' + else + render :new + end + end + + # PATCH/PUT /custom_links/1 + def update + authorize @custom_link + if @custom_link.update(custom_link_params) + redirect_to edit_casa_org_path(current_organization), notice: 'Custom link was successfully updated.' + else + render :edit + end + end + + # DELETE /contact_topics/1/soft_delete + def soft_delete + authorize @custom_link + + if @custom_link.update(soft_delete: true) + redirect_to edit_casa_org_path(current_organization), notice: "Custom link was successfully removed." + else + render :show, status: :unprocessable_entity + end + end + + private + # Use callbacks to share common setup or constraints between actions. + def set_custom_link + @custom_link = CustomLink.find(params[:id]) + end + + # Only allow a list of trusted parameters through. + def custom_link_params + params.require(:custom_link).permit(:text, :url, :active, :casa_org_id) + end + + +end diff --git a/config/routes.rb b/config/routes.rb index 0e7554ad24..699198710c 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -126,6 +126,10 @@ delete "soft_delete", on: :member end + resources :custom_links, except: %i[index show delete] do + delete "soft_delete", on: :member + end + resources :followup_reports, only: :index resources :placement_reports, only: :index resources :banners, except: %i[show] do From be36735e6c1af635e43c6a4c13c4594b4cf81ba7 Mon Sep 17 00:00:00 2001 From: Aakash Nepal Date: Sun, 23 Jun 2024 23:26:04 +0545 Subject: [PATCH 13/31] feat: add views for crud --- app/views/custom_links/_form.html.erb | 39 +++++++++++++++++++++++++++ app/views/custom_links/edit.html.erb | 1 + app/views/custom_links/new.html.erb | 1 + 3 files changed, 41 insertions(+) create mode 100644 app/views/custom_links/_form.html.erb create mode 100644 app/views/custom_links/edit.html.erb create mode 100644 app/views/custom_links/new.html.erb diff --git a/app/views/custom_links/_form.html.erb b/app/views/custom_links/_form.html.erb new file mode 100644 index 0000000000..9011c5d159 --- /dev/null +++ b/app/views/custom_links/_form.html.erb @@ -0,0 +1,39 @@ +
    +
    +
    +
    +

    + <%= title %> +

    +
    +
    +
    +
    + + +
    + <%= form_with(model: custom_link, local: true) do |form| %> + <%= form.hidden_field :casa_org_id %> +
    + <%= render "/shared/error_messages", resource: custom_link %> +
    +
    + <%= form.label :text, "Display Text" %> + <%= form.text_field :text, class: "form-control", required: true %> +
    +
    + <%= form.label :url, "URL" %> + <%= form.text_field :url, rows: 5, class: "form-control", required: true %> +
    +
    + <%= form.check_box :active, class: 'form-check-input' %> + <%= form.label :active, "Active?", class: 'form-check-label' %> +
    +
    + <%= button_tag(type: "submit", class: "btn-sm main-btn primary-btn btn-hover") do %> + Submit + <% end %> +
    + <% end %> +
    + diff --git a/app/views/custom_links/edit.html.erb b/app/views/custom_links/edit.html.erb new file mode 100644 index 0000000000..dd75347084 --- /dev/null +++ b/app/views/custom_links/edit.html.erb @@ -0,0 +1 @@ +<%= render partial: "form", locals: {title: "Custom Link", custom_link: @custom_link} %> diff --git a/app/views/custom_links/new.html.erb b/app/views/custom_links/new.html.erb new file mode 100644 index 0000000000..d225077815 --- /dev/null +++ b/app/views/custom_links/new.html.erb @@ -0,0 +1 @@ +<%= render partial: "form", locals: {title: "New Custom Link", custom_link: @custom_link} %> From 9a5df298d0ad3f2aca5142a5fe20129d45e16179 Mon Sep 17 00:00:00 2001 From: Aakash Nepal Date: Sun, 23 Jun 2024 23:26:28 +0545 Subject: [PATCH 14/31] feat: add association with custom links --- app/models/casa_org.rb | 1 + 1 file changed, 1 insertion(+) diff --git a/app/models/casa_org.rb b/app/models/casa_org.rb index b5b9c7461c..4c7b0158c0 100644 --- a/app/models/casa_org.rb +++ b/app/models/casa_org.rb @@ -26,6 +26,7 @@ class CasaOrg < ApplicationRecord has_many :learning_hour_topics, dependent: :destroy has_many :case_groups, dependent: :destroy has_many :contact_topics + has_many :custom_links has_one_attached :logo has_one_attached :court_report_template From e801d169433d3010508b0e42e43868b42d69e3aa Mon Sep 17 00:00:00 2001 From: Aakash Nepal Date: Sun, 23 Jun 2024 23:27:02 +0545 Subject: [PATCH 15/31] feat: add active column and soft delete validations --- app/models/custom_link.rb | 2 ++ db/schema.rb | 4 +++- 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/app/models/custom_link.rb b/app/models/custom_link.rb index 6c9503c91d..319ee7e48e 100644 --- a/app/models/custom_link.rb +++ b/app/models/custom_link.rb @@ -1,5 +1,6 @@ class CustomLink < ApplicationRecord belongs_to :casa_org + validates :soft_delete, inclusion: [true, false] scope :active, -> { where(active: true, soft_delete: false) } end @@ -8,6 +9,7 @@ class CustomLink < ApplicationRecord # Table name: custom_links # # id :bigint not null, primary key +# active :boolean default(TRUE), not null # soft_delete :boolean default(FALSE), not null # text :string # url :text diff --git a/db/schema.rb b/db/schema.rb index 3393146741..f3ff54a781 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[7.1].define(version: 2024_05_13_155246) do +ActiveRecord::Schema[7.1].define(version: 2024_06_10_071054) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" @@ -304,6 +304,8 @@ t.string "text" t.text "url" t.bigint "casa_org_id", null: false + t.boolean "soft_delete", default: false, null: false + t.boolean "active", default: true, null: false t.datetime "created_at", null: false t.datetime "updated_at", null: false t.index ["casa_org_id"], name: "index_custom_links_on_casa_org_id" From 3fd889b108edb9180206833655e32a1a30b71885 Mon Sep 17 00:00:00 2001 From: Aakash Nepal Date: Sun, 23 Jun 2024 23:32:45 +0545 Subject: [PATCH 16/31] feat: remove unused methods --- app/controllers/custom_links_controller.rb | 43 ++++++++-------------- 1 file changed, 16 insertions(+), 27 deletions(-) diff --git a/app/controllers/custom_links_controller.rb b/app/controllers/custom_links_controller.rb index 5a9dcbf311..babb087384 100644 --- a/app/controllers/custom_links_controller.rb +++ b/app/controllers/custom_links_controller.rb @@ -1,16 +1,6 @@ class CustomLinksController < ApplicationController before_action :set_custom_link, only: %i[edit update soft_delete] - # GET /custom_links - def index - byebug - @custom_links = CustomLink.all - end - - # GET /custom_links/1 - def show - end - # GET /custom_links/new def new authorize CustomLink @@ -46,27 +36,26 @@ def update end end - # DELETE /contact_topics/1/soft_delete - def soft_delete - authorize @custom_link + # DELETE /contact_topics/1/soft_delete + def soft_delete + authorize @custom_link - if @custom_link.update(soft_delete: true) - redirect_to edit_casa_org_path(current_organization), notice: "Custom link was successfully removed." - else - render :show, status: :unprocessable_entity - end + if @custom_link.update(soft_delete: true) + redirect_to edit_casa_org_path(current_organization), notice: 'Custom link was successfully removed.' + else + render :show, status: :unprocessable_entity end + end private - # Use callbacks to share common setup or constraints between actions. - def set_custom_link - @custom_link = CustomLink.find(params[:id]) - end - - # Only allow a list of trusted parameters through. - def custom_link_params - params.require(:custom_link).permit(:text, :url, :active, :casa_org_id) - end + # Use callbacks to share common setup or constraints between actions. + def set_custom_link + @custom_link = CustomLink.find(params[:id]) + end + # Only allow a list of trusted parameters through. + def custom_link_params + params.require(:custom_link).permit(:text, :url, :active, :casa_org_id) + end end From c3dcacb5de76a7d744c7ce51f5c4034b5df302dc Mon Sep 17 00:00:00 2001 From: Aakash Nepal Date: Sun, 23 Jun 2024 23:33:51 +0545 Subject: [PATCH 17/31] feat: add validation for text and url --- app/models/custom_link.rb | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/app/models/custom_link.rb b/app/models/custom_link.rb index 319ee7e48e..b059b52a51 100644 --- a/app/models/custom_link.rb +++ b/app/models/custom_link.rb @@ -1,6 +1,12 @@ class CustomLink < ApplicationRecord belongs_to :casa_org validates :soft_delete, inclusion: [true, false] + # Validate that the URL is present, has a valid format, and is unique + validates :url, presence: true, + format: { with: /\A#{URI::DEFAULT_PARSER.make_regexp(%w[http https])}\z/, message: 'must be a valid URL' } + # Validate that the title is present and has a maximum length of 255 characters + validates :text, presence: true, length: { maximum: 255 } + scope :active, -> { where(active: true, soft_delete: false) } end From 136111be6166c652b072e6a61d97fa19e5c554a8 Mon Sep 17 00:00:00 2001 From: Aakash Nepal Date: Sun, 23 Jun 2024 23:47:24 +0545 Subject: [PATCH 18/31] feat: add custom link to the profile dropdown --- app/views/layouts/_header.html.erb | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/app/views/layouts/_header.html.erb b/app/views/layouts/_header.html.erb index 75ccef2185..14a2431fe5 100644 --- a/app/views/layouts/_header.html.erb +++ b/app/views/layouts/_header.html.erb @@ -72,6 +72,13 @@ <%= current_user.email %> + <% @custom_links.each do |custom_link| %> +
  • + <%= link_to custom_link.url, target: "_blank" do %> + <%= custom_link.text %> + <% end %> +
  • + <% end %>
  • <%= link_to edit_users_path do %> From 8fb37faf00e5206b7b9a6a29cea6d9644a623b69 Mon Sep 17 00:00:00 2001 From: Aakash Nepal Date: Sun, 23 Jun 2024 23:56:21 +0545 Subject: [PATCH 19/31] fix: linter issue fix --- app/views/casa_org/edit.html.erb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/views/casa_org/edit.html.erb b/app/views/casa_org/edit.html.erb index a0bbaad2c7..ed7e045989 100644 --- a/app/views/casa_org/edit.html.erb +++ b/app/views/casa_org/edit.html.erb @@ -176,4 +176,4 @@
    <%= render "custom_links" %> -
    \ No newline at end of file + From cce5956fd69e82b041d1b848181b5485d2fd3acb Mon Sep 17 00:00:00 2001 From: Aakash Nepal Date: Sun, 23 Jun 2024 23:56:44 +0545 Subject: [PATCH 20/31] feat: update factory for custom link --- spec/factories/custom_links.rb | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/spec/factories/custom_links.rb b/spec/factories/custom_links.rb index e780483cdb..557c74de47 100644 --- a/spec/factories/custom_links.rb +++ b/spec/factories/custom_links.rb @@ -1,5 +1,6 @@ FactoryBot.define do factory :custom_link do - + text { 'Example Link' } + url { 'http://example.com' } end end From 1d0d5d7692e4473b3d6d96ff2a1d647e506fac31 Mon Sep 17 00:00:00 2001 From: Aakash Nepal Date: Mon, 24 Jun 2024 00:00:11 +0545 Subject: [PATCH 21/31] fix: linters issue fixed --- app/controllers/application_controller.rb | 31 +++++++++++----------- app/controllers/custom_links_controller.rb | 6 ++--- app/models/custom_link.rb | 4 +-- spec/factories/custom_links.rb | 4 +-- 4 files changed, 22 insertions(+), 23 deletions(-) diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 83e8838ea8..a25d54f396 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -10,6 +10,7 @@ class ApplicationController < ActionController::Base before_action :set_timeout_duration before_action :set_current_organization before_action :set_active_banner + before_action :set_custom_links after_action :verify_authorized, except: :index, unless: :devise_controller? # after_action :verify_policy_scoped, only: :index @@ -42,27 +43,29 @@ def set_active_banner @active_banner = nil if @active_banner&.expired? end + def set_custom_links + @custom_links = current_organization.custom_links.active + end + protected def handle_short_url(url_list) hash_of_short_urls = {} - url_list.each_with_index { |val, index| + url_list.each_with_index do |val, index| # call short io service to shorten url # create an entry in hash if api is success short_io_service = ShortUrlService.new response = short_io_service.create_short_url(val) short_url = short_io_service.short_url hash_of_short_urls[index] = (response.code == 201 || response.code == 200) ? short_url : nil - } + end hash_of_short_urls end # volunteer/supervisor/casa_admin controller uses to send SMS # returns appropriate flash notice for SMS def deliver_sms_to(resource, body_msg) - if resource.phone_number.blank? || !resource.casa_org.twilio_enabled? - return "blank" - end + return "blank" if resource.phone_number.blank? || !resource.casa_org.twilio_enabled? body = body_msg to = resource.phone_number @@ -78,8 +81,8 @@ def deliver_sms_to(resource, body_msg) begin twilio_res = @twilio.send_sms(req_params) twilio_res.error_code.nil? ? "sent" : "error" - rescue Twilio::REST::RestError => error - @error = error + rescue Twilio::REST::RestError => e + @error = e "error" rescue # unverfied error isnt picked up by Twilio::Rest::RestError # https://www.twilio.com/docs/errors/21608 @@ -100,9 +103,9 @@ def sms_acct_creation_notice(resource_name, sms_status) end def store_referring_location - if request.referer && !request.referer.end_with?("users/sign_in") - session[:return_to] = request.referer - end + return unless request.referer && !request.referer.end_with?("users/sign_in") + + session[:return_to] = request.referer end def redirect_back_to_referer(fallback_location:) @@ -141,17 +144,13 @@ def not_authorized end def log_and_reraise(error) - unless KNOWN_ERRORS.include?(error.class) - Bugsnag.notify(error) - end + Bugsnag.notify(error) unless KNOWN_ERRORS.include?(error.class) raise end def check_unconfirmed_email_notice(user) notice = "#{user.role} was successfully updated." - if user.saved_changes.include?("unconfirmed_email") - notice += " Confirmation Email Sent." - end + notice += " Confirmation Email Sent." if user.saved_changes.include?("unconfirmed_email") notice end end diff --git a/app/controllers/custom_links_controller.rb b/app/controllers/custom_links_controller.rb index babb087384..a225ef7c04 100644 --- a/app/controllers/custom_links_controller.rb +++ b/app/controllers/custom_links_controller.rb @@ -20,7 +20,7 @@ def create @custom_link = CustomLink.new(custom_link_params) if @custom_link.save - redirect_to edit_casa_org_path(current_organization), notice: 'Custom link was successfully created.' + redirect_to edit_casa_org_path(current_organization), notice: "Custom link was successfully created." else render :new end @@ -30,7 +30,7 @@ def create def update authorize @custom_link if @custom_link.update(custom_link_params) - redirect_to edit_casa_org_path(current_organization), notice: 'Custom link was successfully updated.' + redirect_to edit_casa_org_path(current_organization), notice: "Custom link was successfully updated." else render :edit end @@ -41,7 +41,7 @@ def soft_delete authorize @custom_link if @custom_link.update(soft_delete: true) - redirect_to edit_casa_org_path(current_organization), notice: 'Custom link was successfully removed.' + redirect_to edit_casa_org_path(current_organization), notice: "Custom link was successfully removed." else render :show, status: :unprocessable_entity end diff --git a/app/models/custom_link.rb b/app/models/custom_link.rb index b059b52a51..b18ad07d55 100644 --- a/app/models/custom_link.rb +++ b/app/models/custom_link.rb @@ -3,9 +3,9 @@ class CustomLink < ApplicationRecord validates :soft_delete, inclusion: [true, false] # Validate that the URL is present, has a valid format, and is unique validates :url, presence: true, - format: { with: /\A#{URI::DEFAULT_PARSER.make_regexp(%w[http https])}\z/, message: 'must be a valid URL' } + format: {with: /\A#{URI::DEFAULT_PARSER.make_regexp(%w[http https])}\z/, message: "must be a valid URL"} # Validate that the title is present and has a maximum length of 255 characters - validates :text, presence: true, length: { maximum: 255 } + validates :text, presence: true, length: {maximum: 255} scope :active, -> { where(active: true, soft_delete: false) } end diff --git a/spec/factories/custom_links.rb b/spec/factories/custom_links.rb index 557c74de47..a76cf50e41 100644 --- a/spec/factories/custom_links.rb +++ b/spec/factories/custom_links.rb @@ -1,6 +1,6 @@ FactoryBot.define do factory :custom_link do - text { 'Example Link' } - url { 'http://example.com' } + text { "Example Link" } + url { "http://example.com" } end end From 4942caf72059447e82f7f81c3a007b2e75fa7646 Mon Sep 17 00:00:00 2001 From: Aakash Nepal Date: Mon, 24 Jun 2024 00:01:57 +0545 Subject: [PATCH 22/31] fix: fix model spec linter --- spec/models/custom_link_spec.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/spec/models/custom_link_spec.rb b/spec/models/custom_link_spec.rb index b02c93c919..73f9974535 100644 --- a/spec/models/custom_link_spec.rb +++ b/spec/models/custom_link_spec.rb @@ -1,4 +1,4 @@ -require 'rails_helper' +require "rails_helper" RSpec.describe CustomLink, type: :model do pending "add some examples to (or delete) #{__FILE__}" From f02036789dab938ebc32375e2ac6d1cbc9aa4054 Mon Sep 17 00:00:00 2001 From: Aakash Nepal Date: Mon, 24 Jun 2024 00:07:16 +0545 Subject: [PATCH 23/31] feat: add casa org association in custom link factory --- spec/factories/custom_links.rb | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/spec/factories/custom_links.rb b/spec/factories/custom_links.rb index a76cf50e41..249d86a124 100644 --- a/spec/factories/custom_links.rb +++ b/spec/factories/custom_links.rb @@ -1,6 +1,7 @@ FactoryBot.define do factory :custom_link do - text { "Example Link" } - url { "http://example.com" } + text { 'Example Link' } + url { 'http://example.com' } + association :casa_org end end From 4d2d99bc3408023ea33b201a0ce623bdc35cb4a1 Mon Sep 17 00:00:00 2001 From: Aakash Nepal Date: Mon, 24 Jun 2024 00:11:27 +0545 Subject: [PATCH 24/31] fix: linter issue fix --- spec/factories/custom_links.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/spec/factories/custom_links.rb b/spec/factories/custom_links.rb index 249d86a124..de892f0a6a 100644 --- a/spec/factories/custom_links.rb +++ b/spec/factories/custom_links.rb @@ -1,7 +1,7 @@ FactoryBot.define do factory :custom_link do - text { 'Example Link' } - url { 'http://example.com' } + text { "Example Link" } + url { "http://example.com" } association :casa_org end end From e905bb1f0f5c8f9a1709cb24a49023b8647ab9d1 Mon Sep 17 00:00:00 2001 From: Aakash Nepal Date: Sat, 29 Jun 2024 09:08:34 +0545 Subject: [PATCH 25/31] fix: return if current organization is not present --- app/controllers/application_controller.rb | 28 ++++++++++++----------- 1 file changed, 15 insertions(+), 13 deletions(-) diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index a25d54f396..f35ad4c14f 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -44,6 +44,8 @@ def set_active_banner end def set_custom_links + return unless current_organization + @custom_links = current_organization.custom_links.active end @@ -57,7 +59,7 @@ def handle_short_url(url_list) short_io_service = ShortUrlService.new response = short_io_service.create_short_url(val) short_url = short_io_service.short_url - hash_of_short_urls[index] = (response.code == 201 || response.code == 200) ? short_url : nil + hash_of_short_urls[index] = response.code == 201 || response.code == 200 ? short_url : nil end hash_of_short_urls end @@ -65,7 +67,7 @@ def handle_short_url(url_list) # volunteer/supervisor/casa_admin controller uses to send SMS # returns appropriate flash notice for SMS def deliver_sms_to(resource, body_msg) - return "blank" if resource.phone_number.blank? || !resource.casa_org.twilio_enabled? + return 'blank' if resource.phone_number.blank? || !resource.casa_org.twilio_enabled? body = body_msg to = resource.phone_number @@ -80,30 +82,30 @@ def deliver_sms_to(resource, body_msg) begin twilio_res = @twilio.send_sms(req_params) - twilio_res.error_code.nil? ? "sent" : "error" + twilio_res.error_code.nil? ? 'sent' : 'error' rescue Twilio::REST::RestError => e @error = e - "error" - rescue # unverfied error isnt picked up by Twilio::Rest::RestError + 'error' + rescue StandardError # unverfied error isnt picked up by Twilio::Rest::RestError # https://www.twilio.com/docs/errors/21608 - @error = "Phone number is unverifiied" - "error" + @error = 'Phone number is unverifiied' + 'error' end end def sms_acct_creation_notice(resource_name, sms_status) case sms_status - when "blank" + when 'blank' "New #{resource_name} created successfully." - when "error" + when 'error' "New #{resource_name} created successfully. SMS not sent. Error: #{@error}." - when "sent" + when 'sent' "New #{resource_name} created successfully. SMS has been sent!" end end def store_referring_location - return unless request.referer && !request.referer.end_with?("users/sign_in") + return unless request.referer && !request.referer.end_with?('users/sign_in') session[:return_to] = request.referer end @@ -139,7 +141,7 @@ def set_current_organization def not_authorized session[:user_return_to] = nil - flash[:notice] = "Sorry, you are not authorized to perform this action." + flash[:notice] = 'Sorry, you are not authorized to perform this action.' redirect_to(root_url) end @@ -150,7 +152,7 @@ def log_and_reraise(error) def check_unconfirmed_email_notice(user) notice = "#{user.role} was successfully updated." - notice += " Confirmation Email Sent." if user.saved_changes.include?("unconfirmed_email") + notice += ' Confirmation Email Sent.' if user.saved_changes.include?('unconfirmed_email') notice end end From dded9f93fe60f7245fac4a16da565931d787ad82 Mon Sep 17 00:00:00 2001 From: Aakash Nepal Date: Sat, 29 Jun 2024 09:09:44 +0545 Subject: [PATCH 26/31] feat: remove soft delete feature from custom link --- app/controllers/custom_links_controller.rb | 14 +++++++------- app/policies/custom_link_policy.rb | 12 ++++++------ app/views/casa_org/_custom_links.html.erb | 2 +- config/routes.rb | 3 +-- db/migrate/20240513155246_create_custom_links.rb | 1 - 5 files changed, 15 insertions(+), 17 deletions(-) diff --git a/app/controllers/custom_links_controller.rb b/app/controllers/custom_links_controller.rb index a225ef7c04..600474cc17 100644 --- a/app/controllers/custom_links_controller.rb +++ b/app/controllers/custom_links_controller.rb @@ -1,5 +1,5 @@ class CustomLinksController < ApplicationController - before_action :set_custom_link, only: %i[edit update soft_delete] + before_action :set_custom_link, only: %i[edit update destroy] # GET /custom_links/new def new @@ -20,7 +20,7 @@ def create @custom_link = CustomLink.new(custom_link_params) if @custom_link.save - redirect_to edit_casa_org_path(current_organization), notice: "Custom link was successfully created." + redirect_to edit_casa_org_path(current_organization), notice: 'Custom link was successfully created.' else render :new end @@ -30,18 +30,18 @@ def create def update authorize @custom_link if @custom_link.update(custom_link_params) - redirect_to edit_casa_org_path(current_organization), notice: "Custom link was successfully updated." + redirect_to edit_casa_org_path(current_organization), notice: 'Custom link was successfully updated.' else render :edit end end - # DELETE /contact_topics/1/soft_delete - def soft_delete + # DELETE /custom_links/1/delete + def destroy authorize @custom_link - if @custom_link.update(soft_delete: true) - redirect_to edit_casa_org_path(current_organization), notice: "Custom link was successfully removed." + if @custom_link.destroy + redirect_to edit_casa_org_path(current_organization), notice: 'Custom link was successfully removed.' else render :show, status: :unprocessable_entity end diff --git a/app/policies/custom_link_policy.rb b/app/policies/custom_link_policy.rb index 416e65094b..19e47fa54b 100644 --- a/app/policies/custom_link_policy.rb +++ b/app/policies/custom_link_policy.rb @@ -1,8 +1,8 @@ class CustomLinkPolicy < ApplicationPolicy - alias_method :create?, :is_admin_same_org? - alias_method :edit?, :is_admin_same_org? - alias_method :new?, :is_admin_same_org? - alias_method :show?, :is_admin_same_org? - alias_method :update?, :is_admin_same_org? - alias_method :soft_delete?, :is_admin_same_org? + alias create? is_admin_same_org? + alias edit? is_admin_same_org? + alias new? is_admin_same_org? + alias show? is_admin_same_org? + alias update? is_admin_same_org? + alias destroy? is_admin_same_org? end diff --git a/app/views/casa_org/_custom_links.html.erb b/app/views/casa_org/_custom_links.html.erb index f8ef91b64f..2358f21137 100644 --- a/app/views/casa_org/_custom_links.html.erb +++ b/app/views/casa_org/_custom_links.html.erb @@ -48,7 +48,7 @@ <% component.with_body(text: [ "This custom link will be deleted and will no longer be visible in profile dropdown."]) %> <% component.with_footer do %> - <%= link_to soft_delete_custom_link_path(custom_link), method: :delete, + <%= link_to custom_link_path(custom_link), method: :delete, class: "btn-sm main-btn danger-btn btn-hover ms-auto" do %> Delete Custom Link diff --git a/config/routes.rb b/config/routes.rb index 699198710c..aca291434c 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -126,8 +126,7 @@ delete "soft_delete", on: :member end - resources :custom_links, except: %i[index show delete] do - delete "soft_delete", on: :member + resources :custom_links, except: %i[index show] do end resources :followup_reports, only: :index diff --git a/db/migrate/20240513155246_create_custom_links.rb b/db/migrate/20240513155246_create_custom_links.rb index c838fdbdbf..86a10fd772 100644 --- a/db/migrate/20240513155246_create_custom_links.rb +++ b/db/migrate/20240513155246_create_custom_links.rb @@ -7,7 +7,6 @@ def change t.string :text t.text :url t.references :casa_org, null: false, foreign_key: true - t.boolean :soft_delete, null: false, default: false t.boolean :active, null: false, default: true t.timestamps end From 990f96b3824ebd2372f250e4d011365fcc6b3031 Mon Sep 17 00:00:00 2001 From: Aakash Nepal Date: Sat, 29 Jun 2024 09:11:45 +0545 Subject: [PATCH 27/31] feat: add icon for the custom link --- app/views/layouts/_header.html.erb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/views/layouts/_header.html.erb b/app/views/layouts/_header.html.erb index 14a2431fe5..47239921a2 100644 --- a/app/views/layouts/_header.html.erb +++ b/app/views/layouts/_header.html.erb @@ -75,7 +75,7 @@ <% @custom_links.each do |custom_link| %>
  • <%= link_to custom_link.url, target: "_blank" do %> - <%= custom_link.text %> + <%= custom_link.text %> <% end %>
  • <% end %> From 3f6847f4a428995f46bf4cec0cc5b261021e1023 Mon Sep 17 00:00:00 2001 From: Aakash Nepal Date: Sat, 29 Jun 2024 09:16:29 +0545 Subject: [PATCH 28/31] feat: include destroy method in custom link policy --- app/controllers/custom_links_controller.rb | 6 +++--- app/policies/custom_link_policy.rb | 12 ++++++------ 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/app/controllers/custom_links_controller.rb b/app/controllers/custom_links_controller.rb index 600474cc17..1df8e54091 100644 --- a/app/controllers/custom_links_controller.rb +++ b/app/controllers/custom_links_controller.rb @@ -20,7 +20,7 @@ def create @custom_link = CustomLink.new(custom_link_params) if @custom_link.save - redirect_to edit_casa_org_path(current_organization), notice: 'Custom link was successfully created.' + redirect_to edit_casa_org_path(current_organization), notice: "Custom link was successfully created." else render :new end @@ -30,7 +30,7 @@ def create def update authorize @custom_link if @custom_link.update(custom_link_params) - redirect_to edit_casa_org_path(current_organization), notice: 'Custom link was successfully updated.' + redirect_to edit_casa_org_path(current_organization), notice: "Custom link was successfully updated." else render :edit end @@ -41,7 +41,7 @@ def destroy authorize @custom_link if @custom_link.destroy - redirect_to edit_casa_org_path(current_organization), notice: 'Custom link was successfully removed.' + redirect_to edit_casa_org_path(current_organization), notice: "Custom link was successfully removed." else render :show, status: :unprocessable_entity end diff --git a/app/policies/custom_link_policy.rb b/app/policies/custom_link_policy.rb index 19e47fa54b..4de5cb29d8 100644 --- a/app/policies/custom_link_policy.rb +++ b/app/policies/custom_link_policy.rb @@ -1,8 +1,8 @@ class CustomLinkPolicy < ApplicationPolicy - alias create? is_admin_same_org? - alias edit? is_admin_same_org? - alias new? is_admin_same_org? - alias show? is_admin_same_org? - alias update? is_admin_same_org? - alias destroy? is_admin_same_org? + alias_method :create?, :is_admin_same_org? + alias_method :edit?, :is_admin_same_org? + alias_method :new?, :is_admin_same_org? + alias_method :show?, :is_admin_same_org? + alias_method :update?, :is_admin_same_org? + alias_method :destroy?, :is_admin_same_org? end From b8f25bff2afc1233cd822da6bd9bc08cc94d2b8e Mon Sep 17 00:00:00 2001 From: Aakash Nepal Date: Sat, 29 Jun 2024 09:23:16 +0545 Subject: [PATCH 29/31] feat: remove sof delete validation and condition --- app/models/custom_link.rb | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/app/models/custom_link.rb b/app/models/custom_link.rb index b18ad07d55..fcdc32e592 100644 --- a/app/models/custom_link.rb +++ b/app/models/custom_link.rb @@ -1,13 +1,12 @@ class CustomLink < ApplicationRecord belongs_to :casa_org - validates :soft_delete, inclusion: [true, false] # Validate that the URL is present, has a valid format, and is unique validates :url, presence: true, - format: {with: /\A#{URI::DEFAULT_PARSER.make_regexp(%w[http https])}\z/, message: "must be a valid URL"} + format: { with: /\A#{URI::DEFAULT_PARSER.make_regexp(%w[http https])}\z/, message: 'must be a valid URL' } # Validate that the title is present and has a maximum length of 255 characters - validates :text, presence: true, length: {maximum: 255} + validates :text, presence: true, length: { maximum: 255 } - scope :active, -> { where(active: true, soft_delete: false) } + scope :active, -> { where(active: true) } end # == Schema Information @@ -16,7 +15,6 @@ class CustomLink < ApplicationRecord # # id :bigint not null, primary key # active :boolean default(TRUE), not null -# soft_delete :boolean default(FALSE), not null # text :string # url :text # created_at :datetime not null From a514b3f37125c077f1261f26656940e762aff52b Mon Sep 17 00:00:00 2001 From: Aakash Nepal Date: Sat, 29 Jun 2024 20:13:21 +0545 Subject: [PATCH 30/31] fix: linter issue fix --- app/models/custom_link.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/app/models/custom_link.rb b/app/models/custom_link.rb index fcdc32e592..8f4959e6c4 100644 --- a/app/models/custom_link.rb +++ b/app/models/custom_link.rb @@ -2,9 +2,9 @@ class CustomLink < ApplicationRecord belongs_to :casa_org # Validate that the URL is present, has a valid format, and is unique validates :url, presence: true, - format: { with: /\A#{URI::DEFAULT_PARSER.make_regexp(%w[http https])}\z/, message: 'must be a valid URL' } + format: {with: /\A#{URI::DEFAULT_PARSER.make_regexp(%w[http https])}\z/, message: "must be a valid URL"} # Validate that the title is present and has a maximum length of 255 characters - validates :text, presence: true, length: { maximum: 255 } + validates :text, presence: true, length: {maximum: 255} scope :active, -> { where(active: true) } end From 6d8a7aae360e91cb95db3224f3472d7a42fa2c4e Mon Sep 17 00:00:00 2001 From: Aakash Nepal Date: Mon, 26 Aug 2024 23:45:42 +0545 Subject: [PATCH 31/31] feat: add test case fir custom link --- spec/requests/cutom_links_spec.rb | 150 ++++++++++++++++++++++++++++++ 1 file changed, 150 insertions(+) create mode 100644 spec/requests/cutom_links_spec.rb diff --git a/spec/requests/cutom_links_spec.rb b/spec/requests/cutom_links_spec.rb new file mode 100644 index 0000000000..78a258181f --- /dev/null +++ b/spec/requests/cutom_links_spec.rb @@ -0,0 +1,150 @@ +require 'rails_helper' + +RSpec.describe CustomLinksController, type: :request do + let(:user) { create(:user) } + let(:admin) { create(:casa_admin, casa_org: user.casa_org) } + let(:custom_link) { create(:custom_link, casa_org_id: user.casa_org_id) } + let(:valid_attributes) { { text: 'Link Text', url: 'http://example.com', active: true, casa_org_id: user.casa_org_id } } + let(:invalid_attributes) { { text: '', url: 'invalid', active: nil } } + + before do + sign_in user + allow_any_instance_of(CustomLinkPolicy).to receive(:new?).and_return(true) + allow_any_instance_of(CustomLinkPolicy).to receive(:edit?).and_return(true) + allow_any_instance_of(CustomLinkPolicy).to receive(:create?).and_return(true) + allow_any_instance_of(CustomLinkPolicy).to receive(:update?).and_return(true) + allow_any_instance_of(CustomLinkPolicy).to receive(:destroy?).and_return(true) + end + + describe 'GET #new' do + it 'authorizes the action' do + expect_any_instance_of(CustomLinkPolicy).to receive(:new?).and_return(true) + get new_custom_link_path + end + + it 'assigns a new CustomLink with the current user\'s casa_org_id to @custom_link' do + get new_custom_link_path + expect(assigns(:custom_link)).to be_a_new(CustomLink) + expect(assigns(:custom_link).casa_org_id).to eq(user.casa_org_id) + end + + it 'renders the new template' do + get new_custom_link_path + expect(response).to render_template(:new) + end + end + + describe 'GET #edit' do + it 'assigns the requested custom_link as @custom_link' do + get edit_custom_link_path(custom_link) + expect(assigns(:custom_link)).to eq(custom_link) + end + + it 'authorizes the action' do + expect_any_instance_of(CustomLinkPolicy).to receive(:edit?).and_return(true) + get edit_custom_link_path(custom_link) + end + end + + describe 'POST #create' do + context 'with valid parameters' do + it 'creates a new CustomLink' do + expect do + post custom_links_path, params: { custom_link: valid_attributes } + end.to change(CustomLink, :count).by(1) + end + + it 'redirects to the edit_casa_org_path' do + post custom_links_path, params: { custom_link: valid_attributes } + expect(response).to redirect_to(edit_casa_org_path(user.casa_org)) + end + + it 'sets a success notice' do + post custom_links_path, params: { custom_link: valid_attributes } + expect(flash[:notice]).to eq('Custom link was successfully created.') + end + + it 'authorizes the action' do + expect_any_instance_of(CustomLinkPolicy).to receive(:create?).and_return(true) + post custom_links_path, params: { custom_link: valid_attributes } + end + end + + context 'with invalid parameters' do + it 'does not create a new CustomLink' do + expect do + post custom_links_path, params: { custom_link: invalid_attributes } + end.to change(CustomLink, :count).by(0) + end + + it 'renders the new template' do + post custom_links_path, params: { custom_link: invalid_attributes } + expect(response).to render_template(:new) + end + end + end + + describe 'PATCH/PUT #update' do + context 'with valid parameters' do + let(:new_attributes) { { text: 'Updated Text', url: 'http://updated.com' } } + + it 'updates the requested custom_link' do + patch custom_link_path(custom_link), params: { custom_link: new_attributes } + custom_link.reload + expect(custom_link.text).to eq('Updated Text') + end + + it 'redirects to the edit_casa_org_path' do + patch custom_link_path(custom_link), params: { custom_link: new_attributes } + expect(response).to redirect_to(edit_casa_org_path(user.casa_org)) + end + + it 'sets a success notice' do + patch custom_link_path(custom_link), params: { custom_link: new_attributes } + expect(flash[:notice]).to eq('Custom link was successfully updated.') + end + + it 'authorizes the action' do + expect_any_instance_of(CustomLinkPolicy).to receive(:update?).and_return(true) + patch custom_link_path(custom_link), params: { custom_link: new_attributes } + end + end + + context 'with invalid parameters' do + it 'does not update the requested custom_link' do + patch custom_link_path(custom_link), params: { custom_link: invalid_attributes } + custom_link.reload + expect(custom_link.text).not_to be_empty + end + + it 'renders the edit template' do + patch custom_link_path(custom_link), params: { custom_link: invalid_attributes } + expect(response).to render_template(:edit) + end + end + end + + describe 'DELETE #destroy' do + it 'destroys the requested custom_link' do + custom_link + expect do + delete custom_link_path(custom_link) + end.to change(CustomLink, :count).by(-1) + end + + it 'redirects to the edit_casa_org_path' do + delete custom_link_path(custom_link) + expect(response).to redirect_to(edit_casa_org_path(user.casa_org)) + end + + it 'sets a success notice' do + delete custom_link_path(custom_link) + expect(flash[:notice]).to eq('Custom link was successfully removed.') + end + + it 'authorizes the action' do + expect_any_instance_of(CustomLinkPolicy).to receive(:destroy?).and_return(true) + delete custom_link_path(custom_link) + end + end +end