Skip to content

Commit af97220

Browse files
committed
Evaluate params data only in MethodDescription
We are going to need information about action in the Param description soon.
1 parent a0f5f92 commit af97220

File tree

3 files changed

+22
-40
lines changed

3 files changed

+22
-40
lines changed

lib/apipie/method_description.rb

+14-16
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,6 @@ def initialize(method, resource, app, dsl_data)
3232
Apipie::ErrorDescription.new(args)
3333
end
3434

35-
@params_ordered = dsl_data[:params].map do |args|
36-
Apipie::ParamDescription.from_dsl_data(args)
37-
end
38-
3935
@see = dsl_data[:see].map do |args|
4036
Apipie::SeeDescription.new(args)
4137
end
@@ -44,9 +40,8 @@ def initialize(method, resource, app, dsl_data)
4440
@examples = dsl_data[:examples]
4541
@examples += load_recorded_examples
4642

47-
parent = @resource.controller.superclass
48-
if parent != ActionController::Base
49-
@parent_resource = parent.controller_name
43+
@params_ordered = dsl_data[:params].map do |args|
44+
Apipie::ParamDescription.from_dsl_data(args)
5045
end
5146
end
5247

@@ -60,15 +55,14 @@ def params
6055

6156
def params_ordered
6257
all_params = []
63-
# get params from parent resource description
64-
if @parent_resource
65-
parent = Apipie.get_resource_description(@parent_resource)
66-
merge_params(all_params, parent._params_ordered) if parent
67-
end
58+
parent = Apipie.get_resource_description(@resource.controller.superclass)
6859

69-
# get params from actual resource description
70-
if @resource
71-
merge_params(all_params, resource._params_ordered)
60+
# get params from parent resource description
61+
[parent, @resource].compact.each do |resource|
62+
resource_params = resource._params_args.map do |args|
63+
Apipie::ParamDescription.from_dsl_data(args)
64+
end
65+
merge_params(all_params, resource_params)
7266
end
7367

7468
merge_params(all_params, @params_ordered)
@@ -79,8 +73,12 @@ def errors
7973
return @merged_errors if @merged_errors
8074
@merged_errors = []
8175
if @resource
76+
resource_errors = @resource._errors_args.map do |args|
77+
Apipie::ErrorDescription.new(args)
78+
end
79+
8280
# exclude overwritten parent errors
83-
@merged_errors = @resource._errors_ordered.find_all do |err|
81+
@merged_errors = resource_errors.find_all do |err|
8482
!@errors.any? { |e| e.code == err.code }
8583
end
8684
end

lib/apipie/resource_description.rb

+5-10
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,13 @@ module Apipie
1212
class ResourceDescription
1313

1414
attr_reader :controller, :_short_description, :_full_description, :_methods, :_id,
15-
:_path, :_name, :_params_ordered, :_errors_ordered, :_formats, :_parent
15+
:_path, :_name, :_params_args, :_errors_args, :_formats, :_parent
1616

1717
def initialize(controller, resource_name, dsl_data = nil, version = nil, &block)
1818

1919
@_methods = ActiveSupport::OrderedHash.new
20-
@_params_ordered = []
21-
@_errors_ordered = []
20+
@_params_args = []
21+
@_errors_args = []
2222

2323
@controller = controller
2424
@_id = resource_name
@@ -35,13 +35,8 @@ def update_from_dsl_data(dsl_data)
3535
@_short_description = dsl_data[:short_description]
3636
@_path = dsl_data[:path] || ""
3737
@_formats = dsl_data[:formats]
38-
@_errors_ordered = dsl_data[:errors].map do |args|
39-
Apipie::ErrorDescription.new(args)
40-
end
41-
42-
@_params_ordered = dsl_data[:params] = dsl_data[:params].map do |args|
43-
Apipie::ParamDescription.from_dsl_data(args)
44-
end
38+
@_errors_args = dsl_data[:errors]
39+
@_params_args = dsl_data[:params]
4540

4641
if dsl_data[:app_info]
4742
Apipie.configuration.app_info[_version] = dsl_data[:app_info]

spec/controllers/users_controller_spec.rb

+3-14
Original file line numberDiff line numberDiff line change
@@ -46,20 +46,9 @@ def compare_hashes(h1, h2)
4646
end
4747

4848
it "should contain params defined on resource level" do
49-
subject._params_ordered.count.should == 2
50-
p = subject._params_ordered.first
51-
p.should_not be(nil)
52-
p.name.should eq(:id)
53-
p.desc.should eq("\n<p>User ID</p>\n")
54-
p.required.should eq(false)
55-
p.validator.class.should eq(Apipie::Validator::IntegerValidator)
56-
57-
p = subject._params_ordered.second
58-
p.should_not be(nil)
59-
p.name.should eq(:resource_param)
60-
p.desc.should eq("\n<p>Param description for all methods</p>\n")
61-
p.required.should eq(false)
62-
p.validator.class.should eq(Apipie::Validator::HashValidator)
49+
subject._params_args.count.should == 2
50+
expected = [:id, Fixnum, {:required=>false, :desc=>"User ID"}, {}, nil]
51+
subject._params_args.first.should == expected
6352
end
6453
end
6554

0 commit comments

Comments
 (0)