Module: RESTFramework::BaseModelControllerMixin::ClassMethods
- Defined in:
- lib/rest_framework/controller_mixins/models.rb
Constant Summary collapse
- IGNORE_VALIDATORS_WITH_KEYS =
[:if, :unless]
Instance Method Summary collapse
-
#get_fields_metadata(fields: nil) ⇒ Object
Get metadata about the resource’s fields.
-
#get_model(from_get_recordset: false) ⇒ Object
Get the model for this controller.
-
#get_options_metadata(fields: nil) ⇒ Object
Get a hash of metadata to be rendered in the ‘OPTIONS` response.
Instance Method Details
#get_fields_metadata(fields: nil) ⇒ Object
Get metadata about the resource’s fields.
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 |
# File 'lib/rest_framework/controller_mixins/models.rb', line 33 def (fields: nil) # Get metadata sources. model = self.get_model fields ||= self.fields || model&.column_names || [] fields = fields.map(&:to_s) columns = model&.columns_hash column_defaults = model&.column_defaults attributes = model&._default_attributes return fields.map { |f| # Initialize metadata to make the order consistent. = { type: nil, kind: nil, label: nil, primary_key: nil, required: nil, read_only: nil } # Determine `primary_key` based on model. if model&.primary_key == f [:primary_key] = true end # Determine `type`, `required`, `label`, and `kind` based on schema. if column = columns[f] [:type] = column.type [:required] = true unless column.null [:label] = column.human_name.instance_eval { |n| n == "Id" ? "ID" : n } [:kind] = "column" end # Determine `default` based on schema; we use `column_defaults` rather than `columns_hash` # because these are casted to the proper type. column_default = column_defaults[f] unless column_default.nil? [:default] = column_default end # Determine `default` and `kind` based on attribute only if not determined by the DB. if attributes.key?(f) && attribute = attributes[f] unless .key?(:default) default = attribute.value_before_type_cast [:default] = default unless default.nil? end unless [:kind] [:kind] = "attribute" end end # Determine if `kind` is a association or method if not determined already. unless [:kind] if association = model.reflections[f] [:kind] = "association.#{association.macro}" elsif model.method_defined?(f) [:kind] = "method" end end # Collect validator options into a hash on their type, while also updating `required` based # on any presence validators. model.validators_on(f).each do |validator| kind = validator.kind = validator. # Reject validator if it includes keys like `:if` and `:unless` because those are # conditionally applied in a way that is not feasible to communicate via the API. next if IGNORE_VALIDATORS_WITH_KEYS.any? { |k| .key?(k) } # Update `required` if we find a presence validator. [:required] = true if kind == :presence [:validators] ||= {} [:validators][kind] ||= [] [:validators][kind] << end next [f, .compact] }.to_h end |
#get_model(from_get_recordset: false) ⇒ Object
Get the model for this controller.
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/rest_framework/controller_mixins/models.rb', line 12 def get_model(from_get_recordset: false) return @model if @model return (@model = self.model) if self.model # Delegate to the recordset's model, if it's defined. unless from_get_recordset # Prevent infinite recursion. if (recordset = self.new.get_recordset) return @model = recordset.klass end end # Try to determine model from controller name. begin return @model = self.name.demodulize.match(/(.*)Controller/)[1].singularize.constantize rescue NameError end return nil end |
#get_options_metadata(fields: nil) ⇒ Object
Get a hash of metadata to be rendered in the ‘OPTIONS` response. Cache the result.
112 113 114 115 116 117 118 |
# File 'lib/rest_framework/controller_mixins/models.rb', line 112 def (fields: nil) return super().merge( { fields: self.(fields: fields), }, ) end |