Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .allow_skipping_tests
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ services/create_casa_admin_service.rb
services/fdf_inputs_service.rb
validators/casa_org_validator.rb
validators/court_report_validator.rb
validators/url_validator.rb
validators/user_validator.rb
values/all_casa_admin_parameters.rb
values/casa_admin_parameters.rb
Expand Down
5 changes: 5 additions & 0 deletions app/controllers/casa_org_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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_org_links, only: %i[edit update]
before_action :require_organization!
after_action :verify_authorized
before_action :set_active_storage_url_options, only: %i[edit update]
Expand Down Expand Up @@ -90,6 +91,10 @@ def set_contact_topics
@contact_topics = @casa_org.contact_topics.where(soft_delete: false)
end

def set_custom_org_links
@custom_org_links = @casa_org.custom_org_links
end

def set_active_storage_url_options
ActiveStorage::Current.url_options = {host: request.base_url}
end
Expand Down
50 changes: 50 additions & 0 deletions app/controllers/custom_org_links_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
class CustomOrgLinksController < ApplicationController
before_action :set_custom_org_link, only: %i[edit update destroy]
after_action :verify_authorized

def new
authorize CustomOrgLink
@custom_org_link = CustomOrgLink.new
end

def create
authorize CustomOrgLink

@custom_org_link = current_organization.custom_org_links.new(custom_org_link_params)

if @custom_org_link.save
redirect_to edit_casa_org_path(current_organization), notice: "Custom link was successfully created."
else
render :new, status: :unprocessable_entity
end
end

def edit
authorize @custom_org_link
end

def update
authorize @custom_org_link
if @custom_org_link.update(custom_org_link_params)
redirect_to edit_casa_org_path(current_organization), notice: "Custom link was successfully updated."
else
render :edit, status: :unprocessable_entity
end
end

def destroy
authorize @custom_org_link
@custom_org_link.destroy
redirect_to edit_casa_org_path(current_organization), notice: "Custom link was successfully deleted."
end

private

def set_custom_org_link
@custom_org_link = CustomOrgLink.find(params[:id])
end

def custom_org_link_params
params.require(:custom_org_link).permit(:text, :url, :active)
end
end
1 change: 1 addition & 0 deletions app/models/casa_org.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ class CasaOrg < ApplicationRecord
has_many :learning_hour_topics, dependent: :destroy
has_many :case_groups, dependent: :destroy
has_many :contact_topics
has_many :custom_org_links, dependent: :destroy
has_one_attached :logo
has_one_attached :court_report_template
has_many :placement_types, dependent: :destroy
Expand Down
30 changes: 30 additions & 0 deletions app/models/custom_org_link.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
class CustomOrgLink < ApplicationRecord
TEXT_MAX_LENGTH = 30

belongs_to :casa_org
validates :text, :url, presence: true
validates :text, length: {maximum: TEXT_MAX_LENGTH}
validates :active, inclusion: {in: [true, false]}
validates :url, url: true
end

# == Schema Information
#
# Table name: custom_org_links
#
# id :bigint not null, primary key
# active :boolean default(TRUE), not null
# text :string not null
# url :string not null
# created_at :datetime not null
# updated_at :datetime not null
# casa_org_id :bigint not null
#
# Indexes
#
# index_custom_org_links_on_casa_org_id (casa_org_id)
#
# Foreign Keys
#
# fk_rails_... (casa_org_id => casa_orgs.id)
#
12 changes: 12 additions & 0 deletions app/policies/custom_org_link_policy.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
class CustomOrgLinkPolicy < ApplicationPolicy
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good policy

class Scope < ApplicationPolicy::Scope
def resolve
case user
when CasaAdmin
scope.where(casa_org: @user.casa_org)
else
scope.none
end
end
end
end
29 changes: 29 additions & 0 deletions app/validators/url_validator.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
class UrlValidator < ActiveModel::EachValidator
InvalidSchemeError = Class.new StandardError
MissingHostError = Class.new StandardError

DEFAULT_SCHEMES = %w[http https].freeze

def validate_each(record, attribute, value)
uri = URI.parse(value)
validate_scheme uri
validate_host uri
rescue URI::InvalidURIError
record.errors.add(attribute, "format is invalid")
rescue InvalidSchemeError, MissingHostError => e
record.errors.add(attribute, e.message)
end

