Module: JsonSti

Extended by:
ActiveSupport::Concern
Defined in:
lib/json_sti.rb,
lib/json_sti/class_master_list.rb,
lib/json_sti/inheritable_seeder.rb

Defined Under Namespace

Classes: ClassMasterList, InheritableSeeder

Class Method Summary collapse

Class Method Details

.initialize_single_table_arel_helpersObject



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
# File 'lib/json_sti.rb', line 12

def self.initialize_single_table_arel_helpers
  JsonSti::ClassMasterList.base_class_list.each do |receiving_class_name|
    receiving_class = receiving_class_name.to_s.camelize.constantize

    next unless ClassMasterList.relations_lookup[receiving_class_name]

    ClassMasterList.relations_lookup[receiving_class_name][:relationships].each do |relationship_to_create|
      ClassMasterList.relations_lookup[relationship_to_create][:members].each do |relationship_to_create_member|
        creation_class = "#{relationship_to_create.to_s.camelize}::#{relationship_to_create_member.to_s.singularize.camelize}".constantize

        ar_association = receiving_class.reflect_on_all_associations.detect do |association|
          association.class_name == relationship_to_create.to_s.camelize
        end

        receiving_class.class_eval do
          if ar_association.to_s.downcase =~ /many/
            # create has_many helper methods for sti subtypes
            define_method "#{relationship_to_create_member.to_s.pluralize}" do
              self.send(relationship_to_create.to_s.pluralize).where(type: creation_class.to_s)
            end

          else
            # create belongs_to helper methods for sti subtypes
            define_method "#{relationship_to_create_member.to_s.singularize}" do
              object = self.send(relationship_to_create.to_s.singularize)
              return object.class.to_s == creation_class.to_s ? object : nil
            end
          end
        end
      end
    end
  end
end