jbuilder-jpartial 0.1.3 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 75e8c4257f48d6ef56dcaaf8047bef63df892137
4
- data.tar.gz: 4f22e415d52ea9f82f097f5e05e2a42c22200c9a
3
+ metadata.gz: 29f17aa56ad6436b58ce9ae71dec2f5bcecf03bf
4
+ data.tar.gz: 21924849d686e532c9ace096a0036295bb0a1a50
5
5
  SHA512:
6
- metadata.gz: da10106742ea3e98614392ee921e09caf4d8f77d6047632e4b1d4a9a065b01253d704c7409c8045dfb84d5804e411ba97a14b47eb61194e24499a94f0af70238
7
- data.tar.gz: 118c304e9eca4ac54df216e59bc8146ca10e427b6dbae58a9e46ed807557cd53b8eea1942cd3e4a04e2487384f257e1d2c992dda09ec627ac1c3af9812acee33
6
+ metadata.gz: ea006e37c63c21f57e17786017e27c272212fb3059b4f5f5ee1f90394a58234dbf095bcdb1b1cfb4c66af5b3fd9379ad0565b1c1252543e767b827defa8b7340
7
+ data.tar.gz: 021b55c1ba1436e5feb0421a5069d8e78e81f2dfaa643341fb78b1e2d0185c7e2b03ca2d4d14d8967d57258ab017b725f2e0f60a6de254a1407cee594ba82419
data/README.md CHANGED
@@ -22,29 +22,32 @@ Or install it yourself as:
22
22
 
23
23
  ## Usage
24
24
 
25
- #### In `config/initializers/jbuilder-jpartial.rb`
25
+ #### In `app/views/posts/_post.jbuilder`
26
26
 
27
27
  ```ruby
28
- Jbuilder::Jpartial.configure do
29
- jpartial :_post do |post|
30
- json.title post.title
31
- json.author post.author.name
32
- json.content post.content
33
- json.set! 'comments', post.comments do |comment|
34
- json._comment comment
35
- end
36
- end
37
-
38
- jpartial :_comment do |comment|
39
- json.content comment.content
40
- json.author comment.author.name
28
+ jpartial._post do |post|
29
+ json.title post.title
30
+ json.author post.author.name
31
+ json.content post.content
32
+ json.partial! 'comments/comment'
33
+ json.set! 'comments', post.comments do |comment|
34
+ json._comment comment
41
35
  end
42
36
  end
43
37
  ```
44
38
 
39
+ #### In `app/views/comments/_comment.jbuilder`
40
+ ```ruby
41
+ jpartial._comment do |comment|
42
+ json.content comment.content
43
+ json.author comment.author.name
44
+ end
45
+ ```
46
+
45
47
  #### In `app/views/posts/index.jbuilder`
46
48
 
47
49
  ```ruby
50
+ json.partial! 'post'
48
51
  json.set! 'posts', @posts do |post|
49
52
  json._post post
50
53
  end
@@ -67,60 +70,102 @@ end
67
70
  }
68
71
  ```
69
72
  #### Why?
70
- When traditional partials are used with `Jbuilder`, with each partial in a different file using standard Rails conventions, render times and memory usage can skyrocket quickly with the number of records, i.e. the number of partials rendered.
73
+ When partials are used with `Jbuilder` render times and memory usage can skyrocket quickly with the number of records, i.e. the number of partials rendered.
71
74
 
72
- Using a simple DSL, `Jbuilder::Jpartial` lets you define all partials in one initializer file and call them wherever you need them: from within other partial definitions or anywhere you use `Jbuilder` in your views.
75
+ Using a simple DSL, `Jbuilder::Jpartial` lets you define your partials in a familiar way while dramatically reducing overhead.
73
76
 
