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
11
# 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.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



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

def add_resource
  resource = resources.find(params[:id])
  
  subresource_class = resource.association(params[:relationship]).klass
  subresource = subresource_class.find_by_id(params[:resource_id])
  if(subresource)
    result = resource.send(params[:relationship]) << subresource
    head result ? :created : :bad_request
  else
    head :not_found
  end
  
end

#calculateObject



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

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



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

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



127
128
129
# File 'lib/standard_api/controller.rb', line 127

def current_mask
  @current_mask ||= {}
end

#destroyObject



94
95
96
97
# File 'lib/standard_api/controller.rb', line 94

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

#indexObject



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

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

#newObject



46
47
48
# File 'lib/standard_api/controller.rb', line 46

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

#remove_resourceObject



99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/standard_api/controller.rb', line 99

def remove_resource
  resource = resources.find(params[:id])
  subresource_class = resource.association(params[:relationship]).klass
  subresource = subresource_class.find_by_id(params[:resource_id])
  
  if(subresource)
    result = resource.send(params[:relationship]).delete(subresource)
    head result ? :no_content : :bad_request
  else
    head :not_found
  end
end

#showObject



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

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

#tablesObject



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

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

  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



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

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