From 3ecb08c2a1a20273d2d5556d97d4c1a1408061f7 Mon Sep 17 00:00:00 2001 From: Han Date: Mon, 11 Nov 2024 15:25:12 +0000 Subject: [PATCH] add api end point to show custom fields exposed in the API --- .../api/v1/custom_fields_controller.rb | 39 +++++++++++++++++++ app/models/custom_field_section.rb | 2 + .../_repeatable-fields.html.erb | 4 ++ config/routes.rb | 1 + 4 files changed, 46 insertions(+) create mode 100644 app/controllers/api/v1/custom_fields_controller.rb diff --git a/app/controllers/api/v1/custom_fields_controller.rb b/app/controllers/api/v1/custom_fields_controller.rb new file mode 100644 index 00000000..cef1a259 --- /dev/null +++ b/app/controllers/api/v1/custom_fields_controller.rb @@ -0,0 +1,39 @@ +class API::V1::CustomFieldsController < ApplicationController + skip_before_action :authenticate_user! + + def index + render json: json_tree(CustomFieldSection.api_public.includes(:custom_fields)).to_json + end + + private + + def json_tree(custom_field_sections) + custom_field_sections.map do |section| + { + id: section.id, + name: section.name, + hint: section.hint, + custom_fields: section.custom_fields.map do |field| + field_hash = { + id: field.id, + current_reference: "#{section.name.parameterize}--#{field.key.parameterize}", + key: field.key, + hint: field.hint, + field_type: field.field_type + } + field_hash[:options] = process_options(field.options) if field.field_type == 'select' + field_hash + end + } + end + end + + def process_options(options_string) + options_string.split(',').map.with_index(1) do |option, index| + { + value: option.strip, + # key: option.strip.parameterize + } + end + end + end \ No newline at end of file diff --git a/app/models/custom_field_section.rb b/app/models/custom_field_section.rb index 00b70f15..ec626193 100644 --- a/app/models/custom_field_section.rb +++ b/app/models/custom_field_section.rb @@ -7,4 +7,6 @@ class CustomFieldSection < ApplicationRecord default_scope { order(sort_order: :asc) } scope :visible_to, -> (current_user){ current_user.admin ? all : where(public: true) } + + scope :api_public, -> { where(api_public: true) } end diff --git a/app/views/admin/custom_field_sections/_repeatable-fields.html.erb b/app/views/admin/custom_field_sections/_repeatable-fields.html.erb index 0c413a81..676026b2 100644 --- a/app/views/admin/custom_field_sections/_repeatable-fields.html.erb +++ b/app/views/admin/custom_field_sections/_repeatable-fields.html.erb @@ -20,6 +20,10 @@ <%= c.text_area :hint, class: "field__input", rows: 1 %> +<% if c.object.custom_field_section&.api_public %> +

Current reference key: <%= c.object.custom_field_section&.name&.parameterize %>--<%= c.object.key&.parameterize %>

+<% end %> + <%= c.hidden_field :_destroy, data: {destroy_field: true} %> \ No newline at end of file diff --git a/config/routes.rb b/config/routes.rb index c8bbf451..c50a37ef 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -82,6 +82,7 @@ resources :accessibilities, only: [:index] get "me", to: "me#show" resources :services, only: [:index, :show] + resources :custom_fields, only: [:index] end end