Class: Exposed::Base

Inherits:
ActionController::Base
  • Object
show all
Defined in:
app/controllers/exposed/base.rb

Instance Method Summary collapse

Constructor Details

#initializeBase

Returns a new instance of Base.



8
9
10
11
12
13
14
15
# File 'app/controllers/exposed/base.rb', line 8

def initialize
  # E.g. 'UsersController' will yield 'User'
  model_name = self.class.name.sub("Controller", "").singularize
  # The model class, should extend ActiveRecord::Base
  @model     = model_name.constantize
  # The model symbol, used to extract parameters
  @model_sym = model_name.downcase.to_sym
end

Instance Method Details

#createObject

POST /collection



31
32
33
34
35
# File 'app/controllers/exposed/base.rb', line 31

def create
  record = @model.new(params[@model_sym])
  record.save!
  render_response record
end

#destroyObject

DELETE /collection/id



45
46
47
# File 'app/controllers/exposed/base.rb', line 45

def destroy
  render_response @model.destroy(params[:id])
end

#indexObject

GET /collection



18
19
20
21
22
# File 'app/controllers/exposed/base.rb', line 18

def index
  filters = params.except(:controller, :action, :include, :exclude)
  collection = @model.where(filters).includes(@include)
  render_response collection
end

#showObject

GET /collection/id



25
26
27
28
# File 'app/controllers/exposed/base.rb', line 25

def show
  record = @model.find(params[:id])
  render_response record
end

#updateObject

PUT /collection/id



38
39
40
41
42
# File 'app/controllers/exposed/base.rb', line 38

def update
  record = @model.find(params[:id])
  record.update_attributes!(params[@model_sym])
  render_response record
end