Skip to content
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 options for select field. #2683

Open
wants to merge 9 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,5 @@ gemfiles/.bundle/
.yardoc
/doc
*.gem
.aider.*
aider.conf.yml
23 changes: 22 additions & 1 deletion app/assets/builds/administrate/application.js
Original file line number Diff line number Diff line change
Expand Up @@ -22146,8 +22146,29 @@
// app/assets/javascripts/administrate/controllers/select_controller.js
var import_jquery2 = __toESM(require_jquery());
var select_controller_default = class extends Controller {
static values = {
selected: { Array, default: [] },
// Use an array to support multiple selected values
includeBlank: { type: Boolean, default: false },
multiple: { type: Boolean, default: false },
maxItems: { type: Number }
};
connect() {
(0, import_jquery2.default)(this.element).selectize({});
var optionsCount = null;
if (this.multipleValue) {
optionsCount = 1;
}
if (this.maxItemsValue) {
optionsCount = this.maxItemsValue;
}
const selectElement = (0, import_jquery2.default)(this.element).selectize({
allowEmptyOption: this.includeBlankValue,
maxItems: optionsCount
});
const selectizeInstance = selectElement[0].selectize;
if (this.selectedValue.length > 0) {
selectizeInstance.setValue(this.selectedValue);
}
}
};

Expand Down
4 changes: 2 additions & 2 deletions app/assets/builds/administrate/application.js.map

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,28 @@ import { Controller } from "@hotwired/stimulus";
import $ from "jquery";

export default class extends Controller {
static values = {
selected: { Array , default: [] }, // Use an array to support multiple selected values
includeBlank: { type: Boolean, default: false },
multiple: { type: Boolean, default: false },
maxItems: { type: Number }
}

connect() {
$(this.element).selectize({});
var optionsCount = null
if (this.multipleValue) { optionsCount = 1 }
if (this.maxItemsValue) { optionsCount = this.maxItemsValue }

const selectElement = $(this.element).selectize({
allowEmptyOption: this.includeBlankValue,
maxItems: optionsCount
});

const selectizeInstance = selectElement[0].selectize;

// Set the default selected value(s)
if (this.selectedValue.length > 0) {
selectizeInstance.setValue(this.selectedValue);
}
}
};
}
15 changes: 12 additions & 3 deletions app/views/fields/select/_form.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,19 @@ to be displayed on a resource's edit form page.
field.attribute,
options_for_select(
field.selectable_options,
field.data,
field.selected.presence || field.data
),
{include_blank: field.include_blank_option},
data: {controller: field.html_controller}
{
include_blank: field.include_blank_option,
multiple: field.options[:multiple],
},
data: {
controller: field.html_controller,
select_selected_value: Array.wrap(field.selected).to_json,
select_include_blank_value: field.include_blank_option,
select_multiple_value: field.options[:multiple],
select_max_items_value: field.options[:max_items]
}
)
%>
</div>
12 changes: 12 additions & 0 deletions lib/administrate/field/select.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,18 @@ def include_blank_option
options.fetch(:include_blank, false)
end

def multiple
options.fetch(:multiple, false)
end

def selected
options.fetch(:selected, nil)
end

def max_items
options.fetch(:max_items, nil)
end

def active_record_enum?
resource.class.defined_enums.key?(attribute.to_s)
end
Expand Down
200 changes: 194 additions & 6 deletions spec/administrate/views/fields/select/_edit_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,16 @@
data: false,
selectable_options: [true, false, nil],
include_blank_option: false,
html_controller: "select"
selected: nil,
html_controller: "select",
options: { multiple: false }

Check failure on line 15 in spec/administrate/views/fields/select/_edit_spec.rb

View workflow job for this annotation

GitHub Actions / standardrb

Layout/SpaceInsideHashLiteralBraces: Space inside { detected.

Check failure on line 15 in spec/administrate/views/fields/select/_edit_spec.rb

View workflow job for this annotation

GitHub Actions / standardrb

Layout/SpaceInsideHashLiteralBraces: Space inside } detected.
)

fields model: customer do |f|
render(
partial: "fields/select/form",
locals: {field: select, f: f}
)
locals: { field: select, f: f }

Check failure on line 21 in spec/administrate/views/fields/select/_edit_spec.rb

View workflow job for this annotation

GitHub Actions / standardrb

Layout/SpaceInsideHashLiteralBraces: Space inside { detected.

Check failure on line 21 in spec/administrate/views/fields/select/_edit_spec.rb

View workflow job for this annotation

GitHub Actions / standardrb

Layout/SpaceInsideHashLiteralBraces: Space inside } detected.
)

Check failure on line 22 in spec/administrate/views/fields/select/_edit_spec.rb

View workflow job for this annotation

GitHub Actions / standardrb

Layout/ClosingParenthesisIndentation: Indent `)` to column 6 (not 8)
end

expect(rendered).to have_css(
Expand All @@ -34,19 +36,205 @@
data: "Yes",
selectable_options: ["Yes", "No"],
include_blank_option: "Unknown",
html_controller: "select"
selected: nil,
html_controller: "select",
options: { multiple: false }

Check failure on line 41 in spec/administrate/views/fields/select/_edit_spec.rb

View workflow job for this annotation

GitHub Actions / standardrb

Layout/SpaceInsideHashLiteralBraces: Space inside { detected.

Check failure on line 41 in spec/administrate/views/fields/select/_edit_spec.rb

View workflow job for this annotation

GitHub Actions / standardrb

Layout/SpaceInsideHashLiteralBraces: Space inside } detected.
)

