Class: Exposed::Base
- Inherits:
-
ActionController::Base
- Object
- ActionController::Base
- Exposed::Base
- Defined in:
- app/controllers/exposed/base.rb
Instance Method Summary collapse
-
#create ⇒ Object
POST /collection.
-
#destroy ⇒ Object
DELETE /collection/id.
-
#index ⇒ Object
GET /collection.
-
#initialize ⇒ Base
constructor
A new instance of Base.
-
#show ⇒ Object
GET /collection/id.
-
#update ⇒ Object
PUT /collection/id.
Constructor Details
#initialize ⇒ Base
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
#create ⇒ Object
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 |
#destroy ⇒ Object
DELETE /collection/id
45 46 47 |
# File 'app/controllers/exposed/base.rb', line 45 def destroy render_response @model.destroy(params[:id]) end |
#index ⇒ Object
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 |
#show ⇒ Object
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 |
#update ⇒ Object
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 |