Sha256: 9e2067926e2bd893efc19fb8141667a206fd0154c3b65b4633ff5658f1549f19

Contents?: true

Size: 1.29 KB

Versions: 32

Compression:

Stored size: 1.29 KB

Contents

module ForestAdminDatasourceToolkit
  module Utils
    class Schema
      def self.foreign_key?(collection, name)
        field = collection.schema[:fields][name]

        field.type == 'Column' &&
          collection.schema[:fields].any? do |_key, relation|
            relation.type == 'ManyToOne' && relation.foreign_key == name
          end
      end

      def self.primary_key?(collection, name)
        field = collection.schema[:fields][name]

        field.type == 'Column' && field.is_primary_key
      end

      def self.primary_keys(collection)
        collection.schema[:fields].keys.select do |field_name|
          field = collection.schema[:fields][field_name]
          field.type == 'Column' && field.is_primary_key
        end
      end

      def self.get_to_many_relation(collection, relation_name)
        unless collection.schema[:fields].key?(relation_name)
          raise Exceptions::ForestException, "Relation #{relation_name} not found"
        end

        relation = collection.schema[:fields][relation_name]

        if relation.type != 'OneToMany' && relation.type != 'ManyToMany'
          raise Exceptions::ForestException,
                "Relation #{relation_name} has invalid type should be one of OneToMany or ManyToMany."
        end

        relation
      end
    end
  end
end

Version data entries

32 entries across 32 versions & 1 rubygems

Version Path
forest_admin_datasource_toolkit-1.0.0.pre.beta.61 lib/forest_admin_datasource_toolkit/utils/schema.rb
forest_admin_datasource_toolkit-1.0.0.pre.beta.60 lib/forest_admin_datasource_toolkit/utils/schema.rb
forest_admin_datasource_toolkit-1.0.0.pre.beta.59 lib/forest_admin_datasource_toolkit/utils/schema.rb
forest_admin_datasource_toolkit-1.0.0.pre.beta.58 lib/forest_admin_datasource_toolkit/utils/schema.rb
forest_admin_datasource_toolkit-1.0.0.pre.beta.57 lib/forest_admin_datasource_toolkit/utils/schema.rb
forest_admin_datasource_toolkit-1.0.0.pre.beta.56 lib/forest_admin_datasource_toolkit/utils/schema.rb
forest_admin_datasource_toolkit-1.0.0.pre.beta.55 lib/forest_admin_datasource_toolkit/utils/schema.rb
forest_admin_datasource_toolkit-1.0.0.pre.beta.54 lib/forest_admin_datasource_toolkit/utils/schema.rb
forest_admin_datasource_toolkit-1.0.0.pre.beta.53 lib/forest_admin_datasource_toolkit/utils/schema.rb
forest_admin_datasource_toolkit-1.0.0.pre.beta.52 lib/forest_admin_datasource_toolkit/utils/schema.rb
forest_admin_datasource_toolkit-1.0.0.pre.beta.45 lib/forest_admin_datasource_toolkit/utils/schema.rb
forest_admin_datasource_toolkit-1.0.0.pre.beta.44 lib/forest_admin_datasource_toolkit/utils/schema.rb
forest_admin_datasource_toolkit-1.0.0.pre.beta.43 lib/forest_admin_datasource_toolkit/utils/schema.rb
forest_admin_datasource_toolkit-1.0.0.pre.beta.42 lib/forest_admin_datasource_toolkit/utils/schema.rb
forest_admin_datasource_toolkit-1.0.0.pre.beta.41 lib/forest_admin_datasource_toolkit/utils/schema.rb
forest_admin_datasource_toolkit-1.0.0.pre.beta.40 lib/forest_admin_datasource_toolkit/utils/schema.rb
forest_admin_datasource_toolkit-1.0.0.pre.beta.39 lib/forest_admin_datasource_toolkit/utils/schema.rb
forest_admin_datasource_toolkit-1.0.0.pre.beta.38 lib/forest_admin_datasource_toolkit/utils/schema.rb
forest_admin_datasource_toolkit-1.0.0.pre.beta.37 lib/forest_admin_datasource_toolkit/utils/schema.rb
forest_admin_datasource_toolkit-1.0.0.pre.beta.36 lib/forest_admin_datasource_toolkit/utils/schema.rb