jbuilder-jpartial 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: de4f8644930394f0f22aa90292f5f66ab813f884
4
+ data.tar.gz: 5970911868f688dfe4ca101f73a1597786fdc1c9
5
+ SHA512:
6
+ metadata.gz: 2b82ed0ce07772d5242a96c2a31a81eab5333969db7944f676a3114d26b8fc79cfa2c6977463032c2c10c5a5de37c53049a06b189de15ef92503a666bac7cd89
7
+ data.tar.gz: 0d99474b5a6c08d1d1a35ad55e1d05f1d029f380c5e2ae447ce88a776a2dd4827954ac384cc49f504bb2b2dc76ed0c3e8b834ebdf62e3ca781bc699a7559c5bf
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2017 M. Simon Borg
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,140 @@
1
+ # Jbuilder::Jpartial
2
+
3
+ A lightweight library that provides a simple DSL for faster partial rendering with Jbuilder.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'jbuilder-jpartial', require: 'jbuilder/jpartial'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install jbuilder-jpartial
20
+
21
+ require 'jbuilder/jpartial'
22
+
23
+ ## Usage
24
+
25
+ #### In `config/initializers/jbuilder-jpartial.rb`
26
+
27
+ ```ruby
28
+ module Jbuilder::Jpartial
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
41
+ end
42
+ end
43
+ ```
44
+
45
+ #### In `app/views/posts/index.jbuilder`
46
+
47
+ ```ruby
48
+ json.set! 'posts', @posts do |post|
49
+ json._post post
50
+ end
51
+ ```
52
+
53
+ #### Result:
54
+ ```js
55
+ {
56
+ "posts": [
57
+ {
58
+ "title": "The Best of Posts",
59
+ "author": "M. Simon Borg",
60
+ "content": "Pizza time!",
61
+ "comments": [
62
+ { "content": "So good", "author": "Matt's mom" },
63
+ { "content": "I agree", "author": "Matt's dad" }
64
+ ]
65
+ }
66
+ ]
67
+ }
68
+ ```
69
+ #### 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.
71
+
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.
73
+
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/posts/_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.
75
+
76
+ #### 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.
78
+
79
+ e.g.
80
+
81
+ ```ruby
82
+ module Jbuilder::Jpartial
83
+ jpartial :_post do |post|
84
+ json.title post.title
85
+ end
86
+ end
87
+ ```
88
+
89
+ Now in your `.jbuilder` templates you can call `json._post @post`.
90
+
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.
92
+
93
+ e.g.
94
+
95
+ ```ruby
96
+ module Jbuilder::Jpartial
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
105
+ end
106
+ ```
107
+
108
+ Now you can call `json._post @post, author: @author`
109
+
110
+ Route helpers that are available within the views can also be used in the configuration file.
111
+
112
+ e.g.
113
+
114
+ ```ruby
115
+ module Jbuilder::Jpartial
116
+ jpartial :_post do |post|
117
+ json.href post_url(post)
118
+ json.title post.title
119
+ end
120
+ end
121
+ ```
122
+
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!`.
124
+
125
+
126
+ ## Development
127
+
128
+ After checking out the repo, run `bin/setup` to install dependencies. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
129
+
130
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
131
+
132
+ ## Contributing
133
+
134
+ Bug reports and pull requests are welcome on GitHub at https://github.com/msimonborg/jbuilder-jpartial.
135
+
136
+
137
+ ## License
138
+
139
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
140
+
@@ -0,0 +1,5 @@
1
+ class Jbuilder
2
+ module Jpartial
3
+ VERSION = '0.1.0'.freeze
4
+ end
5
+ end
@@ -0,0 +1,36 @@
1
+ require 'jbuilder'
2
+
3
+ require 'jbuilder/jpartial/version'
4
+
5
+ # Top level Jbuilder class
6
+ class Jbuilder
7
+ def json
8
+ self
9
+ end
10
+
11
+ alias_method :old_method_missing, :method_missing
12
+
13
+ def method_missing(method_name, *args, &block)
14
+ if method_name.to_s =~ /(.*)_(url|path)/ && defined? @context
15
+ @context.send(method_name, *args, &block)
16
+ else
17
+ old_method_missing(method_name, *args, &block)
18
+ end
19
+ end
20
+
21
+ # Jpartial module
22
+ module Jpartial
23
+ DangerousMethodName = Class.new(ArgumentError)
24
+
25
+ def self.jpartial(name, &block)
26
+ Jbuilder.class_eval do
27
+ if method_defined?(name) || private_method_defined?(name)
28
+ raise DangerousMethodName, "The method `##{name}` is already defined by Jbuilder. "\
29
+ 'Please choose another name to define you partial'
30
+ else
31
+ define_method(name, &block)
32
+ end
33
+ end
34
+ end
35
+ end
36
+ end
metadata ADDED
@@ -0,0 +1,90 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: jbuilder-jpartial
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - M. Simon Borg
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2017-07-10 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: jbuilder
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.14'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.14'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '10.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '10.0'
55
+ description: Simple DSL for faster partial rendering with Jbuilder
56
+ email:
57
+ - msimonborg@gmail.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - LICENSE.txt
63
+ - README.md
64
+ - lib/jbuilder/jpartial.rb
65
+ - lib/jbuilder/jpartial/version.rb
66
+ homepage: https://github.com/msimonborg/jbuilder-jpartial
67
+ licenses:
68
+ - MIT
69
+ metadata: {}
70
+ post_install_message:
71
+ rdoc_options: []
72
+ require_paths:
73
+ - lib
74
+ required_ruby_version: !ruby/object:Gem::Requirement
75
+ requirements:
76
+ - - ">="
77
+ - !ruby/object:Gem::Version
78
+ version: '0'
79
+ required_rubygems_version: !ruby/object:Gem::Requirement
80
+ requirements:
81
+ - - ">="
82
+ - !ruby/object:Gem::Version
83
+ version: '0'
84
+ requirements: []
85
+ rubyforge_project:
86
+ rubygems_version: 2.6.11
87
+ signing_key:
88
+ specification_version: 4
89
+ summary: Simple DSL for faster partial rendering with Jbuilder
90
+ test_files: []