Class: Model

Inherits:
ActiveRecord::Base
  • Object
show all
Includes:
IdsOfAllDependencies, NullifyDependencies
Defined in:
lib/model.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods included from NullifyDependencies

default_dependencies_symbols_to_nullify, #default_dependencies_symbols_to_nullify, #default_dependencies_to_nullify, #nullify_all_dependencies, #nullify_default_dependencies, #nullify_dependencies

Methods included from IdsOfAllDependencies

#ids_of_all_dependencies, #ids_of_all_dependencies_with_filtered, #ids_of_all_dependencies_without_reflection

Methods included from DependencyTree

#dependency_tree

Methods included from IdsOfAllDirectDependencies

#ids_of_all_direct_dependencies

Methods included from SymbolsOfAllDirectDependencies

#symbols_of_all_direct_dependencies

Methods included from IdsOfAllDependenciesNested

#ids_of_all_dependencies_nested

Class Method Details

.get_model(name) ⇒ Object



43
44
45
# File 'lib/model.rb', line 43

def self.get_model(name)
  self.subclasses.find{ |m| m.name == name.to_s.camelcase }
end

.get_model_by_table_name(name) ⇒ Object



47
48
49
# File 'lib/model.rb', line 47

def self.get_model_by_table_name(name)
  self.subclasses.find{ |m| m.table_name == name.to_s }
end

.ids_of_subclasses_entriesObject



51
52
53
54
55
# File 'lib/model.rb', line 51

def self.ids_of_subclasses_entries
  self.subclasses.map do |subclass|
    [subclass.name, subclass.all.map(&:id)] if subclass.any?
  end.compact.to_h
end

.sum_of_subclasses_rows(except: []) ⇒ Object



57
58
59
60
61
62
63
# File 'lib/model.rb', line 57

def self.sum_of_subclasses_rows(except: [])
  self.subclasses.map do |subclass|
    next 0 if except.include?(subclass.table_name) || except.include?(subclass.name)

    subclass.all.size
  end.reduce(:+)
end

Instance Method Details

#associationsObject



32
33
34
35
36
37
# File 'lib/model.rb', line 32

def associations
  self.class.reflect_on_all_associations.map do |association|
    value = self.send(association.name)
    AssociationWithValue.new(association, value)
  end
end

#associations_hashObject



26
27
28
29
30
# File 'lib/model.rb', line 26

def associations_hash
  self.associations.map do |association|
    [association.name, association]
  end.to_h
end

#associations_without_belongs_toObject



39
40
41
# File 'lib/model.rb', line 39

def associations_without_belongs_to
  self.associations.select { |a| a.macro != :belongs_to }
end

#attributes_without_idObject



22
23
24
# File 'lib/model.rb', line 22

def attributes_without_id
  self.attributes.reject{|k, v| k == "id"}
end

#removed?Boolean

Returns:

  • (Boolean)


65
66
67
68
69
70
71
72
# File 'lib/model.rb', line 65

def removed?
  begin
    reload
    false
  rescue ActiveRecord::RecordNotFound => e
    true
  end
end