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
# File 'lib/standard_api/controller.rb', line 4

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

Instance Method Details

#calculateObject



23
24
25
26
27
28
29
30
31
32
# File 'lib/standard_api/controller.rb', line 23

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
  render json: @calculations
end

#createObject



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/standard_api/controller.rb', line 42

def create
  record = model.new(model_params)
  instance_variable_set("@#{model.model_name.singular}", record)
  
  if record.save
    if request.format == :html
      redirect_to record
    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



82
83
84
# File 'lib/standard_api/controller.rb', line 82

def current_mask
  @current_mask ||= {}
end

#destroyObject



76
77
78
79
# File 'lib/standard_api/controller.rb', line 76

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

#indexObject



19
20
21
# File 'lib/standard_api/controller.rb', line 19

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

#newObject



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

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

#showObject



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

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

#tablesObject



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

def tables
  controllers = ApplicationController.descendants# Dir[Rails.root.join('app/controllers/*_controller.rb')].map{ |path| path.match(/(\w+)_controller.rb/)[1].camelize+"Controller" }.map(&:safe_constantize)
  controllers.select! { |c| c.ancestors.include?(self.class) && c != self.class }
  controllers.map!(&:model).compact!
  controllers.map!(&:table_name)
  render json: controllers
end

#updateObject



61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/standard_api/controller.rb', line 61

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

  if record.update_attributes(model_params)
    if request.format == :html
      redirect_to record
    else
      render :show, status: :ok
    end
  else
    render :show, status: :bad_request
  end
end