-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
serializer associated group serializer
- Loading branch information
aifeelit
committed
Nov 8, 2022
1 parent
73fb53a
commit 68c9857
Showing
6 changed files
with
146 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
# frozen_string_literal: true | ||
|
||
class ApiKeyScopeSerializer < ApplicationSerializer | ||
|
||
attributes :resource, | ||
:action, | ||
:parameters, | ||
:urls, | ||
:allowed_parameters, | ||
:key | ||
|
||
def parameters | ||
ApiKeyScope.scope_mappings.dig(object.resource.to_sym, object.action.to_sym, :params).to_a | ||
end | ||
|
||
def urls | ||
ApiKeyScope.scope_mappings.dig(object.resource.to_sym, object.action.to_sym, :urls).to_a | ||
end | ||
|
||
def action | ||
object.action.to_s.gsub('_', ' ') | ||
end | ||
|
||
def key | ||
object.action | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
# frozen_string_literal: true | ||
|
||
class ApiKeySerializer < ApplicationSerializer | ||
|
||
attributes :id, | ||
:key, | ||
:truncated_key, | ||
:description, | ||
:last_used_at, | ||
:created_at, | ||
:updated_at, | ||
:revoked_at | ||
|
||
has_one :user, serializer: BasicUserSerializer, embed: :objects | ||
has_many :api_key_scopes, serializer: ApiKeyScopeSerializer, embed: :objects | ||
|
||
def include_user_id? | ||
!object.user_id.nil? | ||
end | ||
|
||
def include_key? | ||
# Only available when first created. Not stored in db | ||
object.key_available? | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'distributed_cache' | ||
|
||
class ApplicationSerializer < ActiveModel::Serializer | ||
embed :ids, include: true | ||
|
||
class CachedFragment | ||
def initialize(json) | ||
@json = json | ||
end | ||
def as_json(*_args) | ||
@json | ||
end | ||
end | ||
|
||
def self.expire_cache_fragment!(name_or_regexp) | ||
case name_or_regexp | ||
when String | ||
fragment_cache.delete(name_or_regexp) | ||
when Regexp | ||
fragment_cache.hash.keys | ||
.select { |k| k =~ name_or_regexp } | ||
.each { |k| fragment_cache.delete(k) } | ||
end | ||
end | ||
|
||
def self.fragment_cache | ||
@cache ||= DistributedCache.new("am_serializer_fragment_cache") | ||
end | ||
|
||
protected | ||
|
||
def cache_fragment(name, &block) | ||
ApplicationSerializer.fragment_cache.defer_get_set(name, &block) | ||
end | ||
|
||
def cache_anon_fragment(name, &blk) | ||
if scope.anonymous? | ||
cache_fragment(name, &blk) | ||
else | ||
blk.call | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
# frozen_string_literal: true | ||
|
||
class ArchetypeSerializer < ApplicationSerializer | ||
|
||
attributes :id, :name, :options | ||
|
||
def options | ||
object.options.keys.collect do |k| | ||
{ | ||
key: k, | ||
title: I18n.t("archetypes.#{object.id}.options.#{k}.title"), | ||
description: I18n.t("archetypes.#{object.id}.options.#{k}.description"), | ||
option_type: object.options[k] | ||
} | ||
end | ||
end | ||
|
||
def name | ||
I18n.t("archetypes.#{object.id}.title") | ||
end | ||
|
||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
# frozen_string_literal: true | ||
|
||
class AssociatedGroupSerializer < ApplicationSerializer | ||
attributes :id, | ||
:name, | ||
:provider_name, | ||
:label | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
# frozen_string_literal: true | ||
|
||
class AuthProviderSerializer < ApplicationSerializer | ||
|
||
attributes :name, :custom_url, :pretty_name_override, :title_override, | ||
:frame_width, :frame_height, :can_connect, :can_revoke, | ||
:icon | ||
|
||
def title_override | ||
return SiteSetting.get(object.title_setting) if object.title_setting | ||
object.title | ||
end | ||
|
||
def pretty_name_override | ||
return SiteSetting.get(object.pretty_name_setting) if object.pretty_name_setting | ||
object.pretty_name | ||
end | ||
|
||
end |