Module: ActiveScaffold::OrmChecks

Included in:
Config::Core, DataStructures::Column, DataStructures::Sorting
Defined in:
lib/active_scaffold/orm_checks.rb

Class Method Summary collapse

Class Method Details

.active_record?(klass) ⇒ Boolean

Returns:

  • (Boolean)


4
5
6
7
# File 'lib/active_scaffold/orm_checks.rb', line 4

def active_record?(klass)
  return unless defined? ActiveRecord
  klass < ActiveRecord::Base
end

.column_type(klass, column_name) ⇒ Object



78
79
80
81
82
83
84
# File 'lib/active_scaffold/orm_checks.rb', line 78

def column_type(klass, column_name)
  if active_record? klass
    type_for_attribute(klass, column_name).type
  elsif mongoid? klass
    type_for_attribute(klass, column_name)
  end
end

.columns(klass) ⇒ Object



30
31
32
33
34
35
36
37
38
# File 'lib/active_scaffold/orm_checks.rb', line 30

def columns(klass)
  if active_record? klass
    klass.columns
  elsif mongoid? klass
    klass.fields.values
  else
    []
  end
end

.columns_hash(klass) ⇒ Object



40
41
42
43
44
45
46
47
48
# File 'lib/active_scaffold/orm_checks.rb', line 40

def columns_hash(klass)
  if active_record? klass
    klass.columns_hash
  elsif mongoid? klass
    klass.fields
  else
    {}
  end
end

.content_columns(klass) ⇒ Object



60
61
62
63
64
65
66
67
68
# File 'lib/active_scaffold/orm_checks.rb', line 60

def content_columns(klass)
  if active_record? klass
    klass.content_columns
  elsif mongoid? klass
    klass.fields.reject { |field, _| field == '_id' }.values
  else
    []
  end
end

.mongoid?(klass) ⇒ Boolean

Returns:

  • (Boolean)


9
10
11
12
# File 'lib/active_scaffold/orm_checks.rb', line 9

def mongoid?(klass)
  return unless defined? Mongoid
  klass < Mongoid::Document
end

.quoted_table_name(klass) ⇒ Object



26
27
28
# File 'lib/active_scaffold/orm_checks.rb', line 26

def quoted_table_name(klass)
  klass.quoted_table_name if active_record? klass
end

.reflect_on_all_associations(klass) ⇒ Object



50
51
52
53
54
55
56
57
58
# File 'lib/active_scaffold/orm_checks.rb', line 50

def reflect_on_all_associations(klass)
  if active_record? klass
    klass.reflect_on_all_associations
  elsif mongoid? klass
    klass.relations.values
  else
    []
  end
end

.table_name(klass) ⇒ Object



18
19
20
21
22
23
24
# File 'lib/active_scaffold/orm_checks.rb', line 18

def table_name(klass)
  if active_record? klass
    klass.table_name
  elsif mongoid? klass
    klass.collection.name
  end
end

.tableless?(klass) ⇒ Boolean

Returns:

  • (Boolean)


14
15
16
# File 'lib/active_scaffold/orm_checks.rb', line 14

def tableless?(klass)
  klass < ActiveScaffold::Tableless
end

.type_for_attribute(klass, column_name) ⇒ Object



70
71
72
73
74
75
76
# File 'lib/active_scaffold/orm_checks.rb', line 70

def type_for_attribute(klass, column_name)
  if active_record? klass
    klass.type_for_attribute column_name.to_s
  elsif mongoid? klass
    klass.fields[column_name.to_s]&.type
  end
end