-
Notifications
You must be signed in to change notification settings - Fork 465
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Separate nested resource name (#855)
* Fix namespaced resources seperator The expected behavior when `Apipie.configuration.namespaced_resources?` is true is for a nested resource like `V1::Users::TweetsController` to return `v1-users-tweets` however it was returning `v1userstweets` * Refactor resource description spec * Update `ResourceDescription`'s `name` `resource_name` and id - Rename the `resource_name` argument to `id`, it can be missleading - Create `name` method return a human readable resource name depending on the `@id`. Example if id is `some-nested-resource` `#name` will return `Some::Nested::Resource`
- Loading branch information
1 parent
5fc1bf5
commit 1f28905
Showing
4 changed files
with
108 additions
and
51 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,48 +1,91 @@ | ||
require "spec_helper" | ||
|
||
describe Apipie::ResourceDescription do | ||
let(:resource_description) do | ||
Apipie::ResourceDescription.new(controller, id, dsl_data) | ||
end | ||
|
||
let(:controller) { ApplicationController } | ||
let(:id) { 'dummy' } | ||
let(:dsl_data) { ActionController::Base.send(:_apipie_dsl_data_init) } | ||
|
||
describe "metadata" do | ||
describe '#_methods' do | ||
subject(:methods) { resource_description._methods } | ||
|
||
it "should return nil when no metadata is provided" do | ||
resource = Apipie::ResourceDescription.new(ApplicationController, "dummy", dsl_data) | ||
expect(resource.to_json[:metadata]).to eq(nil) | ||
end | ||
context 'when has method descriptions' do | ||
before do | ||
resource_description.add_method_description( | ||
Apipie::MethodDescription.new(:a, resource_description, dsl_data) | ||
) | ||
resource_description.add_method_description( | ||
Apipie::MethodDescription.new(:b, resource_description, dsl_data) | ||
) | ||
resource_description.add_method_description( | ||
Apipie::MethodDescription.new(:c, resource_description, dsl_data) | ||
) | ||
end | ||
|
||
it "should return the metadata" do | ||
meta = { | ||
:lenght => 32, | ||
:weight => '830g' | ||
} | ||
resource = Apipie::ResourceDescription.new(ApplicationController, "dummy", dsl_data.update(:meta => meta)) | ||
expect(resource.to_json[:metadata]).to eq(meta) | ||
it 'should be ordered' do | ||
expect(methods.keys).to eq([:a, :b, :c]) | ||
end | ||
end | ||
|
||
end | ||
|
||
describe "methods descriptions" do | ||
describe '#to_json' do | ||
let(:json_data) { resource_description.to_json } | ||
|
||
describe 'metadata' do | ||
subject { json_data[:metadata] } | ||
|
||
before(:each) do | ||
@resource = Apipie::ResourceDescription.new(ApplicationController, "dummy") | ||
a = Apipie::MethodDescription.new(:a, @resource, dsl_data) | ||
b = Apipie::MethodDescription.new(:b, @resource, dsl_data) | ||
c = Apipie::MethodDescription.new(:c, @resource, dsl_data) | ||
@resource.add_method_description(a) | ||
@resource.add_method_description(b) | ||
@resource.add_method_description(c) | ||
it { is_expected.to be_nil } | ||
|
||
context 'when meta data are provided' do | ||
let(:meta) { { length: 32, weight: '830g' } } | ||
let(:dsl_data) { super().update({ meta: meta }) } | ||
|
||
it { is_expected.to eq(meta) } | ||
end | ||
end | ||
|
||
it "should be ordered" do | ||
expect(@resource._methods.keys).to eq([:a, :b, :c]) | ||
expect(@resource.to_json[:methods].map { |h| h[:name] }).to eq(['a', 'b', 'c']) | ||
describe 'methods' do | ||
subject(:methods_as_json) { json_data[:methods] } | ||
|
||
context 'when has method descriptions' do | ||
before do | ||
resource_description.add_method_description( | ||
Apipie::MethodDescription.new(:a, resource_description, dsl_data) | ||
) | ||
resource_description.add_method_description( | ||
Apipie::MethodDescription.new(:b, resource_description, dsl_data) | ||
) | ||
resource_description.add_method_description( | ||
Apipie::MethodDescription.new(:c, resource_description, dsl_data) | ||
) | ||
end | ||
|
||
it 'should be ordered' do | ||
expect(methods_as_json.map { |h| h[:name] }).to eq(['a', 'b', 'c']) | ||
end | ||
end | ||
end | ||
end | ||
|
||
describe 'name' do | ||
subject { resource_description.name } | ||
|
||
it { is_expected.to eq('Dummy') } | ||
|
||
it "should be still ordered" do | ||
expect(@resource._methods.keys).to eq([:a, :b, :c]) | ||
expect(@resource.to_json[:methods].map { |h| h[:name] }).to eq(['a', 'b', 'c']) | ||
context 'when given id contains dashes' do | ||
let(:id) { 'some-nested-resource' } | ||
|
||
it { is_expected.to eq('Some::Nested::Resource') } | ||
end | ||
|
||
context 'when resource_name is given' do | ||
let(:resource_name) { 'Some-Resource' } | ||
let(:dsl_data) { super().merge!(resource_name: 'Some-Resource') } | ||
|
||
it { is_expected.to eq(resource_name) } | ||
end | ||
end | ||
end |