Class: Decidim::EnhancedTextwork::ParagraphSerializer

Inherits:
Decidim::Exporters::Serializer
  • Object
show all
Includes:
ApplicationHelper, ResourceHelper, TranslationsHelper
Defined in:
lib/decidim/enhanced_textwork/paragraph_serializer.rb

Overview

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

Instance Method Summary collapse

Constructor Details

#initialize(paragraph) ⇒ ParagraphSerializer

Public: Initializes the serializer with a paragraph.



13
14
15
# File 'lib/decidim/enhanced_textwork/paragraph_serializer.rb', line 13

def initialize(paragraph)
  @paragraph = paragraph
end

Instance Method Details

#serializeObject

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



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
# File 'lib/decidim/enhanced_textwork/paragraph_serializer.rb', line 18

def serialize
  {
    id: paragraph.id,
    category: {
      id: paragraph.category.try(:id),
      name: paragraph.category.try(:name) || empty_translatable
    },
    scope: {
      id: paragraph.scope.try(:id),
      name: paragraph.scope.try(:name) || empty_translatable
    },
    participatory_space: {
      id: paragraph.participatory_space.id,
      url: Decidim::ResourceLocatorPresenter.new(paragraph.participatory_space).url
    },
    component: { id: component.id },
    title: paragraph.title,
    body: paragraph.body,
    state: paragraph.state.to_s,
    reference: paragraph.reference,
    answer: ensure_translatable(paragraph.answer),
    supports: paragraph.paragraph_votes_count,
    endorsements: {
      total_count: paragraph.endorsements.count,
      user_endorsements: user_endorsements
    },
    comments: paragraph.comments_count,
    attachments: paragraph.attachments.count,
    followers: paragraph.followers.count,
    published_at: paragraph.published_at,
    url: url,
    meeting_urls: meetings,
    related_paragraphs: related_paragraphs,
    is_amend: paragraph.emendation?,
    original_paragraph: {
      title: paragraph&.amendable&.title,
      url: original_paragraph_url
    }
  }
end