Class: Monolith::ModelsController::Model
- Inherits:
-
Object
- Object
- Monolith::ModelsController::Model
- Defined in:
- app/controllers/monolith/models_controller.rb
Overview
Inline ActiveModel-like object
Instance Attribute Summary collapse
-
#klass ⇒ Object
readonly
Returns the value of attribute klass.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
Class Method Summary collapse
Instance Method Summary collapse
- #associations ⇒ Object
- #callbacks ⇒ Object
- #class_methods ⇒ Object
- #column_names ⇒ Object
-
#initialize(klass) ⇒ Model
constructor
A new instance of Model.
- #instance_methods ⇒ Object
- #primary_key ⇒ Object
- #scopes ⇒ Object
- #source_code ⇒ Object
- #source_location ⇒ Object
- #table_name ⇒ Object
- #to_param ⇒ Object
- #validations ⇒ Object
Constructor Details
#initialize(klass) ⇒ Model
Returns a new instance of Model.
38 39 40 41 |
# File 'app/controllers/monolith/models_controller.rb', line 38 def initialize(klass) @klass = klass @name = klass.name end |
Instance Attribute Details
#klass ⇒ Object (readonly)
Returns the value of attribute klass.
19 20 21 |
# File 'app/controllers/monolith/models_controller.rb', line 19 def klass @klass end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
19 20 21 |
# File 'app/controllers/monolith/models_controller.rb', line 19 def name @name end |
Class Method Details
.all ⇒ Object
21 22 23 24 25 26 27 28 29 |
# File 'app/controllers/monolith/models_controller.rb', line 21 def self.all Rails.application.eager_load! models = ActiveRecord::Base.descendants .reject(&:abstract_class?) .sort_by(&:name) models.map { |klass| new(klass) } end |
.find(name) ⇒ Object
31 32 33 34 35 36 |
# File 'app/controllers/monolith/models_controller.rb', line 31 def self.find(name) Rails.application.eager_load! klass = ActiveRecord::Base.descendants.find { |k| k.name == name } klass && new(klass) end |
Instance Method Details
#associations ⇒ Object
75 76 77 78 79 80 81 82 83 84 85 86 87 |
# File 'app/controllers/monolith/models_controller.rb', line 75 def associations klass.reflect_on_all_associations.map do |assoc| { name: assoc.name, type: assoc.macro, class_name: assoc.class_name, foreign_key: assoc.foreign_key, options: assoc. } end rescue [] end |
#callbacks ⇒ Object
97 98 99 100 101 102 103 104 105 106 107 108 109 110 |
# File 'app/controllers/monolith/models_controller.rb', line 97 def callbacks [:before_validation, :after_validation, :before_save, :after_save, :before_create, :after_create, :before_update, :after_update, :before_destroy, :after_destroy].flat_map do |callback_type| klass._get_callbacks(callback_type).map do |callback| { type: callback_type, filter: callback.filter } end end.compact rescue [] end |
#class_methods ⇒ Object
136 137 138 139 140 141 142 |
# File 'app/controllers/monolith/models_controller.rb', line 136 def class_methods (klass.methods - ActiveRecord::Base.methods) .sort .map(&:to_s) rescue [] end |
#column_names ⇒ Object
51 52 53 54 55 |
# File 'app/controllers/monolith/models_controller.rb', line 51 def column_names klass.column_names rescue [] end |
#instance_methods ⇒ Object
128 129 130 131 132 133 134 |
# File 'app/controllers/monolith/models_controller.rb', line 128 def instance_methods (klass.instance_methods - ActiveRecord::Base.instance_methods) .sort .map(&:to_s) rescue [] end |
#primary_key ⇒ Object
57 58 59 60 61 |
# File 'app/controllers/monolith/models_controller.rb', line 57 def primary_key klass.primary_key rescue nil end |
#scopes ⇒ Object
89 90 91 92 93 94 95 |
# File 'app/controllers/monolith/models_controller.rb', line 89 def scopes # Get custom scopes (not default scopes) scope_names = klass.respond_to?(:scopes) ? klass.scopes.keys : [] scope_names.map(&:to_s).sort rescue [] end |
#source_code ⇒ Object
120 121 122 123 124 125 126 |
# File 'app/controllers/monolith/models_controller.rb', line 120 def source_code return nil unless source_location File.read(source_location) rescue nil end |
#source_location ⇒ Object
112 113 114 115 116 117 118 |
# File 'app/controllers/monolith/models_controller.rb', line 112 def source_location return nil unless klass.respond_to?(:instance_methods) # Try to find the model file file_path = "#{Rails.root}/app/models/#{name.underscore}.rb" File.exist?(file_path) ? file_path : nil end |
#table_name ⇒ Object
47 48 49 |
# File 'app/controllers/monolith/models_controller.rb', line 47 def table_name klass.table_name end |
#to_param ⇒ Object
43 44 45 |
# File 'app/controllers/monolith/models_controller.rb', line 43 def to_param name end |
#validations ⇒ Object
63 64 65 66 67 68 69 70 71 72 73 |
# File 'app/controllers/monolith/models_controller.rb', line 63 def validations klass.validators.map do |validator| { type: validator.class.name.demodulize.gsub(/Validator$/, ''), attributes: validator.attributes, options: validator. } end rescue [] end |