Module: StandardAPI::Controller

Defined in:
lib/standard_api/controller.rb

Defined Under Namespace

Modules: ClassMethods

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(klass) ⇒ Object



4
5
6
7
8
9
10
# File 'lib/standard_api/controller.rb', line 4

def self.included(klass)
  klass.helper_method :includes, :orders, :model, :resource_limit,
    :default_limit, :preloadables
  klass.before_action :set_standardapi_headers
  klass.append_view_path(File.join(File.dirname(__FILE__), 'views'))
  klass.extend(ClassMethods)
end

Instance Method Details

#calculateObject



26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/standard_api/controller.rb', line 26

def calculate
  @calculations = resources.reorder(nil).pluck(*calculate_selects).map do |c|
    if c.is_a?(Array)
      c.map { |v| v.is_a?(BigDecimal) ? v.to_f : v }
    else
      c.is_a?(BigDecimal) ? c.to_f : c
    end
  end
  @calculations = Hash[@calculations] if @calculations[0].is_a?(Array) && params[:group_by]
  
  render json: @calculations
end

#createObject



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/standard_api/controller.rb', line 47

def create
  record = model.new(model_params)
  instance_variable_set("@#{model.model_name.singular}", record)

  if record.save
    if request.format == :html
      redirect_to url_for(
        controller: record.class.base_class.model_name.collection,
        action: 'show',
        id: record.id,
        only_path: true
      )
    else
      render :show, status: :created
    end
  else
    if request.format == :html
      render :edit, status: :bad_request
    else
      render :show, status: :bad_request
    end
  end
end

#current_maskObject

Override if you want to support masking



97
98
99
# File 'lib/standard_api/controller.rb', line 97

def current_mask
  @current_mask ||= {}
end

#destroyObject



91
92
93
94
# File 'lib/standard_api/controller.rb', line 91

def destroy
  resources.find(params[:id]).destroy!
  head :no_content
end

#indexObject



22
23
24
# File 'lib/standard_api/controller.rb', line 22

def index
  instance_variable_set("@#{model.model_name.plural}", resources.limit(limit).offset(params[:offset]).sort(orders))
end

#newObject



43
44
45
# File 'lib/standard_api/controller.rb', line 43

def new
  instance_variable_set("@#{model.model_name.singular}", model.new) if model
end

#showObject



39
40
41
# File 'lib/standard_api/controller.rb', line 39

def show
  instance_variable_set("@#{model.model_name.singular}", resources.find(params[:id]))
end

#tablesObject



12
13
14
15
16
17
18
19
20
# File 'lib/standard_api/controller.rb', line 12

def tables
  Rails.application.eager_load! if Rails.env == 'development'.freeze
  
  controllers = ApplicationController.descendants
  controllers.select! { |c| c.ancestors.include?(self.class) && c != self.class }
  controllers.map!(&:model).compact!
  controllers.map!(&:table_name)
  render json: controllers
end

#updateObject



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/standard_api/controller.rb', line 71

def update
  record = resources.find(params[:id])
  instance_variable_set("@#{model.model_name.singular}", record)

  if record.update(model_params)
    if request.format == :html
      redirect_to url_for(
        controller: record.class.base_class.model_name.collection,
        action: 'show',
        id: record.id,
        only_path: true
      )
    else
      render :show, status: :ok
    end
  else
    render :show, status: :bad_request
  end
end