rabl 0.6.2 → 0.6.3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (75) hide show
  1. data/CHANGELOG.md +5 -0
  2. data/Rakefile +2 -2
  3. data/fixtures/ashared/NOTES +9 -0
  4. data/fixtures/ashared/README +4 -0
  5. data/fixtures/rails3_2/.gitignore +15 -0
  6. data/fixtures/rails3_2/Gemfile +40 -0
  7. data/fixtures/rails3_2/README.rdoc +261 -0
  8. data/fixtures/rails3_2/Rakefile +12 -0
  9. data/fixtures/rails3_2/app/assets/images/rails.png +0 -0
  10. data/fixtures/rails3_2/app/assets/javascripts/application.js +15 -0
  11. data/fixtures/rails3_2/app/assets/javascripts/posts.js.coffee +3 -0
  12. data/fixtures/rails3_2/app/assets/javascripts/users.js.coffee +3 -0
  13. data/fixtures/rails3_2/app/assets/stylesheets/application.css +13 -0
  14. data/fixtures/rails3_2/app/assets/stylesheets/posts.css.scss +3 -0
  15. data/fixtures/rails3_2/app/assets/stylesheets/users.css.scss +3 -0
  16. data/fixtures/rails3_2/app/controllers/application_controller.rb +14 -0
  17. data/fixtures/rails3_2/app/controllers/posts_controller.rb +11 -0
  18. data/fixtures/rails3_2/app/controllers/users_controller.rb +11 -0
  19. data/fixtures/rails3_2/app/helpers/application_helper.rb +2 -0
  20. data/fixtures/rails3_2/app/helpers/posts_helper.rb +2 -0
  21. data/fixtures/rails3_2/app/helpers/users_helper.rb +2 -0
  22. data/fixtures/rails3_2/app/mailers/.gitkeep +0 -0
  23. data/fixtures/rails3_2/config.ru +4 -0
  24. data/fixtures/rails3_2/config/application.rb +59 -0
  25. data/fixtures/rails3_2/config/boot.rb +6 -0
  26. data/fixtures/rails3_2/config/database.yml +25 -0
  27. data/fixtures/rails3_2/config/environment.rb +5 -0
  28. data/fixtures/rails3_2/config/environments/development.rb +37 -0
  29. data/fixtures/rails3_2/config/environments/production.rb +67 -0
  30. data/fixtures/rails3_2/config/environments/test.rb +37 -0
  31. data/fixtures/rails3_2/config/initializers/backtrace_silencers.rb +7 -0
  32. data/fixtures/rails3_2/config/initializers/inflections.rb +15 -0
  33. data/fixtures/rails3_2/config/initializers/mime_types.rb +5 -0
  34. data/fixtures/rails3_2/config/initializers/secret_token.rb +7 -0
  35. data/fixtures/rails3_2/config/initializers/session_store.rb +8 -0
  36. data/fixtures/rails3_2/config/initializers/wrap_parameters.rb +14 -0
  37. data/fixtures/rails3_2/config/locales/en.yml +5 -0
  38. data/fixtures/rails3_2/config/routes.rb +60 -0
  39. data/fixtures/rails3_2/db/schema.rb +42 -0
  40. data/fixtures/rails3_2/db/seeds.rb +7 -0
  41. data/fixtures/rails3_2/doc/README_FOR_APP +2 -0
  42. data/fixtures/rails3_2/lib/assets/.gitkeep +0 -0
  43. data/fixtures/rails3_2/lib/tasks/.gitkeep +0 -0
  44. data/fixtures/rails3_2/log/.gitkeep +0 -0
  45. data/fixtures/rails3_2/public/404.html +26 -0
  46. data/fixtures/rails3_2/public/422.html +26 -0
  47. data/fixtures/rails3_2/public/500.html +25 -0
  48. data/fixtures/rails3_2/public/favicon.ico +0 -0
  49. data/fixtures/rails3_2/public/index.html +241 -0
  50. data/fixtures/rails3_2/public/robots.txt +5 -0
  51. data/fixtures/rails3_2/script/rails +6 -0
  52. data/fixtures/rails3_2/test/fixtures/.gitkeep +0 -0
  53. data/fixtures/rails3_2/test/fixtures/phone_numbers.yml +11 -0
  54. data/fixtures/rails3_2/test/fixtures/posts.yml +11 -0
  55. data/fixtures/rails3_2/test/fixtures/users.yml +11 -0
  56. data/fixtures/rails3_2/test/functional/.gitkeep +0 -0
  57. data/fixtures/rails3_2/test/functional/posts_controller_test.rb +108 -0
  58. data/fixtures/rails3_2/test/functional/users_controller_test.rb +87 -0
  59. data/fixtures/rails3_2/test/integration/.gitkeep +0 -0
  60. data/fixtures/rails3_2/test/performance/browsing_test.rb +12 -0
  61. data/fixtures/rails3_2/test/test_helper.rb +26 -0
  62. data/fixtures/rails3_2/test/unit/.gitkeep +0 -0
  63. data/fixtures/rails3_2/test/unit/helpers/posts_helper_test.rb +4 -0
  64. data/fixtures/rails3_2/test/unit/helpers/users_helper_test.rb +4 -0
  65. data/fixtures/rails3_2/test/unit/phone_number_test.rb +7 -0
  66. data/fixtures/rails3_2/test/unit/post_test.rb +7 -0
  67. data/fixtures/rails3_2/test/unit/user_test.rb +7 -0
  68. data/fixtures/rails3_2/vendor/assets/javascripts/.gitkeep +0 -0
  69. data/fixtures/rails3_2/vendor/assets/stylesheets/.gitkeep +0 -0
  70. data/fixtures/rails3_2/vendor/plugins/.gitkeep +0 -0
  71. data/lib/rabl/builder.rb +6 -0
  72. data/lib/rabl/partials.rb +17 -13
  73. data/lib/rabl/version.rb +1 -1
  74. data/test/partials_test.rb +9 -12
  75. metadata +68 -2
