Skip to content

Commit

Permalink
Resolve proc/symbol inclusion :in keys to values in OPTIONS.
Browse files Browse the repository at this point in the history
  • Loading branch information
gregschmit committed Jan 23, 2023
1 parent 48a5bb5 commit f7e7b90
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 2 deletions.
7 changes: 7 additions & 0 deletions lib/rest_framework/controller_mixins/models.rb
Expand Up @@ -266,6 +266,13 @@ def get_fields_metadata
# Update `required` if we find a presence validator.
metadata[:required] = true if kind == :presence

# Resolve procs (and lambdas), and symbols for certain arguments.
if options[:in].is_a?(Proc)
options = options.merge(in: options[:in].call)
elsif options[:in].is_a?(Symbol)
options = options.merge(in: model.send(options[:in]))
end

metadata[:validators] ||= {}
metadata[:validators][kind] ||= []
metadata[:validators][kind] << options
Expand Down
2 changes: 1 addition & 1 deletion test/app/controllers/test_api_controller.rb
@@ -1,6 +1,6 @@
class TestApiController < ApplicationController
DESCRIPTION = <<~TEXT.lines.map(&:strip).join(" ")
The test API contains a lot of really weird controllers for unit testing specific features.
The test API contains a lot of really weird controllers for testing specific features.
TEXT

include RESTFramework::BaseControllerMixin
Expand Down
11 changes: 10 additions & 1 deletion test/app/models/user.rb
Expand Up @@ -45,7 +45,16 @@ class User < ApplicationRecord

validates_numericality_of :balance, greater_than: 0, allow_nil: true
validates_inclusion_of :state, in: states.keys
validates_inclusion_of :status, in: STATUS_OPTS.keys
validates_inclusion_of :status, in: :status_keys

# Adding this class method to test serializing inclusion symbol in `OPTIONS` endpoint.
def self.status_keys
return STATUS_OPTS.keys
end

def status_keys
return self.class.status_keys
end

# An example of a "calculated" property method.
def calculated_property
Expand Down

0 comments on commit f7e7b90

Please sign in to comment.