Module: RESTFramework::BaseModelControllerMixin::ClassMethods
- Defined in:
- lib/rest_framework/controller_mixins/models.rb
Constant Summary collapse
- IGNORE_VALIDATORS_WITH_KEYS =
[:if, :unless].freeze
Instance Method Summary collapse
-
#fields_metadata ⇒ Object
Get metadata about the resource’s fields.
-
#get_field_config(f) ⇒ Object
Get a field’s config, including defaults.
-
#get_fields(input_fields: nil) ⇒ Object
Get the available fields.
-
#get_label(s) ⇒ Object
Override ‘get_label` to include ActiveRecord i18n-translated column names.
-
#get_model ⇒ Object
Get the model for this controller.
-
#get_options_metadata ⇒ Object
Get a hash of metadata to be rendered in the ‘OPTIONS` response.
-
#rrf_finalize ⇒ Object
Define any behavior to execute at the end of controller definition.
- #setup_delegation ⇒ Object
Instance Method Details
#fields_metadata ⇒ Object
Get metadata about the resource’s fields.
167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 |
# File 'lib/rest_framework/controller_mixins/models.rb', line 167 def return @_fields_metadata if @_fields_metadata # Get metadata sources. model = self.get_model fields = self.get_fields.map(&:to_s) columns = model.columns_hash column_defaults = model.column_defaults reflections = model.reflections attributes = model._default_attributes readonly_attributes = model.readonly_attributes exclude_body_fields = self.exclude_body_fields.map(&:to_s) rich_text_association_names = model.reflect_on_all_associations(:has_one) .collect(&:name) .select { |n| n.to_s.start_with?("rich_text_") } = model. return @_fields_metadata = fields.map { |f| # Initialize metadata to make the order consistent. = { type: nil, kind: nil, label: self.get_label(f), primary_key: nil, required: nil, read_only: nil, } # Determine `primary_key` based on model. if model.primary_key == f [:primary_key] = true end # Determine if the field is a read-only attribute. if [:primary_key] || f.in?(readonly_attributes) || f.in?(exclude_body_fields) [:read_only] = true end # Determine `type`, `required`, `label`, and `kind` based on schema. if column = columns[f] [:kind] = "column" [:type] = column.type [:required] = true unless column.null 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 # Extract details from the model's attributes hash. if attributes.key?(f) && attribute = attributes[f] unless .key?(:default) default = attribute.value_before_type_cast [:default] = default unless default.nil? end [:kind] ||= "attribute" # Get any type information from the attribute. if type = attribute.type [:type] ||= type.type # Get enum variants. if type.is_a?(ActiveRecord::Enum::EnumType) [:enum_variants] = type.send(:mapping) # Custom integration with `translate_enum`. translate_method = "translated_#{f.pluralize}" if model.respond_to?(translate_method) [:enum_translations] = model.send(translate_method) end end end end # Get association metadata. if ref = reflections[f] [:kind] = "association" # Determine if we render id/ids fields. Unfortunately, `has_one` does not provide this # interface. if self.permit_id_assignment && id_field = RESTFramework::Utils.get_id_field(f, ref) [:id_field] = id_field end # Determine if we render nested attributes options. if self.permit_nested_attributes_assignment && ( nested_opts = model.[f.to_sym].presence ) [:nested_attributes_options] = {field: "#{f}_attributes", **nested_opts} end begin pk = ref.active_record_primary_key rescue ActiveRecord::UnknownPrimaryKey end [:association] = { macro: ref.macro, collection: ref.collection?, class_name: ref.class_name, foreign_key: ref.foreign_key, primary_key: pk, polymorphic: ref.polymorphic?, table_name: ref.polymorphic? ? nil : ref.table_name, options: ref..as_json.presence, }.compact end # Determine if this is an ActionText "rich text". if :"rich_text_#{f}".in?(rich_text_association_names) [:kind] = "rich_text" end # Determine if this is an ActiveStorage attachment. if ref = [f] [:kind] = "attachment" [:attachment] = { macro: ref.macro, } end # Determine if this is just a method. if ![:kind] && model.method_defined?(f) [:kind] = "method" 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 # Resolve procs (and lambdas), and symbols for certain arguments. if [:in].is_a?(Proc) = .merge(in: [:in].call) elsif [:in].is_a?(Symbol) = .merge(in: model.send([:in])) end [:validators] ||= {} [:validators][kind] ||= [] [:validators][kind] << end # Serialize any field config. [:config] = self.get_field_config(f).presence next [f, .compact] }.to_h end |
#get_field_config(f) ⇒ Object
Get a field’s config, including defaults.
133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 |
# File 'lib/rest_framework/controller_mixins/models.rb', line 133 def get_field_config(f) f = f.to_sym @_get_field_config ||= {} return @_get_field_config[f] if @_get_field_config[f] config = self.field_config&.dig(f) || {} # Default sub-fields if field is an association. if ref = self.get_model.reflections[f.to_s] if ref.polymorphic? columns = {} else model = ref.klass columns = model.columns_hash end config[:sub_fields] ||= RESTFramework::Utils.sub_fields_for(ref) config[:sub_fields] = config[:sub_fields].map(&:to_s) # Serialize very basic metadata about sub-fields. config[:sub_fields_metadata] = config[:sub_fields].map { |sf| v = {} if columns[sf] v[:kind] = "column" end next [sf, v] }.to_h.compact.presence end return @_get_field_config[f] = config.compact end |
#get_fields(input_fields: nil) ⇒ Object
Get the available fields. Fallback to this controller’s model columns, or an empty array. This should always return an array of strings.
111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 |
# File 'lib/rest_framework/controller_mixins/models.rb', line 111 def get_fields(input_fields: nil) input_fields ||= self.fields # If fields is a hash, then parse it. if input_fields.is_a?(Hash) return RESTFramework::Utils.parse_fields_hash( input_fields, self.get_model, exclude_associations: self.exclude_associations ) elsif !input_fields # Otherwise, if fields is nil, then fallback to columns. model = self.get_model return model ? RESTFramework::Utils.fields_for( model, exclude_associations: self.exclude_associations ) : [] elsif input_fields input_fields = input_fields.map(&:to_s) end return input_fields end |
#get_label(s) ⇒ Object
Override ‘get_label` to include ActiveRecord i18n-translated column names.
105 106 107 |
# File 'lib/rest_framework/controller_mixins/models.rb', line 105 def get_label(s) return self.get_model.human_attribute_name(s, default: super) end |
#get_model ⇒ Object
Get the model for this controller.
91 92 93 94 95 96 97 98 99 100 101 102 |
# File 'lib/rest_framework/controller_mixins/models.rb', line 91 def get_model return @model if @model return (@model = self.model) if self.model # Try to determine model from controller name. begin return @model = self.name.demodulize.chomp("Controller").singularize.constantize rescue NameError end raise RESTFramework::UnknownModelError, self end |
#get_options_metadata ⇒ Object
Get a hash of metadata to be rendered in the ‘OPTIONS` response.
328 329 330 331 332 333 334 335 336 |
# File 'lib/rest_framework/controller_mixins/models.rb', line 328 def return super.merge( { primary_key: self.get_model.primary_key, fields: self., callbacks: self._process_action_callbacks.as_json, }, ) end |
#rrf_finalize ⇒ Object
Define any behavior to execute at the end of controller definition. :nocov:
374 375 376 377 378 379 380 381 382 383 384 385 |
# File 'lib/rest_framework/controller_mixins/models.rb', line 374 def rrf_finalize super self.setup_delegation # self.setup_channel if RESTFramework.config.freeze_config (self::RRF_BASE_MODEL_CONFIG.keys + self::RRF_BASE_MODEL_INSTANCE_CONFIG.keys).each { |k| v = self.send(k) v.freeze if v.is_a?(Hash) || v.is_a?(Array) } end end |
#setup_delegation ⇒ Object
338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 |
# File 'lib/rest_framework/controller_mixins/models.rb', line 338 def setup_delegation # Delegate extra actions. self.extra_actions&.each do |action, config| next unless config.is_a?(Hash) && config.dig(:metadata, :delegate) next unless self.get_model.respond_to?(action) self.define_method(action) do model = self.class.get_model if model.method(action).parameters.last&.first == :keyrest return api_response(model.send(action, **params)) else return api_response(model.send(action)) end end end # Delegate extra member actions. self.extra_member_actions&.each do |action, config| next unless config.is_a?(Hash) && config.dig(:metadata, :delegate) next unless self.get_model.method_defined?(action) self.define_method(action) do record = self.get_record if record.method(action).parameters.last&.first == :keyrest return api_response(record.send(action, **params)) else return api_response(record.send(action)) end end end end |