rabl 0.6.14 → 0.7.0

Sign up to get free protection for your applications and to get access to all the features.
data/CHANGELOG.md CHANGED
@@ -1,6 +1,13 @@
1
1
  # CHANGELOG
2
2
 
3
- ## 0.6.15 (unreleased)
3
+ ## 0.7.1 (Unreleased)
4
+
5
+ ## 0.7.0
6
+
7
+ * Use source_format when looking up partials (Thanks @databyte)
8
+ * Add README note about render_views (Thanks @databyte)
9
+ * Add support for Rails 3.2+ sending custom mime types (Thanks @databyte)
10
+ * Add option to define his own cache_engine (Thanks @shingara)
4
11
 
5
12
  ## 0.6.14
6
13
 
data/README.md CHANGED
@@ -15,6 +15,12 @@ I wanted a simple and flexible system for generating my APIs. In particular, I w
15
15
  Anyone who has tried the 'to_json' method used in ActiveRecord for generating a JSON response has felt the pain of this restrictive approach.
16
16
  RABL is a general templating system created to solve these problems in an entirely new way.
17
17
 
18
+ ## Breaking Changes ##
19
+
20
+ * v0.6.14 (released June 28, 2012) requires the use of render_views
21
+ with RSpec to test templates. Otherwise, the controller will simply
22
+ pass through the render command as it does with ERB templates.
23
+
18
24
  ## Installation ##
19
25
 
20
26
  Install RABL as a gem:
@@ -101,6 +107,7 @@ Rabl.configure do |config|
101
107
  # Commented as these are defaults
102
108
  # config.cache_all_output = false
103
109
  # config.cache_sources = Rails.env != 'development' # Defaults to false
110
+ # config.cache_engine = Rabl::CacheEngine.new # Defaults to Rails cache
104
111
  # config.escape_all_output = false
105
112
  # config.json_engine = nil # Any multi\_json engines
106
113
  # config.msgpack_engine = nil # Defaults to ::MessagePack
@@ -125,6 +132,8 @@ output if the incoming request has a 'callback' parameter.
125
132
  If `include_child_root` is set to false then child objects in the response will not include
126
133
  a root node by default. This allows you to further fine-tune your desired response structure.
127
134
 
135
+ 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.
136
+
128
137
  If `cache_sources` is set to `true`, template lookups will be cached for improved performance.
129
138
  The cache can be reset manually by running `Rabl.reset_source_cache!` within your application.
130
139
 
@@ -234,6 +243,12 @@ attributes :bar => :baz, :dog => :animal
234
243
  # => # { baz : <bar value>, animal : <dog value> }
