You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
@albertosaurus
I'm currently using short scopes and predicates defined as ActiveSupport::Concern.
If I make a PR would you be interested in adding such functionality?
I'm asking because it'll take me a day to port this code and add specs.
It allows the following syntax:
has_enumerated:status,short_scopes: true,predicates: true# existing => newbooking.with_status(:active)=>booking.activebooking.all_except(:active)=>booking.not_activebooking.status === :rejected=>booking.rejected?
# Extensions for PowerEnummodulePowerEnumEnhancementsextendActiveSupport::Concern# TODO: Consider making a PR to PowerEnummoduleClassMethods# Dynamically defines predicate methods from enum names# @param [Symbol] relation_name# @return [Nil]privatedefdefine_enum_predicates(relation_name)reflection=find_reflection_by_name!(relation_name)ifenum_table_exists?(reflection)reflection.klass.names.eachdo |name|
define_method("#{name}?")dopublic_send(reflection.name).like?(name)endendendend# Dynamically defines scopes from enum names# @param [Symbol] relation_name# @return [Nil]privatedefdefine_enum_scopes(relation_name)reflection=find_reflection_by_name!(relation_name)ifenum_table_exists?(reflection)reflection.klass.names.eachdo |name|
scopename,->{public_send("with_#{reflection.name}",name)}scope"not_#{name}",->{public_send("exclude_#{reflection.name}",name)}endendend# Finds a reflection by name# @param [Symbol] relation_name# @raise [ArgumentError]# @return [PowerEnum::Reflection::EnumerationReflection]privatedeffind_reflection_by_name!(relation_name)reflection=reflect_on_all_associations(:belongs_to).find{ |relation| relation.name == relation_name.to_sym}ifreflection&.class != PowerEnum::Reflection::EnumerationReflectionraiseArgumentError,"Relation #{relation_name} doesn't exist"endreflectionend# Checks if there's a DB connection and if given table exists# @param [Symbol] relation_name# @return [Boolean]privatedefenum_table_exists?(reflection)connection.table_exists?(reflection.plural_name)rescueActiveRecord::NoDatabaseError,PG::ConnectionBadfalseendendend
The text was updated successfully, but these errors were encountered:
First of all, sorry about the late reply. Otherwise, my big concern with what you're doing is that I think you're forcing enum values to be read at load time.
@albertosaurus
I'm currently using short scopes and predicates defined as ActiveSupport::Concern.
If I make a PR would you be interested in adding such functionality?
I'm asking because it'll take me a day to port this code and add specs.
It allows the following syntax:
The text was updated successfully, but these errors were encountered: