8000 Add native support for ActiveRecord Enums · Issue #3157 · activeadmin/activeadmin · GitHub
[go: up one dir, main page]

Skip to content

Add native support for ActiveRecord Enums #3157

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

Closed
AlexeyMK opened this issue May 22, 2014 · 19 comments
Closed

Add native support for ActiveRecord Enums #3157

AlexeyMK opened this issue May 22, 2014 · 19 comments

Comments

@AlexeyMK
Copy link
Contributor

Rails 4.1 now has support for enums.

It seems pretty straightforward to default enum columns to a select in the edit form window: http://stackoverflow.com/questions/23414880/how-to-properly-configure-rails-4-1-enums-in-activeadmin/23799562#23799562

Would be pretty awesome to support this natively, without requiring additional customization.

@timoschilling
Copy link
Member

I can take a look on this.

@seanlinsley seanlinsley changed the title Add native support for ActiveRails Enums Add native support for ActiveRecord Enums May 23, 2014
@timoschilling
Copy link
Member

@AlexeyMK I have take a look on that and i think this should be done by Formtastic and not by AA

@AlexeyMK
Copy link
Contributor Author
AlexeyMK commented Jul 9, 2014

makes sense.

@seanlinsley
Copy link
Contributor

If someone can get a PR merged on Formtastic's side, sure, but Active Admin has plenty of custom form code.

@timoschilling
Copy link
Member

Formtastic has that on his feature list (formtastic/formtastic#1059)

@aldavigdis
Copy link
Contributor

@justinfrench has implemented this already according to formtastic/formtastic#1098. Any news about the current status as of now or a workaround that works for edit forms?

8000

@timoschilling
Copy link
Member

Here are no news. Maybe some want to work on this?

@thom-nic
Copy link
thom-nic commented Feb 5, 2015

For the record for other folks coming across this issue, the workaround is to explicitly specify the input for that column as a select with the correct values like so:

  form do |f|
    f.semantic_errors *f.object.errors.keys
    f.inputs do
      f.input :status, as: :select, collection: MyModel.statuses.keys
      # other attribute fields...
    end
    f.actions
  end

...where status is the enum column and statuses is a hash that ActiveRecord automatically generates on the model from the enum values.

@XBeg9
Copy link
XBeg9 commented Feb 24, 2015

@thom-nic Do you know how to filter by enums fields? Thanks

@besi
Copy link
besi commented Aug 6, 2015

👍

@XBeg9: I found this here http://stackoverflow.com/a/25437896/784318:

filter :level, as: :select, collection: Model.levels

@iangreenleaf
Copy link
Contributor
filter :level, as: :select, collection: Model.levels

This solution makes the filtering functionality work, but the "Current filters" section displays the integer values, like "Current filters: Status equals 2". And unfortunately using the keys as suggested for the other solutions won't work here, because ActiveRecord::Enum doesn't support Model.where(status: "foobar").

So, one more small thing that it would be great to have AA do automatically if possible.

@gerryhd
Copy link
gerryhd commented Feb 8, 2018

This solution makes the filtering functionality work, but the "Current filters" section displays the integer values, like "Current filters: Status equals 2"

I somewhat looked into a way to implement enum keys into the filter message. It seems that a method defined_enums exists for models. I'm thinking maybe this can be evaluated in the values method in active_filter.rb to then retrieve the values if the key is found in the defined_enums hash.

@sergey-alekseev
Copy link
Contributor
sergey-alekseev commented May 21, 2021

Just FYI the filter problem is still actual in 2021.

@jcasier
Copy link
jcasier commented Oct 26, 2021

For the record for other folks coming across this issue, the workaround is to explicitly specify the input for that column as a select with the correct values like so:

  form do |f|
    f.semantic_errors *f.object.errors.keys
    f.inputs do
      f.input :status, as: :select, collection: MyModel.statuses.keys
      # other attribute fields...
    end
    f.actions
  end

...where status is the enum column and statuses is a hash that ActiveRecord automatically generates on the model from the enum values.

I needed MyModel.statuses.symbolize_keys to get this to work

@sebfie
Copy link
sebfie commented Jan 20, 2022

Right, the issue is still here :
CleanShot 2022-01-20 at 16 29 46

@harunkumars
Copy link

I somewhat looked into a way to implement enum keys into the filter message. It seems that a method defined_enums exists for models. I'm thinking maybe this can be evaluated in the values method in active_filter.rb to then retrieve the values if the key is found in the defined_enums hash.

This PR (#7790) codifies the comment above from @gerryhd

@javierjulio
Copy link
Member

We have a PR for this. Thank you.

@npgretz
Copy link
npgretz commented Mar 6, 2024

This is still very much a desired feature for me. I inherited an application where a combination of Enumerize and enum are used with ActiveAdmin filter and input forms.

For ActiveRecord models with:

enum status: { 'completed': 0, 'pending': 1}

I cannot use:

filter :status, as: :select, multiple: true ...

and the int is shown in the view of active filters. Alternatively, some of the models have varchar (int was used for models with enum) for their status column type in MySQL and use:

enumerize :status, in: {to_be_scheduled:0, scheduled:1, completed:2, incomplete:4, left_message:5, unreachable:6}, scope: :statuses

For these models, I can use:

filter :status, as: :select, multiple: true ...

and in the view of active filters I will see "Status in scheduled", instead of "Status in 1" that the models using enum show. This was all very confusing to me when I inherited this application and I did not find any help in documentation or online.This may be a helpful work around until the PR is merged.

@javierjulio
Copy link
Member

@npgretz hmm I don't know if it would work with a multi select. For enums, I use check boxes and that I know does work.

filter :status, as: :check_boxes, collection: -> { MyModel.statuses }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

0