74
- The result is faster rendering and lower memory usage, while still being able to leverage the advantages of Jbuilder. In the above example, if we had used traditional partials (defined in `app/views/posts/_post.jbuilder` and `app/views/comments/_comment.jbuilder`) those templates would have to be rendered once for each `post` and/or `comment`. If you have 50 posts, each with 50 comments, that's 2,500 templates rendered! Using `Jbuilder::Jpartial`, only one file (`app/views/posts/index.jbuilder`) is rendered. All of the partial rendering is taken care of in the abstract.
77
+ The result is faster rendering and lower memory usage, while still being able to leverage the advantages of Jbuilder. In the above example, if we had used standard Jbuilder partials those templates would have to be rendered once for each `post` and/or `comment`. If you have 50 posts, each with 50 comments, that's 2,500 templates rendered! Using `Jbuilder::Jpartial`, the partial files are each only called once, to define the partial. After that, all of the partial rendering is taken care of in the abstract from the original file. In our example, you can cut 2,500 template renders down to 3.
78
+
79
+ Alternatively you can define all partials in one initializer file and call them wherever you need them: from within other partial definitions or anywhere you use `Jbuilder` in your views. The advantage here is fewer files, all partials in one place, and since they're initialized at start up, you don't need to call any additional view templates to render the partials. Using the same example as above:
80
+
81
+ #### In `app/config/initializers/jbuilder-jpartial.rb`
82
+ ```ruby
83
+ Jbuilder::Jpartial.configure do |jpartial|
84
+ jpartial._post do |post|
85
+ json.title post.title
86
+ json.author post.author.name
87
+ json.content post.content
88
+ json.set! 'comments', post.comments do |comment|
89
+ json._comment comment
90
+ end
91
+ end
92
+
93
+ jpartial._comment do |comment|
94
+ json.content comment.content
95
+ json.author comment.author.name
96
+ end
97
+ end
98
+ ```
99
+
100
+ #### In `app/views/posts/index.jbuilder`
101
+ ```ruby
102
+ json.set! 'posts', @posts do |post|
103
+ json._post post
104
+ end
105
+ ```
106
+
107
+ Notice that when using this method, we don't make any calls like `json.partial! 'post'` at any point to define the partial before using it. All of the partials are already defined in the initializer file.
108
+
109
+ The advantage is a couple fewer renders. The disadvantage is that helper methods that are available in the views are not available in the configuration file.
75
110
 
76
111
  #### How?
77
- Each `jpartial` block in your initializer file defines a Jbuilder method named after whatever symbol you pass as an argument. The objects you will pass to that method are yielded to the block. Inside the block you can use plain old Jbuilder syntax.
112
+ Each method you call on `jpartial` defines a Jbuilder method of the same name. The objects you will pass to that method are yielded to the block. Inside the block you can use plain old Jbuilder syntax.
78
113
 
79
114
  e.g.
80
115
 
81
116
  ```ruby
82
- Jbuilder::Jpartial.configure do
83
- jpartial :_post do |post|
84
- json.title post.title
85
- end
117
+ jpartial._post do |post|
118
+ json.title post.title
86
119
  end
87
120
  ```
88
121
 
89
122
  Now in your `.jbuilder` templates you can call `json._post @post`.
90
123
 
91
- You can specify multiple arguments and even use keyword arguments if you need to pass more than one local variable to the partial. You can also call partial methods from within other partial methods.
124
+ You can specify multiple arguments and even use keyword options if you need to pass more than one local variable to the partial. You can also call partial methods from within other partial methods.
125
+
126
+ The rule of thumb when defining your partials in views is you need to make sure the partial is initialized by making a call to the template, outside of your render logic and/or any loops/iteration.
92
127
 
93
128
  e.g.
94
129
 
130
+ #### `app/views/authors/_author.jbuilder`
95
131
  ```ruby
96
- Jbuilder::Jpartial.configure do
97
- jpartial :_post do |post, author:|
98
- json.title post.title
99
- json._author author
100
- end
101
-
102
- jpartial :_author do |author|
103
- json.name author.name
104
- end
132
+ jpartial._author do |author|
133
+ json.name author.name
105
134
  end
106
135
  ```
