Class: ForestAdminAgent::Routes::Charts::ApiChartCollection

Inherits:
AbstractAuthenticatedRoute show all
Includes:
Utils
Defined in:
lib/forest_admin_agent/routes/charts/api_chart_collection.rb

Instance Method Summary collapse

Methods inherited from AbstractAuthenticatedRoute

#build, #format_attributes

Methods inherited from AbstractRoute

#add_route, #build, #routes

Constructor Details

#initialize(collection, chart_name) ⇒ ApiChartCollection

Returns a new instance of ApiChartCollection.



10
11
12
13
14
15
# File 'lib/forest_admin_agent/routes/charts/api_chart_collection.rb', line 10

def initialize(collection, chart_name)
  @chart_name = chart_name
  @route_collection = collection

  super()
end

Instance Method Details

#handle_api_chart(args) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/forest_admin_agent/routes/charts/api_chart_collection.rb', line 46

def handle_api_chart(args)
  context = build(args)
  {
    content: Serializer::ForestChartSerializer.serialize(
      context.collection.render_chart(
        context.caller,
        @chart_name,
        Id.unpack_id(context.collection, args[:params]['record_id'])
      )
    )
  }
end

#handle_smart_chart(args) ⇒ Object



59
60
61
62
63
64
65
66
67
68
# File 'lib/forest_admin_agent/routes/charts/api_chart_collection.rb', line 59

def handle_smart_chart(args)
  context = build(args)
  {
    content: context.collection.render_chart(
      context.caller,
      @chart_name,
      Id.unpack_id(context.collection, args[:params]['record_id'])
    )
  }
end

#setup_routesObject



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
# File 'lib/forest_admin_agent/routes/charts/api_chart_collection.rb', line 17

def setup_routes
  # Mount both GET and POST, respectively for smart and api charts.
  collection_name = @route_collection.name
  slug = @chart_name.parameterize

  add_route(
    "forest_chart_#{collection_name}_get_#{slug}",
    'get',
    "/_charts/:collection_name/#{slug}",
    proc { |args| handle_smart_chart(args) }
  )

  add_route(
    "forest_chart_#{collection_name}_post_#{slug}",
    'post',
    "/_charts/:collection_name/#{slug}",
    proc { |args| handle_api_chart(args) }
  )

  unless Facades::Container.cache(:is_production)
    Facades::Container.logger.log(
      'Debug',
      "Chart #{@chart_name} was mounted at /forest/_charts/#{collection_name}/#{slug}"
    )
  end

  self
end