rabl-rails 0.1.2 → 0.1.3
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGELOG.md +5 -0
- data/lib/rabl-rails.rb +15 -5
- data/lib/rabl-rails/compiler.rb +1 -1
- data/lib/rabl-rails/renderers/base.rb +10 -2
- data/lib/rabl-rails/responder.rb +22 -0
- data/lib/rabl-rails/version.rb +1 -1
- data/test/deep_nesting_test.rb +0 -1
- data/test/renderers/json_renderer_test.rb +18 -7
- data/test/test_helper.rb +10 -3
- metadata +3 -2
data/CHANGELOG.md
CHANGED
@@ -1,5 +1,10 @@
|
|
1
1
|
# CHANGELOG
|
2
2
|
|
3
|
+
## 0.1.3
|
4
|
+
* Render correcly when variables are not passed via the assigns ivar but as helper methods
|
5
|
+
(decent_exposure, focused_controller)
|
6
|
+
* Add custom Responder
|
7
|
+
|
3
8
|
## 0.1.2
|
4
9
|
* Add RablRails#render method (see README or source code)
|
5
10
|
* Fix fail when JSON engine is not found. Now fallback to MultiJson.default_adapter
|
data/lib/rabl-rails.rb
CHANGED
@@ -17,31 +17,41 @@ require 'multi_json'
|
|
17
17
|
|
18
18
|
module RablRails
|
19
19
|
extend Renderer
|
20
|
-
|
20
|
+
|
21
|
+
autoload :Responder, 'rabl-rails/responder'
|
22
|
+
|
21
23
|
mattr_accessor :cache_templates
|
22
24
|
@@cache_templates = true
|
23
|
-
|
25
|
+
|
24
26
|
mattr_accessor :include_json_root
|
25
27
|
@@include_json_root = true
|
26
28
|
|
27
29
|
mattr_reader :json_engine
|
28
30
|
@@json_engine = :yajl
|
29
31
|
|
32
|
+
mattr_accessor :use_custom_responder
|
33
|
+
@@use_custom_responder = false
|
34
|
+
|
35
|
+
mattr_accessor :responder_default_template
|
36
|
+
@@responder_default_template = 'show'
|
37
|
+
|
30
38
|
def self.configure
|
31
39
|
yield self
|
40
|
+
|
41
|
+
ActionController::Base.responder = Responder if self.use_custom_responder
|
32
42
|
end
|
33
|
-
|
43
|
+
|
34
44
|
def self.json_engine=(name)
|
35
45
|
MultiJson.engine = name
|
36
46
|
@@json_engine = name
|
37
47
|
rescue LoadError
|
38
48
|
Rails.logger.warn %Q(WARNING: rabl-rails could not load "#{self.json_engine}" as JSON engine, fallback to default)
|
39
49
|
end
|
40
|
-
|
50
|
+
|
41
51
|
def self.cache_templates?
|
42
52
|
ActionController::Base.perform_caching && @@cache_templates
|
43
53
|
end
|
44
|
-
|
54
|
+
|
45
55
|
def self.load_default_engines!
|
46
56
|
self.json_engine = :yajl
|
47
57
|
end
|
data/lib/rabl-rails/compiler.rb
CHANGED
@@ -18,7 +18,9 @@ module RablRails
|
|
18
18
|
# method defined by the renderer.
|
19
19
|
#
|
20
20
|
def render(template)
|
21
|
-
collection_or_resource =
|
21
|
+
collection_or_resource = if template.data
|
22
|
+
template.data.to_s.start_with?('@') ? instance_variable_get(template.data) : @_context.send(template.data)
|
23
|
+
end
|
22
24
|
collection_or_resource = @_context.target_object unless collection_or_resource || template.data == false || !@_context.respond_to?(:target_object)
|
23
25
|
output_hash = collection_or_resource.respond_to?(:each) ? render_collection(collection_or_resource, template.source) :
|
24
26
|
render_resource(collection_or_resource, template.source)
|
@@ -55,7 +57,13 @@ module RablRails
|
|
55
57
|
when Hash
|
56
58
|
current_value = value.dup
|
57
59
|
data_symbol = current_value.delete(:_data)
|
58
|
-
object =
|
60
|
+
object = if data_symbol == nil
|
61
|
+
data
|
62
|
+
else
|
63
|
+
data_symbol.to_s.start_with?('@') ? instance_variable_get(data_symbol)
|
64
|
+
: data.respond_to?(data_symbol) ? data.send(data_symbol)
|
65
|
+
: send(data_symbol)
|
66
|
+
end
|
59
67
|
|
60
68
|
if key.to_s.start_with?('_') # glue
|
61
69
|
current_value.each_pair { |k, v|
|
@@ -0,0 +1,22 @@
|
|
1
|
+
module RablRails
|
2
|
+
#
|
3
|
+
# Override default responder's api behavior to not
|
4
|
+
# user to_format methods on a resource as a default
|
5
|
+
# representation but instead use a rabl template
|
6
|
+
#
|
7
|
+
class Responder < ActionController::Responder
|
8
|
+
protected
|
9
|
+
|
10
|
+
def api_behavior(error)
|
11
|
+
rabl_options = options.merge(template: RablRails.responder_default_template)
|
12
|
+
|
13
|
+
if get?
|
14
|
+
controller.default_render rabl_options
|
15
|
+
elsif post?
|
16
|
+
controller.default_render rabl_options.merge!(status: :created, location: api_location)
|
17
|
+
else
|
18
|
+
head :no_content
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
data/lib/rabl-rails/version.rb
CHANGED
data/test/deep_nesting_test.rb
CHANGED
@@ -19,7 +19,6 @@ class DeepNestingTest < ActiveSupport::TestCase
|
|
19
19
|
@post = Post.new(42, 'I rock !')
|
20
20
|
@user = User.new(1, 'foobar', 'male')
|
21
21
|
@user.stub(:posts).and_return([@post])
|
22
|
-
@user.stub(:respond_to?).with(:each).and_return(false)
|
23
22
|
|
24
23
|
@context = Context.new
|
25
24
|
@context.assigns['user'] = @user
|
@@ -4,7 +4,6 @@ class TestJsonRenderer < ActiveSupport::TestCase
|
|
4
4
|
|
5
5
|
setup do
|
6
6
|
@data = User.new(1, 'foobar', 'male')
|
7
|
-
@data.stub(:respond_to?).with(:each).and_return(false)
|
8
7
|
|
9
8
|
@context = Context.new
|
10
9
|
@context.assigns['data'] = @data
|
@@ -28,6 +27,13 @@ class TestJsonRenderer < ActiveSupport::TestCase
|
|
28
27
|
assert_equal %q([{}]), render_json_output
|
29
28
|
end
|
30
29
|
|
30
|
+
test "render object with local methods (used by decent_exposure)" do
|
31
|
+
@context.stub(:user).and_return(@data)
|
32
|
+
@template.data = :user
|
33
|
+
@template.source = { :id => :id }
|
34
|
+
assert_equal %q({"id":1}), render_json_output
|
35
|
+
end
|
36
|
+
|
31
37
|
test "render single object attributes" do
|
32
38
|
@template.source = { :id => :id, :name => :name }
|
33
39
|
assert_equal %q({"id":1,"name":"foobar"}), render_json_output
|
@@ -44,6 +50,12 @@ class TestJsonRenderer < ActiveSupport::TestCase
|
|
44
50
|
assert_equal %q({"author":{"name":"foobar"}}), render_json_output
|
45
51
|
end
|
46
52
|
|
53
|
+
test "render child with local methods (used by decent_exposure)" do
|
54
|
+
@context.stub(:user).and_return(@data)
|
55
|
+
@template.source = { :author => { :_data => :user, :name => :name } }
|
56
|
+
assert_equal %q({"author":{"name":"foobar"}}), render_json_output
|
57
|
+
end
|
58
|
+
|
47
59
|
test "render glued attributes from single object" do
|
48
60
|
@template.source = { :_glue0 => { :_data => :@data, :name => :name } }
|
49
61
|
assert_equal %q({"name":"foobar"}), render_json_output
|
@@ -75,7 +87,7 @@ class TestJsonRenderer < ActiveSupport::TestCase
|
|
75
87
|
@template.source = { :name => [condition, proc] }
|
76
88
|
assert_equal %q({}), render_json_output
|
77
89
|
end
|
78
|
-
|
90
|
+
|
79
91
|
test "node with context method call" do
|
80
92
|
@context.stub(:respond_to?).with(:context_method).and_return(true)
|
81
93
|
@context.stub(:context_method).and_return('marty')
|
@@ -86,7 +98,6 @@ class TestJsonRenderer < ActiveSupport::TestCase
|
|
86
98
|
|
87
99
|
test "partial should be evaluated at rendering time" do
|
88
100
|
# Set assigns
|
89
|
-
@data.stub(:respond_to?).with(:empty?).and_return(false)
|
90
101
|
@context.assigns['user'] = @data
|
91
102
|
|
92
103
|
# Stub Library#get
|
@@ -114,17 +125,17 @@ class TestJsonRenderer < ActiveSupport::TestCase
|
|
114
125
|
|
115
126
|
assert_equal %q({"users":[]}), render_json_output
|
116
127
|
end
|
117
|
-
|
128
|
+
|
118
129
|
test "render object with root node" do
|
119
130
|
@template.root_name = :author
|
120
131
|
@template.source = { :id => :id, :name => :name }
|
121
|
-
assert_equal %q({"author":{"id":1,"name":"foobar"}}), render_json_output
|
132
|
+
assert_equal %q({"author":{"id":1,"name":"foobar"}}), render_json_output
|
122
133
|
end
|
123
|
-
|
134
|
+
|
124
135
|
test "render object with root options set to false" do
|
125
136
|
RablRails.include_json_root = false
|
126
137
|
@template.root_name = :author
|
127
138
|
@template.source = { :id => :id, :name => :name }
|
128
|
-
assert_equal %q({"id":1,"name":"foobar"}), render_json_output
|
139
|
+
assert_equal %q({"id":1,"name":"foobar"}), render_json_output
|
129
140
|
end
|
130
141
|
end
|
data/test/test_helper.rb
CHANGED
@@ -32,12 +32,12 @@ end
|
|
32
32
|
|
33
33
|
class Context
|
34
34
|
attr_writer :virtual_path
|
35
|
-
|
35
|
+
|
36
36
|
def initialize
|
37
37
|
@_assigns = {}
|
38
38
|
@virtual_path = nil
|
39
39
|
end
|
40
|
-
|
40
|
+
|
41
41
|
def assigns
|
42
42
|
@_assigns
|
43
43
|
end
|
@@ -47,4 +47,11 @@ class Context
|
|
47
47
|
end
|
48
48
|
end
|
49
49
|
|
50
|
-
User
|
50
|
+
class User
|
51
|
+
attr_accessor :id, :name, :sex
|
52
|
+
def initialize(id=nil, name=nil, sex=nil)
|
53
|
+
@id = id
|
54
|
+
@name = name
|
55
|
+
@sex = sex
|
56
|
+
end
|
57
|
+
end
|
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.1.
|
4
|
+
version: 0.1.3
|
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: 2012-
|
12
|
+
date: 2012-09-12 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: activesupport
|
@@ -80,6 +80,7 @@ files:
|
|
80
80
|
- lib/rabl-rails/renderer.rb
|
81
81
|
- lib/rabl-rails/renderers/base.rb
|
82
82
|
- lib/rabl-rails/renderers/json.rb
|
83
|
+
- lib/rabl-rails/responder.rb
|
83
84
|
- lib/rabl-rails/template.rb
|
84
85
|
- lib/rabl-rails/version.rb
|
85
86
|
- lib/tasks/rabl-rails.rake
|