class Decidim::EnhancedTextwork::ParagraphSerializer

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

Attributes

paragraph[R]
resource[R]

Public Class Methods

new(paragraph) click to toggle source

Public: Initializes the serializer with a paragraph.

# File lib/decidim/enhanced_textwork/paragraph_serializer.rb, line 13
def initialize(paragraph)
  @paragraph = paragraph
end

Public Instance Methods

serialize() click to toggle source

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

# 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

Private Instance Methods

component() click to toggle source
# File lib/decidim/enhanced_textwork/paragraph_serializer.rb, line 64
def component
  paragraph.component
end
meetings() click to toggle source
# File lib/decidim/enhanced_textwork/paragraph_serializer.rb, line 68
def meetings
  paragraph.linked_resources(:meetings, "paragraphs_from_meeting").map do |meeting|
    Decidim::ResourceLocatorPresenter.new(meeting).url
  end
end
original_paragraph_url() click to toggle source
# File lib/decidim/enhanced_textwork/paragraph_serializer.rb, line 88
def original_paragraph_url
  return unless paragraph.emendation? && paragraph.amendable.present?

  Decidim::ResourceLocatorPresenter.new(paragraph.amendable).url
end
url() click to toggle source
# File lib/decidim/enhanced_textwork/paragraph_serializer.rb, line 80
def url
  Decidim::ResourceLocatorPresenter.new(paragraph).url
end
user_endorsements() click to toggle source
# File lib/decidim/enhanced_textwork/paragraph_serializer.rb, line 84
def user_endorsements
  paragraph.endorsements.for_listing.map { |identity| identity.normalized_author&.name }
end