rabl-rails 0.3.1 → 0.3.2

Sign up to get free protection for your applications and to get access to all the features.
data/CHANGELOG.md CHANGED
@@ -1,6 +1,10 @@
1
1
  # CHANGELOG
2
2
 
3
- ## 0.3.2 (unreleased)
3
+ ## 0.3.2
4
+ * Using child with a nil value will be correctly formatted as nil
5
+ * Allow controller's assigns to have symbol keys
6
+ * Does not modify in place format extracted from context
7
+ * Add JSONP support
4
8
 
5
9
  ## 0.3.1
6
10
  * Add `merge` keywork
data/README.md CHANGED
@@ -95,6 +95,7 @@ RablRails.configure do |config|
95
95
  # config.xml_engine = 'LibXML'
96
96
  # config.use_custom_responder = false
97
97
  # config.default_responder_template = 'show'
98
+ # config.enable_jsonp_callbacks = false
98
99
  end
99
100
  ```
100
101
 
@@ -207,7 +208,7 @@ node(:full_name) { |u| u.first_name + " " + u.last_name }
207
208
  You can add condition on your custom nodes (if the condition is evaluated to false, the node will not be included).
208
209
 
209
210
  ```ruby
210
- node(:email, if: -> { |u| u.valid_email? }) do |u|
211
+ node(:email, if: ->(u) { u.valid_email? }) do |u|
211
212
  u.email
212
213
  end
