diff options
author | Eugen Rochko <eugen@zeonfederated.com> | 2017-07-07 04:02:06 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-07-07 04:02:06 +0200 |
commit | 8b2cad56374b2dbb6e7a445e7917810935c45536 (patch) | |
tree | bebba9b1e98d599b3caa249a8d98fad0cfd70437 /app/lib | |
parent | 2d6128672fcadeb29c99551a33648b4880969d22 (diff) |
Refactor JSON templates to be generated with ActiveModelSerializers instead of Rabl (#4090)
Diffstat (limited to 'app/lib')
-rw-r--r-- | app/lib/inline_rabl_scope.rb | 17 | ||||
-rw-r--r-- | app/lib/inline_renderer.rb | 36 |
2 files changed, 28 insertions, 25 deletions
diff --git a/app/lib/inline_rabl_scope.rb b/app/lib/inline_rabl_scope.rb deleted file mode 100644 index 26adcb03a..000000000 --- a/app/lib/inline_rabl_scope.rb +++ /dev/null @@ -1,17 +0,0 @@ -# frozen_string_literal: true - -class InlineRablScope - include RoutingHelper - - def initialize(account) - @account = account - end - - def current_user - @account.try(:user) - end - - def current_account - @account - end -end diff --git a/app/lib/inline_renderer.rb b/app/lib/inline_renderer.rb index 8e04ad1d5..7cd9758ec 100644 --- a/app/lib/inline_renderer.rb +++ b/app/lib/inline_renderer.rb @@ -1,13 +1,33 @@ # frozen_string_literal: true class InlineRenderer - def self.render(status, current_account, template) - Rabl::Renderer.new( - template, - status, - view_path: 'app/views', - format: :json, - scope: InlineRablScope.new(current_account) - ).render + def initialize(object, current_account, template) + @object = object + @current_account = current_account + @template = template + end + + def render + case @template + when :status + serializer = REST::StatusSerializer + when :notification + serializer = REST::NotificationSerializer + else + return + end + + serializable_resource = ActiveModelSerializers::SerializableResource.new(@object, serializer: serializer, scope: current_user, scope_name: :current_user) + serializable_resource.as_json + end + + def self.render(object, current_account, template) + new(object, current_account, template).render + end + + private + + def current_user + @current_account&.user end end |