Class: ProjectPresenter

Inherits:
Object
  • Object
show all
Includes:
UrlHelper
Defined in:
app/presenters/project_presenter.rb

Instance Method Summary collapse

Methods included from UrlHelper

#feature_path, #link_to_project_feature

Constructor Details

#initialize(projects) ⇒ ProjectPresenter

Returns a new instance of ProjectPresenter.



4
5
6
# File 'app/presenters/project_presenter.rb', line 4

def initialize(projects)
  @projects = OneOrMany.new(projects)
end

Instance Method Details

#as_json(*args) ⇒ Object



8
9
10
11
12
13
14
15
16
# File 'app/presenters/project_presenter.rb', line 8

def as_json(*args)
  projects = @projects
  projects = Houston.benchmark "[#{self.class.name.underscore}] Load objects" do
    projects.load
  end if projects.is_a?(ActiveRecord::Relation)
  Houston.benchmark "[#{self.class.name.underscore}] Prepare JSON" do
    projects.map(&method(:project_to_json))
  end
end

#project_to_json(project) ⇒ Object



18
19
20
21
22
23
24
25
26
# File 'app/presenters/project_presenter.rb', line 18

def project_to_json(project)
  { id: project.id,
    name: project.name,
    slug: project.slug,
    color: {
      name: project.color.name,
      hex: project.color.hex },
    props: project.props.to_h }
end