Skip to content

Commit

Permalink
TOP-251 add keys to custom fields and service meta
Browse files Browse the repository at this point in the history
  • Loading branch information
apricot13 committed Nov 12, 2024
1 parent 5c068d0 commit 8603281
Show file tree
Hide file tree
Showing 15 changed files with 107 additions and 10 deletions.
1 change: 1 addition & 0 deletions app/controllers/admin/custom_field_sections_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ def custom_field_section_params
custom_fields_attributes: [
:id,
:key,
:label,
:field_type,
:hint,
:options,
Expand Down
1 change: 1 addition & 0 deletions app/controllers/admin/services_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,7 @@ def service_params
],
meta_attributes: [
:id,
:label,
:key,
:value
]
Expand Down
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,
label: field.label,
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
1 change: 1 addition & 0 deletions app/controllers/services_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,7 @@ def service_params
meta_attributes: [
:id,
:key,
:label,
:value
]
)
Expand Down
13 changes: 12 additions & 1 deletion app/models/custom_field.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
class CustomField < ApplicationRecord
validates :key, presence: true, uniqueness: true
before_validation :slugify_key

validates :key, uniqueness: true, format: { with: /\A[a-z0-9\-]+\z/, message: "must be lowercase, numbers, and dashes only" }
validates :label, presence: true, uniqueness: true
validates_presence_of :field_type
belongs_to :custom_field_section, counter_cache: :custom_fields_count

Expand All @@ -12,4 +15,12 @@ def self.types
"Date"
]
end

private

def slugify_key
self.key = key.to_s.parameterize if key.present?
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
4 changes: 2 additions & 2 deletions app/models/service_meta.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
class ServiceMeta < ApplicationRecord
belongs_to :service
validates :key, presence: true
validates_uniqueness_of :key, scope: :service_id
validates :label, presence: true
validates_uniqueness_of :label, scope: :service_id
end
1 change: 1 addition & 0 deletions app/serializers/service_meta_serializer.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@

class ServiceMetaSerializer < ActiveModel::Serializer
attribute :label
attribute :key
attribute :value
end
2 changes: 1 addition & 1 deletion app/views/admin/custom_field_sections/_fields.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
<ul class="repeater__panels" aria-live="polite">
<%= f.fields_for :custom_fields do |c| %>
<li class="repeater__panel" data-custom-fields>
<%= render "admin/custom_field_sections/repeatable-fields", c: c %>
<%= render "admin/custom_field_sections/repeatable-fields", c: c, section: f.object %>
</li>
<% end %>
</ul>
Expand Down
13 changes: 11 additions & 2 deletions app/views/admin/custom_field_sections/_repeatable-fields.html.erb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<div class="field-group field-group--two-cols">
<div class="field field--required">
<%= c.label :key, "Label", class: "field__label" %>
<%= c.text_field :key, class: "field__input" %>
<%= c.label :label, "Label", class: "field__label" %>
<%= c.text_field :label, class: "field__input" %>
</div>

<div class="field field--required">
Expand All @@ -20,6 +20,15 @@
<%= c.text_area :hint, class: "field__input", rows: 1 %>
</div>

<% if @section.api_public %>
<div class="field">
<%= c.label :key, "Key", class: "field__label" %>
<p class="field__hint">This is a unique field used to refer to this field in the API</p>
<%= c.text_field :key, class: "field__input", data: { slugify: true } %>
</div>
<% 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>
7 changes: 4 additions & 3 deletions app/views/admin/services/editors/_custom-fields.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,21 @@
<% end %>

<% section.custom_fields.each do |field| %>
<% meta = s.object.meta.find_or_initialize_by(key: field.key) %>
<% meta = s.object.meta.find_or_initialize_by(label: field.label, key: field.key) %>
<%= s.fields_for :meta, meta do |c| %>
<%= c.hidden_field :label %>
<%= c.hidden_field :key %>

<% if field.field_type === "checkbox" %>
<div class="field">
<div class="checkbox">
<%= c.check_box :value, {class: "checkbox__input"}, "Yes", "No" %>
<%= c.label :value, field.key, class: "checkbox__label" %>
<%= c.label :value, field.label, class: "checkbox__label" %>
</div>
</div>
<% else %>
<div class="field">
<%= c.label :value, field.key, class: "field__label" %>
<%= c.label :value, field.label, class: "field__label" %>
<% if field.hint.present? %>
<p class="field__hint"><%= field.hint %></p>
<% end %>
Expand Down
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
14 changes: 14 additions & 0 deletions db/migrate/20241111163850_add_custom_field_label.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
class AddCustomFieldLabel < ActiveRecord::Migration[6.0]
def change
# Add new label column
add_column :custom_fields, :label, :string

# Copy data from key to label (only on up migration)
reversible do |dir|
dir.up do
CustomField.reset_column_information
CustomField.update_all('label = key')
end
end
end
end
14 changes: 14 additions & 0 deletions db/migrate/20241111181710_add_service_meta_label.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
class AddServiceMetaLabel < ActiveRecord::Migration[6.0]
def change
# Add new label column
add_column :service_meta, :label, :string

# Copy data from key to label (only on up migration)
reversible do |dir|
dir.up do
ServiceMeta.reset_column_information
ServiceMeta.update_all('label = key')
end
end
end
end
4 changes: 3 additions & 1 deletion db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema.define(version: 2024_09_18_091413) do
ActiveRecord::Schema.define(version: 2024_11_11_181710) do

# These are extensions that must be enabled in order to support this database
enable_extension "pg_trgm"
Expand Down Expand Up @@ -87,6 +87,7 @@
t.string "hint"
t.bigint "custom_field_section_id", null: false
t.string "options"
t.string "label"
t.index ["custom_field_section_id"], name: "index_custom_fields_on_custom_field_section_id"
end

Expand Down Expand Up @@ -318,6 +319,7 @@
t.string "value"
t.datetime "created_at", precision: 6, null: false
t.datetime "updated_at", precision: 6, null: false
t.string "label"
t.index ["service_id"], name: "index_service_meta_on_service_id"
end

Expand Down

0 comments on commit 8603281

Please sign in to comment.