Sha256: 37971aed6c25744cedeeffd0eaae56e23cd29068906f396f4f58b692535eb25a

Contents?: true

Size: 1.33 KB

Versions: 20

Compression:

Stored size: 1.33 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 != 'PolymorphicOneToMany' && 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

20 entries across 20 versions & 1 rubygems

Version Path
forest_admin_datasource_toolkit-1.0.0.pre.beta.81 lib/forest_admin_datasource_toolkit/utils/schema.rb
forest_admin_datasource_toolkit-1.0.0.pre.beta.80 lib/forest_admin_datasource_toolkit/utils/schema.rb
forest_admin_datasource_toolkit-1.0.0.pre.beta.79 lib/forest_admin_datasource_toolkit/utils/schema.rb
forest_admin_datasource_toolkit-1.0.0.pre.beta.78 lib/forest_admin_datasource_toolkit/utils/schema.rb
forest_admin_datasource_toolkit-1.0.0.pre.beta.77 lib/forest_admin_datasource_toolkit/utils/schema.rb
forest_admin_datasource_toolkit-1.0.0.pre.beta.76 lib/forest_admin_datasource_toolkit/utils/schema.rb
forest_admin_datasource_toolkit-1.0.0.pre.beta.75 lib/forest_admin_datasource_toolkit/utils/schema.rb
forest_admin_datasource_toolkit-1.0.0.pre.beta.74 lib/forest_admin_datasource_toolkit/utils/schema.rb
forest_admin_datasource_toolkit-1.0.0.pre.beta.73 lib/forest_admin_datasource_toolkit/utils/schema.rb
forest_admin_datasource_toolkit-1.0.0.pre.beta.72 lib/forest_admin_datasource_toolkit/utils/schema.rb
forest_admin_datasource_toolkit-1.0.0.pre.beta.71 lib/forest_admin_datasource_toolkit/utils/schema.rb
forest_admin_datasource_toolkit-1.0.0.pre.beta.70 lib/forest_admin_datasource_toolkit/utils/schema.rb
forest_admin_datasource_toolkit-1.0.0.pre.beta.69 lib/forest_admin_datasource_toolkit/utils/schema.rb
forest_admin_datasource_toolkit-1.0.0.pre.beta.68 lib/forest_admin_datasource_toolkit/utils/schema.rb
forest_admin_datasource_toolkit-1.0.0.pre.beta.67 lib/forest_admin_datasource_toolkit/utils/schema.rb
forest_admin_datasource_toolkit-1.0.0.pre.beta.66 lib/forest_admin_datasource_toolkit/utils/schema.rb
forest_admin_datasource_toolkit-1.0.0.pre.beta.65 lib/forest_admin_datasource_toolkit/utils/schema.rb
forest_admin_datasource_toolkit-1.0.0.pre.beta.64 lib/forest_admin_datasource_toolkit/utils/schema.rb
forest_admin_datasource_toolkit-1.0.0.pre.beta.63 lib/forest_admin_datasource_toolkit/utils/schema.rb
forest_admin_datasource_toolkit-1.0.0.pre.beta.62 lib/forest_admin_datasource_toolkit/utils/schema.rb