Skip to content

Commit

Permalink
update all serializers with Panko
Browse files Browse the repository at this point in the history
  • Loading branch information
kortirso committed Jul 11, 2024
1 parent 004a1a3 commit ed8a4bb
Show file tree
Hide file tree
Showing 21 changed files with 95 additions and 59 deletions.
1 change: 0 additions & 1 deletion Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,6 @@ gem 'commento'
gem 'emailbutler'

# api serializer
gem 'jsonapi-serializer', '2.2.0'
gem 'oj'
gem 'panko_serializer'

Expand Down
3 changes: 0 additions & 3 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -226,8 +226,6 @@ GEM
json_spec (1.1.5)
multi_json (~> 1.0)
rspec (>= 2.0, < 4.0)
jsonapi-serializer (2.2.0)
activesupport (>= 4.2)
jwt (2.8.2)
base64
kudos (0.1.3)
Expand Down Expand Up @@ -528,7 +526,6 @@ DEPENDENCIES
foreman
jsbundling-rails (~> 1.0)
json_spec (= 1.1.5)
jsonapi-serializer (= 2.2.0)
jwt (~> 2.5)
kudos
letter_opener
Expand Down
8 changes: 7 additions & 1 deletion app/controllers/api/frontend/api_access_tokens_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ def create
case create_form.call(user: current_user)
in { errors: errors } then render json: { errors: errors }, status: :ok
in { result: result }
render json: { result: ApiAccessTokenSerializer.new(result).serializable_hash }, status: :ok
render json: Panko::Response.create { |response| json_response(response, result) }, status: :ok
end
end

Expand All @@ -22,6 +22,12 @@ def destroy

private

def json_response(response, result)
{
result: response.serializer(result, ApiAccessTokenSerializer)
}
end

def find_api_access_token
@api_access_token = current_user.api_access_tokens.find_by!(uuid: params[:id])
end
Expand Down
18 changes: 13 additions & 5 deletions app/controllers/api/frontend/excludes/groups_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,7 @@ def create
case create_form.call(company: @company, excludes_rules: create_excludes_rules_params)
in { errors: errors } then render json: { errors: errors }, status: :ok
in { result: result }
render json: {
result: ::Excludes::GroupSerializer.new(
result, params: { rules: excludes_rules(result.id) }
).serializable_hash
}, status: :ok
render json: Panko::Response.create { |response| json_response(response, result) }, status: :ok
end
end

Expand All @@ -30,6 +26,18 @@ def destroy

private

def json_response(response, result)
{
result: response.serializer(
result,
::Excludes::GroupSerializer,
context: {
rules: excludes_rules(result.id)
}
)
}
end

def find_company
@company = current_user.available_write_companies.find_by!(uuid: params[:company_id])
end
Expand Down
8 changes: 7 additions & 1 deletion app/controllers/api/frontend/ignores_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ def create
case create_form.call(company: @company, params: ignore_params)
in { errors: errors } then render json: { errors: errors }, status: :ok
in { result: result }
render json: { result: IgnoreSerializer.new(result).serializable_hash }, status: :ok
render json: Panko::Response.create { |response| json_response(response, result) }, status: :ok
end
end

Expand All @@ -25,6 +25,12 @@ def destroy

private

def json_response(response, result)
{
result: response.serializer(result, IgnoreSerializer)
}
end

def find_company
@company = current_user.available_write_companies.find_by!(uuid: params[:company_id])
end
Expand Down
9 changes: 8 additions & 1 deletion app/controllers/api/frontend/invites_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ def create
# commento: invites.email, invites.access
case create_form.call(inviteable: @inviteable, params: invite_params)
in { errors: errors } then render json: { errors: errors }, status: :ok
in { result: result } then render json: { result: InviteSerializer.new(result).serializable_hash }, status: :ok
in { result: result }
render json: Panko::Response.create { |response| json_response(response, result) }, status: :ok
end
end

Expand All @@ -24,6 +25,12 @@ def destroy

private

def json_response(response, result)
{
result: response.serializer(result, InviteSerializer)
}
end

def find_inviteable
@inviteable = params[:company_id] ? find_company : find_user

Expand Down
8 changes: 7 additions & 1 deletion app/controllers/api/frontend/notifications_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ def create
case create_form.call(notifyable: @company, webhook: @webhook, params: notification_params)
in { errors: errors } then render json: { errors: errors }, status: :ok
in { result: result }
render json: { result: NotificationSerializer.new(result).serializable_hash }, status: :ok
render json: Panko::Response.create { |response| json_response(response, result) }, status: :ok
end
end