107
136
 
108
- Now you can call `json._post @post, author: @author`
137
+ #### `app/views/posts/_post.jbuilder`
138
+ ```ruby
139
+ jpartial._post do |post, author:|
140
+ json.title post.title
141
+ json.partial! 'authors/author'
142
+ json._author author
143
+ end
144
+ ```
109
145
 
110
- Route helpers that are available within the views can also be used in the configuration file.
146
+ Now you can call `json._post @post, author: @author`
111
147
 
112
- e.g.
148
+ Again, if you're defining all of your partials in one config file, the extra call to a partial template is not necessary.
113
149
 
114
150
  ```ruby
115
- Jbuilder::Jpartial.configure do
116
- jpartial :_post do |post|
117
- json.href post_url(post)
151
+ Jbuilder::Jpartial.configure do |jpartial|
152
+ jpartial._post do |post, author:|
118
153
  json.title post.title
154
+ json._author author
155
+ end
156
+
157
+ jpartial._author do |author|
158
+ json.name author.name
119
159
  end
120
160
  end
121
161
  ```
122
162
 
123
- However unlikely, if you try to name a partial with the same name as a method already defined by Jbuilder it will throw an error at start up. Just pick a different name, like `#set_partial!` instead of `#set!`.
163
+ However unlikely, if you try to name a partial with the same name as a method already defined by Jbuilder it will throw an error at start up. Just pick a different name, like `#whatever_partial` instead of `#whatever`.
164
+
165
+ One method taken by this library is `Jbuilder#json`. This method returns the `self` of the receiver, which will be the instance of `Jbuilder` doing the encoding. If you have or need any fields with the key `"json"` use `Jbuilder#set!`, e.g.
166
+ ```ruby
167
+ json.set! 'json', 'some value'
168
+ ```
124
169
 
125
170
 
126
171
  ## Development
@@ -1,5 +1,5 @@
1
1
  class Jbuilder
2
2
  module Jpartial
3
- VERSION = '0.1.3'.freeze
3
+ VERSION = '0.2.0'.freeze
4
4
  end
5
5
  end
@@ -6,16 +6,6 @@ class Jbuilder
6
6
  self
7
7
  end
8
8
 
9
- alias_method :old_method_missing, :method_missing
10
-
11
- def method_missing(method_name, *args, &block)
12
- if method_name.to_s =~ /(.*)_(url|path)/ && defined? @context
13
- @context.send(method_name, *args, &block)
14
- else
15
- old_method_missing(method_name, *args, &block)
16
- end
17
- end
18
-
19
9
  # Jpartial module
20
10
  module Jpartial
21
11
  DangerousMethodName = Class.new(ArgumentError)
@@ -40,14 +30,22 @@ class Jbuilder
40
30
  Jbuilder.private_method_defined?(name)
41
31
  end
42
32
 
43
- def self.configure(&block)
44
- module_eval(&block)
33
+ def self.configure
34
+ yield Template.new
45
35
  end
46
36
 
47
- # Sends all method calls to Jpartial.jpartial for definition
48
- class Template
49
- def method_missing(method_name, &block)
50
- Jpartial.jpartial(method_name, &block)
37
+ # Proxy object that sends all method calls to Jpartial.jpartial
38
+ Template = Class.new(begin
39
+ require 'active_support/proxy_object'
40
+ ActiveSupport::ProxyObject
41
+ rescue LoadError
42
+ require 'active_support/basic_object'
43
+ ActiveSupport::BasicObject
44
+ end) do
45
+
46
+ def method_missing(name, *args, &block)
47
+ name = name.to_s == 'send' ? args.first.to_sym : name
48
+ Jpartial.jpartial(name, &block)
51
49
  end
52
50
  end
53
51
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: jbuilder-jpartial
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.3
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - M. Simon Borg