Class: Decidim::Exporters::Serializer

Inherits:
Object
  • Object
show all
Defined in:
decidim-core/app/serializers/decidim/exporters/serializer.rb

Overview

This is an abstract class with a very naive default implementation for the exporters to use. It can also serve as a superclass of your own implementation.

It is used to be run against each element of an exportable collection in order to extract relevant fields. Every export should specify their own serializer or this default will be used.

Direct Known Subclasses

Accountability::ResultSerializer, Blogs::SchemaOrgBlogPostingPostSerializer, Budgets::DownloadYourDataBudgetsOrderSerializer, Budgets::ProjectSerializer, Comments::CommentSerializer, Comments::CommentVoteSerializer, Conferences::ConferenceRegistrationSerializer, Conferences::DownloadYourDataConferenceInviteSerializer, Conferences::DownloadYourDataConferenceRegistrationSerializer, Debates::DebateSerializer, Debates::DownloadYourDataDebateSerializer, DownloadYourDataSerializers::DownloadYourDataConversationSerializer, DownloadYourDataSerializers::DownloadYourDataFollowSerializer, DownloadYourDataSerializers::DownloadYourDataIdentitySerializer, DownloadYourDataSerializers::DownloadYourDataNotificationSerializer, DownloadYourDataSerializers::DownloadYourDataParticipatorySpacePrivateUserSerializer, DownloadYourDataSerializers::DownloadYourDataReportSerializer, DownloadYourDataSerializers::DownloadYourDataUserGroupSerializer, DownloadYourDataSerializers::DownloadYourDataUserSerializer, OpenDataBlockedUserSerializer, OpenDataModerationSerializer, OpenDataUserGroupSerializer, OpenDataUserSerializer, ParticipatorySpaceComponentsSerializer, ParticipatorySpaceSerializer, Forms::DownloadYourDataUserAnswersSerializer, Forms::UserAnswersSerializer, Meetings::DownloadYourDataInviteSerializer, Meetings::DownloadYourDataRegistrationSerializer, Meetings::DownloadYourDataUserAnswersSerializer, Meetings::MeetingSerializer, Meetings::RegistrationSerializer, Meetings::SchemaOrgEventMeetingSerializer, Meetings::UserAnswersSerializer, Pages::DataSerializer, Proposals::ProposalSerializer, SchemaOrgBreadcrumbListSerializer, Surveys::DataSerializer

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(resource) ⇒ Serializer

Initializes the serializer with a resource.

resource - The Object to serialize.



18
19
20
# File 'decidim-core/app/serializers/decidim/exporters/serializer.rb', line 18

def initialize(resource)
  @resource = resource
end

Instance Attribute Details

#resourceObject (readonly)

Returns the value of attribute resource.



13
14
15
# File 'decidim-core/app/serializers/decidim/exporters/serializer.rb', line 13

def resource
  @resource
end

Instance Method Details

#event_nameObject

Public: Converts serializers class name to event name.

For example: Decidim::Budgets::ProjectSerializer -> “decidim.serialize.budgets.project_serializer”

Returns String



54
55
56
# File 'decidim-core/app/serializers/decidim/exporters/serializer.rb', line 54

def event_name
  ActiveSupport::Inflector.underscore(self.class.to_s).sub("/", ".serialize.").gsub("/", ".")
end

#finalize(serialized_data) ⇒ Object

Public: Publishes notification (event) so that subscribers can modify serialized data.

serialized_data - Hash with the serialized data for this resource.

Returns a nested Hash with the fields by default.



39
40
41
42
43
44
45
46
47
# File 'decidim-core/app/serializers/decidim/exporters/serializer.rb', line 39

def finalize(serialized_data)
  event_data = {
    resource:,
    serialized_data:
  }
  ActiveSupport::Notifications.publish(event_name, event_data)

  event_data[:serialized_data]
end

#runObject

Publishes a serialize event and returns serialized hash by default (can be customized at the event).



23
24
25
# File 'decidim-core/app/serializers/decidim/exporters/serializer.rb', line 23

def run
  finalize(serialize)
end

#serializeObject

Public: Returns a serialized view of the provided resource.

Returns a nested Hash with the fields.



30
31
32
# File 'decidim-core/app/serializers/decidim/exporters/serializer.rb', line 30

def serialize
  @resource.to_h
end