Module: SmartEnum::Associations

Included in:
SmartEnum
Defined in:
lib/smart_enum/associations.rb

Defined Under Namespace

Classes: Association, HasAssociation, ThroughAssociation

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.__assert_enum(klass) ⇒ Object



88
89
90
91
92
# 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

Instance Method Details

#belongs_to_enum(association_name, class_name: nil, foreign_key: nil, when_nil: nil) ⇒ Object



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# 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_associationsObject



94
95
96
# 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) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# 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) ⇒ Object



50
51
52
53
54
55
56
57
58
# 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) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# 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) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
# 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