@@ -0,0 +1,87 @@
1
+ # Lives in <rabl>/test/integration/users_controller_test.rb
2
+ # Symlinked to fixture applications
3
+
4
+ begin # Sinatra
5
+ require File.expand_path(File.dirname(__FILE__) + '/../../test_config.rb')
6
+ rescue LoadError # Rails
7
+ require File.expand_path(File.dirname(__FILE__) + '/../test_helper.rb')
8
+ end
9
+
10
+ context "UsersController" do
11
+ helper(:json_output) { JSON.parse(last_response.body) }
12
+
13
+ setup do
14
+ create_users!
15
+ end
16
+
17
+ context "for index action" do
18
+ # Tests `collection @users` extending from 'show' template
19
+
20
+ setup do
21
+ get "/users"
22
+ end
23
+
24
+ # Attributes (regular)
25
+ asserts("contains user usernames") do
26
+ json_output.map { |u| u["user"]["username"] }
27
+ end.equals { @users.map(&:username) }
28
+ asserts("contains email") do
29
+ json_output.map { |u| u["user"]["email"] }
30
+ end.equals { @users.map(&:email) }
31
+ asserts("contains location") do
32
+ json_output.map { |u| u["user"]["location"] }
33
+ end.equals { @users.map(&:location) }
34
+
35
+ # Attributes (custom name)
36
+ asserts("contains registered_at") do
37
+ json_output.map { |u| u["user"]["registered_at"] }
38
+ end.equals { @users.map(&:created_at).map(&:iso8601) }
39
+
40
+ # Node (renders based on attribute)
41
+ asserts("contains role") do
42
+ json_output.map { |u| u["user"]["role"] }
43
+ end.equals ['normal', 'normal', 'admin']
44
+
45
+ # Child (custom collection name)
46
+ asserts("contains formatted phone numbers") do
47
+ json_output.map { |u| u["user"]["pnumbers"].map { |n| n["pnumber"]["formatted"] } }
48
+ end.equals { @users.map { |u| u.phone_numbers.map(&:formatted) } }
49
+
50
+ # Node (renders collection partial)
51
+ asserts("contains formatted node numbers") do
52
+ json_output.map { |u| u["user"]["node_numbers"].map { |n| n["formatted"] } }
53
+ end.equals { @users.map { |u| u.phone_numbers.map(&:formatted) } }
54
+ end # index
55
+
56
+ context "for show action" do
57
+ # Tests `object :user => :person` custom parent node name
58
+ setup do
59
+ get "/users/#{@user1.id}"
60
+ end
61
+
62
+ # Attributes (regular)
63
+ asserts("contains username") { json_output["person"]["username"] }.equals { @user1.username }
64
+ asserts("contains email") { json_output["person"]["email"] }.equals { @user1.email }
65
+ asserts("contains location") { json_output["person"]["location"] }.equals { @user1.location }
66
+ # Attributes (custom name)
67
+ asserts("contains registered_at") { json_output["person"]["registered_at"] }.equals { @user1.created_at.iso8601 }
68
+ # Node (renders based on attribute)
69
+ asserts("contains role node") { json_output["person"]["role"] }.equals "normal"
70
+
71
+ # Child (custom collection name)
72
+ asserts("contains first phone number") {
73
+ json_output["person"]["pnumbers"][0]["pnumber"]["formatted"]
74
+ }.equals { @user1.phone_numbers[0].formatted }
75
+ asserts("contains second phone number") {
76
+ json_output["person"]["pnumbers"][1]["pnumber"]["formatted"]
77
+ }.equals { @user1.phone_numbers[1].formatted }
78
+
79
+ # Node (renders collection partial)
80
+ asserts("contains first node number") {
81
+ json_output["person"]["node_numbers"][0]["formatted"]
82
+ }.equals { @user1.phone_numbers[0].formatted }
83
+ asserts("contains second node number") {
84
+ json_output["person"]["node_numbers"][1]["formatted"]
85
+ }.equals { @user1.phone_numbers[1].formatted }
86
+ end # show
87
+ end
@@ -0,0 +1,12 @@
1
+ require 'test_helper'
2
+ require 'rails/performance_test_help'
3
+
4
+ class BrowsingTest < ActionDispatch::PerformanceTest
5
+ # Refer to the documentation for all available options
6
+ # self.profile_options = { :runs => 5, :metrics => [:wall_time, :memory]
7
+ # :output => 'tmp/performance', :formats => [:flat] }
8
+
9
+ def test_homepage
10
+ get '/'
11
+ end
12
+ end
@@ -0,0 +1,26 @@
1
+ # Load Silence Functionality
2
+ require File.expand_path(File.dirname(__FILE__) + "/../../../test/silence.rb")
3
+
4
+ # Load Environment
5
+ silence_warnings do
6
+ ENV["RAILS_ENV"] = "test"
7
+ require File.expand_path('../../config/environment', __FILE__)
8
+ require 'rails/test_help'
9
+ end
10
+
11
+ # Load Riot Test Environment
12
+ require File.expand_path(File.dirname(__FILE__) + "/../../../test/integration/test_init.rb")
13
+
14
+ # Run Migrations
15
+ silence_stream(STDOUT) do
16
+ dbconf = YAML::load(File.open('config/database.yml'))[Rails.env]
17
+ ActiveRecord::Base.establish_connection(dbconf)
18
+ ActiveRecord::Base.logger = Logger.new(File.open('log/database.log', 'a'))
19
+ silence_stream(STDOUT) { ActiveRecord::Migrator.up('db/migrate') }
20
+ end
21
+
22
+ class Riot::Situation
23
+ def app
24
+ Rails.application
25
+ end
26
+ end
File without changes
@@ -0,0 +1,4 @@
1
+ require 'test_helper'
2
+
3
+ class PostsHelperTest < ActionView::TestCase
4
+ end
@@ -0,0 +1,4 @@
1
+ require 'test_helper'
2
+
3
+ class UsersHelperTest < ActionView::TestCase
4
+ end
@@ -0,0 +1,7 @@
1
+ require 'test_helper'
2
+
3
+ class PhoneNumberTest < ActiveSupport::TestCase
4
+ # test "the truth" do
5
+ # assert true
6
+ # end
7
+ end
@@ -0,0 +1,7 @@
1
+ require 'test_helper'
2
+
3
+ class PostTest < ActiveSupport::TestCase
4
+ # test "the truth" do
5
+ # assert true
6
+ # end
7
+ end
@@ -0,0 +1,7 @@
1
+ require 'test_helper'
2
+
3
+ class UserTest < ActiveSupport::TestCase
4
+ # test "the truth" do
5
+ # assert true
6
+ # end
7
+ end
@@ -124,6 +124,12 @@ module Rabl
124
124
 