213
214
  ```
@@ -14,8 +14,7 @@ module RablRails
14
14
 
15
15
  compiled_template = compile_template_from_source(source, path)
16
16
 
17
- format = context.params[:format] ? context.params[:format].to_s : 'json'
18
- format.upcase!
17
+ format = context.params[:format] ? context.params[:format].to_s.upcase : :JSON
19
18
  Renderers.const_get(format).new(context, locals).render(compiled_template)
20
19
  end
21
20
 
@@ -30,7 +29,7 @@ module RablRails
30
29
 
31
30
  def compile_template_from_path(path)
32
31
  return @cached_templates[path].dup if @cached_templates.has_key?(path)
33
-
32
+
34
33
  t = @lookup_context.find_template(path, [], false)
35
34
  compile_template_from_source(t.source, path)
36
35
  end
@@ -57,11 +57,11 @@ module RablRails
57
57
  result = instance_exec data, &value
58
58
 
59
59
  if key.to_s.start_with?('_') # merge
60
- raise PartialError, '`merge` block should return a hash' unless result.is_a?(Hash)
60
+ raise PartialError, '`merge` block should return a hash' unless result.is_a?(Hash)
61
61
  output.merge!(result)
62
62
  next output
63
63
  else # node
64
- result
64
+ result
65
65
  end
66
66
  when Array # node with condition
67
67
  next output if !instance_exec data, &(value.first)
@@ -81,7 +81,11 @@ module RablRails
81
81
  output.merge!(render_resource(object, current_value))
82
82
  next output
83
83
  else # child
84
- object.respond_to?(:each) ? render_collection(object, current_value) : render_resource(object, current_value)
84
+ if object
85
+ object.respond_to?(:each) ? render_collection(object, current_value) : render_resource(object, current_value)
86
+ else
87
+ nil
88
+ end
85
89
  end
86
90
  when Condition
87
91
  if instance_exec data, &(value.proc)
@@ -94,6 +98,10 @@ module RablRails
94
98
  }
95
99
  end
96
100
 
101
+ def params
102
+ @_context.params
103
+ end
104
+
97
105
  #
98
106
  # Call the render_resource mtehod on each object of the collection
99
107
  # and return an array of the returned values.
@@ -132,7 +140,7 @@ module RablRails
132
140
  #
133
141
  def setup_render_context
134
142
  @_context.instance_variable_get(:@_assigns).each_pair { |k, v|
135
- instance_variable_set("@#{k}", v) unless k.start_with?('_')
143
+ instance_variable_set("@#{k}", v) unless k.to_s.start_with?('_')
136
144
  }
137
145
  end
138
146
  end
@@ -3,7 +3,9 @@ module RablRails
3
3
  class JSON < Base
4
4
  def format_output(hash)
5
5
  hash = { _options[:root_name] => hash } if _options[:root_name] && RablRails.include_json_root
6
- MultiJson.encode(hash)
6
+ json = MultiJson.encode(hash)
7
+
8
+ RablRails.enable_jsonp_callbacks && params.has_key?(:callback) ? "#{params[:callback]}(#{json})" : json
7
9
  end
8
10
  end
9
11
  end
@@ -1,3 +1,3 @@
1
1
  module RablRails
2
- VERSION = '0.3.1'
2
+ VERSION = '0.3.2'
3
3
  end
data/lib/rabl-rails.rb CHANGED
@@ -39,6 +39,9 @@ module RablRails
39
39
  mattr_accessor :include_plist_root
40
40
  @@include_plist_root = nil
41
41
 
42
+ mattr_accessor :enable_jsonp_callbacks
43
+ @@enable_jsonp_callbacks = false
44
+
42
45
  def self.configure
43
46
  yield self
44
47
 
data/rabl-rails.gemspec CHANGED
@@ -8,8 +8,8 @@ Gem::Specification.new do |s|
8
8
  s.authors = ["Christopher Cocchi-Perrier"]
9
9
  s.email = ["cocchi.c@gmail.com"]
10
10
  s.homepage = "https://github.com/ccocchi/rabl-rails"
11
- s.summary = "Fast Rails 3+ templating system with JSON and XML support"
12
- s.description = "Fast Rails 3+ templating system with JSON and XML support"
11
+ s.summary = "Fast Rails 3+ templating system with JSON, XML and PList support"
12
+ s.description = "Fast Rails 3+ templating system with JSON, XML and PList support"
13
13
 
14
14
  s.files = `git ls-files`.split("\n")
15
15
  s.test_files = `git ls-files -- test/*`.split("\n")
@@ -19,4 +19,5 @@ Gem::Specification.new do |s|
19
19
  s.add_dependency "railties", "~> 3.0"
20
20
 
21
21
  s.add_development_dependency "actionpack", "~> 3.0"
22
+ s.add_development_dependency "rspec-mocks"
22
23
  end
@@ -0,0 +1,35 @@
1
+ require 'test_helper'
2
+
3
+ class TestBaseRenderer < ActiveSupport::TestCase
4
+
5
+ RablRails::Renderers::Base.class_eval do
6
+ def format_output(hash)
7
+ hash
8
+ end
9
+ end
10
+
11
+ setup do
12
+ @data = User.new(1, 'foobar', 'male')
13
+
14
+ @context = Context.new
15
+ @context.assigns['data'] = @data
16
+
17
+ @template = RablRails::CompiledTemplate.new
18
+ @template.data = :@data
19
+ end
20
+
21
+ def render_hash
22
+ RablRails::Renderers::Base.new(@context).render(@template)
23
+ end
24
+
25
+ test "child with nil data should render nil" do
26
+ @template.source = { :author => { :_data => :@nil, :name => :name } }
27
+ assert_equal({ :author => nil }, render_hash)
28
+ end
29
+
30
+ test "properly handle assigns with symbol keys" do
31
+ @context.assigns[:foo] = 'bar'
32
+ @template.source = {}
33
+ assert_nothing_raised { render_hash }
34
+ end
35
+ end
data/test/keyword_test.rb CHANGED
@@ -25,7 +25,7 @@ class KeywordTest < ActiveSupport::TestCase
25
25
  @context.stub(lookup_context: nil)
26
26
  end
27
27
 
28
- test "collections model should render correctly" do
28
+ test "collections model should not collide with rabl-rails reserved keyword" do
29
29
  source = %{
30
30
  object :@user
31
31
  child(:@collections => :collections) do
@@ -13,7 +13,7 @@ class TestJsonRenderer < ActiveSupport::TestCase
13
13
  end
14
14
 
15
15
  def render_json_output
16
- RablRails::Renderers::JSON.new(@context).render(@template).to_s
16
+ RablRails::Renderers::JSON.new(@context).render(@template)
17
17
  end
18
18
 
19
19
  test "render object wth empty template" do
@@ -167,4 +167,11 @@ class TestJsonRenderer < ActiveSupport::TestCase
167
167
  @template.source = { :_merge0 => ->(c) { { :custom => c.name } } }
168
168
  assert_equal %q({"custom":"foobar"}), render_json_output
169
169
  end
170
+
171
+ test "render with jsonp callback" do
172
+ RablRails.enable_jsonp_callbacks = true
173
+ @context.stub(:params).and_return({ callback: 'some_callback' })
174
+ @template.source = { :name => :name }
175
+ assert_equal %q[some_callback({"name":"foobar"})], render_json_output
176
+ end
170
177
  end
data/test/test_helper.rb CHANGED
@@ -2,7 +2,9 @@ ENV["RAILS_ENV"] = "test"
2
2
  $:.unshift File.expand_path('../../lib', __FILE__)
3
3
 
4
4
  require 'rspec/mocks'
5
- require 'minitest/autorun'
5
+ require 'minitest/unit'
6
+ MiniTest::Unit.autorun
7
+
6
8
  require 'active_support/test_case'
7
9
 
8
10
  require 'action_controller'
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rabl-rails
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.1
4
+ version: 0.3.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-03-05 00:00:00.000000000 Z
12
+ date: 2013-05-28 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: activesupport
@@ -59,7 +59,23 @@ dependencies:
59
59
  - - ~>
60
60
  - !ruby/object:Gem::Version
61
61
  version: '3.0'
62
- description: Fast Rails 3+ templating system with JSON and XML support
62
+ - !ruby/object:Gem::Dependency
63
+ name: rspec-mocks
64
+ requirement: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ! '>='
68
+ - !ruby/object:Gem::Version
69
+ version: '0'
70
+ type: :development
71
+ prerelease: false
72
+ version_requirements: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ! '>='
76
+ - !ruby/object:Gem::Version
77
+ version: '0'
78
+ description: Fast Rails 3+ templating system with JSON, XML and PList support
63
79
  email:
64
80
  - cocchi.c@gmail.com
65
81
  executables: []
@@ -89,6 +105,7 @@ files:
89
105
  - lib/rabl-rails/version.rb
90
106
  - lib/tasks/rabl-rails.rake
91
107
  - rabl-rails.gemspec
108
+ - test/base_renderer_test.rb
92
109
  - test/cache_templates_test.rb
93
110
  - test/compiler_test.rb
94
111
  - test/deep_nesting_test.rb
@@ -119,11 +136,12 @@ required_rubygems_version: !ruby/object:Gem::Requirement
119
136
  version: '0'
120
137
  requirements: []
121
138
  rubyforge_project:
122
- rubygems_version: 1.8.24
139
+ rubygems_version: 1.8.21
123
140
  signing_key:
124
141
  specification_version: 3
125
- summary: Fast Rails 3+ templating system with JSON and XML support
142
+ summary: Fast Rails 3+ templating system with JSON, XML and PList support
126
143
  test_files:
144
+ - test/base_renderer_test.rb
127
145
  - test/cache_templates_test.rb
128
146
  - test/compiler_test.rb
129
147
  - test/deep_nesting_test.rb