acts_as_api 0.3.10 → 0.3.11
Sign up to get free protection for your applications and to get access to all the features.
- data/History.txt +8 -0
- data/acts_as_api.gemspec +1 -1
- data/lib/acts_as_api/rendering.rb +11 -2
- data/lib/acts_as_api/version.rb +1 -1
- data/spec/controllers/respond_with_users_controller_spec.rb +0 -1
- data/spec/models/mongoid_spec.rb +2 -2
- data/spec/rails_app/app/controllers/respond_with_users_controller.rb +6 -0
- data/spec/rails_app/app/controllers/users_controller.rb +11 -1
- data/spec/rails_app/app/models/mongo_user.rb +19 -0
- data/spec/rails_app/app/models/user.rb +19 -0
- data/spec/rails_app/app/models/vanilla_user.rb +19 -0
- data/spec/rails_app/config/routes.rb +2 -0
- data/spec/support/controller_examples.rb +77 -1
- metadata +12 -12
data/History.txt
CHANGED
data/acts_as_api.gemspec
CHANGED
@@ -8,7 +8,16 @@ module ActsAsApi
|
|
8
8
|
# to simply generate API outputs.
|
9
9
|
#
|
10
10
|
# The default Rails serializers are used to serialize the data.
|
11
|
-
def render_for_api(
|
11
|
+
def render_for_api(api_template_or_options, render_options)
|
12
|
+
if api_template_or_options.is_a?(Hash)
|
13
|
+
api_template = []
|
14
|
+
api_template << api_template_or_options.delete(:prefix)
|
15
|
+
api_template << api_template_or_options.delete(:template)
|
16
|
+
api_template << api_template_or_options.delete(:postfix)
|
17
|
+
api_template = api_template.reject(&:blank?).join('_')
|
18
|
+
else
|
19
|
+
api_template = api_template_or_options
|
20
|
+
end
|
12
21
|
|
13
22
|
# extract the api format and model
|
14
23
|
api_format_options = {}
|
@@ -82,4 +91,4 @@ module ActsAsApi
|
|
82
91
|
|
83
92
|
end
|
84
93
|
|
85
|
-
end
|
94
|
+
end
|
data/lib/acts_as_api/version.rb
CHANGED
data/spec/models/mongoid_spec.rb
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
|
-
describe Mongoid, :orm => "mongoid" do
|
3
|
+
describe "Mongoid", :orm => "mongoid" do
|
4
4
|
|
5
5
|
before(:each) do
|
6
6
|
setup_mongoid_models
|
@@ -26,4 +26,4 @@ describe Mongoid, :orm => "mongoid" do
|
|
26
26
|
it_supports "defining a model callback"
|
27
27
|
end
|
28
28
|
|
29
|
-
end
|
29
|
+
end
|
@@ -38,6 +38,12 @@ class RespondWithUsersController < ApplicationController
|
|
38
38
|
respond_with @user
|
39
39
|
end
|
40
40
|
|
41
|
+
def show_prefix_postfix
|
42
|
+
@user = @user_model.find(params[:id])
|
43
|
+
# :root => :user is only used here because we need it for the node name of the MongoUser model
|
44
|
+
respond_with @user, :api_template => {:template => params[:api_template], :prefix => params[:api_prefix], :postfix => params[:api_postfix]}, :root => :user
|
45
|
+
end
|
46
|
+
|
41
47
|
def create
|
42
48
|
@user = @user_model.new(params[:user])
|
43
49
|
|
@@ -56,4 +56,14 @@ class UsersController < ApplicationController
|
|
56
56
|
end
|
57
57
|
end
|
58
58
|
|
59
|
-
|
59
|
+
def show_prefix_postfix
|
60
|
+
@user = @user_model.find(params[:id])
|
61
|
+
template = {:template => params[:api_template], :prefix => params[:api_prefix], :postfix => params[:api_postfix]}
|
62
|
+
respond_to do |format|
|
63
|
+
# :root => :user is only used here because we need it for the node name of the MongoUser model
|
64
|
+
format.xml { render_for_api template, :xml => @user, :root => :user }
|
65
|
+
format.json { render_for_api template, :json => @user, :root => :user }
|
66
|
+
end
|
67
|
+
end
|
68
|
+
|
69
|
+
end
|
@@ -119,6 +119,25 @@ class MongoUser
|
|
119
119
|
t.add :last_name, :unless => lambda{|u| nil }
|
120
120
|
end
|
121
121
|
|
122
|
+
api_accessible :with_prefix_name_only do |t|
|
123
|
+
t.add lambda{|model| 'true' }, :as => :prefix
|
124
|
+
t.add :first_name
|
125
|
+
t.add :last_name
|
126
|
+
end
|
127
|
+
|
128
|
+
api_accessible :name_only_with_postfix do |t|
|
129
|
+
t.add :first_name
|
130
|
+
t.add :last_name
|
131
|
+
t.add lambda{|model| 'true' }, :as => :postfix
|
132
|
+
end
|
133
|
+
|
134
|
+
api_accessible :with_prefix_name_only_with_postfix do |t|
|
135
|
+
t.add lambda{|model| 'true' }, :as => :prefix
|
136
|
+
t.add :first_name
|
137
|
+
t.add :last_name
|
138
|
+
t.add lambda{|model| 'true' }, :as => :postfix
|
139
|
+
end
|
140
|
+
|
122
141
|
def before_api_response(api_response)
|
123
142
|
@before_api_response_called = true
|
124
143
|
end
|
@@ -111,6 +111,25 @@ class User < ActiveRecord::Base
|
|
111
111
|
t.add :first_name
|
112
112
|
t.add :last_name, :unless => lambda{|u| nil }
|
113
113
|
end
|
114
|
+
|
115
|
+
api_accessible :with_prefix_name_only do |t|
|
116
|
+
t.add lambda{|model| 'true' }, :as => :prefix
|
117
|
+
t.add :first_name
|
118
|
+
t.add :last_name
|
119
|
+
end
|
120
|
+
|
121
|
+
api_accessible :name_only_with_postfix do |t|
|
122
|
+
t.add :first_name
|
123
|
+
t.add :last_name
|
124
|
+
t.add lambda{|model| 'true' }, :as => :postfix
|
125
|
+
end
|
126
|
+
|
127
|
+
api_accessible :with_prefix_name_only_with_postfix do |t|
|
128
|
+
t.add lambda{|model| 'true' }, :as => :prefix
|
129
|
+
t.add :first_name
|
130
|
+
t.add :last_name
|
131
|
+
t.add lambda{|model| 'true' }, :as => :postfix
|
132
|
+
end
|
114
133
|
|
115
134
|
def before_api_response(api_response)
|
116
135
|
@before_api_response_called = true
|
@@ -122,6 +122,25 @@ class VanillaUser
|
|
122
122
|
t.add :last_name, :unless => lambda{|u| nil }
|
123
123
|
end
|
124
124
|
|
125
|
+
api_accessible :with_prefix_name_only do |t|
|
126
|
+
t.add lambda{|model| 'true' }, :as => :prefix
|
127
|
+
t.add :first_name
|
128
|
+
t.add :last_name
|
129
|
+
end
|
130
|
+
|
131
|
+
api_accessible :name_only_with_postfix do |t|
|
132
|
+
t.add :first_name
|
133
|
+
t.add :last_name
|
134
|
+
t.add lambda{|model| 'true' }, :as => :postfix
|
135
|
+
end
|
136
|
+
|
137
|
+
api_accessible :with_prefix_name_only_with_postfix do |t|
|
138
|
+
t.add lambda{|model| 'true' }, :as => :prefix
|
139
|
+
t.add :first_name
|
140
|
+
t.add :last_name
|
141
|
+
t.add lambda{|model| 'true' }, :as => :postfix
|
142
|
+
end
|
143
|
+
|
125
144
|
def before_api_response(api_response)
|
126
145
|
@before_api_response_called = true
|
127
146
|
end
|
@@ -8,6 +8,7 @@ RailsApp::Application.routes.draw do
|
|
8
8
|
member do
|
9
9
|
get 'show_meta'
|
10
10
|
get 'show_default'
|
11
|
+
get 'show_prefix_postfix'
|
11
12
|
end
|
12
13
|
end
|
13
14
|
|
@@ -19,6 +20,7 @@ RailsApp::Application.routes.draw do
|
|
19
20
|
member do
|
20
21
|
get 'show_meta'
|
21
22
|
get 'show_default'
|
23
|
+
get 'show_prefix_postfix'
|
22
24
|
end
|
23
25
|
end
|
24
26
|
|
@@ -385,4 +385,80 @@ shared_examples_for "a controller with ActsAsApi responses" do
|
|
385
385
|
end
|
386
386
|
end
|
387
387
|
|
388
|
-
|
388
|
+
|
389
|
+
describe 'api prefix' do
|
390
|
+
|
391
|
+
describe 'get single user' do
|
392
|
+
|
393
|
+
before(:each) do
|
394
|
+
get :show_prefix_postfix, :format => 'xml', :api_template => :name_only, :api_prefix => :with_prefix, :id => @luke.id, :orm => @orm_for_testing
|
395
|
+
end
|
396
|
+
|
397
|
+
it "should have a root node named user" do
|
398
|
+
response_body.should have_selector("user")
|
399
|
+
end
|
400
|
+
|
401
|
+
it "should contain the specified attributes" do
|
402
|
+
response_body.should have_selector("user > prefix")
|
403
|
+
response_body.should have_selector("user > first-name")
|
404
|
+
response_body.should have_selector("user > last-name")
|
405
|
+
end
|
406
|
+
|
407
|
+
it "should not contain the specified attributes" do
|
408
|
+
response_body.should_not have_selector("user > postfix")
|
409
|
+
end
|
410
|
+
|
411
|
+
end
|
412
|
+
|
413
|
+
end
|
414
|
+
|
415
|
+
describe 'api postfix' do
|
416
|
+
|
417
|
+
describe 'get single user' do
|
418
|
+
|
419
|
+
before(:each) do
|
420
|
+
get :show_prefix_postfix, :format => 'xml', :api_template => :name_only, :api_postfix => :with_postfix, :id => @luke.id, :orm => @orm_for_testing
|
421
|
+
end
|
422
|
+
|
423
|
+
it "should have a root node named user" do
|
424
|
+
response_body.should have_selector("user")
|
425
|
+
end
|
426
|
+
|
427
|
+
it "should contain the specified attributes" do
|
428
|
+
response_body.should have_selector("user > first-name")
|
429
|
+
response_body.should have_selector("user > last-name")
|
430
|
+
response_body.should have_selector("user > postfix")
|
431
|
+
end
|
432
|
+
|
433
|
+
it "should not contain the specified attributes" do
|
434
|
+
response_body.should_not have_selector("user > prefix")
|
435
|
+
end
|
436
|
+
|
437
|
+
end
|
438
|
+
|
439
|
+
end
|
440
|
+
|
441
|
+
describe 'api prefix and api postfix' do
|
442
|
+
|
443
|
+
describe 'get single user' do
|
444
|
+
|
445
|
+
before(:each) do
|
446
|
+
get :show_prefix_postfix, :format => 'xml', :api_template => :name_only, :api_prefix => :with_prefix, :api_postfix => :with_postfix, :id => @luke.id, :orm => @orm_for_testing
|
447
|
+
end
|
448
|
+
|
449
|
+
it "should have a root node named user" do
|
450
|
+
response_body.should have_selector("user")
|
451
|
+
end
|
452
|
+
|
453
|
+
it "should contain the specified attributes" do
|
454
|
+
response_body.should have_selector("user > prefix")
|
455
|
+
response_body.should have_selector("user > first-name")
|
456
|
+
response_body.should have_selector("user > last-name")
|
457
|
+
response_body.should have_selector("user > postfix")
|
458
|
+
end
|
459
|
+
|
460
|
+
end
|
461
|
+
|
462
|
+
end
|
463
|
+
|
464
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: acts_as_api
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.3.
|
4
|
+
version: 0.3.11
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,12 +9,12 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2011-
|
12
|
+
date: 2011-11-06 00:00:00.000000000 +01:00
|
13
13
|
default_executable:
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|
16
16
|
name: activemodel
|
17
|
-
requirement: &
|
17
|
+
requirement: &2198135880 !ruby/object:Gem::Requirement
|
18
18
|
none: false
|
19
19
|
requirements:
|
20
20
|
- - ! '>='
|
@@ -22,10 +22,10 @@ dependencies:
|
|
22
22
|
version: 3.0.0
|
23
23
|
type: :runtime
|
24
24
|
prerelease: false
|
25
|
-
version_requirements: *
|
25
|
+
version_requirements: *2198135880
|
26
26
|
- !ruby/object:Gem::Dependency
|
27
27
|
name: activesupport
|
28
|
-
requirement: &
|
28
|
+
requirement: &2198135380 !ruby/object:Gem::Requirement
|
29
29
|
none: false
|
30
30
|
requirements:
|
31
31
|
- - ! '>='
|
@@ -33,10 +33,10 @@ dependencies:
|
|
33
33
|
version: 3.0.0
|
34
34
|
type: :runtime
|
35
35
|
prerelease: false
|
36
|
-
version_requirements: *
|
36
|
+
version_requirements: *2198135380
|
37
37
|
- !ruby/object:Gem::Dependency
|
38
38
|
name: rack
|
39
|
-
requirement: &
|
39
|
+
requirement: &2198134840 !ruby/object:Gem::Requirement
|
40
40
|
none: false
|
41
41
|
requirements:
|
42
42
|
- - ! '>='
|
@@ -44,10 +44,10 @@ dependencies:
|
|
44
44
|
version: 1.1.0
|
45
45
|
type: :runtime
|
46
46
|
prerelease: false
|
47
|
-
version_requirements: *
|
47
|
+
version_requirements: *2198134840
|
48
48
|
- !ruby/object:Gem::Dependency
|
49
49
|
name: rails
|
50
|
-
requirement: &
|
50
|
+
requirement: &2198134240 !ruby/object:Gem::Requirement
|
51
51
|
none: false
|
52
52
|
requirements:
|
53
53
|
- - ! '>='
|
@@ -55,10 +55,10 @@ dependencies:
|
|
55
55
|
version: 3.1.0
|
56
56
|
type: :development
|
57
57
|
prerelease: false
|
58
|
-
version_requirements: *
|
58
|
+
version_requirements: *2198134240
|
59
59
|
- !ruby/object:Gem::Dependency
|
60
60
|
name: mongoid
|
61
|
-
requirement: &
|
61
|
+
requirement: &2198133600 !ruby/object:Gem::Requirement
|
62
62
|
none: false
|
63
63
|
requirements:
|
64
64
|
- - ! '>='
|
@@ -66,7 +66,7 @@ dependencies:
|
|
66
66
|
version: 2.0.1
|
67
67
|
type: :development
|
68
68
|
prerelease: false
|
69
|
-
version_requirements: *
|
69
|
+
version_requirements: *2198133600
|
70
70
|
description: acts_as_api enriches the models and controllers of your app in a rails-like
|
71
71
|
way so you can easily determine how your XML/JSON API responses should look like.
|
72
72
|
email:
|