125
125
  private
126
126
 
127
+ # Returns a guess at the format in this scope
128
+ # request_format => "xml"
129
+ def request_format
130
+ @options[:format] if @options[:format] != "hash"
131
+ end
132
+
127
133
  # Caches the results of the block based on object cache_key
128
134
  # cache_results { compile_hash(options) }
129
135
  def cache_results(&block)
@@ -32,27 +32,31 @@ module Rabl
32
32
  def fetch_source(file, options={})
33
33
  Rabl.source_cache(file, options[:view_path]) do
34
34
  if defined? Padrino
35
- root_path = Padrino.root
35
+ view_path = Array(options[:view_path] || context_scope.settings.views)
36
36
  # use Padrino's own template resolution mechanism
37
- file_path, _ = @_scope.instance_eval { resolve_template(file) }
37
+ file_path, _ = context_scope.instance_eval { resolve_template(file) }
38
38
  # Padrino chops the extension, stitch it back on
39
- file_path = File.join(@_scope.settings.views, (file_path.to_s + ".rabl"))
40
- elsif defined? Rails
41
- if defined?(@_scope) && @_scope.respond_to?(:find_template)
42
- # use Rails's own template resolution mechanism (partials and no partial)
43
- lookup_proc = lambda { |partial| @_scope.find_template(file, [], partial) }
39
+ file_path = File.join(view_path.first.to_s, (file_path.to_s + ".rabl"))
40
+ elsif defined?(Rails) && context_scope
41
+ # use Rails template resolution mechanism if possible (find_template)
42
+ source_format = request_format if defined?(request_format)
43
+ view_path = Array(options[:view_path] || context_scope.view_paths.to_a)
44
+ if source_format && context_scope.respond_to?(:lookup_context) # Rails 3
45
+ lookup_proc = lambda { |partial| context_scope.lookup_context.find_template(file, [], partial) }
44
46
  template = lookup_proc.call(false) rescue lookup_proc.call(true)
