Class: Orthoses::ActiveRecord::BelongsTo
- Inherits:
-
Object
- Object
- Orthoses::ActiveRecord::BelongsTo
- Defined in:
- lib/orthoses/active_record/belongs_to.rb
Instance Method Summary collapse
- #call ⇒ Object
-
#initialize(loader) ⇒ BelongsTo
constructor
A new instance of BelongsTo.
Constructor Details
#initialize(loader) ⇒ BelongsTo
Returns a new instance of BelongsTo.
6 7 8 |
# File 'lib/orthoses/active_record/belongs_to.rb', line 6 def initialize(loader) @loader = loader end |
Instance Method Details
#call ⇒ Object
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/orthoses/active_record/belongs_to.rb', line 10 def call @loader.call.tap do |store| ::ActiveRecord::Base.descendants.each do |base| next if base.abstract_class? base_name = Orthoses::Utils.module_name(base) || next lines = base.reflect_on_all_associations(:belongs_to).flat_map do |ref| # FIXME: Can I get list of class for polymorphic? type = if ref.polymorphic? 'untyped' else Orthoses::ActiveRecord.reflection_klass_name(ref) or next end opt = "#{type}?" [ "def #{ref.name}: () -> #{opt}", "def #{ref.name}=: (#{opt}) -> #{opt}", "def reload_#{ref.name}: () -> #{opt}", ].tap do |ary| if !ref.polymorphic? ary << "def build_#{ref.name}: (?untyped attributes) ?{ (#{type}) -> void } -> #{type}" ary << "def create_#{ref.name}: (?untyped attributes) ?{ (#{type}) -> void } -> #{type}" ary << "def create_#{ref.name}!: (?untyped attributes) ?{ (#{type}) -> void } -> #{type}" end end end generated_association_methods = "#{base_name}::GeneratedAssociationMethods" store[generated_association_methods].tap do |content| content.header = "module #{generated_association_methods}" content.concat(lines) end sig = "include #{generated_association_methods}" store[base_name] << sig if !store[base_name].body.include?(sig) end end end |