Expand All @@ -26,6 +26,12 @@ def destroy

private

def json_response(response, result)
{
result: response.serializer(result, NotificationSerializer)
}
end

def find_company
@company = current_user.available_write_companies.find_by!(uuid: params[:company_id])
end
Expand Down
8 changes: 7 additions & 1 deletion app/controllers/api/frontend/vacations_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ def create
case create_form.call(user: current_user, params: vacation_params)
in { errors: errors } then render json: { errors: errors }, status: :ok
in { result: result }
render json: { result: VacationSerializer.new(result).serializable_hash }, status: :ok
render json: Panko::Response.create { |response| json_response(response, result) }, status: :ok
end
end

Expand All @@ -23,6 +23,12 @@ def destroy

private

def json_response(response, result)
{
result: response.serializer(result, VacationSerializer)
}
end

def find_vacation
@vacation = current_user.vacations.find(params[:id])
end
Expand Down
8 changes: 7 additions & 1 deletion app/controllers/api/frontend/webhooks_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ def create
case create_form.call(company: @company, params: webhook_params)
in { errors: errors } then render json: { errors: errors }, status: :ok
in { result: result }
render json: { result: WebhookSerializer.new(result).serializable_hash }, status: :ok
render json: Panko::Response.create { |response| json_response(response, result) }, status: :ok
end
end

Expand All @@ -25,6 +25,12 @@ def destroy

private

def json_response(response, result)
{
result: response.serializer(result, WebhookSerializer)
}
end

def find_company
@company = current_user.available_write_companies.find_by!(uuid: params[:company_id])
end
Expand Down
13 changes: 9 additions & 4 deletions app/controllers/companies/configurations_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,15 @@ def find_webhooks

def find_excludes_groups
@excludes_groups =
Excludes::GroupSerializer.new(
@company.excludes_groups.order(id: :desc),
params: { rules: excludes_rules }
).serializable_hash
JSON.parse(
Panko::ArraySerializer.new(
@company.excludes_groups.order(id: :desc),
each_serializer: Excludes::GroupSerializer,
context: {
rules: excludes_rules
}
).to_json
)
end

def excludes_rules
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/profiles_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ def find_end_time
end

def find_vacations
@vacations = VacationSerializer.new(current_user.vacations.order(start_time: :desc)).serializable_hash
@vacations = current_user.vacations.order(start_time: :desc).hashable_pluck(:id, :start_time, :end_time)
end

def find_identities
Expand Down
14 changes: 7 additions & 7 deletions app/javascript/components/Company/CompanyConfiguration.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ export const CompanyConfiguration = ({
inviteAccess: 'read',
errors: [],
notifications: notifications,
excludeGroups: excludeGroups.data,
excludeGroups: excludeGroups,
excludeRules: []
});

