-
Notifications
You must be signed in to change notification settings - Fork 468
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
A way to extend an exiting API via concern
Sometimes, it's needed to extend an existing controller method with additional parameters (usually when extending exiting API from plugins/rails engines). The concern can be also used for this purposed, using `update_api` method. The params defined in this block are merged with the params of the original method in the controller this concern is included to. ```ruby module Concerns module OauthConcern extend Apipie::DSL::Concern update_api(:create, :update) do param :user, Hash do param :oauth, String, :desc => 'oauth param' end end end end ``` The concern needs to be included to the controller after the methods are defined (either at the end of the class, or by using `Controller.send(:include, Concerns::OauthConcern)`.
- Loading branch information
Showing
8 changed files
with
149 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
require "spec_helper" | ||
|
||
describe ExtendedController do | ||
|
||
it 'should include params from both original controller and extending concern' do | ||
user_param = Apipie["extended#create"].params[:user] | ||
expect(user_param.validator.params_ordered.map(&:name)).to eq [:name, :password, :from_concern] | ||
end | ||
end | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
module Concerns | ||
module ExtendingConcern | ||
extend Apipie::DSL::Concern | ||
|
||
update_api(:create) do | ||
param :user, Hash do | ||
param :from_concern, String, :desc => 'param from concern', :allow_nil => false | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
class ExtendedController < ApplicationController | ||
|
||
api :POST, '/extended' | ||
param :user, Hash do | ||
param :name, String | ||
param :password, String | ||
end | ||
def create | ||
end | ||
|
||
include Concerns::ExtendingConcern | ||
end |