Skip to content

Commit

Permalink
serializer associated group serializer
Browse files Browse the repository at this point in the history
  • Loading branch information
aifeelit committed Nov 8, 2022
1 parent 73fb53a commit 68c9857
Show file tree
Hide file tree
Showing 6 changed files with 146 additions and 0 deletions.
27 changes: 27 additions & 0 deletions app/serializers/api_key_scope_serializer.rb
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
25 changes: 25 additions & 0 deletions app/serializers/api_key_serializer.rb
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
45 changes: 45 additions & 0 deletions app/serializers/application_serializer.rb
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
22 changes: 22 additions & 0 deletions app/serializers/archetype_serializer.rb
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
8 changes: 8 additions & 0 deletions app/serializers/associated_group_serializer.rb
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
19 changes: 19 additions & 0 deletions app/serializers/auth_provider_serializer.rb
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

0 comments on commit 68c9857

Please sign in to comment.