acts_as_api 0.3.11 → 0.4

Sign up to get free protection for your applications and to get access to all the features.
Files changed (198) hide show
  1. data/.gitignore +7 -4
  2. data/.travis.yml +5 -1
  3. data/Gemfile +2 -1
  4. data/History.txt +4 -0
  5. data/README.md +9 -4
  6. data/Rakefile +28 -5
  7. data/acts_as_api.gemspec +1 -1
  8. data/lib/acts_as_api/api_template.rb +54 -63
  9. data/lib/acts_as_api/array.rb +2 -2
  10. data/lib/acts_as_api/base.rb +19 -13
  11. data/lib/acts_as_api/config.rb +2 -2
  12. data/lib/acts_as_api/rendering.rb +4 -4
  13. data/lib/acts_as_api/version.rb +1 -1
  14. data/spec/README.md +55 -0
  15. data/spec/active_record_dummy/.gitignore +15 -0
  16. data/spec/active_record_dummy/Gemfile +16 -0
  17. data/spec/active_record_dummy/README.rdoc +261 -0
  18. data/spec/active_record_dummy/Rakefile +7 -0
  19. data/spec/{rails_app/public → active_record_dummy/app/assets}/images/rails.png +0 -0
  20. data/spec/active_record_dummy/app/assets/javascripts/application.js +15 -0
  21. data/spec/active_record_dummy/app/assets/stylesheets/application.css +13 -0
  22. data/spec/active_record_dummy/app/controllers/application_controller.rb +3 -0
  23. data/spec/{rails_app → active_record_dummy}/app/helpers/application_helper.rb +0 -0
  24. data/spec/{rails_app/lib/tasks → active_record_dummy/app/mailers}/.gitkeep +0 -0
  25. data/spec/{rails_app/public/stylesheets → active_record_dummy/app/models}/.gitkeep +0 -0
  26. data/spec/{rails_app → active_record_dummy}/app/models/profile.rb +1 -0
  27. data/spec/active_record_dummy/app/models/task.rb +4 -0
  28. data/spec/{rails_app → active_record_dummy}/app/models/untouched.rb +0 -0
  29. data/spec/{rails_app → active_record_dummy}/app/models/user.rb +1 -0
  30. data/spec/active_record_dummy/app/views/layouts/application.html.erb +14 -0
  31. data/spec/active_record_dummy/config.ru +4 -0
  32. data/spec/active_record_dummy/config/application.rb +59 -0
  33. data/spec/{rails_app → active_record_dummy}/config/boot.rb +0 -0
  34. data/spec/{rails_app → active_record_dummy}/config/database.yml +5 -3
  35. data/spec/active_record_dummy/config/environment.rb +5 -0
  36. data/spec/active_record_dummy/config/environments/development.rb +37 -0
  37. data/spec/active_record_dummy/config/environments/production.rb +67 -0
  38. data/spec/active_record_dummy/config/environments/test.rb +37 -0
  39. data/spec/{rails_app → active_record_dummy}/config/initializers/backtrace_silencers.rb +0 -0
  40. data/spec/active_record_dummy/config/initializers/generators.rb +2 -0
  41. data/spec/{rails_app → active_record_dummy}/config/initializers/inflections.rb +5 -0
  42. data/spec/{rails_app → active_record_dummy}/config/initializers/mime_types.rb +0 -0
  43. data/spec/active_record_dummy/config/initializers/secret_token.rb +7 -0
  44. data/spec/active_record_dummy/config/initializers/session_store.rb +8 -0
  45. data/spec/active_record_dummy/config/initializers/wrap_parameters.rb +14 -0
  46. data/spec/{rails_app → active_record_dummy}/config/locales/en.yml +1 -1
  47. data/spec/active_record_dummy/config/routes.rb +59 -0
  48. data/spec/{rails_app → active_record_dummy}/db/migrate/20110214201640_create_tables.rb +0 -0
  49. data/spec/{rails_app → active_record_dummy}/db/schema.rb +0 -0
  50. data/spec/{rails_app → active_record_dummy}/db/seeds.rb +0 -0
  51. data/spec/active_record_dummy/doc/README_FOR_APP +2 -0
  52. data/spec/{rails_app/public/favicon.ico → active_record_dummy/lib/assets/.gitkeep} +0 -0
  53. data/spec/active_record_dummy/lib/tasks/.gitkeep +0 -0
  54. data/spec/active_record_dummy/log/.gitkeep +0 -0
  55. data/spec/{rails_app → active_record_dummy}/public/404.html +0 -0
  56. data/spec/{rails_app → active_record_dummy}/public/422.html +0 -0
  57. data/spec/{rails_app → active_record_dummy}/public/500.html +0 -1
  58. data/spec/active_record_dummy/public/favicon.ico +0 -0
  59. data/spec/{rails_app → active_record_dummy}/public/index.html +10 -8
  60. data/spec/{rails_app → active_record_dummy}/public/robots.txt +0 -0
  61. data/spec/{rails_app → active_record_dummy}/script/rails +0 -0
  62. data/spec/active_record_dummy/vendor/assets/javascripts/.gitkeep +0 -0
  63. data/spec/active_record_dummy/vendor/assets/stylesheets/.gitkeep +0 -0
  64. data/spec/active_record_dummy/vendor/plugins/.gitkeep +0 -0
  65. data/spec/controllers/respond_with_users_controller_spec.rb +18 -33
  66. data/spec/controllers/users_controller_spec.rb +8 -25
  67. data/spec/models/{active_record_spec.rb → model_spec.rb} +5 -3
  68. data/spec/mongoid_dummy/.gitignore +15 -0
  69. data/spec/mongoid_dummy/Gemfile +18 -0
  70. data/spec/mongoid_dummy/README.rdoc +261 -0
  71. data/spec/mongoid_dummy/Rakefile +7 -0
  72. data/spec/mongoid_dummy/app/assets/images/rails.png +0 -0
  73. data/spec/mongoid_dummy/app/assets/javascripts/application.js +15 -0
  74. data/spec/mongoid_dummy/app/assets/stylesheets/application.css +13 -0
  75. data/spec/mongoid_dummy/app/controllers/application_controller.rb +3 -0
  76. data/spec/mongoid_dummy/app/helpers/application_helper.rb +2 -0
  77. data/spec/mongoid_dummy/app/mailers/.gitkeep +0 -0
  78. data/spec/mongoid_dummy/app/models/.gitkeep +0 -0
  79. data/spec/{rails_app/app/models/mongo_profile.rb → mongoid_dummy/app/models/profile.rb} +2 -2
  80. data/spec/{rails_app/app/models/mongo_task.rb → mongoid_dummy/app/models/task.rb} +2 -2
  81. data/spec/{rails_app/app/models/mongo_untouched.rb → mongoid_dummy/app/models/untouched.rb} +1 -1
  82. data/spec/mongoid_dummy/app/models/user.rb +48 -0
  83. data/spec/mongoid_dummy/app/views/layouts/application.html.erb +14 -0
  84. data/spec/mongoid_dummy/config.ru +4 -0
  85. data/spec/mongoid_dummy/config/application.rb +65 -0
  86. data/spec/mongoid_dummy/config/boot.rb +6 -0
  87. data/spec/mongoid_dummy/config/environment.rb +5 -0
  88. data/spec/{rails_app → mongoid_dummy}/config/environments/development.rb +10 -4
  89. data/spec/{rails_app → mongoid_dummy}/config/environments/production.rb +29 -14
  90. data/spec/{rails_app → mongoid_dummy}/config/environments/test.rb +8 -8
  91. data/spec/mongoid_dummy/config/initializers/backtrace_silencers.rb +7 -0
  92. data/spec/mongoid_dummy/config/initializers/generators.rb +2 -0
  93. data/spec/mongoid_dummy/config/initializers/include_acts_as_api.rb +3 -0
  94. data/spec/mongoid_dummy/config/initializers/inflections.rb +15 -0
  95. data/spec/mongoid_dummy/config/initializers/mime_types.rb +5 -0
  96. data/spec/mongoid_dummy/config/initializers/secret_token.rb +7 -0
  97. data/spec/mongoid_dummy/config/initializers/session_store.rb +8 -0
  98. data/spec/mongoid_dummy/config/initializers/wrap_parameters.rb +10 -0
  99. data/spec/mongoid_dummy/config/locales/en.yml +5 -0
  100. data/spec/{rails_app → mongoid_dummy}/config/mongoid.yml +8 -3
  101. data/spec/mongoid_dummy/config/routes.rb +59 -0
  102. data/spec/mongoid_dummy/db/seeds.rb +7 -0
  103. data/spec/mongoid_dummy/doc/README_FOR_APP +2 -0
  104. data/spec/mongoid_dummy/lib/assets/.gitkeep +0 -0
  105. data/spec/mongoid_dummy/lib/tasks/.gitkeep +0 -0
  106. data/spec/mongoid_dummy/log/.gitkeep +0 -0
  107. data/spec/mongoid_dummy/public/404.html +26 -0
  108. data/spec/mongoid_dummy/public/422.html +26 -0
  109. data/spec/mongoid_dummy/public/500.html +25 -0
  110. data/spec/mongoid_dummy/public/favicon.ico +0 -0
  111. data/spec/mongoid_dummy/public/index.html +241 -0
  112. data/spec/mongoid_dummy/public/robots.txt +5 -0
  113. data/spec/mongoid_dummy/script/rails +6 -0
  114. data/spec/mongoid_dummy/vendor/assets/javascripts/.gitkeep +0 -0
  115. data/spec/mongoid_dummy/vendor/assets/stylesheets/.gitkeep +0 -0
  116. data/spec/mongoid_dummy/vendor/plugins/.gitkeep +0 -0
  117. data/spec/shared_engine/.gitignore +7 -0
  118. data/spec/shared_engine/Gemfile +14 -0
  119. data/spec/shared_engine/MIT-LICENSE +20 -0
  120. data/spec/shared_engine/README.rdoc +3 -0
  121. data/spec/shared_engine/Rakefile +29 -0
  122. data/spec/shared_engine/app/assets/images/shared_engine/.gitkeep +0 -0
  123. data/spec/shared_engine/app/assets/javascripts/shared_engine/application.js +15 -0
  124. data/spec/shared_engine/app/assets/stylesheets/shared_engine/application.css +13 -0
  125. data/spec/shared_engine/app/controllers/shared_engine/application_controller.rb +4 -0
  126. data/spec/shared_engine/app/controllers/shared_engine/respond_with_users_controller.rb +59 -0
  127. data/spec/shared_engine/app/controllers/shared_engine/users_controller.rb +71 -0
  128. data/spec/shared_engine/app/helpers/shared_engine/application_helper.rb +4 -0
  129. data/spec/shared_engine/app/models/user_template.rb +154 -0
  130. data/spec/shared_engine/app/views/layouts/shared_engine/application.html.erb +14 -0
  131. data/spec/{rails_app → shared_engine}/config/routes.rb +2 -4
  132. data/spec/shared_engine/dummy/README.rdoc +261 -0
  133. data/spec/{rails_app → shared_engine/dummy}/Rakefile +2 -2
  134. data/spec/shared_engine/dummy/app/assets/javascripts/application.js +15 -0
  135. data/spec/shared_engine/dummy/app/assets/stylesheets/application.css +13 -0
  136. data/spec/shared_engine/dummy/app/controllers/application_controller.rb +3 -0
  137. data/spec/shared_engine/dummy/app/helpers/application_helper.rb +2 -0
  138. data/spec/shared_engine/dummy/app/mailers/.gitkeep +0 -0
  139. data/spec/shared_engine/dummy/app/models/.gitkeep +0 -0
  140. data/spec/shared_engine/dummy/app/views/layouts/application.html.erb +14 -0
  141. data/spec/{rails_app → shared_engine/dummy}/config.ru +1 -1
  142. data/spec/{rails_app → shared_engine/dummy}/config/application.rb +30 -10
  143. data/spec/shared_engine/dummy/config/boot.rb +10 -0
  144. data/spec/shared_engine/dummy/config/database.yml +25 -0
  145. data/spec/{rails_app → shared_engine/dummy}/config/environment.rb +1 -1
  146. data/spec/shared_engine/dummy/config/environments/development.rb +37 -0
  147. data/spec/shared_engine/dummy/config/environments/production.rb +67 -0
  148. data/spec/shared_engine/dummy/config/environments/test.rb +37 -0
  149. data/spec/shared_engine/dummy/config/initializers/backtrace_silencers.rb +7 -0
  150. data/spec/shared_engine/dummy/config/initializers/inflections.rb +15 -0
  151. data/spec/shared_engine/dummy/config/initializers/mime_types.rb +5 -0
  152. data/spec/{rails_app → shared_engine/dummy}/config/initializers/secret_token.rb +1 -1
  153. data/spec/{rails_app → shared_engine/dummy}/config/initializers/session_store.rb +2 -2
  154. data/spec/shared_engine/dummy/config/initializers/wrap_parameters.rb +14 -0
  155. data/spec/shared_engine/dummy/config/locales/en.yml +5 -0
  156. data/spec/shared_engine/dummy/config/routes.rb +4 -0
  157. data/spec/shared_engine/dummy/lib/assets/.gitkeep +0 -0
  158. data/spec/shared_engine/dummy/log/.gitkeep +0 -0
  159. data/spec/shared_engine/dummy/public/404.html +26 -0
  160. data/spec/shared_engine/dummy/public/422.html +26 -0
  161. data/spec/shared_engine/dummy/public/500.html +25 -0
  162. data/spec/shared_engine/dummy/public/favicon.ico +0 -0
  163. data/spec/shared_engine/dummy/script/rails +6 -0
  164. data/spec/shared_engine/lib/shared_engine.rb +4 -0
  165. data/spec/shared_engine/lib/shared_engine/engine.rb +5 -0
  166. data/spec/shared_engine/lib/shared_engine/version.rb +3 -0
  167. data/spec/shared_engine/lib/tasks/shared_engine_tasks.rake +4 -0
  168. data/spec/shared_engine/script/rails +8 -0
  169. data/spec/shared_engine/shared_engine.gemspec +22 -0
  170. data/spec/spec_helper.rb +16 -11
  171. data/spec/support/controller_examples.rb +35 -41
  172. data/spec/support/model_examples/associations.rb +13 -13
  173. data/spec/support/model_examples/enabled.rb +2 -2
  174. data/spec/support/model_examples/extending.rb +6 -6
  175. data/spec/support/model_examples/options.rb +53 -0
  176. data/spec/support/model_examples/untouched.rb +2 -2
  177. data/spec/support/simple_fixtures.rb +31 -59
  178. metadata +324 -132
  179. data/spec/models/mongoid_spec.rb +0 -29
  180. data/spec/models/vanilla_ruby_spec.rb +0 -31
  181. data/spec/rails_app/.gitignore +0 -4
  182. data/spec/rails_app/app/controllers/application_controller.rb +0 -11
  183. data/spec/rails_app/app/controllers/respond_with_users_controller.rb +0 -57
  184. data/spec/rails_app/app/controllers/users_controller.rb +0 -69
  185. data/spec/rails_app/app/models/mongo_user.rb +0 -192
  186. data/spec/rails_app/app/models/task.rb +0 -3
  187. data/spec/rails_app/app/models/vanilla_profile.rb +0 -16
  188. data/spec/rails_app/app/models/vanilla_task.rb +0 -16
  189. data/spec/rails_app/app/models/vanilla_untouched.rb +0 -2
  190. data/spec/rails_app/app/models/vanilla_user.rb +0 -195
  191. data/spec/rails_app/app/views/layouts/application.html.erb +0 -14
  192. data/spec/rails_app/config/initializers/acts_as_api_mongoid.rb +0 -6
  193. data/spec/rails_app/public/javascripts/application.js +0 -2
  194. data/spec/rails_app/public/javascripts/controls.js +0 -965
  195. data/spec/rails_app/public/javascripts/dragdrop.js +0 -974
  196. data/spec/rails_app/public/javascripts/effects.js +0 -1123
  197. data/spec/rails_app/public/javascripts/prototype.js +0 -6001
  198. data/spec/rails_app/public/javascripts/rails.js +0 -191
