rabl 0.7.9 → 0.7.10
Sign up to get free protection for your applications and to get access to all the features.
- data/.travis.yml +1 -1
- data/CHANGELOG.md +8 -1
- data/README.md +12 -1
- data/lib/rabl.rb +1 -1
- data/lib/rabl/builder.rb +13 -1
- data/lib/rabl/configuration.rb +3 -0
- data/lib/rabl/helpers.rb +5 -1
- data/lib/rabl/partials.rb +2 -1
- data/lib/rabl/template.rb +1 -1
- data/lib/rabl/version.rb +1 -1
- data/test/builder_test.rb +17 -3
- data/test/configuration_test.rb +10 -0
- data/test/partials_test.rb +44 -0
- metadata +2 -2
data/.travis.yml
CHANGED
data/CHANGELOG.md
CHANGED
@@ -1,6 +1,13 @@
|
|
1
1
|
# CHANGELOG
|
2
2
|
|
3
|
-
## 0.7.
|
3
|
+
## 0.7.11
|
4
|
+
|
5
|
+
## 0.7.10
|
6
|
+
|
7
|
+
* Add early support for Rails 4 (Thanks @jopotts)
|
8
|
+
* Add configuration option for raising on missing attributes (Thanks @ReneB)
|
9
|
+
* Allow caching outside the Rails environment (Thanks @flyerhzm)
|
10
|
+
* Fix template lookup on Rails (Thanks @vimutter)
|
4
11
|
|
5
12
|
## 0.7.9
|
6
13
|
|
data/README.md
CHANGED
@@ -18,7 +18,7 @@ I wanted a simple and flexible system for generating my APIs. In particular, I w
|
|
18
18
|
Anyone who has tried the 'to_json' method used in ActiveRecord for generating a JSON response has felt the pain of this restrictive approach.
|
19
19
|
RABL is a general templating system created to solve these problems in an entirely new way.
|
20
20
|
|
21
|
-
For a breakdown of common misconceptions about RABL, please check out our guide to
|
21
|
+
For a breakdown of common misconceptions about RABL, please check out our guide to
|
22
22
|
[understanding RABL](https://github.com/nesquena/rabl/wiki/Understanding-RABL) which can help clear up any confusion about this project.
|
23
23
|
|
24
24
|
## Breaking Changes ##
|
@@ -115,6 +115,7 @@ Rabl.configure do |config|
|
|
115
115
|
# config.cache_all_output = false
|
116
116
|
# config.cache_sources = Rails.env != 'development' # Defaults to false
|
117
117
|
# config.cache_engine = Rabl::CacheEngine.new # Defaults to Rails cache
|
118
|
+
# config.perform_caching = false
|
118
119
|
# config.escape_all_output = false
|
119
120
|
# config.json_engine = nil # Any multi_json engines or a Class with #encode method
|
120
121
|
# config.msgpack_engine = nil # Defaults to ::MessagePack
|
@@ -129,6 +130,7 @@ Rabl.configure do |config|
|
|
129
130
|
# config.enable_json_callbacks = false
|
130
131
|
# config.xml_options = { :dasherize => true, :skip_types => false }
|
131
132
|
# config.view_paths = []
|
133
|
+
# config.raise_on_missing_attribute = true # Defaults to false
|
132
134
|
end
|
133
135
|
```
|
134
136
|
|
@@ -141,6 +143,10 @@ a root node by default. This allows you to further fine-tune your desired respon
|
|
141
143
|
|
142
144
|
If `cache_engine` is set, you should assign it to a class with a `fetch` method. See the [default engine](https://github.com/nesquena/rabl/blob/master/lib/rabl/cache_engine.rb) for an example.
|
143
145
|
|
146
|
+
If `perform_caching` is set to `true` then it will perform caching. You
|
147
|
+
can ignore this option if you are using Rails, it's same to Rails
|
148
|
+
`config.action_controller.perform_caching`
|
149
|
+
|
144
150
|
If `cache_sources` is set to `true`, template lookups will be cached for improved performance.
|
145
151
|
The cache can be reset manually by running `Rabl.reset_source_cache!` within your application.
|
146
152
|
|
@@ -154,6 +160,11 @@ Custom nodes will not be escaped, use `ERB::Util.h(value)`.
|
|
154
160
|
If `view_paths` is set to a path, this view path will be checked for every rabl template within your application.
|
155
161
|
Add to this path especially when including Rabl in an engine and using view paths within a another Rails app.
|
156
162
|
|
163
|
+
If `raise_on_missing_attribute` is set to `true`, a RuntimeError will be raised whenever Rabl
|
164
|
+
attempts to render an attribute that does not exist. Otherwise, the attribute will simply be omitted.
|
165
|
+
Setting this to true during development may help increase the robustness of your code, but using `true` in
|
166
|
+
production code is not recommended.
|
167
|
+
|
157
168
|
Note that the `json_engine` option uses [multi_json](http://intridea.com/2010/6/14/multi-json-the-swappable-json-handler) engine
|
158
169
|
defaults so that in most cases you **don't need to configure this** directly. For example, if you wish to use [oj](https://github.com/ohler55/oj) as
|
159
170
|
the primary JSON encoding engine simply add that to your Gemfile:
|
data/lib/rabl.rb
CHANGED
@@ -14,7 +14,7 @@ require 'rabl/configuration'
|
|
14
14
|
require 'rabl/renderer'
|
15
15
|
require 'rabl/cache_engine'
|
16
16
|
require 'rabl/json_engine'
|
17
|
-
require 'rabl/railtie' if defined?(Rails) && Rails.version =~ /^
|
17
|
+
require 'rabl/railtie' if defined?(Rails) && Rails.version =~ /^[34]/
|
18
18
|
|
19
19
|
# Rabl.register!
|
20
20
|
module Rabl
|
data/lib/rabl/builder.rb
CHANGED
@@ -65,7 +65,7 @@ module Rabl
|
|
65
65
|
# attribute :foo, :as => "bar"
|
66
66
|
# attribute :foo, :as => "bar", :if => lambda { |m| m.foo }
|
67
67
|
def attribute(name, options={})
|
68
|
-
if @_object &&
|
68
|
+
if @_object && attribute_present?(name) && resolve_condition(options)
|
69
69
|
@_result[options[:as] || name] = data_object_attribute(name)
|
70
70
|
end
|
71
71
|
end
|
@@ -127,6 +127,18 @@ module Rabl
|
|
127
127
|
|
128
128
|
private
|
129
129
|
|
130
|
+
# Checks if an attribute is present. If not, check if the configuration specifies that this is an error
|
131
|
+
# attribute_present?(created_at) => true
|
132
|
+
def attribute_present?(name)
|
133
|
+
if @_object.respond_to?(name)
|
134
|
+
return true
|
135
|
+
elsif Rabl.configuration.raise_on_missing_attribute
|
136
|
+
raise "Failed to render missing attribute #{name}"
|
137
|
+
else
|
138
|
+
return false
|
139
|
+
end
|
140
|
+
end
|
141
|
+
|
130
142
|
# Returns a guess at the format in this scope
|
131
143
|
# request_format => "xml"
|
132
144
|
def request_format
|
data/lib/rabl/configuration.rb
CHANGED
@@ -37,6 +37,8 @@ module Rabl
|
|
37
37
|
attr_accessor :escape_all_output
|
38
38
|
attr_accessor :view_paths
|
39
39
|
attr_accessor :cache_engine
|
40
|
+
attr_accessor :raise_on_missing_attribute
|
41
|
+
attr_accessor :perform_caching
|
40
42
|
|
41
43
|
DEFAULT_XML_OPTIONS = { :dasherize => true, :skip_types => false }
|
42
44
|
|
@@ -60,6 +62,7 @@ module Rabl
|
|
60
62
|
@escape_all_output = false
|
61
63
|
@view_paths = []
|
62
64
|
@cache_engine = Rabl::CacheEngine.new
|
65
|
+
@perform_caching = false
|
63
66
|
end
|
64
67
|
|
65
68
|
# @param [Symbol, Class] engine_name The name of a JSON engine,
|
data/lib/rabl/helpers.rb
CHANGED
@@ -101,7 +101,11 @@ module Rabl
|
|
101
101
|
|
102
102
|
# Returns true if the cache has been enabled for the application
|
103
103
|
def template_cache_configured?
|
104
|
-
defined?(Rails)
|
104
|
+
if defined?(Rails)
|
105
|
+
defined?(ActionController::Base) && ActionController::Base.perform_caching
|
106
|
+
else
|
107
|
+
Rabl.configuration.perform_caching
|
108
|
+
end
|
105
109
|
end
|
106
110
|
|
107
111
|
# Escape output if configured and supported
|
data/lib/rabl/partials.rb
CHANGED
@@ -76,7 +76,8 @@ module Rabl
|
|
76
76
|
source_format = rendered_format unless rendered_format == :html
|
77
77
|
context_scope.lookup_context.find(file, [], partial, [], {:formats => [source_format]})
|
78
78
|
end }
|
79
|
-
template = lookup_proc.call(false) rescue
|
79
|
+
template = lookup_proc.call(false) rescue nil
|
80
|
+
template ||= lookup_proc.call(true) rescue nil
|
80
81
|
template.identifier if template
|
81
82
|
elsif source_format && context_scope.respond_to?(:view_paths) # Rails 2
|
82
83
|
template = context_scope.view_paths.find_template(file, source_format, false)
|
data/lib/rabl/template.rb
CHANGED
@@ -41,7 +41,7 @@ if defined?(ActionView) && defined?(Rails) && Rails.version =~ /^2/
|
|
41
41
|
end
|
42
42
|
|
43
43
|
# Rails 3.X Template
|
44
|
-
if defined?(ActionView) && defined?(Rails) && Rails.version =~ /^
|
44
|
+
if defined?(ActionView) && defined?(Rails) && Rails.version =~ /^[34]/
|
45
45
|
module ActionView
|
46
46
|
module Template::Handlers
|
47
47
|
class Rabl
|
data/lib/rabl/version.rb
CHANGED
data/test/builder_test.rb
CHANGED
@@ -49,9 +49,23 @@ context "Rabl::Builder" do
|
|
49
49
|
build_hash @user, :attributes => { :name => {}, :city => { :as => :city } }
|
50
50
|
end.equivalent_to({:name => 'rabl', :city => 'irvine'})
|
51
51
|
|
52
|
-
|
53
|
-
|
54
|
-
|
52
|
+
context "that with a non-existent attribute" do
|
53
|
+
context "when non-existent attributes are allowed by the configuration" do
|
54
|
+
setup { stub(Rabl.configuration).raise_on_missing_attribute { false } }
|
55
|
+
|
56
|
+
asserts "the node" do
|
57
|
+
build_hash @user, :attributes => { :fake => :fake }
|
58
|
+
end.equals({})
|
59
|
+
end
|
60
|
+
|
61
|
+
context "when non-existent attributes are forbidden by the configuration" do
|
62
|
+
setup { stub(Rabl.configuration).raise_on_missing_attribute { true } }
|
63
|
+
|
64
|
+
asserts "the node" do
|
65
|
+
build_hash @user, :attributes => { :fake => :fake }
|
66
|
+
end.raises(RuntimeError)
|
67
|
+
end
|
68
|
+
end
|
55
69
|
end
|
56
70
|
|
57
71
|
context "#node" do
|
data/test/configuration_test.rb
CHANGED
@@ -42,4 +42,14 @@ context 'Rabl::Configuration' do
|
|
42
42
|
end
|
43
43
|
}.raises(RuntimeError)
|
44
44
|
end # invalid
|
45
|
+
|
46
|
+
context 'raise on missing attributes' do
|
47
|
+
setup do
|
48
|
+
Rabl.configure do |c|
|
49
|
+
c.raise_on_missing_attribute = true
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
asserts(:raise_on_missing_attribute).equals true
|
54
|
+
end # raise on missing
|
45
55
|
end
|
data/test/partials_test.rb
CHANGED
@@ -88,4 +88,48 @@ context "Rabl::Partials" do
|
|
88
88
|
Rabl.configuration.view_paths = []
|
89
89
|
end
|
90
90
|
end
|
91
|
+
|
92
|
+
context "fetch source with Rails" do
|
93
|
+
context "and :view_path" do
|
94
|
+
helper(:tmp_path) { @tmp_path ||= Pathname.new(Dir.mktmpdir) }
|
95
|
+
|
96
|
+
setup do
|
97
|
+
::Rails = stub(Class.new)
|
98
|
+
::ActionPack = Module.new
|
99
|
+
::ActionPack::VERSION = Module.new
|
100
|
+
::ActionPack::VERSION::MAJOR = 3
|
101
|
+
::ActionPack::VERSION::MINOR = 2
|
102
|
+
@it = TestPartial.new
|
103
|
+
|
104
|
+
def @it.context_scope; @context_scope ||= Object.new; end
|
105
|
+
def @it.request_format; :json; end
|
106
|
+
context_scope = @it.context_scope
|
107
|
+
|
108
|
+
def context_scope.view_paths; []; end
|
109
|
+
def context_scope.lookup_context; @lookup_context ||= Object.new; end
|
110
|
+
lookup_context = context_scope.lookup_context
|
111
|
+
|
112
|
+
def lookup_context.rendered_format; :json; end
|
113
|
+
def lookup_context.find(*args)
|
114
|
+
raise RuntimeError, 'Something happen with Rails lookup'
|
115
|
+
end
|
116
|
+
|
117
|
+
File.open(tmp_path + "test.json.rabl", "w") { |f| f.puts "content" }
|
118
|
+
|
119
|
+
@it
|
120
|
+
end
|
121
|
+
|
122
|
+
asserts('rails lookups dont break manual') do
|
123
|
+
@it.fetch_source('test', :view_path => tmp_path.to_s)
|
124
|
+
end.equals do
|
125
|
+
["content\n", (tmp_path + "test.json.rabl").to_s ]
|
126
|
+
end
|
127
|
+
|
128
|
+
teardown do
|
129
|
+
Object.send(:remove_const, :Rails)
|
130
|
+
Object.send(:remove_const, :ActionPack)
|
131
|
+
Rabl.configuration.view_paths = []
|
132
|
+
end
|
133
|
+
end
|
134
|
+
end
|
91
135
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rabl
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.7.
|
4
|
+
version: 0.7.10
|
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:
|
12
|
+
date: 2013-02-11 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: activesupport
|