private

def validate_scheme(uri)
accepted_schemes = Array.wrap(options[:scheme] || DEFAULT_SCHEMES)
return if uri.scheme.in? accepted_schemes

raise InvalidSchemeError, "scheme invalid - only #{accepted_schemes.join(", ")} allowed"
end

def validate_host(uri)
raise MissingHostError, "host cannot be blank" if uri.host.blank?
end
end
65 changes: 65 additions & 0 deletions app/views/casa_org/_custom_org_links.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
<div class="row">
<div class="col-lg-12">
<div class="card-style mb-30">
<div class="row align-items-center">
<div class="col-md-6">
<h3>Custom Links</h3>
</div>
<div class="col-md-6">
<div class="breadcrumb-wrapper">
<span class="ml-5">
<%= link_to new_custom_org_link_path, class: "btn-sm main-btn primary-btn btn-hover" do %>
<i class="lni lni-plus mr-10"></i> New Custom Link
<% end %>
</span>
</div>
</div>
</div>
<div class="table-wrapper table-responsive">
<table class="table striped-table">
<thead>
<tr>
<th>Display Text</th>
<th>URL</th>
<th>Active?</th>
<th>Actions</th>
</tr>
</thead>
<tbody>
<% if @custom_org_links.blank? %>
<tr>
<td colspan="4">
<span class="fw-light fst-italic text-center">No custom links have been added for this organization.</span>
</td>
</tr>
<% else %>
<% @custom_org_links.each do |custom_link| %>
<tr>
<td><%= custom_link.text %></td>
<td><%= truncate(custom_link.url, length: 90) %></td>
<td><%= custom_link.active ? "Yes" : "No" %></td>
<td>
<%= link_to edit_custom_org_link_path(custom_link) do %>
<div class="action">
<button class="text-danger">
<i class="lni lni-pencil-alt"></i> Edit
</button>
</div>
<% end %>
<%= link_to custom_org_link_path(custom_link), method: :delete do %>
<div class="action">
<button class="text-danger">
<i class="lni lni-trash-can"></i> Delete
</button>
</div>
<% end %>
</td>
</tr>
<% end %>
<% end %>
</tbody>
</table>
</div>
</div>
</div>
</div>
3 changes: 3 additions & 0 deletions app/views/casa_org/edit.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,9 @@
<div class="tables-wrapper">
<%= render "languages", languages: current_organization.languages %>
</div>
<div class="tables-wrapper">
<%= render "custom_org_links" %>
</div>
<!-- end -->
<div class="title-wrapper pt-30">
<div class="row align-items-center">
Expand Down
42 changes: 42 additions & 0 deletions app/views/custom_org_links/_form.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<div class="title-wrapper pt-30">
<div class="row align-items-center">
<div class="col-md-6">
<div class="title mb-30">
<h1>
<%= title %>
</h1>
</div>
</div>
</div>
</div><!-- ==== end title ==== -->
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: comments not needed here


<!-- ========== card start ========== -->
<div class="card-style mb-30">
<%= form_with model: @custom_org_link, local: true do |form| %>
<div class="alert-box danger-alert">
<%= render "/shared/error_messages", resource: @custom_org_link %>
</div>

<div class="input-style-1">
<%= form.label :name, "Display Text" %>
<%= form.text_field :text, class: "form-control", required: true %>
</div>

<div class="input-style-1">
<%= form.label :name, "URL" %>
<%= form.text_field :url, class: "form-control", required: true %>
</div>

<div class="form-check checkbox-style mb-20">
<%= form.check_box :active, as: :boolean, class: 'form-check-input' %>
<%= form.label :active, "Active?", class: 'form-check-label' %>
</div>

