acts_as_api 0.3.0 → 0.3.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore CHANGED
@@ -7,4 +7,5 @@ acts_as_api.tmproj
7
7
  spec/rails_app/db/*.sqlite3
8
8
  spec/rails_app/tmp/**/*
9
9
  spec/rails_app/log/*.log
10
- .rspec
10
+ .rspec
11
+ .rvmrc
@@ -14,10 +14,8 @@ Gem::Specification.new do |s|
14
14
 
15
15
  s.add_dependency('activemodel','>= 3.0.0')
16
16
  s.add_dependency('activesupport','>= 3.0.0')
17
- s.add_dependency('rack','>= 1.1.0')
18
- # TODO: Problem in Rails with JSON dependency?!
19
- # s.add_dependency('json','>= 1.4.6')
20
-
17
+ s.add_dependency('rack','>= 1.1.0')
18
+
21
19
  s.add_development_dependency('rails', ['>= 3.0.0'])
22
20
 
23
21
  s.has_rdoc = true
@@ -2,8 +2,21 @@ module ActsAsApi
2
2
  #
3
3
  class ApiTemplate < Hash
4
4
 
5
+ def options_for(key)
6
+ @options[key]
7
+ end
8
+
9
+ def option_for(key, value)
10
+ @options[key][value] if @options[key]
11
+ end
12
+
5
13
  def add(val, options = {})
6
- self[(options[:as] || val).to_sym] = val
14
+ item_key = (options[:as] || val).to_sym
15
+
16
+ self[item_key] = val
17
+
18
+ @options ||= {}
19
+ @options[item_key] = options
7
20
  end
8
21
 
9
22
  def remove(key)
@@ -40,9 +40,6 @@ module ActsAsApi
40
40
 
41
41
  def api_accessible(api_template, options = {}, &block)
42
42
 
43
- # return api_accessible_deprecated([api_template]) if api_template.is_a? Hash
44
-
45
-
46
43
  attributes = api_accessible_attributes(api_template) || ApiTemplate.new
47
44
 
48
45
  attributes.merge!(api_accessible_attributes(options[:extend])) if options[:extend]
@@ -61,7 +58,6 @@ module ActsAsApi
61
58
 
62
59
  end
63
60
 
64
-
65
61
  module InstanceMethods
66
62
 
67
63
  # Creates the api response of the model and returns it as a Hash.
@@ -81,29 +77,21 @@ module ActsAsApi
81
77
 
82
78
  leaf[:item].each do |k,v|
83
79
 
80
+ if leaf[:item].respond_to?(:option_for)
81
+ sub_template = leaf[:item].option_for(k, :template) || api_template
82
+ else
83
+ sub_template = api_template
84
+ end
85
+
84
86
  case v
85
87
  when Symbol
86
88
 
87
89
  if self.respond_to?(v)
88
90
  out = send v
89
-
90
- if out.respond_to?(:as_api_response)
91
- out = out.send(:as_api_response, api_template)
92
- end
93
-
94
- leaf[:parent][k] = out
95
-
96
91
  end
97
92
 
98
- when Proc
99
-
93
+ when Proc
100
94
  out = v.call(self)
101
-
102
- if out.respond_to?(:as_api_response)
103
- out = out.send(:as_api_response, api_template)
104
- end
105
-
106
- leaf[:parent][k] = out
107
95
 
108
96
  when String
109
97
  # go up the call chain
@@ -112,16 +100,17 @@ module ActsAsApi
112
100
  out = out.send(method.to_sym)
113
101
  end
114
102
 
115
- if out.respond_to?(:as_api_response)
116
- out = out.send(:as_api_response, api_template)
117
- end
118
-
119
- leaf[:parent][k] = out
120
-
121
103
  when Hash
122
104
  leaf[:parent][k] ||= {}
123
105
  queue << { :parent => leaf[:parent][k], :item => v}
106
+ next
107
+ end
108
+
109
+ if out.respond_to?(:as_api_response)
110
+ out = out.send(:as_api_response, sub_template)
124
111
  end
112
+
113
+ leaf[:parent][k] = out
125
114
 
126
115
  end
127
116
 
@@ -7,7 +7,7 @@ module ActsAsApi
7
7
  # The accepted response formats
8
8
  # Default is <tt>[:xml, :json]</tt>
9
9
  attr_accessor_with_default :accepted_api_formats, [:xml, :json] # :nodoc:
10
-
10
+
11
11
  # Holds formats that should be dasherized
12
12
  # Default is <tt>[:xml]</tt>
13
13
  attr_accessor_with_default :dasherize_for, [:xml]
@@ -1,4 +1,4 @@
1
1
  module ActsAsApi
2
2
 
3
- VERSION = "0.3.0"
3
+ VERSION = "0.3.1"
4
4
  end
@@ -1,6 +1,6 @@
1
1
  require File.dirname(__FILE__) + '/../spec_helper.rb'
2
2
 
3
- describe UsersController do
3
+ describe UsersController, :orm => :active_record do
4
4
 
5
5
  include ApiTestHelpers
6
6
 
@@ -1,12 +1,14 @@
1
1
  require File.dirname(__FILE__) + '/../spec_helper.rb'
2
2
 
3
- describe "acts_as_api" do
3
+ describe "acts_as_api", :orm => :active_record do
4
4
 
5
5
  before(:each) do
6
- @luke = User.create({ :first_name => 'Luke', :last_name => 'Skywalker', :age => 25, :active => true })
6
+ @luke = User.create({ :first_name => 'Luke', :last_name => 'Skywalker', :age => 25, :active => true })
7
7
  @han = User.create({ :first_name => 'Han', :last_name => 'Solo', :age => 35, :active => true })
8
8
  @leia = User.create({ :first_name => 'Princess', :last_name => 'Leia', :age => 25, :active => false })
9
9
 
10
+ @luke.profile = Profile.create({ :avatar => 'picard.jpg', :homepage => 'lukasarts.com' })
11
+
10
12
  @destroy_deathstar = @luke.tasks.create({ :heading => "Destroy Deathstar", :description => "XWing, Shoot, BlowUp", :time_spent => 30, :done => true })
11
13
  @study_with_yoda = @luke.tasks.create({ :heading => "Study with Yoda", :description => "Jedi Stuff, ya know", :time_spent => 60, :done => true })
12
14
  @win_rebellion = @luke.tasks.create({ :heading => "Win Rebellion", :description => "no idea yet...", :time_spent => 180, :done => false })
@@ -273,14 +275,99 @@ describe "acts_as_api" do
273
275
  end
274
276
 
275
277
  describe "including an association (which does acts_as_api) in the api template" do
278
+
279
+ context "has_many" do
280
+
281
+ before(:each) do
282
+ Task.acts_as_api
283
+ Task.api_accessible :include_tasks do |t|
284
+ t.add :heading
285
+ t.add :done
286
+ end
287
+ @response = @luke.as_api_response(:include_tasks)
288
+ end
289
+
290
+ it "should return a hash" do
291
+ @response.should be_kind_of(Hash)
292
+ end
293
+
294
+ it "should return the correct number of keys" do
295
+ @response.should have(1).key
296
+ end
297
+
298
+ it "should return all specified fields" do
299
+ @response.keys.should include(:tasks)
300
+ end
301
+
302
+ it "should return the correct values for the specified fields" do
303
+ @response[:tasks].should be_an Array
304
+ @response[:tasks].should have(3).tasks
305
+ end
306
+
307
+ it "should contain the associated child models with the determined api template" do
308
+ @response[:tasks].each do |task|
309
+ task.keys.should include(:heading, :done)
310
+ task.keys.should have(2).attributes
311
+ end
312
+ end
313
+
314
+ it "should contain the correct data of the child models" do
315
+ task_hash = [ @destroy_deathstar, @study_with_yoda, @win_rebellion ].collect{|t| { :done => t.done, :heading => t.heading } }
316
+ @response[:tasks].should eql task_hash
317
+ end
318
+
319
+ end
320
+
321
+ context "has_one" do
322
+
323
+ before(:each) do
324
+ Profile.acts_as_api
325
+ Profile.api_accessible :include_profile do |t|
326
+ t.add :avatar
327
+ t.add :homepage
328
+ end
329
+ @response = @luke.as_api_response(:include_profile)
330
+ end
331
+
332
+ it "should return a hash" do
333
+ @response.should be_kind_of(Hash)
334
+ end
335
+
336
+ it "should return the correct number of keys" do
337
+ @response.should have(1).key
338
+ end
339
+
340
+ it "should return all specified fields" do
341
+ @response.keys.should include(:profile)
342
+ end
343
+
344
+ it "should return the correct values for the specified fields" do
345
+ @response[:profile].should be_a Hash
346
+ @response[:profile].should have(2).attributes
347
+ end
348
+
349
+ it "should contain the associated child models with the determined api template" do
350
+ @response[:profile].keys.should include(:avatar, :homepage)
351
+ end
352
+
353
+ it "should contain the correct data of the child models" do
354
+ profile_hash = { :avatar => @luke.profile.avatar, :homepage => @luke.profile.homepage }
355
+ @response[:profile].should eql profile_hash
356
+ end
357
+
358
+ end
359
+
360
+ end
361
+
362
+ describe "including an association (which does acts_as_api, but with using another template name) in the api template", :meow => true do
276
363
 
277
364
  before(:each) do
278
365
  Task.acts_as_api
279
- Task.api_accessible :include_tasks do |t|
280
- t.add :heading
281
- t.add :done
366
+ Task.api_accessible :other_template do |t|
367
+ t.add :description
368
+ t.add :time_spent
282
369
  end
283
- @response = @luke.as_api_response(:include_tasks)
370
+ @response = @luke.as_api_response(:other_sub_template)
284
371
  end
285
372
 
286
373
  it "should return a hash" do
@@ -288,9 +375,17 @@ describe "acts_as_api" do
288
375
  end
289
376
 
290
377
  it "should return the correct number of keys" do
291
- @response.should have(1).key
378
+ @response.should have(2).keys
379
+ end
380
+
381
+ it "should return all specified fields" do
382
+ @response.keys.should include(:first_name)
292
383
  end
293
384
 
385
+ it "should return the correct values for the specified fields" do
386
+ @response.values.should include(@luke.first_name)
387
+ end
388
+
294
389
  it "should return all specified fields" do
295
390
  @response.keys.should include(:tasks)
296
391
  end
@@ -302,17 +397,17 @@ describe "acts_as_api" do
302
397
 
303
398
  it "should contain the associated child models with the determined api template" do
304
399
  @response[:tasks].each do |task|
305
- task.keys.should include(:heading, :done)
400
+ task.keys.should include(:description, :time_spent)
306
401
  task.keys.should have(2).attributes
307
402
  end
308
403
  end
309
404
 
310
405
  it "should contain the correct data of the child models" do
311
- task_hash = [ @destroy_deathstar, @study_with_yoda, @win_rebellion ].collect{|t| { :done => t.done, :heading => t.heading } }
406
+ task_hash = [ @destroy_deathstar, @study_with_yoda, @win_rebellion ].collect{|t| { :description => t.description, :time_spent => t.time_spent } }
312
407
  @response[:tasks].should eql task_hash
313
408
  end
314
409
 
315
- end
410
+ end
316
411
 
317
412
  describe "including a scoped association in the api template" do
318
413
 
@@ -0,0 +1,3 @@
1
+ class Profile < ActiveRecord::Base
2
+ belongs_to :user
3
+ end
@@ -2,6 +2,8 @@ class User < ActiveRecord::Base
2
2
 
3
3
  has_many :tasks
4
4
 
5
+ has_one :profile
6
+
5
7
  acts_as_api
6
8
 
7
9
  api_accessible :name_only do |t|
@@ -40,11 +42,19 @@ class User < ActiveRecord::Base
40
42
  t.add lambda{|model| model.full_name.upcase }, :as => :all_caps_name
41
43
  t.add lambda{ Time.now.class.to_s }, :as => :without_param
42
44
  end
43
-
44
45
  User.api_accessible :include_tasks do |t|
45
46
  t.add :tasks
46
47
  end
47
-
48
+
49
+ api_accessible :include_profile do |t|
50
+ t.add :profile
51
+ end
52
+
53
+ api_accessible :other_sub_template do |t|
54
+ t.add :first_name
55
+ t.add :tasks, :template => :other_template
56
+ end
57
+
48
58
  api_accessible :include_completed_tasks do |t|
49
59
  t.add "tasks.completed.all", :as => :completed_tasks
50
60
  end
@@ -20,6 +20,14 @@ class CreateTables < ActiveRecord::Migration
20
20
  t.datetime "updated_at"
21
21
  end
22
22
 
23
+ create_table "profiles", :force => true do |t|
24
+ t.integer "user_id"
25
+ t.string "avatar"
26
+ t.string "homepage"
27
+ t.datetime "created_at"
28
+ t.datetime "updated_at"
29
+ end
30
+
23
31
  create_table :untoucheds do |t|
24
32
  t.string "nothing"
25
33
  t.timestamps
@@ -29,6 +37,7 @@ class CreateTables < ActiveRecord::Migration
29
37
 
30
38
  def self.down
31
39
  drop_table :untoucheds
40
+ drop_table :profiles
32
41
  drop_table :tasks
33
42
  drop_table :users
34
43
  end
@@ -21,6 +21,14 @@ ActiveRecord::Schema.define(:version => 20110214201640) do
21
21
  t.datetime "created_at"
22
22
  t.datetime "updated_at"
23
23
  end
24
+
25
+ create_table "profiles", :force => true do |t|
26
+ t.integer "user_id"
27
+ t.string "avatar"
28
+ t.string "homepage"
29
+ t.datetime "created_at"
30
+ t.datetime "updated_at"
31
+ end
24
32
 
25
33
  create_table "users", :force => true do |t|
26
34
  t.string "first_name"
@@ -30,5 +38,10 @@ ActiveRecord::Schema.define(:version => 20110214201640) do
30
38
  t.datetime "created_at"
31
39
  t.datetime "updated_at"
32
40
  end
41
+
42
+ create_table "untoucheds", :force => true do |t|
43
+ t.string "nothing"
44
+ t.timestamps
45
+ end
33
46
 
34
47
  end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: acts_as_api
3
3
  version: !ruby/object:Gem::Version
4
- hash: 19
4
+ hash: 17
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
8
  - 3
9
- - 0
10
- version: 0.3.0
9
+ - 1
10
+ version: 0.3.1
11
11
  platform: ruby
12
12
  authors:
13
13
  - "Christian B\xC3\xA4uerlein"
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2011-02-22 00:00:00 +01:00
18
+ date: 2011-04-08 00:00:00 +02:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency
@@ -119,6 +119,7 @@ files:
119
119
  - spec/rails_app/app/controllers/respond_with_users_controller.rb
120
120
  - spec/rails_app/app/controllers/users_controller.rb
121
121
  - spec/rails_app/app/helpers/application_helper.rb
122
+ - spec/rails_app/app/models/profile.rb
122
123
  - spec/rails_app/app/models/task.rb
123
124
  - spec/rails_app/app/models/untouched.rb
124
125
  - spec/rails_app/app/models/user.rb
@@ -192,7 +193,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
192
193
  requirements: []
193
194
 
194
195
  rubyforge_project:
195
- rubygems_version: 1.4.2
196
+ rubygems_version: 1.6.2
196
197
  signing_key:
197
198
  specification_version: 3
198
199
  summary: Makes creating XML/JSON responses in Rails 3 easy and fun.
@@ -206,6 +207,7 @@ test_files:
206
207
  - spec/rails_app/app/controllers/respond_with_users_controller.rb
207
208
  - spec/rails_app/app/controllers/users_controller.rb
208
209
  - spec/rails_app/app/helpers/application_helper.rb
210
+ - spec/rails_app/app/models/profile.rb
209
211
  - spec/rails_app/app/models/task.rb
210
212
  - spec/rails_app/app/models/untouched.rb
211
213
  - spec/rails_app/app/models/user.rb