45
- file_path = File.join(Rails.root.to_s, template.inspect) if template
47
+ file_path = template.identifier if template
48
+ elsif source_format && context_scope.respond_to?(:view_paths) # Rails 2
49
+ template = context_scope.view_paths.find_template(file, source_format, false)
50
+ file_path = template.filename if template
46
51
  else # fallback to manual
47
- root_path = Rails.root
48
- view_path = options[:view_path] || File.join(root_path, "app/views/")
49
- file_path = Dir[File.join(view_path, file + ".{*.,}rabl")].first
52
+ file_path = Dir[File.join("{#{view_path.join(",")}}", file + ".{*.,}rabl")].first
50
53
  end
51
54
  elsif defined? Sinatra
52
- view_path = options[:view_path] || @_scope.settings.views
53
- file_path = Dir[File.join(view_path, file + ".{*.,}rabl")].first
55
+ view_path = Array(options[:view_path] || context_scope.settings.views)
56
+ file_path = Dir[File.join("{#{view_path.join(",")}}", file + ".{*.,}rabl")].first
54
57
  end
55
58
 
59
+ raise "Cannot find rabl template '#{file}' within registered views!" unless File.exist?(file_path.to_s)
56
60
  [File.read(file_path.to_s), file_path.to_s] if file_path
57
61
  end
58
62
  end
@@ -1,3 +1,3 @@
1
1
  module Rabl
2
- VERSION = "0.6.2"
2
+ VERSION = "0.6.3"
3
3
  end
@@ -7,12 +7,11 @@ class TestPartial
7
7
  end
8
8
 
9
9
  context "Rabl::Partials" do
10
- context "fetch_source" do
10
+ context "fetch_source with json" do
11
11
  helper(:tmp_path) { @tmp_path ||= Pathname.new(Dir.mktmpdir) }
