Class: Decidim::Plans::PlanSerializer

Inherits:
Exporters::Serializer
  • Object
show all
Includes:
ApplicationHelper, ResourceHelper
Defined in:
lib/decidim/plans/plan_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(plan) ⇒ PlanSerializer

Public: Initializes the serializer with a plan.



12
13
14
# File 'lib/decidim/plans/plan_serializer.rb', line 12

def initialize(plan)
  @plan = plan
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
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/decidim/plans/plan_serializer.rb', line 17

def serialize
  values = {
    id: plan.id,
    authors: author_details,
    category: {
      id: plan.category.try(:id),
      name: plan.category.try(:name)
    },
    scope: {
      id: plan.scope.try(:id),
      name: plan.scope.try(:name)
    },
    participatory_space: {
      id: plan.participatory_space.id,
      url: Decidim::ResourceLocatorPresenter.new(plan.participatory_space).url
    },
    component: { id: component.id },
    state: plan.state.to_s,
    comments: plan.comments.count,
    attachments: plan.attachments.count,
    followers: plan.followers.count,
    published_at: plan.published_at,
    closed_at: plan.closed_at,
    url: url,
    related_proposals: {
      ids: related_proposal_ids,
      urls: related_proposal_urls
    },
    title: plan.title
  }

  # Add section content
  plan.sections.each do |sect|
    content = plan.contents.find_by(section: sect)
    values["section_#{sect.id}".to_sym] = {
      title: sect.body,
      value: content.try(:body)
    }
  end

  values
end