Add option to filter select values - #18
superkat64 wants to merge 6 commits into
Conversation
Include evaluator to filter options used in enum select in form view
Allow later versions of rails
* 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>
There was a problem hiding this comment.
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 whenoption_filteris omitted. - CHANGELOG 0.0.9 documents the Rails constraint change, not
option_filter. README is also silent on the new option. include_blank: falsewas already in the PR diff context, so it was not treated as a regression from this change.- Considered flagging the local
optionsparameter name.option_filterstill calls the Fieldoptionsmethod, 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 |
There was a problem hiding this comment.
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] |
There was a problem hiding this comment.
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'] |
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
This job uses ubuntu-latest. GitHub Actions in this environment should run on the custom runners X64 or ARM64, not ubuntu-latest.
There was a problem hiding this comment.
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) andfiltered_optionsto 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 |
| ## [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' |
| def filtered_options(options) | ||
| options.select(&option_filter).map do |k, v| |
Why?
We had the need to restrict certain enum values from the dropdown select form
Changes
option_filterwhich returns either a no-op filter proc by default or a filter proc that evaluates and filters the provided options and &filtered_optionsto return the new select options arrayform.erbto usefiltered_options