Skip to content

Commit

Permalink
Merge pull request #322 from iNecas/api-from-routes-concerns
Browse files Browse the repository at this point in the history
Support the api! keyword from concerns.
  • Loading branch information
iNecas committed Jan 22, 2015
2 parents 01050f6 + 55de157 commit 7b25a9a
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 20 deletions.
4 changes: 2 additions & 2 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -425,7 +425,7 @@ There are some default substitutions available:

:controller_path
value of ``controller.controller_path``, e.g. ``api/users`` for
``Api::UsersController``
``Api::UsersController``. Only if not using the ``api!`` keyword.

:resource_id
Apipie identifier of the resource, e.g. ``users`` for
Expand All @@ -445,7 +445,7 @@ Example
# ...
end
api :GET, '/:resource_id/:id', 'Show a :resource'
api! 'Show a :resource'
def show
# ...
end
Expand Down
15 changes: 1 addition & 14 deletions lib/apipie/dsl_definition.rb
Original file line number Diff line number Diff line change
Expand Up @@ -376,20 +376,7 @@ def method_added(method_name) #:doc:
super
return if !Apipie.active_dsl? || !_apipie_dsl_data[:api]

if _apipie_dsl_data[:api_from_routes]
desc = _apipie_dsl_data[:api_from_routes][:desc]
options = _apipie_dsl_data[:api_from_routes][:options]

api_from_routes = Apipie.routes_for_action(self, method_name, {:desc => desc, :options => options}).map do |route_info|
[route_info[:verb],
route_info[:path],
route_info[:desc],
(route_info[:options] || {}).merge(:from_routes => true)]
end
_apipie_dsl_data[:api_args].concat(api_from_routes)
end

return if _apipie_dsl_data[:api_args].blank?
return if _apipie_dsl_data[:api_args].blank? && _apipie_dsl_data[:api_from_routes].blank?

# remove method description if exists and create new one
Apipie.remove_method_description(self, _apipie_dsl_data[:api_versions], method_name)
Expand Down
20 changes: 18 additions & 2 deletions lib/apipie/method_description.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,7 @@ def initialize(method, resource, dsl_data)
@method = method.to_s
@resource = resource
@from_concern = dsl_data[:from_concern]

@apis = dsl_data[:api_args].map do |mthd, path, desc, opts|
@apis = api_data(dsl_data).map do |mthd, path, desc, opts|
MethodDescription::Api.new(mthd, concern_subst(path), concern_subst(desc), opts)
end

Expand Down Expand Up @@ -154,6 +153,23 @@ def from_concern?

private

def api_data(dsl_data)
ret = dsl_data[:api_args].dup
if dsl_data[:api_from_routes]
desc = dsl_data[:api_from_routes][:desc]
options = dsl_data[:api_from_routes][:options]

api_from_routes = Apipie.routes_for_action(resource.controller, method, {:desc => desc, :options => options}).map do |route_info|
[route_info[:verb],
route_info[:path],
route_info[:desc],
(route_info[:options] || {}).merge(:from_routes => true)]
end
ret.concat(api_from_routes)
end
ret
end

def merge_params(params, new_params)
new_param_names = Set.new(new_params.map(&:name))
params.delete_if { |p| new_param_names.include?(p.name) }
Expand Down
2 changes: 1 addition & 1 deletion spec/controllers/concerns_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@

it "replaces a placeholder doc specified in concern with a real path" do
path = Apipie["concern_resources#index"].apis.first.path
path.should == '/concerns'
path.should == '/api/concerns'

path = Apipie["concern_resources#show"].apis.first.path
path.should == '/concern_resources/:id'
Expand Down
2 changes: 1 addition & 1 deletion spec/dummy/app/controllers/concerns/sample_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ module Concerns
module SampleController
extend Apipie::DSL::Concern

api :GET, '/:controller_path'
api!
def index
render :text => "OK #{params.inspect}"
end
Expand Down

0 comments on commit 7b25a9a

Please sign in to comment.