Skip to content

Module Poly::Role

  • Extended by: ActiveSupport::Concern
  • Defined in: lib/poly/role.rb

Role semantics for a polymorphic belongs_to: a normalized, validated <association>_role column that says what part the association plays (“this is the primary address, not just an address”).

class Address < ApplicationRecord
belongs_to :resource, polymorphic: true
include Poly::Role
poly_role :resource # expects a resource_role column
end

Public Class Methods

for_role

for_role(role)

Scope: records whose role column equals role, normalized the same way.

  • @param role [String, Symbol]
  • @return [ActiveRecord::Relation]

poly_role

poly_role(assoc_name, max_length: 64, immutable: false)

Declares a role column on a polymorphic belongs_to. Roles are stripped and downcased before validation, must contain only lowercase letters, digits and underscores, and are required.

poly_role :schedulable # expects a schedulable_role column
poly_role :resource # expects a resource_role column
  • @param assoc_name [Symbol] the polymorphic association the role belongs to
  • @param max_length [Integer] maximum role length
  • @param immutable [Boolean] reject changes to the role once persisted
  • @return [void]