rabl-rails 0.4.1 → 0.4.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 2874d3a8bc282b5f68501b4b20a238a764a7a50e
4
+ data.tar.gz: 84d6d590864951016038bee486096ef7637efc5f
5
+ SHA512:
6
+ metadata.gz: 6022f49acacdc484fc278c2e770db3f418f89602c17821d83e06339c824228fe4990e37aa9e197cf402155caad62fd35474ac522d2b13aff71e537e13708ed8e
7
+ data.tar.gz: cfbc8c2ae6a2f39a1202ec502c6a4fa1d7d0a39952633ac6f27375865ef764f974cfa438168c49d623833816ca71b33dcfe583b482542a0e4d6679f5c71380d8
@@ -3,6 +3,7 @@ env:
3
3
  - "RAILS_VERSION=3.2.0"
4
4
  - "RAILS_VERSION=4.0.0"
5
5
  - "RAILS_VERSION=4.1.0"
6
+ - "RAILS_VERSION=4.2.0"
6
7
  rvm:
7
8
  - 1.9.3
8
9
  - jruby-19mode
@@ -1,5 +1,9 @@
1
1
  # CHANGELOG
2
2
 
3
+ ## 0.4.2
4
+ * Allow to pass locals to partials
5
+ * Add condition to `attributes`
6
+
3
7
  ## 0.4.1
4
8
  * Make classes that should not be treated as collection configurable
5
9
  * Internal change to determine rendering format
data/Gemfile CHANGED
@@ -8,7 +8,7 @@ rails = case rails_version
8
8
  when 'master'
9
9
  { github: 'rails/rails' }
10
10
  when "default"
11
- '~> 3.2.0'
11
+ '~> 4.2.0'
12
12
  else
13
13
  "~> #{rails_version}"
14
14
  end
@@ -35,4 +35,3 @@ end
35
35
  platforms :jruby do
36
36
  gem 'nokogiri'
37
37
  end
38
-
data/README.md CHANGED
@@ -168,6 +168,11 @@ attributes title: :foo, to_s: :bar
168
168
  # => { "foo" : <title value>, "bar" : <to_s value> }
169
169
  ```
170
170
 
171
+ or show attributes only if a condition is true
172
+ ```ruby
173
+ attributes :published_at, :anchor, if: ->(post) { post.published? }
174
+ ```
175
+
171
176
  ### Child nodes
172
177
 
173
178
  You can include informations from data associated with the parent model or arbitrary data. These informations can be grouped under a node or directly merged into current node.
@@ -242,8 +247,11 @@ Often objects have a basic representation that is shared accross different views
242
247
  attributes :id, :name
243
248
 
244
249
  # app/views/users/private.json.rabl
245
- extends 'users/base'
246
250
  attributes :super_secret_attribute
251
+
252
+ extends 'users/base'
253
+ # or using partial instead of extends
254
+ # merge { |u| partial('users/base', object: u) }
247
255
  ```
248
256
 
249
257
  You can also extends template in child nodes using `partial` option (this is the same as using `extends` in the child block)
@@ -262,6 +270,17 @@ node(:location) do |user|
262
270
  end
263
271
  ```
264
272
 
273
+ When used within `node`, partials can take locals variables that can be accessed in the included template.
274
+ ```ruby
275
+ # base.json.rabl
276
+ node(:credit_card, if: ->(u) { locals[:display_credit_card] }) do |user|
277
+ user.credit_card_info
278
+ end
279
+
280
+ # user.json.rabl
281
+ merge { |u| partial('users/base', object: u, locals: { display_credit_card: true }) }
282
+
283
+
265
284
  ### Nesting
266
285
 
267
286
  Rabl allow you to define easily your templates, even with hierarchy of 2 or 3 levels. Let's suppose your have a `thread` model that has many `posts` and that each post has many `comments`. We can display a full thread in a few lines
@@ -51,6 +51,7 @@ module RablRails
51
51
  key = options[:as] || name
52
52
  node[key] = name
53
53
  }
54
+ node.condition = options[:if]
54
55
  end
55
56
 
56
57
  @template.add_node node
@@ -172,4 +173,4 @@ module RablRails
172
173
  @template = old_template
173
174
  end
174
175
  end
175
- end
176
+ end
@@ -7,11 +7,12 @@ module RablRails
7
7
  extend Forwardable
8
8
 
9
9
  def_delegators :@hash, :[]=, :each
10
- attr_reader :hash
10
+ attr_reader :hash
11
+ attr_accessor :condition
11
12
 
12
13
  def initialize(hash = {})
13
14
  @hash = hash
14
15
  end
15
16
  end
16
17
  end
17
- end
18
+ end
@@ -1,3 +1,3 @@
1
1
  module RablRails
