Skip to content

Add option to filter select values - #18

Open
superkat64 wants to merge 6 commits into
valiot:masterfrom
customink:master
Open

superkat64 wants to merge 6 commits into
valiot:masterfrom
customink:master

Conversation

@superkat64

Copy link
Copy Markdown

Why?

We had the need to restrict certain enum values from the dropdown select form

Changes

  • Added option_filter which returns either a no-op filter proc by default or a filter proc that evaluates and filters the provided options and & filtered_options to return the new select options array
  • Updated form.erb to use filtered_options

Kate and others added 6 commits June 11, 2021 08:02
Include evaluator to filter options used in enum select in form view
* Update gemspec with updated author & email info

* Add CODEOWNERS file under /.github

* Add workflow for managing stale Github branches

* Add catalog.yaml for Ops Level purposes
Co-authored-by: Claude Opus 5 (1M context) <noreply@anthropic.com>
Copilot AI balanced review requested due to automatic review settings September 2, 2026 11:32

@palantir-valiot palantir-valiot Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Overall Assessment

This PR adds an option_filter option and moves enum select-option building into filtered_options. The form wiring is straightforward, but the new specs do not run as written, and filtered_options will raise if Administrate does not set resource on the field.

Findings

# Severity File Issue
1 High spec/lib/administrate/field/enum_spec.rb Default option_filter spec calls field.evaluator, which does not exist, so the example raises.
2 Medium lib/administrate/field/enum.rb filtered_options uses resource.class for I18n. A nil resource raises NoMethodError on the form.
3 Low spec/lib/administrate/field/enum_spec.rb #filtered_options is tested with an Array, not an enum Hash, and never sets resource.
4 Low .github/workflows/stale.yml The workflow uses ubuntu-latest instead of the X64 / ARM64 runners.

Notes

  • The default Proc.new { true } is a reasonable no-op when option_filter is omitted.
  • CHANGELOG 0.0.9 documents the Rails constraint change, not option_filter. README is also silent on the new option.
  • include_blank: false was already in the PR diff context, so it was not treated as a regression from this change.
  • Considered flagging the local options parameter name. option_filter still calls the Field options method, so it is not a shadowing bug.

🤖 PR Reviewer · Grok 4.6 (xAI)

page = :form
field = Administrate::Field::Enum.new(:status, 'status', page)

expect(field.evaluator.call).to be_truthy

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This example raises. Administrate::Field::Enum has no evaluator method.

The default path is option_filter. Use that, then call the proc, e.g. expect(field.option_filter.call).to be_truthy.

Until this is fixed, the new #option_filter group does not cover the no-op default.


def filtered_options(options)
options.select(&option_filter).map do |k, v|
[I18n.t("activerecord.attributes.#{resource.class.name.underscore}.#{attribute.to_s.pluralize}.#{k}", default: k.humanize), k]

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This line interpolates resource.class.name before I18n.t runs. If resource is nil, the form raises NoMethodError instead of rendering the select.

That happens whenever Administrate does not pass resource: into the field (older Administrate, and this gem's own specs, which call .new(:status, 'status', page) with no resource).

The previous template used f.object.class, which is always present on the form. Pass the model class in from _form.html.erb, or fall back when resource is nil, so the I18n key does not depend on that optional field state.

describe '#filtered_options' do
it 'returns array of filtered arrays' do
page = :form
options = ['I', 'Am', 'Groot']

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The form passes an enum Hash (Model.public_send(attribute.to_s.pluralize)), not an Array of strings. Hash#select yields (key, value); this example never hits that shape.

It also never sets resource, so filtered_options will raise on resource.class before the expectation runs. Use a Hash like { "I" => 0, "Am" => 1, "Groot" => 2 } and a field with resource: set to a model instance (or stub resource).


jobs:
close-issues:
runs-on: ubuntu-latest

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This job uses ubuntu-latest. GitHub Actions in this environment should run on the custom runners X64 or ARM64, not ubuntu-latest.

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Warning

Copilot couldn't run its full agentic review because it didn't start before the timeout. Make sure your repository has a runner available, or add a copilot-code-review.yml file specifying one with the runs-on attribute. See the docs for more details.

Pull request overview

Adds an option_filter hook to Administrate::Field::Enum so enum select dropdowns can exclude specific values, and refactors the enum form partial to use the filtered option list.

Changes:

  • Introduced option_filter (default no-op) and filtered_options to filter enum options.
  • Updated the enum form partial to render select options via filtered_options.
  • Bumped gem version / loosened Rails dependency constraint; added repo metadata files (changelog, workflow, CODEOWNERS, catalog).

Reviewed changes

Copilot reviewed 8 out of 8 changed files in this pull request and generated 5 comments.

Show a summary per file
File Description
spec/lib/administrate/field/enum_spec.rb Adds specs covering option_filter + filtered_options.
lib/administrate/field/enum.rb Implements filtering and option mapping for enum selects.
app/views/fields/enum/_form.html.erb Switches select rendering to use field.filtered_options(...).
administrate-field-enum.gemspec Bumps version and loosens Rails dependency requirement.
CHANGELOG.md Introduces changelog entry for 0.0.9.
.github/workflows/stale.yml Adds automation to mark/close stale PRs.
.github/CODEOWNERS Adds code ownership metadata.
.customink/catalog.yaml Adds internal service catalog metadata.

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

end

def filtered_options(options)
options.select(&option_filter).map do |k, v|
page = :form
field = Administrate::Field::Enum.new(:status, 'status', page)

expect(field.evaluator.call).to be_truthy
Comment thread CHANGELOG.md
Comment on lines +8 to +10
## [0.0.9] - 2021/12/29
### Changed
- allow gem installation on Rails 4.2 and higher No newline at end of file

s.add_dependency 'administrate'
s.add_dependency 'rails', '>= 4.2', '<= 6.0'
s.add_dependency 'rails', '>= 4.2'
Comment on lines +19 to +20
def filtered_options(options)
options.select(&option_filter).map do |k, v|
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants