Sha256: c07f4a3f81a56fb10383079716c5025450ee0abaa75034d50703b9ef271506d4

Contents?: true

Size: 1.22 KB

Versions: 3

Compression:

Stored size: 1.22 KB

Contents

module ForestLiana
  class SchemaUtils

    def self.associations(active_record_class)
      active_record_class
        .reflect_on_all_associations
        .select {|a| !polymorphic?(a)}
    end

    def self.one_associations(active_record_class)
      self.associations(active_record_class).select do |x|
        [:has_one, :belongs_to].include?(x.macro)
      end
    end

    def self.find_model_from_table_name(table_name)
      model = nil

      ForestLiana.models.each do |subclass|
        if subclass.abstract_class?
          model = self.find_model_from_abstract_class(subclass, table_name)
        elsif subclass.table_name == table_name
          model = subclass
        end

        break if model
      end

      model
    end

    def self.tables_names
      ActiveRecord::Base.connection.tables
    end

    private

    def self.polymorphic?(association)
      association.options[:polymorphic]
    end

    def self.find_model_from_abstract_class(abstract_class, table_name)
      abstract_class.subclasses.find do |subclass|
        if subclass.abstract_class?
          return self.find_model_from_table_name(subclass, table_name)
        else
          subclass.table_name == table_name
        end
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
forest_liana-1.3.45 app/services/forest_liana/schema_utils.rb
forest_liana-1.3.44 app/services/forest_liana/schema_utils.rb
forest_liana-1.3.43 app/services/forest_liana/schema_utils.rb