Class: Decidim::Proposals::ProposalSerializer

Inherits:
Exporters::Serializer
  • Object
show all
Includes:
ApplicationHelper, ResourceHelper, TranslationsHelper, HtmlToPlainText
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.



14
15
16
# File 'lib/decidim/proposals/proposal_serializer.rb', line 14

def initialize(proposal)
  @proposal = proposal
end

Instance Method Details

#serializeObject

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



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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/decidim/proposals/proposal_serializer.rb', line 19

def serialize
  {
    id: proposal.id,
    author: {
      **author_fields
    },
    category: {
      id: proposal.category.try(:id),
      name: proposal.category.try(:name) || empty_translatable
    },
    scope: {
      id: proposal.scope.try(:id),
      name: proposal.scope.try(:name) || empty_translatable
    },
    participatory_space: {
      id: proposal.participatory_space.id,
      url: Decidim::ResourceLocatorPresenter.new(proposal.participatory_space).url
    },
    component: { id: component.id },
    title: proposal.title,
    body: convert_to_plain_text(proposal.body),
    address: proposal.address,
    latitude: proposal.latitude,
    longitude: proposal.longitude,
    state: proposal.state.to_s,
    reference: proposal.reference,
    answer: ensure_translatable(proposal.answer),
    answered_at: proposal.answered_at,
    votes: proposal.proposal_votes_count,
    endorsements: {
      total_count: proposal.endorsements.size,
      user_endorsements:
    },
    comments: proposal.comments_count,
    attachments: proposal.attachments.size,
    followers: proposal.follows.size,
    published_at: proposal.published_at,
    url:,
    meeting_urls: meetings,
    related_proposals:,
    is_amend: proposal.emendation?,
    original_proposal: {
      title: proposal&.amendable&.title,
      url: original_proposal_url
    },
    withdrawn: proposal.withdrawn?,
    withdrawn_at: proposal.withdrawn_at
  }
end