Skip to content

Commit

Permalink
Merge a4aaba0 into 9ecdadc
Browse files Browse the repository at this point in the history
  • Loading branch information
blanquer committed Jul 6, 2016
2 parents 9ecdadc + a4aaba0 commit 4e6bab6
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 13 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
* Allow Plugin registration without requiring config_key
* registration will select a default config_key based on the class name
* Make Traits accumulate block definitions for `params`,`headers` and `payload` rather than overriding them.
* Switch to lazy evaluation of `base_params` from `ApiDefinition` to properly inherit them into the resources
and their corresponding actions even before the application's `MediaTtypes` have been finalized.


## 0.20.1
Expand Down
8 changes: 0 additions & 8 deletions lib/praxis/action_definition.rb
Original file line number Diff line number Diff line change
Expand Up @@ -118,14 +118,6 @@ def params(type=Attributor::Struct, **opts, &block)
update_attribute(@params, opts, block)
else
@params = create_attribute(type, **opts, &block)
if (api_info = ApiDefinition.instance.info(resource_definition.version))
if api_info.base_params
base_attrs = api_info.base_params.attributes
@params.attributes.merge!(base_attrs) do |key, oldval, newval|
oldval
end
end
end
end

@params
Expand Down
26 changes: 24 additions & 2 deletions lib/praxis/resource_definition.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ module ResourceDefinition
@version = 'n/a'.freeze
@actions = Hash.new
@responses = Hash.new
@action_defaults = Trait.new

@action_defaults = Trait.new &ResourceDefinition.generate_defaults_block

@version_options = {}
@metadata = {}
Expand All @@ -35,9 +36,28 @@ module ResourceDefinition
Application.instance.resource_definitions << self
end

def self.generate_defaults_block( version: nil )

# Ensure we inherit any base params defined in the API definition for the passed in version
base_attributes = if (base_params = ApiDefinition.instance.info(version).base_params)
base_params.attributes
else
{}
end

Proc.new do
unless base_attributes.empty?
params do
base_attributes.each do |base_name, base_attribute|
attribute base_name, base_attribute.type, base_attribute.options
end
end
end
end
end

def self.finalize!
Application.instance.resource_definitions.each do |resource_definition|

while (block = resource_definition.on_finalize.shift)
block.call
end
Expand Down Expand Up @@ -178,6 +198,8 @@ def version(version=nil, options=nil)
@version_prefix = "#{Praxis::Request::path_version_prefix}#{self.version}"
end
end

@action_defaults.instance_eval &ResourceDefinition.generate_defaults_block( version: version )
end


Expand Down
2 changes: 1 addition & 1 deletion spec/praxis/action_definition_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -340,7 +340,7 @@ def self.name
end

its('routes.first.path.to_s') { should eq '/apps/:app_name/foobars/hello_world/:one' }
its('params.attributes.keys') { should eq [:inherited, :app_name, :one]}
its('params.attributes.keys') { should match_array [:inherited, :app_name, :one]}

context 'where the action overrides a base_param' do

Expand Down
53 changes: 51 additions & 2 deletions spec/praxis/resource_definition_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@

context 'for a resource with a parent' do
let(:resource_definition) { ApiResources::VolumeSnapshots}

its([:parent]) { should eq ApiResources::Volumes.id }
end

Expand Down Expand Up @@ -78,6 +78,7 @@
include Praxis::ResourceDefinition
media_type Person

version '1.0'
def self.name
'FooBar'
end
Expand All @@ -101,12 +102,60 @@ def self.name
end
end

let(:non_singleton_api) do
api_def=Praxis::ApiDefinition.__send__(:new)
api_def.instance_eval do |api|

api.info do
base_path '/api/:base_param'
base_params do
attribute :base_param, String
attribute :grouped_params do
attribute :nested_param, String
end
end
end

api.info '1.0' do
base_params do
attribute :app_name, String
end
end
api.info '2.0' do
base_params do
attribute :v2_param, String
end
end
end
api_def
end

before do
allow(Praxis::ApiDefinition).to receive(:instance).and_return(non_singleton_api)
end

it 'are applied to actions' do
action = resource_definition.actions[:show]
expect(action.params.attributes).to have_key(:id)
expect(action.routes.first.path.to_s).to eq '/api/people/:id'
expect(action.routes.first.path.to_s).to eq '/api/:base_param/people/:id'
end

context 'includes base_params from the APIDefinition' do
let(:show_action_params){ resource_definition.actions[:show].params }

it 'including globally defined' do
expect(show_action_params.attributes).to have_key(:base_param)
expect(show_action_params.attributes).to have_key(:grouped_params)
grouped = show_action_params.attributes[:grouped_params]
expect(grouped.type.ancestors).to include(::Attributor::Struct)
expect(grouped.type.attributes.keys).to eq([:nested_param])
end
it 'including the ones defined for its own version' do
expect(show_action_params.attributes).to have_key(:app_name)
expect(show_action_params.attributes).to_not have_key(:v2_param)
end

end
end

context 'setting other values' do
Expand Down

0 comments on commit 4e6bab6

Please sign in to comment.