Class: ForestAdminDatasourceToolkit::Utils::Schema

Inherits:
Object
  • Object
show all
Defined in:
lib/forest_admin_datasource_toolkit/utils/schema.rb

Class Method Summary collapse

Class Method Details

.foreign_key?(collection, name) ⇒ Boolean

Returns:

  • (Boolean)


4
5
6
7
8
9
10
11
# File 'lib/forest_admin_datasource_toolkit/utils/schema.rb', line 4

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

.get_to_many_relation(collection, relation_name) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/forest_admin_datasource_toolkit/utils/schema.rb', line 26

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

.primary_key?(collection, name) ⇒ Boolean

Returns:

  • (Boolean)


13
14
15
16
17
# File 'lib/forest_admin_datasource_toolkit/utils/schema.rb', line 13

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

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

.primary_keys(collection) ⇒ Object



19
20
21
22
23
24
# File 'lib/forest_admin_datasource_toolkit/utils/schema.rb', line 19

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