12
12
 
13
13
  setup do
14
- ::Rails = stub(Class.new)
15
- ::Rails.root.returns(tmp_path)
14
+ ::Sinatra = stub(Class.new)
16
15
  File.open(tmp_path + "test.json.rabl", "w") do |f|
17
16
  f.puts "content"
18
17
  end
@@ -26,15 +25,14 @@ context "Rabl::Partials" do
26
25
 
27
26
  asserts(:first).equals {["content\n", (tmp_path + "test.json.rabl").to_s ]}
28
27
  asserts(:last).equals {["content_v1\n", (tmp_path + "test_v1.json.rabl").to_s ]}
29
- teardown { Object.send(:remove_const, :Rails) }
28
+ teardown { Object.send(:remove_const, :Sinatra) }
30
29
  end
31
30
 
32
- context "fetch_source" do
31
+ context "fetch_source with rabl" do
33
32
  helper(:tmp_path) { @tmp_path ||= Pathname.new(Dir.mktmpdir) }
34
33
 
35
34
  setup do
36
- ::Rails = stub(Class.new)
37
- ::Rails.root.returns(tmp_path)
35
+ ::Sinatra = stub(Class.new)
38
36
  File.open(tmp_path + "test.rabl", "w") do |f|
39
37
  f.puts "content"
40
38
  end
@@ -43,15 +41,14 @@ context "Rabl::Partials" do
43
41
  asserts('detects file.rabl') { topic }.equals do
44
42
  ["content\n", (tmp_path + 'test.rabl').to_s]
45
43
  end
46
- teardown { Object.send(:remove_const, :Rails) }
44
+ teardown { Object.send(:remove_const, :Sinatra) }
47
45
  end
48
46
 
49
- context "fetch_source" do
47
+ context "fetch_source with view_path" do
50
48
  helper(:tmp_path) { @tmp_path ||= Pathname.new(Dir.mktmpdir) }
51
49
 
52
50
  setup do
53
- ::Rails = stub(Class.new)
54
- ::Rails.root.returns(tmp_path)
51
+ ::Sinatra = stub(Class.new)
55
52
  File.open(tmp_path + "test.rabl", "w") do |f|
56
53
  f.puts "content"
57
54
  end
@@ -63,6 +60,6 @@ context "Rabl::Partials" do
63
60
  asserts('detects file.json.rabl first') { topic }.equals do
64
61
  ["content2\n", (tmp_path + 'test.json.rabl').to_s]
65
62
  end
66
- teardown { Object.send(:remove_const, :Rails) }
63
+ teardown { Object.send(:remove_const, :Sinatra) }
67
64
  end
68
65
  end
metadata CHANGED
@@ -2,7 +2,7 @@
2
2
  name: rabl
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: 0.6.2
5
+ version: 0.6.3
6
6
  platform: ruby
7
7
  authors:
8
8
  - Nathan Esquenazi
@@ -10,7 +10,7 @@ autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
12
 
13
- date: 2012-03-21 00:00:00 Z
13
+ date: 2012-03-27 00:00:00 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: multi_json
@@ -255,6 +255,72 @@ files:
255
255
  - fixtures/rails3/test/functional/posts_controller_test.rb
256
256
  - fixtures/rails3/test/functional/users_controller_test.rb
257
257
  - fixtures/rails3/test/test_helper.rb