Expand All @@ -94,7 +94,7 @@ export const CompanyConfiguration = ({
else setPageState({
...pageState,
ingoreFormIsOpen: false,
ignores: pageState.ignores.concat(result.result.data.attributes),
ignores: pageState.ignores.concat(result.result),
entityValue: '',
errors: []
})
Expand Down Expand Up @@ -159,7 +159,7 @@ export const CompanyConfiguration = ({
else setPageState({
...pageState,
inviteFormIsOpen: false,
invites: pageState.invites.concat(result.result.data.attributes),
invites: pageState.invites.concat(result.result),
inviteEmail: '',
errors: []
})
Expand Down Expand Up @@ -264,7 +264,7 @@ export const CompanyConfiguration = ({
});
if (result.errors) setPageState({ ...pageState, errors: result.errors })
else {
const webhooks = pageState.webhooks.concat(result.result.data.attributes);
const webhooks = pageState.webhooks.concat(result.result);
const notificationWebhookUuid = webhooks.length === 1 ? webhooks[0].uuid : pageState.notificationWebhookUuid;
setPageState({
...pageState,
Expand Down Expand Up @@ -352,7 +352,7 @@ export const CompanyConfiguration = ({
else setPageState({
...pageState,
notificationFormIsOpen: false,
notifications: pageState.notifications.concat(result.result.data.attributes),
notifications: pageState.notifications.concat(result.result),
errors: []
});
};
Expand Down Expand Up @@ -433,7 +433,7 @@ export const CompanyConfiguration = ({
else setPageState({
...pageState,
excludeFormIsOpen: false,
excludeGroups: pageState.excludeGroups.concat(result.result.data),
excludeGroups: pageState.excludeGroups.concat(result.result),
excludeRules: [],
errors: []
})
Expand Down Expand Up @@ -495,7 +495,7 @@ export const CompanyConfiguration = ({
{pageState.excludeGroups.map((group) => (
<div className="zebra-list-element" key={group.id}>
<div className="flex flex-col">
{group.attributes.excludes_rules.map((excludeRule) => (
{group.excludes_rules.map((excludeRule) => (
<p key={excludeRule.uuid}>
{EXCLUDE_RULES_TARGETS[excludeRule.target]} {excludeRule.condition.split('_').join(' ')} {excludeRule.value}
</p>
Expand Down
9 changes: 5 additions & 4 deletions app/javascript/components/Profile/ProfileConfiguration.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export const ProfileConfiguration = ({
const [pageState, setPageState] = useState({
vacationFormIsOpen: false,
inviteFormIsOpen: false,
vacations: vacations.data.map((item) => item.attributes),
vacations: vacations,
acceptedInvites: acceptedInvites,
invites: invites,
apiAccessTokens: apiAccessTokens,
Expand All @@ -48,8 +48,9 @@ export const ProfileConfiguration = ({
});
if (result.errors) setPageState({ ...pageState, errors: result.errors })
else setPageState({
...pageState,
vacationFormIsOpen: false,
vacations: pageState.vacations.concat(result.result.data.attributes),
vacations: pageState.vacations.concat(result.result),
startTime: '',
endTime: '',
errors: []
Expand Down Expand Up @@ -114,7 +115,7 @@ export const ProfileConfiguration = ({
else setPageState({
...pageState,
inviteFormIsOpen: false,
invites: pageState.invites.concat(result.result.data.attributes),
invites: pageState.invites.concat(result.result),
inviteEmail: '',
errors: []
})
Expand Down Expand Up @@ -193,7 +194,7 @@ export const ProfileConfiguration = ({
if (result.errors) setPageState({ ...pageState, errors: result.errors })
else setPageState({
...pageState,
apiAccessTokens: pageState.apiAccessTokens.concat(result.result.data.attributes),
apiAccessTokens: pageState.apiAccessTokens.concat(result.result),
errors: []
})
};
Expand Down
9 changes: 1 addition & 8 deletions app/serializers/application_serializer.rb
Original file line number Diff line number Diff line change
@@ -1,11 +1,4 @@
# frozen_string_literal: true

class ApplicationSerializer
include JSONAPI::Serializer

set_id { SecureRandom.hex }

def self.required_field?(params, field_name)
params[:include_fields]&.include?(field_name) || params[:exclude_fields]&.exclude?(field_name)
end
class ApplicationSerializer < Panko::Serializer
end
2 changes: 1 addition & 1 deletion app/serializers/company_serializer.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# frozen_string_literal: true

class CompanySerializer < PankoApplicationSerializer
class CompanySerializer < ApplicationSerializer
ATTRIBUTES = %w[uuid title repositories_count accessable].freeze

attributes :uuid, :title, :repositories_count, :accessable
Expand Down
6 changes: 3 additions & 3 deletions app/serializers/excludes/group_serializer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@

module Excludes
class GroupSerializer < ApplicationSerializer
set_id :uuid
attributes :uuid, :excludes_rules

attribute :excludes_rules do |object, params|
params[:rules][object.uuid].presence || []
def excludes_rules
context[:rules][object.uuid].presence || []
end
end
end
2 changes: 1 addition & 1 deletion app/serializers/insight_serializer.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# frozen_string_literal: true

class InsightSerializer < PankoApplicationSerializer
class InsightSerializer < ApplicationSerializer
include Helpers::Insight

attributes :id, :values, :entity
Expand Down
4 changes: 2 additions & 2 deletions app/serializers/notification_serializer.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
# frozen_string_literal: true

class NotificationSerializer < ApplicationSerializer
attributes :uuid, :notification_type
attributes :uuid, :notification_type, :webhooks_uuid

attribute :webhooks_uuid do |object|
def webhooks_uuid
object.webhook.uuid
end
end
4 changes: 0 additions & 4 deletions app/serializers/panko_application_serializer.rb

This file was deleted.

Loading

0 comments on commit ed8a4bb

Please sign in to comment.