Skip to content

Commit

Permalink
add api end point to show custom fields exposed in the API
Browse files Browse the repository at this point in the history
  • Loading branch information
apricot13 committed Nov 11, 2024
1 parent 9e13a41 commit 3ecb08c
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 0 deletions.
39 changes: 39 additions & 0 deletions app/controllers/api/v1/custom_fields_controller.rb
Original file line number Diff line number Diff line change
@@ -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
2 changes: 2 additions & 0 deletions app/models/custom_field_section.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@
<%= c.text_area :hint, class: "field__input", rows: 1 %>
</div>

<% if c.object.custom_field_section&.api_public %>
<p>Current reference key: <em><%= c.object.custom_field_section&.name&.parameterize %>--<%= c.object.key&.parameterize %></em></p>
<% end %>

<%= c.hidden_field :_destroy, data: {destroy_field: true} %>

<button type="button" class="repeater__closer" data-close="true" title="Remove this field">Remove this field</button>
1 change: 1 addition & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down

0 comments on commit 3ecb08c

Please sign in to comment.