Class: PowerEnum::Reflection::EnumerationReflection

Inherits:
ActiveRecord::Reflection::MacroReflection
  • Object
show all
Defined in:
lib/power_enum/reflection.rb

Overview

Reflection class for enum reflections. See ActiveRecord::Reflection

Defined Under Namespace

Classes: EnumJoinKeys

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, options, active_record) ⇒ EnumerationReflection

See ActiveRecord::Reflection::MacroReflection



40
41
42
# File 'lib/power_enum/reflection.rb', line 40

def initialize(name, options, active_record)
  super name, nil, options, active_record
end

Instance Attribute Details

#counter_cache_columnObject (readonly)

Returns the value of attribute counter_cache_column.



36
37
38
# File 'lib/power_enum/reflection.rb', line 36

def counter_cache_column
  @counter_cache_column
end

#parent_reflectionObject

Returns the value of attribute parent_reflection.



37
38
39
# File 'lib/power_enum/reflection.rb', line 37

def parent_reflection
  @parent_reflection
end

Instance Method Details

#active_record_primary_keyObject Also known as: join_primary_key



64
65
66
# File 'lib/power_enum/reflection.rb', line 64

def active_record_primary_key
  @active_record_primary_key ||= options[:primary_key] || active_record.primary_key
end

#association_classObject



74
75
76
# File 'lib/power_enum/reflection.rb', line 74

def association_class
  ::ActiveRecord::Associations::HasOneAssociation
end

#association_primary_key(klass = nil) ⇒ Object

Returns the primary key of the active record model that owns the has_enumerated association.



103
104
105
# File 'lib/power_enum/reflection.rb', line 103

def association_primary_key(klass = nil)
  active_record.primary_key
end

#belongs_to?Boolean

Normally defined on AR::AssociationReflection::MacroReflection. Realistically, this is a belongs-to relationship.

Returns:

  • (Boolean)


156
157
158
# File 'lib/power_enum/reflection.rb', line 156

def belongs_to?
  true
end

#chainObject

Returns an array of this instance as the only member.



150
151
152
# File 'lib/power_enum/reflection.rb', line 150

def chain
  [self]
end

#check_preloadable!Object Also known as: check_eager_loadable!



48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/power_enum/reflection.rb', line 48

def check_preloadable!
  return unless scope
  if scope.arity > 0
    ActiveSupport::Deprecation.warn(<<-MSG.squish)
The association scope '#{name}' is instance dependent (the scope
block takes an argument). Preloading happens before the individual
instances are created. This means that there is no instance being
passed to the association scope. This will most likely result in
broken or incorrect behavior. Joining, Preloading and eager loading
of these associations is deprecated and will be removed in the future.
    MSG
  end
end

#check_validity!Object

Does nothing.



108
# File 'lib/power_enum/reflection.rb', line 108

def check_validity!; end

#class_nameObject

Returns the class name of the enum



85
86
87
# File 'lib/power_enum/reflection.rb', line 85

def class_name
  @class_name ||= (@options[:class_name] || @name).to_s.camelize
end

#collection?Boolean

Always returns true.

Returns:

  • (Boolean)


129
130
131
# File 'lib/power_enum/reflection.rb', line 129

def collection?
  true
end

#conditionsObject

In this case, returns [[]]



139
140
141
# File 'lib/power_enum/reflection.rb', line 139

def conditions
  [[]]
end

#deprecated?Boolean

Returns:

  • (Boolean)


134
135
136
# File 'lib/power_enum/reflection.rb', line 134

def deprecated?
  false
end

#foreign_keyObject Also known as: join_foreign_key

Returns the foreign key on the association owner's table.



90
91
92
# File 'lib/power_enum/reflection.rb', line 90

def foreign_key
  @foreign_key ||= (@options[:foreign_key] || "#{@name}_id").to_s
end

#join_keys(*_) ⇒ Object



80
81
82
# File 'lib/power_enum/reflection.rb', line 80

def join_keys(*_)
  EnumJoinKeys.new(active_record_primary_key, foreign_key)
end

#klassObject



70
71
72
# File 'lib/power_enum/reflection.rb', line 70

def klass
  @klass ||= active_record.send(:compute_type, class_name)
end

#macroObject



44
45
46
# File 'lib/power_enum/reflection.rb', line 44

def macro
  :has_enumerated
end

#polymorphic?Boolean

Always returns false. Necessary for stuff like Booking.where(:status => BookingStatus)

Returns:

  • (Boolean)


124
125
126
# File 'lib/power_enum/reflection.rb', line 124

def polymorphic?
  false
end

#scope_chainObject

An array of arrays of scopes. Each item in the outside array corresponds to a reflection in the #chain.



162
163
164
# File 'lib/power_enum/reflection.rb', line 162

def scope_chain
  scope ? [[scope]] : [[]]
end

#source_macroObject

Returns :belongs_to. Kind of hackish, but otherwise AREL joins logic gets confused.



145
146
147
# File 'lib/power_enum/reflection.rb', line 145

def source_macro
  :belongs_to
end

#source_reflectionObject

Returns nil



111
112
113
# File 'lib/power_enum/reflection.rb', line 111

def source_reflection
  nil
end

#table_nameObject

Returns the name of the enum table



97
98
99
# File 'lib/power_enum/reflection.rb', line 97

def table_name
  @table_name ||= self.class.const_get(class_name).table_name
end

#typeObject Also known as: join_primary_type

Returns nil



116
117
118
# File 'lib/power_enum/reflection.rb', line 116

def type
  nil
end