Module: EnumExt
- Includes:
- BasicHelpers, HumanizeHelpers, SupersetHelpers
- Defined in:
- lib/enum_ext.rb,
lib/enum_ext/config.rb,
lib/enum_ext/version.rb
Overview
Let’s assume we have model Request with enum status, and we have model Order with requests like this: class Request
extend EnumExt
belongs_to :order
enum status: { in_cart: 0, waiting_for_payment: 1, payed: 2, ready_for_shipment: 3, on_delivery: 4, delivered: 5 }
end
class Order
has_many :requests
end
Defined Under Namespace
Modules: Annotated, BasicHelpers, HumanizeHelpers, SupersetHelpers Classes: EnumExtConfig, EnumWrapper
Constant Summary collapse
- VERSION =
"1.0.0"
Class Method Summary collapse
Instance Method Summary collapse
-
#enum(name = nil, values = nil, **options) ⇒ Object
extending enum with inplace settings enum status: {}, ext: [:enum_i, :mass_assign_enum, :enum_multi_scopes, enum_supersets: { }] and wrapping and replacing original enum with a wrapper object.
-
#enum_ext(enum_name, extensions) ⇒ Object
its an extension helper, on the opposite to basic enum method could be called multiple times.
Methods included from HumanizeHelpers
define_superset_humanization_helpers, #human_attribute_name, #humanize_enum, #translate_enum
Class Method Details
.config ⇒ Object
16 17 18 |
# File 'lib/enum_ext/config.rb', line 16 def config @config ||= EnumExtConfig.new end |
.configure {|config| ... } ⇒ Object
11 12 13 14 |
# File 'lib/enum_ext/config.rb', line 11 def configure yield(config) config.application_record_class&.extend( EnumExt ) if config.application_record_class.is_a?(Class) end |
Instance Method Details
#enum(name = nil, values = nil, **options) ⇒ Object
extending enum with inplace settings enum status: {}, ext: [:enum_i, :mass_assign_enum, :enum_multi_scopes, enum_supersets: { }] and wrapping and replacing original enum with a wrapper object
I’m using signature of a ActiveRecord 7 here: enum(name = nil, values = nil, **options) in earlier versions of ActiveRecord signature looks different: enum(definitions), so calling super should be different based on ActiveRecord major version
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/enum_ext.rb', line 32 def enum(name = nil, values = nil, **) single_enum_definition = name.present? extensions = [*EnumExt.config.default_helpers, *.delete(:ext)] = .dup (ActiveRecord::VERSION::MAJOR >= 7 ? super : super()).tap do |multiple_enum_definitions| if single_enum_definition replace_enum_with_wrapper(name, ) enum_ext(name, [*extensions]) else multiple_enum_definitions.each { |enum_name,| replace_enum_with_wrapper(enum_name, ) enum_ext(enum_name, [*extensions]) } end end end |
#enum_ext(enum_name, extensions) ⇒ Object
its an extension helper, on the opposite to basic enum method could be called multiple times
51 52 53 54 55 56 |
# File 'lib/enum_ext.rb', line 51 def enum_ext(enum_name, extensions) # [:enum_i, :enum_multi_scopes, enum_supersets: { valid: [:fresh, :cool], invalid: [:stale] }] # --> [:enum_i, :enum_multi_scopes, [:enum_supersets, { valid: [:fresh, :cool], invalid: [:stale] }] [*extensions].map { _1.try(:to_a)&.flatten || _1 } .each { |(ext_method, params)| send(*[ext_method, enum_name, params].compact) } end |