zmbacker / enum_help Goto Github PK
View Code? Open in Web Editor NEWHelp ActiveRecord::Enum feature to work fine with I18n and simple_form.
License: MIT License
Help ActiveRecord::Enum feature to work fine with I18n and simple_form.
License: MIT License
I have a column called reference
of type string. It is not related to enums. It works just fine when not using enum_help
gem. But by just adding enum_help
gem in Gemfile it gives The method .references() must contain arguments in this line of view:
<%= f.input :reference %>
Stack trace:
activerecord (4.1.2.rc1) lib/active_record/relation/query_methods.rb:1086:in `check_if_method_has_arguments!'
activerecord (4.1.2.rc1) lib/active_record/relation/query_methods.rb:182:in `references'
activerecord (4.1.2.rc1) lib/active_record/querying.rb:10:in `references'
enum_help (0.0.6) lib/enum_help/simple_form.rb:20:in `initialize'
simple_form (3.1.0.rc1) lib/simple_form/form_builder.rb:501:in `new'
simple_form (3.1.0.rc1) lib/simple_form/form_builder.rb:501:in `find_input'
simple_form (3.1.0.rc1) lib/simple_form/form_builder.rb:112:in `input'
app/views/products/_form.html.erb:5:in `block in _app_views_products__form_html_erb___1832036717949421423_70310092763500'
I don't want or need it. Cluttering up the html. Is there an option to not output those classes?
Is there a way to capitalize the outputted choices from an enum? For instance, I have:
<%= f.input :status, as: :radio_buttons %>
This outputs the statuses that I have declared with an enum, but they are lowercase. I just want to capitalize the first letter of each choice.
Any way to do this?
For sorting action need [["planed", 0], ["done", 1], ["loaded", 2]] with translated key.
But Model.statuses_i18n returns array with value = translated(key) and there is no integer keys to find something in database.
Maybe it's not so hard to make enums translated with integer keys?
When I have an enum option that is translated as "No", simple_form shows "false".
Am I wrong?
PS: workaround is adding a space after "No":
enums:
user:
product_already_have_km:
already_customer: 'Sì'
new_customerr: 'No '
probably 'form-control' class does bad things.
so now I using ":wrapper => :vertical_radio_and_checkboxes" option.
I hope fix it.
thanks.
Probably change is needed at lib/enum_help/simple_form.rb:73
from class_eval to creating a module and prepending it
I'm not sure what is causing this, but when this gem is added to a Rails 5 project, belongs_to associations are no longer required by default.
Current workaround is to add required: true to all belongs_to relationships where you want the Rails 5 default behavior.
Hello,
this gem works great!
One feature could be great, if it could work with delegated attribute. Eg. when is attribute with enum values delegated to another class simple_form doesn't show select box.
Rails 7 was in stable version, Pls release package a new gem version 🙏
@zmbacker
error trace
gems/enum_help-0.0.9/lib/enum_help/railtie.rb:2:in `<module:EnumHelp>': uninitialized constant EnumHelp::Rails (NameError)
I use enum in model concern because of using it in multiple models:
module Sizeable
extend ActiveSupport::Concern
included do
enum pizza_size: { d22: 22, d33: 33, d38: 38 }
end
end
How should I write translations in .yml file? I can write duplicated translations for each model used concern, but this isn't DRY
Nice gem!
Worked ok, with default simple_form, or with radio_buttons on bootstrap. But when I use :as => :string option, it won´t retrieve the I18n transation.
thanks for such a great gem.
i have a question, is there a way to overwrite the default behavior in view level? Because sometimes I just want to display the actual value of the enum field in the HTML page. But currently if I do
<%=task.status%>
In the html file, I see following
{:pending => "Pending", :confirmed => "Confirm" ...}
It will be great if I can just see its status.
Thanks
If I use <%= f.input :status %>
(as explained in the documentation) it generates a numeric input field instead of a select. To achieve the expected behavior I have to put = f.input :state, collection: Project.statuses_i18n, label_method: :last, value_method: :first
. Is this a Bug or an error in the documentation?
When I try to use this gem with simple form and try to show values as radio buttons it shows radio buttons with Yes
, No
labels (there are only two options in enum for now).
class Plan
enum payout_period: [:monthly, :at_the_end]
end
= f.input :payout_period, as: :radio_buttons
Wrapper
config.wrappers :vertical_inline_radio_and_checkboxes, tag: 'fieldset', class: 'form-group', error_class: 'has-danger',
item_wrapper_tag: :span, item_wrapper_class: "form-check-inline", item_label_class: 'form-check-label' do |b|
b.use :html5
b.optional :readonly
b.use :label, class: 'form-control-label'
b.wrapper tag: 'div', class: 'row' do |c|
c.use :input, class: 'form-check-input'
end
b.use :error, wrap_with: { tag: 'span', class: 'form-control-feedback' }
b.use :hint, wrap_with: { tag: 'span', class: 'form-text text-muted' }
end
expected result
<fieldset class="form-group radio_buttons required user_enrollment col-md-12">
<label class="form-control-label radio_buttons required"><abbr title="required">*</abbr>Your are</label>
<div class="row">
<input type="hidden" name="user[enrollment]" value="">
<span class="radio form-check-inline">
<label class="form-check-label" for="user_enrollment_student">
<input class="form-check-input radio_buttons required" type="radio" value="student" name="user[enrollment]" id="user_enrollment_student">
Student
</label>
</span>
<span class="radio form-check-inline">
<label class="form-check-label" for="user_enrollment_lecturer">
<input class="form-check-input radio_buttons required" type="radio" value="lecturer" name="user[enrollment]" id="user_enrollment_lecturer">
Lecturer
</label>
</span>
</div>
</fieldset>
result
<fieldset class="form-group radio_buttons required user_enrollment col-md-12">
<label class="form-control-label radio_buttons required"><abbr title="required">*</abbr>Your are</label>
<div class="row">
<input type="hidden" name="user[enrollment]" value="">
<span class="radio radio radio">
<label class="form-check-label" for="user_enrollment_student">
<input class="form-check-input radio_buttons required" type="radio" value="student" name="user[enrollment]" id="user_enrollment_student">
Student
</label>
</span>
<span class="radio radio radio">
<label class="form-check-label" for="user_enrollment_lecturer">
<input class="form-check-input radio_buttons required" type="radio" value="lecturer" name="user[enrollment]" id="user_enrollment_lecturer">
Lecturer
</label>
</span>
</div>
</fieldset>
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.