models: factor out api_attributes to policies.

Refactor models so that we define attribute API permissions in policy
files instead of directly in models.

This is cleaner because a) permissions are better handled by policies
and b) which attributes are visible to the API is an API-level concern
that models shouldn't have to care about.

This fixes an issue with not being able to precompile CSS/JS assets
unless the database was up and running. This was a problem when building
Docker images because we don't have a database at build time. We needed
the database because `api_attributes` was a class-level macro in some
places, which meant it ran at boot time, but this triggered a database
call because api_attributes used database introspection to get the list
of allowed API attributes.
This commit is contained in:
evazion
2020-06-08 18:38:02 -05:00
parent b6ed63841d
commit eacb4d4df3
20 changed files with 81 additions and 79 deletions

View File

@@ -69,4 +69,8 @@ class ApplicationPolicy
def permitted_attributes_for_edit
permitted_attributes_for_update
end
def api_attributes
record.class.attribute_types.reject { |name, attr| attr.type.in?([:inet, :tsvector]) }.keys.map(&:to_sym)
end
end