Module: AzaharaSchema::ControllerAdditions

Defined in:
lib/azahara_schema/controller_additions.rb

Instance Method Summary collapse

Instance Method Details

#azahara_schema_api_index(**options, &block) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/azahara_schema/controller_additions.rb', line 30

def azahara_schema_api_index(**options, &block)
  azahara_schema_for_index(**options, &block)
  respond_to do |format|
    format.json {
      json_result = {}
      if params['_type'] == 'count'
        json_result = {count: @resource_schema.entity_count}
      else
        json_result = {entities: @resource_schema, count: @resource_schema.entity_count}
      end
      render json: json_result
    }
  end
end

#azahara_schema_index(**options, &block) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/azahara_schema/controller_additions.rb', line 4

def azahara_schema_index(**options, &block)
  azahara_schema_for_index(**options, &block)
  respond_to do |format|
    format.html
    format.json {
      json_result = {}
      if params['_type'] == 'query'
        json_result[:results] = @resource_schema.entities.collect do |o|
            {id: o.id, text: o.to_s}
          end
      elsif params['_type'] == 'count'
        json_result = {count: @resource_schema.entity_count}
      else
        json_result = {entities: @resource_schema, count: @resource_schema.entity_count}
      end
      render json: json_result
    }
    format.csv {
      require 'csv'
      headers['Content-Disposition'] = "attachment; filename=\"#{@resource_schema.model.model_name.human(count: :other)}.csv\""
      headers['Content-Type'] ||= 'text/csv'
      render @resource_schema.csv_template, layout: false
    }
  end
end