fields model: customer do |f|
render(
partial: "fields/select/form",
locals: {field: select, f: f}
)
locals: { field: select, f: f }

Check failure on line 47 in spec/administrate/views/fields/select/_edit_spec.rb

View workflow job for this annotation

GitHub Actions / standardrb

Layout/SpaceInsideHashLiteralBraces: Space inside { detected.

Check failure on line 47 in spec/administrate/views/fields/select/_edit_spec.rb

View workflow job for this annotation

GitHub Actions / standardrb

Layout/SpaceInsideHashLiteralBraces: Space inside } detected.
)

Check failure on line 48 in spec/administrate/views/fields/select/_edit_spec.rb

View workflow job for this annotation

GitHub Actions / standardrb

Layout/ClosingParenthesisIndentation: Indent `)` to column 6 (not 8)
end

expect(rendered).to have_css(
%(select[name="customer[email_subscriber]"][data-controller~="select"] option[value=""]),
text: "Unknown"
)
end

it 'uses the selected value when given' do
customer = build(:customer)
select = instance_double(
'Administrate::Field::Select',
attribute: :email_subscriber,
data: nil,
selectable_options: ['Yes', 'No'],
include_blank_option: false,
selected: 'No',
html_controller: 'select',
options: { multiple: false }
)
fields model: customer do |f|
render(
partial: 'fields/select/form',
locals: { field: select, f: f }
)
end

expect(rendered).to have_css(
%(select[name="customer[email_subscriber]"][data-controller~="select"] option[value="No"][selected="selected"]),
text: 'No'
)
end

it "uses the selected value when given" do
customer = build(:customer)
select = instance_double(
"Administrate::Field::Select",
attribute: :email_subscriber,
data: nil,
selectable_options: ["Yes", "No"],
include_blank_option: false,
selected: "No",
html_controller: "select",
options: { multiple: false }
)

fields model: customer do |f|
render(
partial: "fields/select/form",
locals: {field: select, f: f}
)
end

expect(rendered).to have_css(
%(select[name="customer[email_subscriber]"][data-controller~="select"] option[value="No"][selected="selected"]),
text: "No"
)
end

it "pre-selects the specified option" do
customer = build(:customer)
select = instance_double(
"Administrate::Field::Select",
attribute: :email_subscriber,
data: nil,
selectable_options: ["Yes", "No", "Maybe"],
include_blank_option: false,
selected: "Maybe",
html_controller: "select",
options: { multiple: false }
)

fields model: customer do |f|
render(
partial: "fields/select/form",
locals: { field: select, f: f }
)
end

expect(rendered).to have_css(
'select[name="customer[email_subscriber]"][data-controller="select"] option[value="Maybe"][selected="selected"]',
text: "Maybe"
)
end

it "sets the max_items value in data attributes" do
customer = build(:customer)
select = instance_double(
"Administrate::Field::Select",
attribute: :tags,
data: nil,
selectable_options: ["Ruby", "Rails", "JavaScript", "Python"],
include_blank_option: false,
selected: ["Ruby", "Rails"],
html_controller: "select",
options: { multiple: true, max_items: 3 }
)

fields model: customer do |f|
render(
partial: "fields/select/form",
locals: { field: select, f: f }
)
end

# Check that the select has the correct name, data attributes, and multiple attribute
expect(rendered).to have_css(
'select[name="customer[tags][]"][data-controller="select"][multiple="multiple"]'
)

# Verify data attributes
expect(rendered).to have_css(
'select[data-select-max-items-value="3"]'
)

# Check that the correct options are selected
expect(rendered).to have_css(
'select[name="customer[tags][]"] option[value="Ruby"][selected="selected"]',
text: "Ruby"
)
expect(rendered).to have_css(
'select[name="customer[tags][]"] option[value="Rails"][selected="selected"]',
text: "Rails"
)
end

it "allows multiple selections when multiple option is true" do
customer = build(:customer)
select = instance_double(
"Administrate::Field::Select",
attribute: :roles,
data: nil,
selectable_options: ["Admin", "Editor", "Viewer"],
include_blank_option: false,
selected: ["Admin", "Viewer"],
html_controller: "select",
options: { multiple: true }
)

fields model: customer do |f|
render(
partial: "fields/select/form",
locals: { field: select, f: f }
)
end

expect(rendered).to have_css(
'select[name="customer[roles][]"][data-controller="select"][multiple="multiple"]'
)

# Check multiple selected options
expect(rendered).to have_css(
'select[name="customer[roles][]"] option[value="Admin"][selected="selected"]',
text: "Admin"
)
expect(rendered).to have_css(
'select[name="customer[roles][]"] option[value="Viewer"][selected="selected"]',
text: "Viewer"
)
end

it "includes a blank option when include_blank_option is true" do
customer = build(:customer)
select = instance_double(
"Administrate::Field::Select",
attribute: :email_subscriber,
data: nil, # Ensure no pre-selection from data
selectable_options: ["Yes", "No"],
include_blank_option: "Unknown",
selected: nil,
html_controller: "select",
options: { multiple: false }
)

fields model: customer do |f|
render(
partial: "fields/select/form",
locals: { field: select, f: f }
)
end

# Check that the blank option exists with the correct value and text
expect(rendered).to have_css(
'select[name="customer[email_subscriber]"][data-controller="select"] option[value=""]',
text: "Unknown"
)

# Optionally, verify that no other option is selected if necessary
# Since Rails doesn't add 'selected="selected"' on the first option,
# the browser will select it by default.
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
class Admin::ProgrammingLanguagesController < Admin::ApplicationController

end
Loading
Loading