-
Notifications
You must be signed in to change notification settings - Fork 24
WAIT: Add OrganizationType base-data model backing org and registration forms #1886
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
maebeale
wants to merge
3
commits into
main
Choose a base branch
from
maebeale/organization-type-model
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,65 @@ | ||
| class OrganizationTypesController < ApplicationController | ||
| include AhoyTracking | ||
| before_action :set_organization_type, only: [ :show, :edit, :update, :destroy ] | ||
|
|
||
| def index | ||
| authorize! | ||
| per_page = params[:number_of_items_per_page].presence || 25 | ||
| base_scope = authorized_scope(OrganizationType.all) | ||
| filtered = base_scope.filter_scope(params) | ||
| @organization_types = filtered.ordered.paginate(page: params[:page], per_page: per_page) | ||
| @organization_counts = Organization.where(organization_type_id: @organization_types.map(&:id)) | ||
| .group(:organization_type_id).count | ||
|
|
||
| @count_display = filtered.count == base_scope.count ? base_scope.count : "#{filtered.count}/#{base_scope.count}" | ||
| end | ||
|
|
||
| def show | ||
| authorize! @organization_type | ||
| end | ||
|
|
||
| def new | ||
| @organization_type = OrganizationType.new | ||
| authorize! @organization_type | ||
| end | ||
|
|
||
| def edit | ||
| authorize! @organization_type | ||
| end | ||
|
|
||
| def create | ||
| @organization_type = OrganizationType.new(organization_type_params) | ||
| authorize! @organization_type | ||
|
|
||
| if @organization_type.save | ||
| redirect_to @organization_type, notice: "Organization type was successfully created." | ||
| else | ||
| render :new, status: :unprocessable_content | ||
| end | ||
| end | ||
|
|
||
| def update | ||
| authorize! @organization_type | ||
| if @organization_type.update(organization_type_params) | ||
| redirect_to @organization_type, notice: "Organization type was successfully updated.", status: :see_other | ||
| else | ||
| render :edit, status: :unprocessable_content | ||
| end | ||
| end | ||
|
|
||
| def destroy | ||
| authorize! @organization_type | ||
| @organization_type.destroy! | ||
| redirect_to organization_types_path, notice: "Organization type was successfully destroyed." | ||
| end | ||
|
|
||
| private | ||
|
|
||
| def set_organization_type | ||
| @organization_type = OrganizationType.find(params[:id]) | ||
| end | ||
|
|
||
| def organization_type_params | ||
| params.require(:organization_type).permit(:name, :published, :description) | ||
| end | ||
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| class OrganizationTypeDecorator < ApplicationDecorator | ||
| def title | ||
| name | ||
| end | ||
|
|
||
| def detail(length: nil) | ||
| "Organization type: #{name}" | ||
| end | ||
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,37 @@ | ||
| class OrganizationType < ApplicationRecord | ||
| include NameFilterable, Publishable | ||
|
|
||
| # Canonical classifications offered on the organization profile and public | ||
| # registration forms. Seeded as published records (see db/seeds.rb) and used | ||
| # as the fallback list when no published types exist yet (e.g. a fresh test | ||
| # database without seeds), so the forms always have sensible options. | ||
| DEFAULT_NAMES = [ | ||
| "501c3/nonprofit", | ||
| "For-profit", | ||
| "Government agency", | ||
| "Other" | ||
| ].freeze | ||
|
|
||
| has_many :organizations, dependent: :nullify | ||
|
|
||
| validates :name, presence: true, uniqueness: { case_sensitive: false } | ||
|
|
||
| # Names sort cleanly: digits precede letters, so "501c3/nonprofit" leads and | ||
| # "Other" trails — exactly the intended display order. | ||
| scope :ordered, -> { order(:name) } | ||
| scope :name_contains, ->(term) { term.present? ? where("name LIKE ?", "%#{sanitize_sql_like(term)}%") : all } | ||
|
|
||
| # Published names for the forms, falling back to the canonical defaults when | ||
| # nothing is seeded yet. | ||
| def self.published_names | ||
| published.ordered.pluck(:name).presence || DEFAULT_NAMES | ||
| end | ||
|
|
||
| scope :filter_scope, ->(params) do | ||
| filtered = all | ||
| filtered = filtered.name_contains(params[:name]) | ||
| filtered = filtered.published if params[:published] == "true" | ||
| filtered = filtered.where(published: false) if params[:published] == "false" | ||
| filtered | ||
| end | ||
| end | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| class OrganizationTypePolicy < ApplicationPolicy | ||
| def index? = admin? | ||
| def show? = admin? | ||
| def create? = admin? | ||
| def update? = admin? | ||
| def destroy? = record.persisted? && admin? | ||
|
|
||
| relation_scope do |relation| | ||
| next relation if admin? | ||
| relation.none | ||
| end | ||
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,45 @@ | ||
| <%= simple_form_for(@organization_type) do |f| %> | ||
| <div class="space-y-6"> | ||
|
|
||
| <!-- Errors --> | ||
| <%= render "shared/errors", resource: @organization_type if @organization_type.errors.any? %> | ||
|
|
||
| <!-- One-line Flex Fields --> | ||
| <div class="flex flex-wrap gap-6"> | ||
|
|
||
| <!-- Name --> | ||
| <div class="flex-1 min-w-[220px]"> | ||
| <%= f.input :name, | ||
| label: "Name", | ||
| input_html: { class: "form-control" } %> | ||
| </div> | ||
|
|
||
| <div class="flex items-center min-w-[150px] pt-6"> | ||
| <%= f.input :published, | ||
| as: :boolean, | ||
| wrapper_html: { class: "flex items-center gap-2" }, | ||
| input_html: { class: "form-checkbox" } %> | ||
| </div> | ||
|
|
||
| </div> | ||
|
|
||
| <!-- Description --> | ||
| <div> | ||
| <%= f.input :description, | ||
| as: :text, | ||
| label: "Description", | ||
| hint: "Optional. Shown alongside this type to clarify its meaning when the name alone isn't enough.", | ||
| input_html: { class: "form-control", rows: 2 } %> | ||
| </div> | ||
|
|
||
| <div class="action-buttons mt-8 flex justify-center gap-3"> | ||
| <% if allowed_to?(:destroy?, f.object) %> | ||
| <%= link_to "Delete", @organization_type, class: "btn btn-danger-outline", | ||
| data: { turbo_method: :delete, turbo_confirm: "Are you sure you want to delete?" } %> | ||
| <% end %> | ||
| <%= link_to "Cancel", organization_types_path, class: "btn btn-secondary-outline" %> | ||
| <%= f.button :submit, class: "btn btn-primary" %> | ||
| </div> | ||
|
|
||
| </div> | ||
| <% end %> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
| <!-- Filters --> | ||
| <div class="mb-6 p-4 bg-white border border-gray-200 rounded-lg"> | ||
| <%= form_with url: organization_types_path, | ||
| method: :get, | ||
| local: true, | ||
| class: "grid grid-cols-1 md:grid-cols-5 gap-4 items-end" do |f| %> | ||
|
|
||
| <!-- Name Search --> | ||
| <div> | ||
| <%= f.label :name, "Name contains", | ||
| class: "block text-sm font-medium text-gray-700" %> | ||
|
|
||
| <%= f.text_field :name, | ||
| value: params[:name], | ||
| class: "mt-1 block w-full rounded-md border border-gray-300 p-2", | ||
| oninput: "this.form.requestSubmit()" %> | ||
| </div> | ||
|
|
||
| <!-- Published --> | ||
| <div> | ||
| <%= f.label :published, "Published", | ||
| class: "block text-sm font-medium text-gray-700" %> | ||
|
|
||
| <%= f.select :published, | ||
| options_for_select([["All", ""], ["Published", "true"], ["Unpublished", "false"]], params[:published]), | ||
| {}, | ||
| class: "mt-1 block w-full rounded-md border border-gray-300 p-2", | ||
| onchange: "this.form.requestSubmit()" %> | ||
| </div> | ||
|
|
||
| <!-- Clear --> | ||
| <div> | ||
| <%= link_to "Clear filters", | ||
| organization_types_path, | ||
| class: "btn btn-utility-outline whitespace-nowrap", | ||
| data: { action: "collection#clearAndSubmit" } %> | ||
| </div> | ||
|
|
||
| <% end %> | ||
| </div> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| <% content_for(:page_bg_class, "admin-only bg-blue-100") %> | ||
| <div class="<%= DomainTheme.bg_class_for(:organization_types) %> border border-gray-200 rounded-xl shadow p-6"> | ||
| <div class="flex flex-wrap justify-end gap-2 mb-4"> | ||
| <%= link_to "Home", root_path, class: "text-sm text-gray-500 hover:text-gray-700 px-2 py-1" %> | ||
| <%= link_to "Organization types", organization_types_path, class: "text-sm text-gray-500 hover:text-gray-700 px-2 py-1" %> | ||
| <%= link_to "View", organization_type_path(@organization_type), class: "text-sm text-gray-500 hover:text-gray-700 px-2 py-1" %> | ||
| </div> | ||
| <h1 class="text-2xl font-semibold text-gray-900 mb-6">Edit organization type</h1> | ||
|
|
||
| <div class="space-y-6"> | ||
| <div class="mt-4"> | ||
| <%= render "form", organization_type: @organization_type %> | ||
| <%= render "shared/audit_info", resource: @organization_type %> | ||
| </div> | ||
| </div> | ||
| </div> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,99 @@ | ||
| <% content_for(:page_bg_class, "admin-only bg-blue-100") %> | ||
| <div class="<%= DomainTheme.bg_class_for(:organization_types) %> border border-gray-200 rounded-xl shadow p-6"> | ||
| <div class="space-y-6"> | ||
|
|
||
| <!-- Header --> | ||
| <div class="flex items-center justify-between mb-6"> | ||
| <h1 class="text-2xl font-semibold text-gray-900"> | ||
| Organization types (<%= @count_display %>) | ||
| </h1> | ||
| <div class="flex gap-2"> | ||
| <%= link_to "New #{OrganizationType.model_name.human.downcase}", | ||
| new_organization_type_path, | ||
| class: "admin-only bg-blue-100 btn btn-primary-outline" %> | ||
| </div> | ||
| </div> | ||
|
|
||
| <%= render "search_boxes" %> | ||
|
|
||
| <!-- Table --> | ||
| <div class="rounded-xl bg-white p-6"> | ||
| <div class="overflow-x-auto"> | ||
| <% if @organization_types.any? %> | ||
| <table class="w-full table-fixed border-collapse border border-gray-200"> | ||
| <thead class="bg-gray-100"> | ||
| <tr> | ||
| <th class="px-4 py-2 text-left text-sm font-semibold text-gray-700 w-1/3"> | ||
| Name | ||
| </th> | ||
|
|
||
| <th class="px-4 py-2 text-center text-sm font-semibold text-gray-700 w-1/6"> | ||
| Published | ||
| </th> | ||
|
|
||
| <th class="px-4 py-2 text-center text-sm font-semibold text-gray-700 w-1/6"> | ||
| Organizations | ||
| </th> | ||
|
|
||
| <th class="px-4 py-2 text-center text-sm font-semibold text-gray-700 w-[80px]"> | ||
| Actions | ||
| </th> | ||
| </tr> | ||
| </thead> | ||
|
|
||
| <tbody class="divide-y divide-gray-200"> | ||
| <% @organization_types.each do |organization_type| %> | ||
| <tr class=" <%= "bg-gray-200" unless organization_type.published? %> | ||
| <%= organization_type.published? ? "hover:bg-gray-100" : "hover:bg-gray-50" %> transition-colors duration-150"> | ||
|
|
||
| <td class="px-4 py-2 text-md text-gray-800 truncate font-bold"> | ||
| <%= organization_type.name %> | ||
| <% if organization_type.description.present? %> | ||
| <i class="fa-solid fa-circle-info ml-1 text-sm font-normal text-gray-400 hover:text-blue-600 cursor-help" | ||
| title="<%= organization_type.description %>"></i> | ||
| <% end %> | ||
| </td> | ||
|
|
||
| <td class="px-4 py-2 text-center"> | ||
| <% if organization_type.published? %> | ||
| <span class="inline-block px-2 py-1 text-xs font-semibold text-green-800 bg-green-100 rounded"> | ||
| Yes | ||
| </span> | ||
| <% else %> | ||
| <span class="inline-block px-2 py-1 text-xs font-semibold text-gray-700 bg-gray-200 rounded"> | ||
| No | ||
| </span> | ||
| <% end %> | ||
| </td> | ||
|
|
||
| <td class="px-4 py-2 text-center text-sm text-gray-700"> | ||
| <%= @organization_counts[organization_type.id].to_i %> | ||
| </td> | ||
|
|
||
| <!-- Actions --> | ||
| <td class="px-4 py-2 text-center"> | ||
| <%= link_to "Edit", | ||
| edit_organization_type_path(organization_type), | ||
| class: "btn btn-secondary-outline whitespace-nowrap" %> | ||
| </td> | ||
|
|
||
| </tr> | ||
| <% end %> | ||
| </tbody> | ||
| </table> | ||
| <% else %> | ||
| <!-- Empty state --> | ||
| <p class="text-gray-500 text-center py-6"> | ||
| No <%= OrganizationType.model_name.human.pluralize.downcase %> found. | ||
| </p> | ||
| <% end %> | ||
| </div> | ||
| </div> | ||
|
|
||
| <!-- Pagination --> | ||
| <div class="pagination flex justify-center mt-12"> | ||
| <%= tailwind_paginate @organization_types %> | ||
| </div> | ||
|
|
||
| </div> | ||
| </div> |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🤖 From Claude: Fallback to
DEFAULT_NAMESkeeps the org + registration forms populated when no types are published yet — notably fresh test DBs that do not load seeds, so the existing form-builder spec stays green without seeding.