<div class="actions mb-10">
<%= button_tag type: "submit", class: "btn-sm main-btn primary-btn btn-hover" do %>
<i class="lni lni-checkmark-circle mr-5"></i> <%= action %>
<% end %>
</div>
<% end %>
</div>
<!-- card end -->
1 change: 1 addition & 0 deletions app/views/custom_org_links/edit.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<%= render partial: "form", locals: { title: "Edit Custom Link", action: 'Update' } %>
1 change: 1 addition & 0 deletions app/views/custom_org_links/new.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<%= render partial: "form", locals: { title: "New Custom Link", action: 'Create' } %>
6 changes: 6 additions & 0 deletions app/views/layouts/_sidebar.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,12 @@
{ title: "Case Contact Topics", icon: "comments", path: edit_casa_org_path(current_organization, anchor: 'case-contact-topics'), render_check: policy(:application).modify_organization? }
]) %>
<% end %>
<% if current_organization.custom_org_links.any?(&:active) %>
<hr>
<% current_organization.custom_org_links.select(&:active).each do |custom_link| %>
<%= render(Sidebar::LinkComponent.new(title: custom_link.text, icon: "link", path: custom_link.url)) %>
<% end %>
<% end %>
</ul>
</nav>
</nav>
Expand Down
1 change: 1 addition & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,7 @@
resources :languages, only: %i[new create edit update] do
delete :remove_from_volunteer
end
resources :custom_org_links, only: %i[new create edit update destroy]

direct :help_admins_supervisors do
"https://thunder-flower-8c2.notion.site/Casa-Volunteer-Tracking-App-HelpSite-3b95705e80c742ffa729ccce7beeabfa"
Expand Down
11 changes: 11 additions & 0 deletions db/migrate/20250404200715_create_custom_org_links.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
class CreateCustomOrgLinks < ActiveRecord::Migration[7.2]
def change
create_table :custom_org_links do |t|
t.references :casa_org, null: false, foreign_key: true
t.string :text, null: false
t.string :url, null: false
t.boolean :active, null: false, default: true
t.timestamps
end
end
end
11 changes: 11 additions & 0 deletions db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,16 @@
t.index ["casa_case_id"], name: "index_court_dates_on_casa_case_id"
end

create_table "custom_org_links", force: :cascade do |t|
t.bigint "casa_org_id", null: false
t.string "text", null: false
t.string "url", 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_org_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
Expand Down Expand Up @@ -682,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_org_links", "casa_orgs"
add_foreign_key "emancipation_options", "emancipation_categories"
add_foreign_key "followups", "users", column: "creator_id"
add_foreign_key "judges", "casa_orgs"
Expand Down
8 changes: 8 additions & 0 deletions spec/factories/custom_org_links.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
FactoryBot.define do
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good factory :)

factory :custom_org_link do
casa_org
text { "Custom Link Text" }
url { "https://custom.link" }
active { true }
end
end
1 change: 1 addition & 0 deletions spec/models/casa_org_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
it { is_expected.to have_one_attached(:logo) }
it { is_expected.to have_one_attached(:court_report_template) }
it { is_expected.to have_many(:contact_topics) }
it { is_expected.to have_many(:custom_org_links).dependent(:destroy) }

it "has unique name" do
org = create(:casa_org)
Expand Down
18 changes: 18 additions & 0 deletions spec/models/custom_org_link_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
require "rails_helper"

RSpec.describe CustomOrgLink, type: :model do
it { is_expected.to belong_to :casa_org }
it { is_expected.to validate_presence_of :text }
it { is_expected.to validate_presence_of :url }
it { is_expected.to validate_length_of(:text).is_at_most described_class::TEXT_MAX_LENGTH }
it { is_expected.to validate_inclusion_of(:active).in_array [true, false] }

describe "url validation - only allow http or https schemes" do
it { is_expected.to allow_value("http://example.com").for(:url) }
it { is_expected.to allow_value("https://example.com").for(:url) }

it { is_expected.not_to allow_value("ftp://example.com").for(:url) }
it { is_expected.not_to allow_value("example.com").for(:url) }
it { is_expected.not_to allow_value("some arbitrary string").for(:url) }
end
end
21 changes: 21 additions & 0 deletions spec/policies/custom_org_link_policy_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
require "rails_helper"

RSpec.describe CustomOrgLinkPolicy, type: :policy do
let(:casa_admin) { build_stubbed(:casa_admin) }
let(:supervisor) { build_stubbed(:supervisor) }
let(:volunteer) { build_stubbed(:volunteer) }

permissions :new?, :create?, :edit?, :update? do
it "permits casa_admins" do
expect(described_class).to permit(casa_admin)
end

it "does not permit supervisor" do
expect(described_class).not_to permit(supervisor)
end

it "does not permit volunteer" do
expect(described_class).not_to permit(volunteer)
end
end
end
Loading
Loading