Skip to content

Commit

Permalink
Add render_api and rename get_field_config to field_config_for.
Browse files Browse the repository at this point in the history
  • Loading branch information
gregschmit committed Dec 20, 2024
1 parent 1b8f5ba commit c04f88f
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 9 deletions.
2 changes: 1 addition & 1 deletion lib/rest_framework/filters/model_query_filter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ def _get_filter_params
field, sub_field = match[1..2]
next false unless field.in?(fields)

sub_fields = @controller.class.get_field_config(field)[:sub_fields] || []
sub_fields = @controller.class.field_config_for(field)[:sub_fields] || []
if sub_field.in?(sub_fields)
includes << field.to_sym
next true
Expand Down
4 changes: 3 additions & 1 deletion lib/rest_framework/mixins/base_controller_mixin.rb
Original file line number Diff line number Diff line change
Expand Up @@ -341,7 +341,9 @@ def api_response(payload, **kwargs)
end
end

# Provide a generic `OPTIONS` response with metadata such as available actions.
# TODO: Might make this the default render method in the future.
alias_method :render_api, :api_response

def options
return api_response(self.options_metadata)
end
Expand Down
12 changes: 6 additions & 6 deletions lib/rest_framework/mixins/model_controller_mixin.rb
Original file line number Diff line number Diff line change
Expand Up @@ -117,10 +117,10 @@ def get_fields(input_fields: nil)
end

# Get a field's config, including defaults.
def get_field_config(f)
def field_config_for(f)
f = f.to_sym
@_get_field_config ||= {}
return @_get_field_config[f] if @_get_field_config[f]
@_field_config_for ||= {}
return @_field_config_for[f] if @_field_config_for[f]

config = self.field_config&.dig(f) || {}

Expand All @@ -147,7 +147,7 @@ def get_field_config(f)
}.to_h.compact.presence
end

return @_get_field_config[f] = config.compact
return @_field_config_for[f] = config.compact
end

# Get metadata about the resource's fields.
Expand Down Expand Up @@ -305,7 +305,7 @@ def fields_metadata
end

# Serialize any field config.
metadata[:config] = self.get_field_config(f).presence
metadata[:config] = self.field_config_for(f).presence

next [f, metadata.compact]
}.to_h
Expand Down Expand Up @@ -450,7 +450,7 @@ def get_allowed_parameters

if self.permit_nested_attributes_assignment
hash_variations["#{f}_attributes"] = (
self.class.get_field_config(f)[:sub_fields] + ["_destroy"]
self.class.field_config_for(f)[:sub_fields] + ["_destroy"]
)
end

Expand Down
2 changes: 1 addition & 1 deletion lib/rest_framework/serializers/native_serializer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ def _get_controller_serializer_config(fields)
attachment_reflections = @model.attachment_reflections

fields.each do |f|
field_config = @controller.class.get_field_config(f)
field_config = @controller.class.field_config_for(f)
next if field_config[:write_only]

if f.in?(column_names)
Expand Down

0 comments on commit c04f88f

Please sign in to comment.