class Decidim::Plans::PlanSerializer

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

Attributes

plan[R]

Public Class Methods

new(plan) click to toggle source

Public: Initializes the serializer with a plan.

# File lib/decidim/plans/plan_serializer.rb, line 12
def initialize(plan)
  @plan = plan
end

Public Instance Methods

serialize() click to toggle source

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

# 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

Private Instance Methods

author_details() click to toggle source
# File lib/decidim/plans/plan_serializer.rb, line 64
def author_details
  plan.authors.map do |author|
    "#{author.class}/#{author.id}"
  end
end
component() click to toggle source
# File lib/decidim/plans/plan_serializer.rb, line 70
def component
  plan.component
end
url() click to toggle source
# File lib/decidim/plans/plan_serializer.rb, line 88
def url
  Decidim::ResourceLocatorPresenter.new(plan).url
end