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



6
7
8
9
10
11
12
13
14
# File 'lib/standard_api/controller.rb', line 6

def self.included(klass)
  klass.helper_method :includes, :orders, :model, :models, :resource_limit,
    :default_limit
  klass.before_action :set_standardapi_headers
  klass.rescue_from StandardAPI::ParameterMissing, with: :bad_request
  klass.rescue_from StandardAPI::UnpermittedParameters, with: :bad_request
  klass.append_view_path(File.join(File.dirname(__FILE__), 'views'))
  klass.extend(ClassMethods)
end

Instance Method Details

#add_resourceObject



125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/standard_api/controller.rb', line 125

def add_resource
  resource = resources.find(params[:id])
  association = resource.association(params[:relationship])
  subresource = association.klass.find_by_id(params[:resource_id])

  if(subresource)
    if association.is_a? ActiveRecord::Associations::HasManyAssociation
      result = resource.send(params[:relationship]) << subresource
    else
      result = resource.send("#{params[:relationship]}=", subresource)
    end
    head result ? :created : :bad_request
  else
    head :not_found
  end

end

#calculateObject



37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/standard_api/controller.rb', line 37

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



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/standard_api/controller.rb', line 59

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



144
145
146
# File 'lib/standard_api/controller.rb', line 144

def current_mask
  @current_mask ||= {}
end

#destroyObject



103
104
105
106
# File 'lib/standard_api/controller.rb', line 103

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

#indexObject



32
33
34
35
# File 'lib/standard_api/controller.rb', line 32

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

#newObject



55
56
57
# File 'lib/standard_api/controller.rb', line 55

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

#remove_resourceObject



108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/standard_api/controller.rb', line 108

def remove_resource
  resource = resources.find(params[:id])
  association = resource.association(params[:relationship])
  subresource = association.klass.find_by_id(params[:resource_id])

  if(subresource)
    if association.is_a? ActiveRecord::Associations::HasManyAssociation
      resource.send(params[:relationship]).delete(subresource)
    else
      resource.send("#{params[:relationship]}=", nil)
    end
    head :no_content
  else
    head :not_found
  end
end

#schemaObject



27
28
29
# File 'lib/standard_api/controller.rb', line 27

def schema
  Rails.application.eager_load! if !Rails.application.config.eager_load
end

#showObject



50
51
52
53
# File 'lib/standard_api/controller.rb', line 50

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

#tablesObject



16
17
18
19
20
21
22
23
24
# File 'lib/standard_api/controller.rb', line 16

def tables
  Rails.application.eager_load! if !Rails.application.config.eager_load

  tables = ApplicationController.descendants
  tables.select! { |c| c.ancestors.include?(self.class) && c != self.class }
  tables.map!(&:model).compact!
  tables.map!(&:table_name)
  render json: tables
end

#updateObject



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/standard_api/controller.rb', line 83

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