Skip to content

Commit 741bc3d

Browse files
committed
Rename is_authorized to authorize.
+ fixed tests and slightly changed implementation
1 parent 32a5807 commit 741bc3d

File tree

4 files changed

+46
-27
lines changed

4 files changed

+46
-27
lines changed

README.rst

+4-6
Original file line numberDiff line numberDiff line change
@@ -581,10 +581,8 @@ authenticate
581581
Pass a proc in order to authenticate user. Pass nil for
582582
no authentication (by default).
583583

584-
is_authorized
585-
Pass a proc in order to authorize controllers and methods. Don't set
586-
to use the default authorize all. The Proc is evaluated in the controller
587-
context.
584+
authorize
585+
Pass a proc in order to authorize controllers and methods. The Proc is evaluated in the controller context.
588586

589587
show_all_examples
590588
Set this to true to set show_in_doc=1 in all recorded examples
@@ -634,7 +632,7 @@ Example:
634632
username == "test" && password == "supersecretpassword"
635633
end
636634
end
637-
config.is_authorized = Proc.new do |controller, method, doc|
635+
config.authorize = Proc.new do |controller, method, doc|
638636
!method # show all controller doc, but no method docs.
639637
end
640638
end
@@ -1107,7 +1105,7 @@ When you want to avoid any unnecessary computation in production mode,
11071105
you can generate a cache with ``rake apipie:cache`` and configure the
11081106
app to use it in production with ``config.use_cache = Rails.env.production?``
11091107

1110-
Default cache dir is ``File.join(Rails.root, "public", "apipie-cache")``,
1108+
Default cache dir is ``File.join(Rails.root, "public", "apipie-cache")``,
11111109
you can change it to where you want, example: ``config.cache_dir = File.join(Rails.root, "doc", "apidoc")``.
11121110

11131111
If, for some complex cases, you need to generate/re-generate just part of the cache

app/controllers/apipie/apipies_controller.rb

+17-9
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ def index
3333
I18n.locale = @language
3434
@doc = Apipie.to_json(params[:version], params[:resource], params[:method], @language)
3535

36-
@doc = authorize(@doc)
36+
@doc = authorized_doc
3737

3838
format.json do
3939
if @doc
@@ -92,16 +92,24 @@ def get_language
9292
lang
9393
end
9494

95-
def authorize document
96-
document[:docs][:resources].select! do |k, v|
97-
instance_exec(k, nil, v, &Apipie.configuration.authorized?)
98-
end
99-
document[:docs][:resources].each do |k, v|
100-
v[:methods].select! do |h|
101-
instance_exec(k, h[:name], h, &Apipie.configuration.authorized?)
95+
def authorized_doc
96+
97+
return @doc unless Apipie.configuration.authorize
98+
99+
new_doc = { :docs => @doc[:docs].clone }
100+
101+
new_doc[:docs][:resources] = @doc[:docs][:resources].select do |k, v|
102+
if instance_exec(k, nil, v, &Apipie.configuration.authorize)
103+
v[:methods] = v[:methods].select do |h|
104+
instance_exec(k, h[:name], h, &Apipie.configuration.authorize)
105+
end
106+
true
107+
else
108+
false
102109
end
103110
end
104-
document
111+
112+
new_doc
105113
end
106114

107115
def get_format

lib/apipie/configuration.rb

+1-3
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,11 @@ class Configuration
77
:validate, :validate_value, :validate_presence, :validate_key, :authenticate, :doc_path,
88
:show_all_examples, :process_params, :update_checksum, :checksum_path,
99
:link_extension, :record, :languages, :translate, :locale, :default_locale,
10-
:persist_show_in_doc, :is_authorized
10+
:persist_show_in_doc, :authorize
1111

1212
alias_method :validate?, :validate
1313
alias_method :required_by_default?, :required_by_default
1414
alias_method :namespaced_resources?, :namespaced_resources
15-
alias_method :authorized?, :is_authorized
1615

1716
# matcher to be used in Dir.glob to find controllers to be reloaded e.g.
1817
#
@@ -166,7 +165,6 @@ def initialize
166165
@translate = lambda { |str, locale| str }
167166
@persist_show_in_doc = false
168167
@routes_formatter = RoutesFormatter.new
169-
@is_authorized = ->(controller, method, document) { true }
170168
end
171169
end
172170
end

spec/controllers/apipies_controller_spec.rb

+24-9
Original file line numberDiff line numberDiff line change
@@ -144,29 +144,44 @@
144144
describe "authorize document" do
145145
it "if an authroize method is set" do
146146
test = false
147-
Apipie.configuration.is_authorized = Proc.new do |controller, method, doc|
147+
Apipie.configuration.authorize = Proc.new do |controller, method, doc|
148148
test = true
149149
true
150150
end
151151
get :index
152-
test.should == true
152+
expect(test).to eq(true)
153153
end
154154
it "remove all resources" do
155-
Apipie.configuration.is_authorized = Proc.new do |&args|
155+
Apipie.configuration.authorize = Proc.new do |&args|
156156
false
157157
end
158158
get :index
159-
assigns(:doc)[:resources].should == {}
159+
expect(assigns(:doc)[:resources]).to eq({})
160160
end
161161
it "remove all methods" do
162-
Apipie.configuration.is_authorized = Proc.new do |controller, method, doc|
162+
Apipie.configuration.authorize = Proc.new do |controller, method, doc|
163163
!method
164164
end
165165
get :index
166-
pp assigns(:doc)
167-
assigns(:doc)[:resources]["concern_resources"][:methods].should == []
168-
assigns(:doc)[:resources]["twitter_example"][:methods].should == []
169-
assigns(:doc)[:resources]["users"][:methods].should == []
166+
expect(assigns(:doc)[:resources]["concern_resources"][:methods]).to eq([])
167+
expect(assigns(:doc)[:resources]["twitter_example"][:methods]).to eq([])
168+
expect(assigns(:doc)[:resources]["users"][:methods]).to eq([])
169+
end
170+
it "remove specific method" do
171+
Apipie.configuration.authorize = nil
172+
get :index
173+
174+
users_methods = assigns(:doc)[:resources]["users"][:methods].size
175+
twitter_example_methods = assigns(:doc)[:resources]["twitter_example"][:methods].size
176+
177+
Apipie.configuration.authorize = Proc.new do |controller, method, doc|
178+
controller == "users" ? method != "index" : true
179+
end
180+
181+
get :index
182+
183+
expect(assigns(:doc)[:resources]["users"][:methods].size).to eq(users_methods - 1)
184+
expect(assigns(:doc)[:resources]["twitter_example"][:methods].size).to eq(twitter_example_methods)
170185
end
171186
end
172187

0 commit comments

Comments
 (0)