Class: Decidim::Proposals::ProposalSerializer

Inherits:
Exporters::Serializer
  • Object
show all
Includes:
ApplicationHelper, ResourceHelper
Defined in:
lib/decidim/proposals/proposal_serializer.rb

Overview

This class serializes a Proposal so can be exported to CSV, JSON or other formats.

Instance Method Summary collapse

Constructor Details

#initialize(proposal) ⇒ ProposalSerializer

Public: Initializes the serializer with a proposal.



12
13
14
# File 'lib/decidim/proposals/proposal_serializer.rb', line 12

def initialize(proposal)
  @proposal = proposal
end

Instance Method Details

#serializeObject

Public: Exports a hash with the serialized data for this proposal.



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/decidim/proposals/proposal_serializer.rb', line 17

def serialize
  {
    id: proposal.id,
    category: {
      id: proposal.category.try(:id),
      name: proposal.category.try(:name)
    },
    scope: {
      id: proposal.scope.try(:id),
      name: proposal.scope.try(:name)
    },
    participatory_space: {
      id: proposal.participatory_space.id,
      url: Decidim::ResourceLocatorPresenter.new(proposal.participatory_space).url
    },
    component: { id: component.id },
    title: present(proposal).title,
    body: present(proposal).body,
    state: proposal.state.to_s,
    reference: proposal.reference,
    supports: proposal.proposal_votes_count,
    endorsements: proposal.endorsements.count,
    comments: proposal.comments.count,
    attachments: proposal.attachments.count,
    followers: proposal.followers.count,
    published_at: proposal.published_at,
    url: url,
    meeting_urls: meetings,
    related_proposals: related_proposals
  }
end