rabl-rails 0.4.0 → 0.4.1
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGELOG.md +4 -0
- data/README.md +1 -0
- data/lib/rabl-rails/configuration.rb +6 -1
- data/lib/rabl-rails/handler.rb +0 -3
- data/lib/rabl-rails/helpers.rb +4 -1
- data/lib/rabl-rails/library.rb +12 -2
- data/lib/rabl-rails/renderer.rb +5 -1
- data/lib/rabl-rails/version.rb +1 -1
- data/test/helper.rb +13 -1
- data/test/test_helpers.rb +23 -0
- data/test/test_library.rb +9 -21
- metadata +4 -2
data/CHANGELOG.md
CHANGED
@@ -1,5 +1,9 @@
|
|
1
1
|
# CHANGELOG
|
2
2
|
|
3
|
+
## 0.4.1
|
4
|
+
* Make classes that should not be treated as collection configurable
|
5
|
+
* Internal change to determine rendering format
|
6
|
+
|
3
7
|
## 0.4.0
|
4
8
|
* Internal cleanup and refactor
|
5
9
|
* Remove the `allow_empty_format_in_template` option, since it has become
|
data/README.md
CHANGED
@@ -99,6 +99,7 @@ RablRails.configure do |config|
|
|
99
99
|
# config.replace_nil_values_with_empty_strings = false
|
100
100
|
# config.replace_empty_string_values_with_nil = false
|
101
101
|
# config.exclude_nil_values = false
|
102
|
+
# config.non_collection_classes = Set.new(['Struct'])
|
102
103
|
end
|
103
104
|
```
|
104
105
|
|
@@ -1,3 +1,5 @@
|
|
1
|
+
require 'set'
|
2
|
+
|
1
3
|
module RablRails
|
2
4
|
class Configuration
|
3
5
|
attr_accessor :json_engine, :include_json_root, :enable_jsonp_callbacks
|
@@ -9,6 +11,7 @@ module RablRails
|
|
9
11
|
attr_accessor :replace_nil_values_with_empty_strings
|
10
12
|
attr_accessor :replace_empty_string_values_with_nil
|
11
13
|
attr_accessor :exclude_nil_values
|
14
|
+
attr_accessor :non_collection_classes
|
12
15
|
|
13
16
|
def initialize
|
14
17
|
@json_engine = defined?(::Oj) ? ::Oj : ::JSON
|
@@ -20,7 +23,7 @@ module RablRails
|
|
20
23
|
@plist_engine = defined?(::Plist) ? ::Plist::Emit : nil
|
21
24
|
@include_plist_root = false
|
22
25
|
|
23
|
-
@cache_templates
|
26
|
+
@cache_templates = ActionController::Base.perform_caching
|
24
27
|
|
25
28
|
@use_custom_responder = false
|
26
29
|
@responder_default_template = 'show'
|
@@ -28,6 +31,8 @@ module RablRails
|
|
28
31
|
@replace_nil_values_with_empty_strings = false
|
29
32
|
@replace_empty_string_values_with_nil = false
|
30
33
|
@exclude_nil_values = false
|
34
|
+
|
35
|
+
@non_collection_classes = Set.new(['Struct'])
|
31
36
|
end
|
32
37
|
|
33
38
|
def use_custom_responder=(value)
|
data/lib/rabl-rails/handler.rb
CHANGED
data/lib/rabl-rails/helpers.rb
CHANGED
@@ -1,7 +1,10 @@
|
|
1
1
|
module RablRails
|
2
2
|
module Helpers
|
3
3
|
def collection?(resource)
|
4
|
-
|
4
|
+
klass = resource.class
|
5
|
+
|
6
|
+
resource && resource.respond_to?(:each) &&
|
7
|
+
klass.ancestors.none? { |a| RablRails.configuration.non_collection_classes.include? a.name }
|
5
8
|
end
|
6
9
|
end
|
7
10
|
end
|
data/lib/rabl-rails/library.rb
CHANGED
@@ -6,6 +6,15 @@ module RablRails
|
|
6
6
|
class Library
|
7
7
|
include Singleton
|
8
8
|
|
9
|
+
UnknownFormat = Class.new(StandardError)
|
10
|
+
|
11
|
+
RENDERER_MAP = {
|
12
|
+
json: Renderers::JSON,
|
13
|
+
xml: Renderers::XML,
|
14
|
+
ruby: Renderers::Hash,
|
15
|
+
plist: Renderers::PLIST
|
16
|
+
}.freeze
|
17
|
+
|
9
18
|
def initialize
|
10
19
|
@cached_templates = ThreadSafe::Cache.new
|
11
20
|
@mutex = Monitor.new
|
@@ -17,8 +26,9 @@ module RablRails
|
|
17
26
|
|
18
27
|
def get_rendered_template(source, view, locals = nil)
|
19
28
|
compiled_template = compile_template_from_source(source, view)
|
20
|
-
format = view.
|
21
|
-
|
29
|
+
format = view.lookup_context.rendered_format || :json
|
30
|
+
raise UnknownFormat, "#{format} is not supported in rabl-rails" unless RENDERER_MAP.key?(format)
|
31
|
+
RENDERER_MAP[format].render(compiled_template, view, locals)
|
22
32
|
end
|
23
33
|
|
24
34
|
def compile_template_from_source(source, view)
|
data/lib/rabl-rails/renderer.rb
CHANGED
@@ -16,6 +16,10 @@ module RablRails
|
|
16
16
|
@format = format.downcase
|
17
17
|
end
|
18
18
|
|
19
|
+
def rendered_format
|
20
|
+
@format.to_sym
|
21
|
+
end
|
22
|
+
|
19
23
|
#
|
20
24
|
# Manually find given rabl template file with given format.
|
21
25
|
# View path can be set via options, otherwise default Rails
|
@@ -42,7 +46,7 @@ module RablRails
|
|
42
46
|
|
43
47
|
def initialize(path, options)
|
44
48
|
@virtual_path = path
|
45
|
-
@format = options.delete(:format) ||
|
49
|
+
@format = options.delete(:format) || :json
|
46
50
|
@_assigns = {}
|
47
51
|
@options = options
|
48
52
|
|
data/lib/rabl-rails/version.rb
CHANGED
data/test/helper.rb
CHANGED
@@ -46,11 +46,23 @@ module ActionController
|
|
46
46
|
end
|
47
47
|
|
48
48
|
class Context
|
49
|
+
class LookupContext
|
50
|
+
def initialize(format)
|
51
|
+
@format = format
|
52
|
+
end
|
53
|
+
|
54
|
+
def rendered_format
|
55
|
+
@format.to_sym
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
49
59
|
attr_writer :virtual_path
|
60
|
+
attr_reader :lookup_context
|
50
61
|
|
51
|
-
def initialize
|
62
|
+
def initialize(format = :json)
|
52
63
|
@_assigns = {}
|
53
64
|
@virtual_path = nil
|
65
|
+
@lookup_context = LookupContext.new(format)
|
54
66
|
end
|
55
67
|
|
56
68
|
def assigns
|
@@ -0,0 +1,23 @@
|
|
1
|
+
require 'helper'
|
2
|
+
require 'set'
|
3
|
+
|
4
|
+
class TestHelpers < MINITEST_TEST_CLASS
|
5
|
+
include RablRails::Helpers
|
6
|
+
|
7
|
+
def test_collection_with_default
|
8
|
+
assert collection?(['foo'])
|
9
|
+
refute collection?(User.new(1))
|
10
|
+
end
|
11
|
+
|
12
|
+
NotACollection = Class.new do
|
13
|
+
def each; end
|
14
|
+
end
|
15
|
+
|
16
|
+
def test_collection_with_configuration
|
17
|
+
assert collection?(NotACollection.new)
|
18
|
+
|
19
|
+
with_configuration(:non_collection_classes, Set.new(['Struct', 'TestHelpers::NotACollection'])) do
|
20
|
+
refute collection?(NotACollection.new), 'NotACollection triggers #collection?'
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
data/test/test_library.rb
CHANGED
@@ -13,41 +13,29 @@ class TestLibrary < MINITEST_TEST_CLASS
|
|
13
13
|
|
14
14
|
describe '#get_rendered_template' do
|
15
15
|
it 'compiles and renders template' do
|
16
|
-
renderer = MiniTest::Mock.new
|
17
|
-
renderer.expect :render, '{}', [@template, @context, nil]
|
18
|
-
|
19
16
|
result = @library.stub :compile_template_from_source, @template do
|
20
|
-
|
21
|
-
@library.get_rendered_template '', @context
|
22
|
-
end
|
17
|
+
@library.get_rendered_template '', @context
|
23
18
|
end
|
24
19
|
|
25
20
|
assert_equal '{}', result
|
26
|
-
assert renderer.verify
|
27
21
|
end
|
28
22
|
|
29
|
-
it '
|
23
|
+
it 'uses for from lookup context' do
|
24
|
+
context = Context.new(:xml)
|
30
25
|
result = @library.stub :compile_template_from_source, @template do
|
31
|
-
|
32
|
-
|
33
|
-
@library.get_rendered_template '', @context
|
34
|
-
end
|
26
|
+
RablRails::Renderers::XML.stub :render, '<xml>' do
|
27
|
+
@library.get_rendered_template '', context
|
35
28
|
end
|
36
29
|
end
|
37
30
|
|
38
31
|
assert_equal '<xml>', result
|
39
32
|
end
|
40
33
|
|
41
|
-
it '
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
@library.get_rendered_template '', @context
|
46
|
-
end
|
47
|
-
end
|
34
|
+
it 'raises if format is not supported' do
|
35
|
+
context = Context.new(:unsupported)
|
36
|
+
@library.stub :compile_template_from_source, @template do
|
37
|
+
assert_raises(RablRails::Library::UnknownFormat) { @library.get_rendered_template '', context }
|
48
38
|
end
|
49
|
-
|
50
|
-
assert_equal '<plist>', result
|
51
39
|
end
|
52
40
|
end
|
53
41
|
|
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.4.
|
4
|
+
version: 0.4.1
|
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: 2014-
|
12
|
+
date: 2014-12-28 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: activesupport
|
@@ -108,6 +108,7 @@ files:
|
|
108
108
|
- test/test_compiler.rb
|
109
109
|
- test/test_configuration.rb
|
110
110
|
- test/test_hash_visitor.rb
|
111
|
+
- test/test_helpers.rb
|
111
112
|
- test/test_library.rb
|
112
113
|
- test/test_render.rb
|
113
114
|
homepage: https://github.com/ccocchi/rabl-rails
|
@@ -143,5 +144,6 @@ test_files:
|
|
143
144
|
- test/test_compiler.rb
|
144
145
|
- test/test_configuration.rb
|
145
146
|
- test/test_hash_visitor.rb
|
147
|
+
- test/test_helpers.rb
|
146
148
|
- test/test_library.rb
|
147
149
|
- test/test_render.rb
|