data/.gitignore CHANGED
@@ -4,8 +4,11 @@ pkg/*
4
4
  Gemfile.lock
5
5
  test
6
6
  acts_as_api.tmproj
7
- spec/rails_app/db/*.sqlite3
8
- spec/rails_app/tmp/**/*
9
- spec/rails_app/log/*.log
7
+ spec/active_record_dummy/db/*.sqlite3
8
+ spec/active_record_dummy/tmp/**/*
9
+ spec/active_record_dummy/log/*.log
10
+ spec/mongoid_dummy/tmp/**/*
11
+ spec/mongoid_dummy/log/*.log
10
12
  .rspec
11
- .rvmrc
13
+ .rvmrc
14
+ *.sqlite3-journal
data/.travis.yml CHANGED
@@ -1,4 +1,8 @@
1
+ script: bundle exec rake spec:all
2
+
1
3
  rvm:
2
4
  - 1.8.7
3
5
  - 1.9.2
4
- - ree
6
+ - 1.9.3
7
+ - ruby-head
8
+ - ree
data/Gemfile CHANGED
@@ -6,7 +6,8 @@ gemspec
6
6
  group :test do
7
7
  gem 'sqlite3-ruby'
8
8
  gem 'mongoid', '>= 2.0.1'
9
+ gem 'rspec', '>= 2.9.0'
9
10
  gem 'rspec-rails', '>= 2.5.0'
10
11
  gem 'webrat'
11
- gem 'rocco', :git => 'git://github.com/fabrik42/rocco.git'
12
+ gem 'shared_engine', :path => './spec/shared_engine'
12
13
  end
data/History.txt CHANGED
@@ -1,3 +1,7 @@
1
+ === 0.4 2012-04-30
2
+
3
+ * Added support for a second parameter (options) to pass to as_api_response
4
+
1
5
  === 0.3.11 2011-11-06
2
6
 
3
7
  * Added :postfix, :prefix options for template rendering
data/README.md CHANGED
@@ -6,6 +6,8 @@ It provides a simple interface to determine the representation of your model dat
6
6
 
7
7
  In addition to Rails it theoretically can be used with any ruby app and any database (__ActiveRecord__ and __Mongoid__ are supported out of the box) as it only has few dependencies.
8
8
 
9
+ The lib is _very_ fast in generating your responses and battle tested in production with platforms like [Diaspora](https://joindiaspora.com) or [flinc](https://flinc.org).
10
+
9
11
  ## Introduction
10
12
 
11
13
  acts_as_api enriches the models and controllers of your app in a Rails-like way so you can easily determine how your API responses should look like:
@@ -54,7 +56,7 @@ There are a lot of how-tos like:
54
56
  * DRY templates for your api responses
55
57
  * Ships with support for __ActiveRecord__ and __Mongoid__
56
58
  * Support for Rails 3 Responders
57
- * Plays very well together with client libs like [Backbone.js](http://documentcloud.github.com/backbone) or [RestKit](http://restkit.org) (iOS).
59
+ * Plays very well together with client libs like [Backbone.js](http://documentcloud.github.com/backbone), [RestKit](http://restkit.org) (iOS) or [gson](http://code.google.com/p/google-gson) (Android).
58
60
  * Easy but very flexible syntax for defining the templates
59
61
  * XML, JSON and JSON-P support out of the box, easy to extend
60
62
  * Minimal dependecies (you can also use it without Rails)
@@ -76,14 +78,17 @@ There are a lot of how-tos like:
76
78
 
77
79
  * Wiki: https://github.com/fabrik42/acts_as_api/wiki
78
80
 
81
+ * Want to contribute - the spec suite is explained here: https://github.com/fabrik42/acts_as_api/tree/master/spec
82
+
79
83
  ### Travis CI build status ![acts_as_api on travis ci](http://travis-ci.org/fabrik42/acts_as_api.png)
80
84
 
81
- Specs run with 1.9.2, 1.8.7 and REE: http://travis-ci.org/#!/fabrik42/acts_as_api
85
+ Specs run with 1.9.3, 1.9.2, 1.8.7 and REE: http://travis-ci.org/#!/fabrik42/acts_as_api
82
86
 
83
87
  ### Tested with:
84
88
 
85
- * MRI 1.9.2-p180
86
- * MRI 1.8.7-p334
89
+ * MRI 1.9.3-p125
90
+ * MRI 1.9.2-p290
91
+ * MRI 1.8.7-p358
87
92
  * But it just should work fine with other versions too... :)
88
93
 
89
94
  ### Downwards Compatibility
data/Rakefile CHANGED
@@ -8,17 +8,40 @@ RSpec::Core::RakeTask.new
8
8
 
9
9
  namespace :spec do
10
10
 
11
- desc "Run ActiveRecord RSpec code examples only"
12
- RSpec::Core::RakeTask.new do |t|
13
- t.name = :active_record
14
- t.rspec_opts = ["--color", "--format", "documentation", "--tag", "orm:active_record"]
11
+ supported_orms = %w(mongoid active_record)
12
+
13
+ supported_orms.each do |orm|
14
+ desc "Run #{orm} specs only"
15
+ RSpec::Core::RakeTask.new do |t|
16
+ t.name = orm
17
+ t.rspec_opts = ["--color"]
18
+ end
19
+
20
+ task "prepare_#{orm}" do
21
+ ENV['ACTS_AS_API_ORM'] = orm
22
+ end
23
+
24
+ Rake::Task["spec:#{orm}"].prerequisites << "spec:prepare_#{orm}"
25
+ end
26
+
27
+
28
+ # task :all => supported_orms.map{|orm| "spec:#{orm}"}
29
+
30
+ desc "Runs specs for all ORMs (#{supported_orms.join(', ')})"
31
+ task :all do
32
+ supported_orms.each do |orm|
33
+ puts "Starting to run specs for #{orm}..."
34
+ system("bundle exec rake spec:#{orm}")
35
+ raise "#{orm} failed!" unless $?.exitstatus == 0
36
+ end
15
37
  end
16
38
 
17
39
  end
18
40
 
41
+
19
42
  gemspec = Gem::Specification.load("acts_as_api.gemspec")
20
43
 
21
- task :default => :spec
44
+ task :default => "spec:all"
22
45
 
23
46
  desc "Generate the gh_pages site"
24
47
  task :rocco do
data/acts_as_api.gemspec CHANGED
@@ -16,7 +16,7 @@ Gem::Specification.new do |s|
16
16
  s.add_dependency('activesupport','>= 3.0.0')
17
17
  s.add_dependency('rack','>= 1.1.0')
18
18
 
19
- s.add_development_dependency('rails', ['>= 3.1.0'])
19
+ s.add_development_dependency('rails', ['>= 3.2.3'])
20
20
  s.add_development_dependency('mongoid', ['>= 2.0.1'])
21
21
 
22
22
  s.rdoc_options = ['--charset=UTF-8']
@@ -11,18 +11,11 @@ module ActsAsApi
11
11
 
12
12
  # The name of the api template as a Symbol.
13
13
  attr_accessor :api_template
14
-
15
- attr_reader :options
16
14
 
17
- # Returns a new ApiTemplate with the api template name
18
- # set to the passed template.
19
- def self.create(template)
20
- t = ApiTemplate.new
21
- t.api_template = template
22
- return t
23
- end
15
+ attr_reader :options
24
16
 
25
- def initialize
17
+ def initialize(api_template)
18
+ self.api_template = api_template
26
19
  @options ||= {}
27
20
  end
28
21
 
@@ -67,82 +60,80 @@ module ActsAsApi
67
60
  # If a special template name for the passed item is specified
68
61
  # it will be returned, if not the original api template.
69
62
  def api_template_for(fieldset, field)
70
- return api_template unless fieldset.is_a? ActsAsApi::ApiTemplate
71
63
  fieldset.option_for(field, :template) || api_template
72
64
  end
73
65
 
74
66
  # Decides if the passed item should be added to
75
67
  # the response based on the conditional options passed.
76
- def allowed_to_render?(fieldset, field, model)
68
+ def allowed_to_render?(fieldset, field, model, options)
77
69
  return true unless fieldset.is_a? ActsAsApi::ApiTemplate
78
- allowed = true
79
- allowed = condition_fulfilled?(model, fieldset.option_for(field, :if)) if fieldset.option_for(field, :if)
80
- allowed = !(condition_fulfilled?(model, fieldset.option_for(field, :unless))) if fieldset.option_for(field, :unless)
81
- return allowed
70
+
71
+ fieldset_options = fieldset.options_for(field)
72
+
73
+ if fieldset_options[:unless]
74
+ !(condition_fulfilled?(model, fieldset_options[:unless], options))
75
+ elsif fieldset_options[:if]
76
+ condition_fulfilled?(model, fieldset_options[:if], options)
77
+ else
78
+ true
79
+ end
82
80
  end
83
81
 
84
82
  # Checks if a condition is fulfilled
85
83
  # (result is not nil or false)
86
- def condition_fulfilled?(model, condition)
84
+ def condition_fulfilled?(model, condition, options)
87
85
  case condition
88
86
  when Symbol
89
87
  result = model.send(condition)
90
88
  when Proc
91
- result = condition.call(model)
89
+ result = call_proc(condition, model, options)
92
90
  end
93
- !result.nil? && !result.is_a?(FalseClass)
91
+ !!result
94
92
  end
95
93
 
96
94
  # Generates a hash that represents the api response based on this
97
95
  # template for the passed model instance.
98
- def to_response_hash(model)
99
- queue = []
96
+ def to_response_hash(model, fieldset = self, options = {})
100
97
  api_output = {}
101
-
102
- queue << { :output => api_output, :item => self }
103
-
104
- until queue.empty? do
105
- leaf = queue.pop
106
- fieldset = leaf[:item]
107
-
108
- fieldset.each do |field, value|
109
-
110
- next unless allowed_to_render?(fieldset, field, model)
111
-
112
- case value
113
- when Symbol
114
- if model.respond_to?(value)
115
- out = model.send value
116
- end
117
-
118
- when Proc
119
- out = value.call(model)
120
-
121
- when String
122
- # go up the call chain
123
- out = model
124
- value.split(".").each do |method|
125
- out = out.send(method.to_sym)
126
- end
127
-
128
- when Hash
129
- leaf[:output][field] ||= {}
130
- queue << { :output => leaf[:output][field], :item => value }
131
- next
132
- end
133
-
134
- if out.respond_to?(:as_api_response)
135
- sub_template = api_template_for(fieldset, field)
136
- out = out.send(:as_api_response, sub_template)
137
- end
138
-
139
- leaf[:output][field] = out
98
+
99
+ fieldset.each do |field, value|
100
+ next unless allowed_to_render?(fieldset, field, model, options)
101
+
102
+ out = process_value(model, value, options)
103
+
104
+ if out.respond_to?(:as_api_response)
105
+ sub_template = api_template_for(fieldset, field)
106
+ out = out.as_api_response(sub_template, options)
140
107
  end
141
-
108
+
109
+ api_output[field] = out
142
110
  end
143
-
111
+
144
112
  api_output
145
113
  end
146
-
114
+
115
+ private
116
+
117
+ def process_value(model, value, options)
118
+ case value
119
+ when Symbol
120
+ model.send(value)
121
+ when Proc
122
+ call_proc(value,model,options)
123
+ when String
124
+ value.split('.').inject(model) { |result, method| result.send(method) }
125
+ when Hash
126
+ to_response_hash(model, value)
127
+ end
128
+ end
129
+
130
+ def call_proc(the_proc,model,options)
131
+ if the_proc.arity == 2
132
+ the_proc.call(model, options)
133
+ else
134
+ the_proc.call(model)
135
+ end
136
+ end
137
+
147
138
  end
148
139
  end
@@ -6,11 +6,11 @@ class Array
6
6
  # The Array checks all its items if they respond to the +as_api_response+ method.
7
7
  # If they do, the result of this method will be collected.
8
8
  # If they don't, the item itself will be collected.
9
- def as_api_response(api_template)
9
+ def as_api_response(api_template, options = {})
10
10
 
11
11
  collect do |item|
12
12
  if item.respond_to?(:as_api_response)
13
- item.as_api_response(api_template)
13
+ item.as_api_response(api_template,options)
14
14
  else
15
15
  item
16
16
  end
@@ -34,7 +34,7 @@ module ActsAsApi
34
34
  # be contained in the api responses.
35
35
  def api_accessible(api_template, options = {}, &block)
36
36
 
37
- attributes = api_accessible_attributes(api_template) || ApiTemplate.create(api_template)
37
+ attributes = api_accessible_attributes(api_template) || ApiTemplate.new(api_template)
38
38
 
39
39
  attributes.merge!(api_accessible_attributes(options[:extend])) if options[:extend]
40
40
 
@@ -56,25 +56,31 @@ module ActsAsApi
56
56
 
57
57
  # Creates the api response of the model and returns it as a Hash.
58
58
  # Will raise an exception if the passed api template is not defined for the model
59
- def as_api_response(api_template)
59
+ def as_api_response(api_template, options = {})
60
60
  api_attributes = self.class.api_accessible_attributes(api_template)
61
61
  raise ActsAsApi::TemplateNotFoundError.new("acts_as_api template :#{api_template.to_s} was not found for model #{self.class}") if api_attributes.nil?
62
-
63
- before_api_response(api_template) if respond_to? :before_api_response
64
-
65
- response_hash = if respond_to? :around_api_response
66
- around_api_response api_template do
67
- api_attributes.to_response_hash(self)
68
- end
69
- else
70
- api_attributes.to_response_hash(self)
71
- end
72
62
 
73
- after_api_response(api_template) if respond_to? :after_api_response
63
+ before_api_response(api_template)
64
+ response_hash = around_api_response(api_template) do
65
+ api_attributes.to_response_hash(self, api_attributes, options)
66
+ end
67
+ after_api_response(api_template)
74
68
 
75
69
  response_hash
76
70
  end
77
71
 
72
+ protected
73
+
74
+ def before_api_response(api_remplate)
75
+ end
76
+
77
+ def after_api_response(api_remplate)
78
+ end
79
+
80
+ def around_api_response(api_remplate)
81
+ yield
82
+ end
83
+
78
84
  end
79
85
 
80
86
  end
@@ -49,10 +49,10 @@ module ActsAsApi
49
49
  # If true the jsonp function call will get the http status passed
50
50
  # as a second parameter
51
51
  def add_http_status_to_jsonp_response
52
- @add_http_status_to_jsonp_response || true
52
+ @add_http_status_to_jsonp_response.nil? ? true : @add_http_status_to_jsonp_response
53
53
  end
54
54
  end
55
55
 
56
56
  end
57
57
 
58
- end
58
+ end
@@ -41,14 +41,14 @@ module ActsAsApi
41
41
 
42
42
  if !output_params[:root].nil?
43
43
  api_root_name = output_params[:root].to_s
44
+ elsif api_model.class.respond_to?(:model_name)
45
+ api_root_name = api_model.class.model_name
44
46
  elsif api_model.respond_to?(:collection_name)
45
47
  api_root_name = api_model.collection_name
46
- elsif api_model.respond_to?(:model_name)
47
- api_root_name = api_model.model_name
48
48
  elsif api_model.is_a?(Array) && !api_model.empty? && api_model.first.class.respond_to?(:model_name)
49
49
  api_root_name = api_model.first.class.model_name
50
- elsif api_model.class.respond_to?(:model_name)
51
- api_root_name = api_model.class.model_name
50
+ elsif api_model.respond_to?(:model_name)
51
+ api_root_name = api_model.model_name
52
52
  else
53
53
  api_root_name = ActsAsApi::Config.default_root.to_s
54
54
  end
@@ -1,3 +1,3 @@
1
1
  module ActsAsApi
2
- VERSION = "0.3.11"
2
+ VERSION = "0.4"
3
3
  end
data/spec/README.md ADDED
@@ -0,0 +1,55 @@
1
+ # Spec folder intro
2
+
3
+ ## Running specs
4
+
5
+ Every ORM has a rake task. Run `rake -T` to see them all.
6
+
7
+ `rake spec:all` will run all spec suites in a row.
8
+
9
+ `rake spec:#{orm_name}` will run the spec suite for a specific ORM.
10
+
11
+ ## Working with the specs
12
+
13
+ acts_as_api can be used with lots of different configurations, depending e.g. on the ORM (ActiveRecord, Mongoid, vanilla ruby) or the way the content is rendered (usual Rails controller, vs. Responder).
14
+
15
+ A goal of the lib is to stay consistent in its behaviour over these different configurations, so it won't get in your way, once you change other parts of your application.
16
+
17
+ To achieve this goal and because of the need to keep the specs as DRY as possible, the following spec setup was created:
18
+
19
+ ### A shared engine
20
+
21
+ In order to keep the spec suite DRY it uses a Rails engine, available at `./shared_engine`.
22
+
23
+ It contains the controllers that are re-used by the ORM-specific Rails apps and some mixins that can be shared over the models of different ORMs.
24
+
25
+ ### Dummy Rails apps
26
+
27
+ There used to be one Rails app that contained all supported ORMs. But multiple times this setup veiled problems e.g. with ORM-specific dependencies.
28
+
29
+ Now there are multiple Rails apps, one for every supported ORM: `./active_record_dummy` and `./mongoid_dummy`.
30
+
31
+ These are very simple apps, basically just clicked together on [railswizard.org](http://railswizard.org).
32
+
33
+ They contain **no controllers** to be tested, just models that match the tested ones.
34
+
35
+ ### Adding a dummy Rails app
36
+
37
+ * Create a new Rails app in the folder `./spec/#{orm_name}_dummy`.
38
+
39
+ * Create to Models used in the spec (`User, Profile, Untouched, Task`).
40
+
41
+ * Include `SharedEngine::UserTemplate` in your `User` model.
42
+
43
+ * Add `mount SharedEngine::Engine => "/shared", :as => "shared"` to your `routes.rb`
44
+
45
+ * Add the following lines to your Gemfile:
46
+
47
+ ```ruby
48
+ gem 'shared_engine', :path => '../shared_engine'
49
+ gem 'acts_as_api', :path => '../../'
50
+ ```
51
+
52
+ * Add your dummy app to the `Rakefile` in the root folder by adding it to the `supported_orms` array.
53
+
54
+
55
+ If you have to do some special setup (e.g. creating a schema) you can do this in `./spec_helper.rb`.