258
+ - fixtures/rails3_2/.gitignore
259
+ - fixtures/rails3_2/Gemfile
260
+ - fixtures/rails3_2/README.rdoc
261
+ - fixtures/rails3_2/Rakefile
262
+ - fixtures/rails3_2/app/assets/images/rails.png
263
+ - fixtures/rails3_2/app/assets/javascripts/application.js
264
+ - fixtures/rails3_2/app/assets/javascripts/posts.js.coffee
265
+ - fixtures/rails3_2/app/assets/javascripts/users.js.coffee
266
+ - fixtures/rails3_2/app/assets/stylesheets/application.css
267
+ - fixtures/rails3_2/app/assets/stylesheets/posts.css.scss
268
+ - fixtures/rails3_2/app/assets/stylesheets/users.css.scss
269
+ - fixtures/rails3_2/app/controllers/application_controller.rb
270
+ - fixtures/rails3_2/app/controllers/posts_controller.rb
271
+ - fixtures/rails3_2/app/controllers/users_controller.rb
272
+ - fixtures/rails3_2/app/helpers/application_helper.rb
273
+ - fixtures/rails3_2/app/helpers/posts_helper.rb
274
+ - fixtures/rails3_2/app/helpers/users_helper.rb
275
+ - fixtures/rails3_2/app/mailers/.gitkeep
276
+ - fixtures/rails3_2/config.ru
277
+ - fixtures/rails3_2/config/application.rb
278
+ - fixtures/rails3_2/config/boot.rb
279
+ - fixtures/rails3_2/config/database.yml
280
+ - fixtures/rails3_2/config/environment.rb
281
+ - fixtures/rails3_2/config/environments/development.rb
282
+ - fixtures/rails3_2/config/environments/production.rb
283
+ - fixtures/rails3_2/config/environments/test.rb
284
+ - fixtures/rails3_2/config/initializers/backtrace_silencers.rb
285
+ - fixtures/rails3_2/config/initializers/inflections.rb
286
+ - fixtures/rails3_2/config/initializers/mime_types.rb
287
+ - fixtures/rails3_2/config/initializers/secret_token.rb
288
+ - fixtures/rails3_2/config/initializers/session_store.rb
289
+ - fixtures/rails3_2/config/initializers/wrap_parameters.rb
290
+ - fixtures/rails3_2/config/locales/en.yml
291
+ - fixtures/rails3_2/config/routes.rb
292
+ - fixtures/rails3_2/db/schema.rb
293
+ - fixtures/rails3_2/db/seeds.rb
294
+ - fixtures/rails3_2/doc/README_FOR_APP
295
+ - fixtures/rails3_2/lib/assets/.gitkeep
296
+ - fixtures/rails3_2/lib/tasks/.gitkeep
297
+ - fixtures/rails3_2/log/.gitkeep
298
+ - fixtures/rails3_2/public/404.html
299
+ - fixtures/rails3_2/public/422.html
300
+ - fixtures/rails3_2/public/500.html
301
+ - fixtures/rails3_2/public/favicon.ico
302
+ - fixtures/rails3_2/public/index.html
303
+ - fixtures/rails3_2/public/robots.txt
304
+ - fixtures/rails3_2/script/rails
305
+ - fixtures/rails3_2/test/fixtures/.gitkeep
306
+ - fixtures/rails3_2/test/fixtures/phone_numbers.yml
307
+ - fixtures/rails3_2/test/fixtures/posts.yml
308
+ - fixtures/rails3_2/test/fixtures/users.yml
309
+ - fixtures/rails3_2/test/functional/.gitkeep
310
+ - fixtures/rails3_2/test/functional/posts_controller_test.rb
311
+ - fixtures/rails3_2/test/functional/users_controller_test.rb
312
+ - fixtures/rails3_2/test/integration/.gitkeep
313
+ - fixtures/rails3_2/test/performance/browsing_test.rb
314
+ - fixtures/rails3_2/test/test_helper.rb
315
+ - fixtures/rails3_2/test/unit/.gitkeep
316
+ - fixtures/rails3_2/test/unit/helpers/posts_helper_test.rb
317
+ - fixtures/rails3_2/test/unit/helpers/users_helper_test.rb
318
+ - fixtures/rails3_2/test/unit/phone_number_test.rb
319
+ - fixtures/rails3_2/test/unit/post_test.rb
320
+ - fixtures/rails3_2/test/unit/user_test.rb
321
+ - fixtures/rails3_2/vendor/assets/javascripts/.gitkeep
322
+ - fixtures/rails3_2/vendor/assets/stylesheets/.gitkeep
323
+ - fixtures/rails3_2/vendor/plugins/.gitkeep
258
324
  - fixtures/sinatra_test/Gemfile
259
325
  - fixtures/sinatra_test/Rakefile
260
326
  - fixtures/sinatra_test/app.rb