235
244
  ```
236
245
 
246
+ This currently does not work:
247
+
248
+ ```ruby
249
+ attributes :foo, :bar => :baz # throws exception
250
+ ```
251
+
237
252
  ### Child Nodes ###
238
253
 
239
254
  Often a response requires including nested information from data associated with the parent model:
@@ -353,6 +368,8 @@ end
353
368
 
354
369
  Using partials and inheritance can significantly reduce code duplication in your templates.
355
370
 
371
+ You can see more examples on the [Reusing Templates wiki page](https://github.com/nesquena/rabl/wiki/Reusing-templates).
372
+
356
373
  ### Template Scope ###
357
374
 
358
375
  In RABL, you have access to everything you need to build an API response. Each RABL template has full access to the controllers
@@ -515,6 +532,7 @@ Thanks to [Miso](http://gomiso.com) for allowing me to create this for our appli
515
532
  * [Alli Witheford](https://github.com/alzeih) - Added Plist format support
516
533
  * [Ryan Bigg](https://github.com/radar) - Improved template resolution code
517
534
  * [Ivan Vanderbyl](https://github.com/ivanvanderbyl) - Added general purpose renderer
535
+ * [Cyril Mougel](https://github.com/shingara) - Added cache_engine pluggable support
518
536
 
519
537
  and many more contributors listed in the [CHANGELOG](https://github.com/nesquena/rabl/blob/master/CHANGELOG.md).
520
538
 
@@ -0,0 +1,7 @@
1
+ object @post
2
+
3
+ attribute :title => :title_v1
4
+
5
+ child :user do
6
+ extends "users/show"
7
+ end
@@ -0,0 +1,6 @@
1
+ attributes :prefix, :suffix, :area_code
2
+ attributes :is_primary => :primary
3
+
4
+ node :formatted do |n|
5
+ n.formatted
6
+ end
@@ -0,0 +1,3 @@
1
+ object @user => :person
2
+
3
+ attribute :username => :username_v1
@@ -1,5 +1,5 @@
1
1
  class PostsController < ApplicationController
2
- respond_to :json, :xml, :html
2
+ respond_to :json, :xml, :html, :rabl_test_v1
3
3
 
4
4
  def index
5
5
  @posts = Post.all(:order => "id ASC")
@@ -3,3 +3,5 @@
3
3
  # Add new mime types for use in respond_to blocks:
4
4
  # Mime::Type.register "text/richtext", :rtf
5
5
  # Mime::Type.register_alias "text/html", :iphone
6
+
7
+ Mime::Type.register 'application/vnd.rabl-test_v1+json', :rabl_test_v1
@@ -154,6 +154,15 @@ context "PostsController" do
154
154
  asserts(:body).includes { "<html>" }
155
155
  end # show action, html
156
156
 
157
+ context "mime_type" do
158
+ setup do
159
+ get "/posts/#{@post1.id}", format: :rabl_test_v1
160
+ end
161
+
162
+ asserts("contains post title") { json_output['post']['title_v1'] }.equals { @post1.title }
163
+ asserts("contains username") { json_output['post']['user']['username_v1'] }.equals { @post1.user.username }
164
+ end
165
+
157
166
  context "caching" do
158
167
  helper(:cache_hit) do |key|
159
168
  Rails.cache.read(ActiveSupport::Cache.expand_cache_key(key, :rabl))
data/lib/rabl.rb CHANGED
@@ -11,6 +11,7 @@ require 'rabl/engine'
11
11
  require 'rabl/builder'
12
12
  require 'rabl/configuration'
13
13
  require 'rabl/renderer'
14
+ require 'rabl/cache_engine'
14
15
  require 'rabl/railtie' if defined?(Rails) && Rails.version =~ /^3/
15
16
 
16
17
  # Rabl.register!
@@ -0,0 +1,25 @@
1
+ # Defines the default cache engine for RABL when caching is invoked for a template.
2
+ # You can define your own caching engines by creating an object that responds to fetch and
3
+ # setting the configuration option:
4
+ #
5
+ # config.cache_engine = AdvancedCacheEngine.new
6
+ #
7
+
8
+ module Rabl
9
+ class CacheEngine
10
+
11
+ # Fetch given a key and options and a fallback block attempts to find the key in the cache
12
+ # and stores the block result in there if no key is found.
13
+ #
14
+ # cache = Rabl::CacheEngine.new; cache.fetch("some_key") { "fallback data" }
15
+ #
16
+ def fetch(key, cache_options, &block)
17
+ if defined?(Rails)
18
+ Rails.cache.fetch(key, cache_options, &block)
19
+ else
20
+ yield
21
+ end
22
+ end
23
+
24
+ end
25
+ end
@@ -39,6 +39,7 @@ module Rabl
39
39
  attr_accessor :cache_all_output
40
40
  attr_accessor :escape_all_output
41
41
  attr_accessor :view_paths
42
+ attr_accessor :cache_engine
42
43
 
43
44
  DEFAULT_XML_OPTIONS = { :dasherize => true, :skip_types => false }
44
45
 
@@ -61,6 +62,7 @@ module Rabl
61
62
  @cache_all_output = false
62
63
  @escape_all_output = false
63
64
  @view_paths = []
65
+ @cache_engine = Rabl::CacheEngine.new
64
66
  end
65
67
 
66
68
  # @param [Symbol, String, #encode] engine_name The name of a JSON engine,
data/lib/rabl/helpers.rb CHANGED
@@ -87,7 +87,7 @@ module Rabl
87
87
  # fetch_from_cache('some_key') { ...rabl template result... }
88
88
  def fetch_result_from_cache(cache_key, cache_options=nil, &block)
89
89
  expanded_cache_key = ActiveSupport::Cache.expand_cache_key(cache_key, :rabl)
90
- Rails.cache.fetch(expanded_cache_key, cache_options, &block) if defined?(Rails)
90
+ Rabl.configuration.cache_engine.fetch(expanded_cache_key, cache_options, &block)
91
91
  end
92
92
 
93
93
  # Returns true if the cache has been enabled for the application
data/lib/rabl/partials.rb CHANGED
@@ -71,7 +71,10 @@ module Rabl
71
71
  if ActionPack::VERSION::MAJOR == 3 && ActionPack::VERSION::MINOR < 2
72
72
  context_scope.lookup_context.find(file, [], partial)
73
73
  else # Rails 3.2 and higher
74
- context_scope.lookup_context.find(file, [], partial, [], {:formats => [:json]})
74
+ # pull format directly from rails unless it is html
75
+ rendered_format = context_scope.lookup_context.rendered_format
76
+ source_format = rendered_format unless rendered_format == :html
77
+ context_scope.lookup_context.find(file, [], partial, [], {:formats => [source_format]})
75
78
  end }
76
79
  template = lookup_proc.call(false) rescue lookup_proc.call(true)
77
80
  template.identifier if template
data/lib/rabl/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Rabl
2
- VERSION = "0.6.14"
2
+ VERSION = "0.7.0"
3
3
  end
@@ -13,6 +13,7 @@ context 'Rabl::Configuration' do
13
13
  asserts(:enable_json_callbacks).equals false
14
14
  asserts(:view_paths).equals []
15
15
  asserts(:json_engine).equals { json_engine }
16
+ asserts(:cache_engine).is_a?(Rabl::CacheEngine)
16
17
  end
17
18
 
18
19
  context 'custom JSON engine' do
@@ -154,6 +154,15 @@ context "PostsController" do
154
154
  asserts(:body).includes { "<html>" }
155
155
  end # show action, html
156
156
 
157
+ context "mime_type" do
158
+ setup do
159
+ get "/posts/#{@post1.id}", format: :rabl_test_v1
160
+ end
161
+
162
+ asserts("contains post title") { json_output['post']['title_v1'] }.equals { @post1.title }
163
+ asserts("contains username") { json_output['post']['user']['username_v1'] }.equals { @post1.user.username }
164
+ end
165
+
157
166
  context "caching" do
158
167
  helper(:cache_hit) do |key|
159
168
  Rails.cache.read(ActiveSupport::Cache.expand_cache_key(key, :rabl))
@@ -84,6 +84,9 @@ context "Rabl::Partials" do
84
84
  asserts('detects file.json.rabl first') { topic }.equals do
85
85
  ["content2\n", (tmp_path + 'test.json.rabl').to_s]
86
86
  end
87
- teardown { Object.send(:remove_const, :Sinatra) }
87
+ teardown do
88
+ Object.send(:remove_const, :Sinatra)
89
+ Rabl.configuration.view_paths = []
90
+ end
88
91
  end
89
92
  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.6.14
4
+ version: 0.7.0
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-06-28 00:00:00.000000000 Z
12
+ date: 2012-07-16 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: activesupport
@@ -213,9 +213,12 @@ files:
213
213
  - fixtures/ashared/views_rails_3/posts/index.rabl
214
214
  - fixtures/ashared/views_rails_3/posts/show.html.erb
215
215
  - fixtures/ashared/views_rails_3/posts/show.rabl
216
+ - fixtures/ashared/views_rails_3/posts/show.rabl_test_v1.rabl
216
217
  - fixtures/ashared/views_rails_3/users/index.json.rabl
217
218
  - fixtures/ashared/views_rails_3/users/phone_number.json.rabl
219
+ - fixtures/ashared/views_rails_3/users/phone_number.xml.rabl
218
220
  - fixtures/ashared/views_rails_3/users/show.json.rabl
221
+ - fixtures/ashared/views_rails_3/users/show.rabl_test_v1.rabl
219
222
  - fixtures/padrino_test/.components
220
223
  - fixtures/padrino_test/.gitignore
221
224
  - fixtures/padrino_test/Gemfile
@@ -391,6 +394,7 @@ files:
391
394
  - fixtures/sinatra_test/test/test_helper.rb
392
395
  - lib/rabl.rb
393
396
  - lib/rabl/builder.rb
397
+ - lib/rabl/cache_engine.rb
394
398
  - lib/rabl/configuration.rb
395
399
  - lib/rabl/engine.rb
396
400
  - lib/rabl/helpers.rb