2
- VERSION = '0.4.1'
2
+ VERSION = '0.4.2'
3
3
  end
@@ -8,6 +8,7 @@ module Visitors
8
8
  @_context = view_context
9
9
  @_result = {}
10
10
  @_resource = resource
11
+ @_locals = {}
11
12
 
12
13
  copy_instance_variables_from_context
13
14
  end
@@ -22,7 +23,9 @@ module Visitors
22
23
  end
23
24
 
24
25
  def visit_Attribute n
25
- n.each { |k, v| @_result[k] = _resource.send(v) }
26
+ if !n.condition || instance_exec(_resource, &(n.condition))
27
+ n.each { |k, v| @_result[k] = _resource.send(v) }
28
+ end
26
29
  end
27
30
 
28
31
  def visit_Child n
@@ -83,6 +86,10 @@ module Visitors
83
86
  @_context.respond_to?(name) ? @_context.send(name, *args, &block) : super
84
87
  end
85
88
 
89
+ def locals
90
+ @_locals
91
+ end
92
+
86
93
  #
87
94
  # Allow to use partial inside of node blocks (they are evaluated at
88
95
  # rendering time).
@@ -90,6 +97,7 @@ module Visitors
90
97
  def partial(template_path, options = {})
91
98
  raise RablRails::Renderer::PartialError.new("No object was given to partial #{template_path}") unless options[:object]
92
99
  object = options[:object]
100
+ @_locals = options[:locals].freeze
93
101
 
94
102
  return [] if object.respond_to?(:empty?) && object.empty?
95
103
 
@@ -99,6 +107,8 @@ module Visitors
99
107
  else
100
108
  sub_visit object, template.nodes
101
109
  end
110
+ ensure
111
+ @_locals = {}
102
112
  end
103
113
 
104
114
  private
@@ -128,4 +138,4 @@ module Visitors
128
138
  end
129
139
  end
130
140
  end
131
- end
141
+ end
@@ -113,6 +113,12 @@ class TestCompiler < MINITEST_TEST_CLASS
113
113
  assert_equal([{ :bar => :foo, :uid => :id }], extract_attributes(t.nodes))
114
114
  end
115
115
 
116
+ it "compiles attribtues with a condition" do
117
+ t = @compiler.compile_source(%( attributes :id, if: ->(o) { false } ))
118
+ assert_equal([{ id: :id }], extract_attributes(t.nodes))
119
+ refute_nil t.nodes.first.condition
120
+ end
121
+
116
122
  it "compiles child with record association" do
117
123
  t = @compiler.compile_source(%{ child :address do attributes :foo end})
118
124
 
@@ -280,4 +286,4 @@ class TestCompiler < MINITEST_TEST_CLASS
280
286
  end
281
287
  end
282
288
  end
283
- end
289
+ end
@@ -24,6 +24,13 @@ class TestHashVisitor < MINITEST_TEST_CLASS
24
24
  assert_equal({ id: 1 }, visitor_result)
25
25
  end
26
26
 
27
+ it 'renders attributes with a condition' do
28
+ n = RablRails::Nodes::Attribute.new(id: :id)
29
+ n.condition = lambda { |o| false }
30
+ @nodes << n
31
+ assert_equal({}, visitor_result)
32
+ end
33
+
27
34
  it 'renders array of nodes' do
