api: refactor api attribute declarations.

Replace the `method_attributes` and `hidden_attributes` methods with
`api_attributes`. `api_attributes` can be used as a class macro:

    # include only the given attributes.
    api_attributes :id, :created_at, :creator_name, ...

    # include all default attributes plus the `creator_name` method.
    api_attributes including: [:creator_name]

or as an instance method:

    def api_attributes
       [:id, :created_at, :creator_name, ...]
    end

By default, all attributes are included except for IP addresses and
tsvector columns.
This commit is contained in:
evazion
2019-09-08 23:28:02 -05:00
parent 19f2cc1e74
commit d0f060d8eb
22 changed files with 98 additions and 209 deletions

View File

@@ -17,7 +17,7 @@ class ApplicationResponder < ActionController::Responder
options[:root] ||= resource.table_name.dasherize if resource.respond_to?(:table_name)
end
options[:only] ||= params["only"].scan(/\w+/) if params["only"]
options[:only] ||= params["only"].scan(/\w+/).map(&:to_sym) if params["only"]
super
end

View File

@@ -1,32 +1,13 @@
module Reports
class UploadTags < ::Post
def readonly?
true
end
module ApiMethods
def as_json(options = {})
options ||= {}
options[:only] ||= [:id]
super(options)
end
def to_xml(options = {}, &block)
options ||= {}
options[:only] ||= [:id, :uploader_id]
super(options, &block)
end
def method_attributes
[:uploader_tags, :added_tags, :removed_tags]
end
def api_attributes
[:id, :uploader_id, :uploader_tags, :added_tags, :removed_tags]
end
include ApiMethods
def uploader_tags_array
@uploader_tags ||= begin
uploader_versions = versions.where(updater_id: uploader_id)