Module: ActiveScaffold::OrmChecks

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

Class Method Summary collapse

Instance 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

.cast(klass, column_name, value) ⇒ Object



94
95
96
97
98
99
100
# File 'lib/active_scaffold/orm_checks.rb', line 94

def cast(klass, column_name, value)
  if active_record? klass
    type_for_attribute(klass, column_name).cast value
  elsif mongoid? klass
    type_for_attribute(klass, column_name)&.evolve value
  end
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

.default_value(klass, column_name) ⇒ Object



86
87
88
89
90
91
92
# File 'lib/active_scaffold/orm_checks.rb', line 86

def default_value(klass, column_name)
  if ActiveScaffold::OrmChecks.mongoid? klass
    columns_hash(klass)[column_name.to_s]&.default_val
  elsif ActiveScaffold::OrmChecks.active_record? klass
    klass._default_attributes[column_name.to_s]&.value
  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

Instance Method Details

#cast(column_name, value) ⇒ Object



121
122
123
# File 'lib/active_scaffold/orm_checks.rb', line 121

def cast(column_name, value)
  ActiveScaffold::OrmChecks.cast active_record_class, column_name, value
end