Class: ActiveScaffold::DataStructures::Association::Abstract

Inherits:
Object
  • Object
show all
Defined in:
lib/active_scaffold/data_structures/association/abstract.rb

Direct Known Subclasses

ActiveRecord, Mongoid

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(association) ⇒ Abstract

Returns a new instance of Abstract.



3
4
5
# File 'lib/active_scaffold/data_structures/association/abstract.rb', line 3

def initialize(association)
  @association = association
end

Instance Attribute Details

#reverse(klass = nil) ⇒ Object



97
98
99
100
101
102
# File 'lib/active_scaffold/data_structures/association/abstract.rb', line 97

def reverse(klass = nil)
  unless polymorphic? || defined?(@reverse)
    @reverse ||= inverse || get_reverse&.name
  end
  @reverse || (get_reverse(klass)&.name unless klass.nil?)
end

Instance Method Details

#allow_join?Boolean

Returns:

  • (Boolean)


9
10
11
# File 'lib/active_scaffold/data_structures/association/abstract.rb', line 9

def allow_join?
  !polymorphic?
end

#asObject



79
# File 'lib/active_scaffold/data_structures/association/abstract.rb', line 79

def as; end

#belongs_to?Boolean

Returns:

  • (Boolean)


21
22
23
# File 'lib/active_scaffold/data_structures/association/abstract.rb', line 21

def belongs_to?
  @association.macro == :belongs_to
end

#collection?Boolean

Returns:

  • (Boolean)


41
42
43
# File 'lib/active_scaffold/data_structures/association/abstract.rb', line 41

def collection?
  has_many? || habtm?
end

#counter_cacheObject



61
62
63
# File 'lib/active_scaffold/data_structures/association/abstract.rb', line 61

def counter_cache
  @association.options[:counter_cache]
end

#counter_cache_hack?Boolean

Returns:

  • (Boolean)


85
86
87
# File 'lib/active_scaffold/data_structures/association/abstract.rb', line 85

def counter_cache_hack?
  false
end

#habtm?Boolean

Returns:

  • (Boolean)


33
34
35
# File 'lib/active_scaffold/data_structures/association/abstract.rb', line 33

def habtm?
  @association.macro == :has_and_belongs_to_many
end

#has_many?Boolean

rubocop:disable Naming/PredicateName

Returns:

  • (Boolean)


29
30
31
# File 'lib/active_scaffold/data_structures/association/abstract.rb', line 29

def has_many? # rubocop:disable Naming/PredicateName
  @association.macro == :has_many
end

#has_one?Boolean

rubocop:disable Naming/PredicateName

Returns:

  • (Boolean)


25
26
27
# File 'lib/active_scaffold/data_structures/association/abstract.rb', line 25

def has_one? # rubocop:disable Naming/PredicateName
  @association.macro == :has_one
end

#inverse_for?(klass) ⇒ Boolean

Returns:

  • (Boolean)


104
105
106
107
# File 'lib/active_scaffold/data_structures/association/abstract.rb', line 104

def inverse_for?(klass)
  inverse_class = reverse_association(klass)&.inverse_klass
  inverse_class.present? && (inverse_class == klass || klass < inverse_class)
end

#klass(record = nil) ⇒ Object



13
14
15
16
17
18
19
# File 'lib/active_scaffold/data_structures/association/abstract.rb', line 13

def klass(record = nil)
  if polymorphic?
    record&.send(foreign_type)&.safe_constantize
  else
    @association.klass
  end
end

#polymorphic?Boolean

Returns:

  • (Boolean)


65
66
67
# File 'lib/active_scaffold/data_structures/association/abstract.rb', line 65

def polymorphic?
  false
end

#primary_keyObject



57
58
59
# File 'lib/active_scaffold/data_structures/association/abstract.rb', line 57

def primary_key
  @association.options[:primary_key]
end

#quoted_primary_keyObject



93
94
95
# File 'lib/active_scaffold/data_structures/association/abstract.rb', line 93

def quoted_primary_key
  raise "define quoted_primary_key method in #{self.class.name} class"
end

#quoted_table_nameObject



89
90
91
# File 'lib/active_scaffold/data_structures/association/abstract.rb', line 89

def quoted_table_name
  raise "define quoted_table_name method in #{self.class.name} class"
end

#readonly?Boolean

Returns:

  • (Boolean)


69
70
71
# File 'lib/active_scaffold/data_structures/association/abstract.rb', line 69

def readonly?
  false
end

#respond_to_target?Boolean

Returns:

  • (Boolean)


81
82
83
# File 'lib/active_scaffold/data_structures/association/abstract.rb', line 81

def respond_to_target?
  false
end

#reverse_association(klass = nil) ⇒ Object



109
110
111
112
113
114
115
116
117
118
# File 'lib/active_scaffold/data_structures/association/abstract.rb', line 109

def reverse_association(klass = nil)
  assoc =
    if polymorphic?
      get_reverse(klass) unless klass.nil?
    else
      reverse_name = reverse(klass)
      reflect_on_association(reverse_name) if reverse_name
    end
  self.class.new(assoc) if assoc
end

#scopeObject



77
# File 'lib/active_scaffold/data_structures/association/abstract.rb', line 77

def scope; end

#singular?Boolean

Returns:

  • (Boolean)


37
38
39
# File 'lib/active_scaffold/data_structures/association/abstract.rb', line 37

def singular?
  !collection?
end

#source_reflectionObject



75
# File 'lib/active_scaffold/data_structures/association/abstract.rb', line 75

def source_reflection; end

#through?Boolean

Returns:

  • (Boolean)


45
46
47
# File 'lib/active_scaffold/data_structures/association/abstract.rb', line 45

def through?
  false
end

#through_collection?Boolean

Returns:

  • (Boolean)


53
54
55
# File 'lib/active_scaffold/data_structures/association/abstract.rb', line 53

def through_collection?
  through? && through_reflection.collection?
end

#through_reflectionObject



73
# File 'lib/active_scaffold/data_structures/association/abstract.rb', line 73

def through_reflection; end

#through_singular?Boolean

Returns:

  • (Boolean)


49
50
51
# File 'lib/active_scaffold/data_structures/association/abstract.rb', line 49

def through_singular?
  through? && !through_reflection.collection?
end