Module: StandardAPI

Defined in:
lib/standard_api.rb,
lib/standard_api/orders.rb,
lib/standard_api/includes.rb,
lib/standard_api/test_case/show_tests.rb,
lib/standard_api/test_case/index_tests.rb,
lib/standard_api/test_case/schema_test.rb,
lib/standard_api/test_case/create_tests.rb,
lib/standard_api/test_case/update_tests.rb,
lib/standard_api/test_case/destroy_tests.rb,
lib/standard_api/test_case/calculate_tests.rb

Defined Under Namespace

Modules: ClassMethods, Includes, Orders, TestCase

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(klass) ⇒ Object



20
21
22
23
24
25
# File 'lib/standard_api.rb', line 20

def self.included(klass)
  klass.hide_action :current_mask
  klass.helper_method :includes, :orders, :model
  klass.append_view_path(File.join(File.dirname(__FILE__), 'standard_api', 'views'))
  klass.extend(ClassMethods)
end

Instance Method Details

#calculateObject



44
45
46
47
48
49
50
51
52
53
# File 'lib/standard_api.rb', line 44

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



60
61
62
63
64
# File 'lib/standard_api.rb', line 60

def create
  @record = model.new(model_params)
  instance_variable_set("@#{model.model_name.singular}", @record)
  render :show, status: @record.save ? :created : :bad_request
end

#current_maskObject

Override if you want to support masking



78
79
80
# File 'lib/standard_api.rb', line 78

def current_mask
  @current_mask ||= {}
end

#destroyObject



72
73
74
75
# File 'lib/standard_api.rb', line 72

def destroy
  resources.find(params[:id]).destroy!
  render nothing: true, status: :no_content
end

#indexObject



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

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

#pingObject



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

def ping
  render :text => 'pong'
end

#showObject



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

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

#tablesObject



31
32
33
34
35
36
37
# File 'lib/standard_api.rb', line 31

def tables
  controllers = 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!.map!(&:table_name)
  
  render json: controllers
end

#updateObject



66
67
68
69
70
# File 'lib/standard_api.rb', line 66

def update
  @record = resources.find(params[:id])
  instance_variable_set("@#{model.model_name.singular}", @record)
  render :show, status: @record.update_attributes(model_params) ? :ok : :bad_request
end