Skip to content

Commit

Permalink
add label and key fields to meta
Browse files Browse the repository at this point in the history
  • Loading branch information
apricot13 committed Nov 12, 2024
1 parent 77f3e1c commit 01d347a
Show file tree
Hide file tree
Showing 8 changed files with 26 additions and 7 deletions.
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
2 changes: 1 addition & 1 deletion app/controllers/api/v1/custom_fields_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ def json_tree(custom_field_sections)
custom_fields: section.custom_fields.map do |field|
field_hash = {
id: field.id,
current_reference: "#{section.name.parameterize}--#{field.key.parameterize}",
label: field.label,
key: field.key,
hint: field.hint,
field_type: field.field_type
Expand Down
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
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
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
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
3 changes: 2 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_11_11_163850) 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 @@ -319,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 01d347a

Please sign in to comment.