-
Notifications
You must be signed in to change notification settings - Fork 3
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
Add support for more field types #51
Merged
Merged
Changes from 1 commit
Commits
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 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 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 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 |
---|---|---|
@@ -1,3 +1,84 @@ | ||
class FormField < ApplicationRecord | ||
belongs_to :page | ||
|
||
# Using string enum for better readability | ||
enum field_type: { | ||
string: "string", # Text input | ||
number: "number", # Numeric input | ||
boolean: "boolean", # Yes/No values | ||
single_select: "single_select", # Single choice from options | ||
multi_select: "multi_select" # Multiple choices from options | ||
} | ||
|
||
validates :title, presence: true | ||
validates :friendly_name, presence: true | ||
validates :field_type, presence: true | ||
validate :validate_select_options | ||
validate :validate_number_constraints | ||
|
||
# Virtual attribute for enum options | ||
attr_writer :enum_options_raw | ||
|
||
def enum_options_raw | ||
@enum_options_raw || enum_options&.join("\n") | ||
end | ||
|
||
before_validation :process_enum_options | ||
|
||
def to_json_schema_for_ai | ||
schema = { | ||
type: json_schema_type, | ||
description: description | ||
} | ||
|
||
case field_type | ||
when "single_select", "multi_select" | ||
schema[:enum] = enum_options if enum_options.present? | ||
when "number" | ||
schema[:minimum] = minimum if minimum.present? | ||
schema[:maximum] = maximum if maximum.present? | ||
end | ||
|
||
schema.compact | ||
end | ||
|
||
private | ||
|
||
def json_schema_type | ||
case field_type | ||
when "single_select" | ||
"string" | ||
when "multi_select" | ||
"array" | ||
else | ||
field_type | ||
end | ||
end | ||
|
||
def validate_select_options | ||
return unless ["single_select", "multi_select"].include?(field_type) | ||
Check failure on line 59 in app/models/form_field.rb
|
||
|
||
if enum_options.blank? || enum_options.any?(&:blank?) | ||
errors.add(:enum_options, "must have at least one non-empty option for select fields") | ||
end | ||
end | ||
|
||
def validate_number_constraints | ||
return unless field_type == "number" | ||
|
||
if minimum.present? && maximum.present? && maximum.to_f < minimum.to_f | ||
errors.add(:maximum, "must be greater than minimum") | ||
end | ||
end | ||
|
||
def process_enum_options | ||
return unless @enum_options_raw.present? | ||
|
||
# Split by newlines, remove empty lines and whitespace | ||
self.enum_options = @enum_options_raw | ||
.split("\n") | ||
.map(&:strip) | ||
.reject(&:blank?) | ||
.uniq | ||
end | ||
end |
This file contains 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,15 @@ | ||
<div class="field-unit__label"> | ||
<%= f.label field.attribute %> | ||
</div> | ||
<div class="field-unit__field"> | ||
<%= f.text_area "#{field.attribute}_raw", | ||
value: field.data&.join("\n"), | ||
class: "string-array-input", | ||
rows: 5, | ||
placeholder: "Enter one option per line" %> | ||
<% if field.options.key?(:hint) %> | ||
<p class="field-unit__hint"> | ||
<%= field.options[:hint] %> | ||
</p> | ||
<% end %> | ||
</div> |
This file contains 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 @@ | ||
<%= field.to_s %> |
This file contains 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,5 @@ | ||
<% field.data.map do |item| %> | ||
<div> | ||
- <%= item %> | ||
</div> | ||
<% end %> | ||
21 changes: 21 additions & 0 deletions
21
db/migrate/20241113130440_add_validation_fields_to_form_fields.rb
This file contains 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,21 @@ | ||
class AddValidationFieldsToFormFields < ActiveRecord::Migration[8.0] | ||
def change | ||
add_column :form_fields, :friendly_name, :string | ||
add_column :form_fields, :field_type, :string, null: false, default: 'string' | ||
|
||
# For number fields | ||
add_column :form_fields, :minimum, :string | ||
add_column :form_fields, :maximum, :string | ||
bodhish marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
# For select fields | ||
add_column :form_fields, :enum_options, :string, array: true, default: [] | ||
|
||
# Copy existing title values to friendly_name | ||
FormField.find_each do |field| | ||
field.update_column(:friendly_name, field.title) | ||
end | ||
bodhish marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
# Make friendly_name required after copying data | ||
change_column_null :form_fields, :friendly_name, false | ||
end | ||
end |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.
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.
Add nil check to prevent potential errors
The current implementation might raise an error if
field.data
is nil.Apply this diff to add nil protection:
This change:
present?
check<ul>
and<li>
tags for list structuremap
witheach
since we're not collecting results📝 Committable suggestion