module SmartEnum::Associations

Public Class Methods

__assert_enum(klass) click to toggle source
# File lib/smart_enum/associations.rb, line 88
def self.__assert_enum(klass)
  unless klass <= SmartEnum
    fail "enum associations can only associate to classes which descend from SmartEnum. #{klass} does not."
  end
end

Public Instance Methods

belongs_to_enum(association_name, class_name: nil, foreign_key: nil, when_nil: nil) click to toggle source
# File lib/smart_enum/associations.rb, line 60
def belongs_to_enum(association_name, class_name: nil, foreign_key: nil, when_nil: nil)
  association_name = association_name.to_sym
  association = Association.new(self, association_name, class_name: class_name, foreign_key: foreign_key)
  enum_associations[association_name] = association

  define_method(association_name) do
    id_to_find = self.public_send(association.foreign_key) || when_nil
    association.association_class[id_to_find]
  end

  fk_writer_name = "#{association.foreign_key}=".to_sym

  generate_writer = instance_methods.include?(fk_writer_name) || (
    # ActiveRecord may not have generated the FK writer method yet.
    # We'll assume that it will get a writer if it has a column with the same name.
    defined?(ActiveRecord::Base) &&
    self <= ActiveRecord::Base &&
    self.respond_to?(:column_names) &&
    self.column_names.include?(association.foreign_key.to_s)
  )

  if generate_writer
    define_method("#{association_name}=") do |value|
      self.public_send(fk_writer_name, value&.id)
    end
  end
end
enum_associations() click to toggle source
# File lib/smart_enum/associations.rb, line 94
def enum_associations
  @enum_associations ||= {}
end
has_many_enums(association_name, class_name: nil, as: nil, foreign_key: nil, through: nil, source: nil) click to toggle source
# File lib/smart_enum/associations.rb, line 6
def has_many_enums(association_name, class_name: nil, as: nil, foreign_key: nil, through: nil, source: nil)
  association_name = association_name.to_sym
  if through
    return has_many_enums_through(association_name, through, source: source)
  end

  association = HasAssociation.new(self, association_name, class_name: class_name, as: as, foreign_key: foreign_key)
  enum_associations[association_name] = association

  define_method(association.generated_method_name) do
    association.association_class.values.select{|instance|
      instance.attributes[association.foreign_key] == self.id
    }
  end
end
has_many_enums_through(association_name, through_association, source: nil) click to toggle source
# File lib/smart_enum/associations.rb, line 50
def has_many_enums_through(association_name, through_association, source: nil)
  association = ThroughAssociation.new(association_name, through_association, source: source)
  enum_associations[association_name] = association

  define_method(association_name) do
    public_send(association.through_association).
      flat_map(&association.association_method).compact.tap(&:freeze)
  end
end
has_one_enum(association_name, class_name: nil, foreign_key: nil, through: nil, source: nil) click to toggle source
# File lib/smart_enum/associations.rb, line 22
def has_one_enum(association_name, class_name: nil, foreign_key: nil, through: nil, source: nil)
  if through
    return has_one_enum_through(association_name, through, source: source)
  end

  association_name = association_name.to_sym
  association = HasAssociation.new(self, association_name, class_name: class_name, foreign_key: foreign_key)
  enum_associations[association_name] = association

  define_method(association_name) do
    association.association_class.values.detect{|instance|
      instance.attributes[association.foreign_key] == self.id
    }
  end
end
has_one_enum_through(association_name, through_association, source: nil) click to toggle source
# File lib/smart_enum/associations.rb, line 38
def has_one_enum_through(association_name, through_association, source: nil)
  association = ThroughAssociation.new(association_name, through_association, source: source)
  enum_associations[association_name] = association

  define_method(association_name) do
    intermediate = public_send(association.through_association)
    if intermediate
      intermediate.public_send(association.association_method)
    end
  end
end