28
35
  @nodes = [
29
36
  RablRails::Nodes::Attribute.new(id: :id),
@@ -153,7 +160,7 @@ class TestHashVisitor < MINITEST_TEST_CLASS
153
160
  assert_raises(RablRails::Renderer::PartialError) { visitor_result }
154
161
  end
155
162
 
156
- it 'renders partial defined in code node' do
163
+ it 'renders partial defined in node' do
157
164
  template = RablRails::CompiledTemplate.new
158
165
  template.add_node(RablRails::Nodes::Attribute.new(name: :name))
159
166
  proc = ->(u) { partial('users/base', object: u) }
@@ -169,6 +176,22 @@ class TestHashVisitor < MINITEST_TEST_CLASS
169
176
  library.verify
170
177
  end
171
178
 
179
+ it 'allows uses of locals variables with partials' do
180
+ template = RablRails::CompiledTemplate.new
181
+ template.add_node(RablRails::Nodes::Code.new(:hide_comments, ->(u) { locals[:hide_comments] }, ->(u) { locals.key?(:hide_comments) }))
182
+ proc = ->(u) { partial('users/locals', object: u, locals: { hide_comments: true }) }
183
+
184
+ library = MiniTest::Mock.new
185
+ library.expect :compile_template_from_path, template, ['users/locals', @context]
186
+
187
+ @nodes << RablRails::Nodes::Code.new(:user, proc)
188
+ RablRails::Library.stub :instance, library do
189
+ assert_equal({ user: { hide_comments: true } }, visitor_result)
190
+ end
191
+
192
+ library.verify
193
+ end
194
+
172
195
  it 'renders partial with empty target' do
173
196
  proc = ->(u) { partial('users/base', object: []) }
174
197
  @nodes << RablRails::Nodes::Code.new(:users, proc)
@@ -221,4 +244,4 @@ class TestHashVisitor < MINITEST_TEST_CLASS
221
244
  end
222
245
  end
223
246
  end
224
- end
247
+ end
metadata CHANGED
@@ -1,62 +1,55 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rabl-rails
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.1
5
- prerelease:
4
+ version: 0.4.2
6
5
  platform: ruby
7
6
  authors:
8
7
  - Christopher Cocchi-Perrier
9
8
  autorequire:
10
9
  bindir: bin
11
10
  cert_chain: []
12
- date: 2014-12-28 00:00:00.000000000 Z
11
+ date: 2016-01-19 00:00:00.000000000 Z
13
12
  dependencies:
14
13
  - !ruby/object:Gem::Dependency
15
14
  name: activesupport
16
15
  requirement: !ruby/object:Gem::Requirement
17
- none: false
18
16
  requirements:
19
- - - ! '>='
17
+ - - ">="
20
18
  - !ruby/object:Gem::Version
21
19
  version: '3.1'
22
20
  type: :runtime
23
21
  prerelease: false
24
22
  version_requirements: !ruby/object:Gem::Requirement
25
- none: false
26
23
  requirements:
27
- - - ! '>='
24
+ - - ">="
28
25
  - !ruby/object:Gem::Version
29
26
  version: '3.1'
30
27
  - !ruby/object:Gem::Dependency
31
28
  name: railties
32
29
  requirement: !ruby/object:Gem::Requirement
33
- none: false
34
30
  requirements:
35
- - - ! '>='
31
+ - - ">="
36
32
  - !ruby/object:Gem::Version
37
33
  version: '3.1'
38
34
  type: :runtime
39
35
  prerelease: false
40
36
  version_requirements: !ruby/object:Gem::Requirement
41
- none: false
42
37
  requirements:
43
- - - ! '>='
38
+ - - ">="
44
39
  - !ruby/object:Gem::Version
45
40
  version: '3.1'
46
41
  - !ruby/object:Gem::Dependency
47
42
  name: thread_safe
48
43
  requirement: !ruby/object:Gem::Requirement
49
- none: false
50
44
  requirements:
51
- - - ~>
45
+ - - "~>"
52
46
  - !ruby/object:Gem::Version
53
47
  version: 0.3.1
54
48
  type: :runtime
55
49
  prerelease: false
56
50
  version_requirements: !ruby/object:Gem::Requirement
57
- none: false
58
51
  requirements:
59
- - - ~>
52
+ - - "~>"
60
53
  - !ruby/object:Gem::Version
61
54
  version: 0.3.1
62
55
  description: Fast Rails 3+ templating system with JSON, XML and PList support
@@ -66,8 +59,8 @@ executables: []
66
59
  extensions: []
67
60
  extra_rdoc_files: []
68
61
  files:
69
- - .gitignore
70
- - .travis.yml
62
+ - ".gitignore"
63
+ - ".travis.yml"
71
64
  - CHANGELOG.md
72
65
  - Gemfile
73
66
  - MIT-LICENSE
@@ -113,27 +106,26 @@ files:
113
106
  - test/test_render.rb
114
107
  homepage: https://github.com/ccocchi/rabl-rails
115
108
  licenses: []
109
+ metadata: {}
116
110
  post_install_message:
117
111
  rdoc_options: []
118
112
  require_paths:
119
113
  - lib
120
114
  required_ruby_version: !ruby/object:Gem::Requirement
121
- none: false
122
115
  requirements:
123
- - - ! '>='
116
+ - - ">="
124
117
  - !ruby/object:Gem::Version
125
118
  version: '0'
126
119
  required_rubygems_version: !ruby/object:Gem::Requirement
127
- none: false
128
120
  requirements:
129
- - - ! '>='
121
+ - - ">="
130
122
  - !ruby/object:Gem::Version
131
123
  version: '0'
132
124
  requirements: []
133
125
  rubyforge_project:
134
- rubygems_version: 1.8.23
126
+ rubygems_version: 2.4.5
135
127
  signing_key:
136
- specification_version: 3
128
+ specification_version: 4
137
129
  summary: Fast Rails 3+ templating system with JSON, XML and PList support
138
130
  test_files:
139
131
  - test/helper.rb
@@ -147,3 +139,4 @@ test_files:
147
139
  - test/test_helpers.rb
148
140
  - test/test_library.rb
149
141
  - test/test_render.rb
142
+ has_rdoc: