-
Notifications
You must be signed in to change notification settings - Fork 330
Enum Columns
You can use columns like enum with ActiveScaffold.
In create and update actions this will generate a select tag and into list, this will show the translated value. You can set different strings for value and label, for example storing m
or f
and showing male
and female
in the form and list. ActiveScaffold will translate only symbols, so text to show must be a symbol if you want ActiveScaffold to translate it.
config/locales/en.yml
en:
activerecord:
attributes:
person:
m: 'Male'
f: 'Female'
# app/models/person.rb
class Person < ActiveRecord::Base
SEX = %w(m f)
validates_inclusion_of :sex, :in => SEX
end
# app/controllers/people.rb
class PeopleController < ApplicationController
active_scaffold do |config|
config.columns[:sex].form_ui = :select
config.columns[:sex].options = {:options => Person::SEX.map(&:to_sym)}
end
end
Setting options in config block works for static options. If different options must be used in different requests, because they depend on signed in user or other record’s columns, active_scaffold_enum_options helper may be overrided:
# app/helpers/people_helper.rb
module PeopleHelper
def active_scaffold_enum_options(column, record)
if column == :sex
# return array of value, text arrays, or array of symbols
else
super
end
end
end
It can be defined with model name prefix, so it’s called only with columns for that model. In that case, the method should call the original AS helper instead of super in the else clause.
This is useful when clear_helpers
is not called in ApplicationController
, as the same method may be defined in many helper modules, and when some models have associations with the same name, they may need different conditions. Also, it prevents of calling many methods in the different helper modules.
module PeopleHelper
def person_active_scaffold_enum_options(column, record, ui_options: ui_options)
if column == :sex
# return array of value, text arrays, or array of symbols
else
active_scaffold_enum_options(column, record, ui_options: ui_options)
end
end
end
If the association is defined in a STI model, prefixing with the base class is supported too, so subclasses can use a helper override prefixed with own class name, which is specific for the subclass, or share a helper override with the base class name prefix. For example:
class User < ApplicationRecord
belongs_to :role
end
class Admin < User
end
class Member < User
end
Admin will look for admin_active_scaffold_enum_options
and user_active_scaffold_enum_options
, and Member will look for member_active_scaffold_enum_options
and user_active_scaffold_enum_options
, so user_active_scaffold_enum_options
can be defined if the same options must be used in both models. This is more useful when clear_helpers
is not called in ApplicationController
, or when the method is defined in ApplicationHelper
or any other shared Helper module.