content_pack_rails 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/.gitignore +11 -0
- data/Gemfile +5 -0
- data/MIT-LICENSE +20 -0
- data/README.md +145 -0
- data/Rakefile +13 -0
- data/content_pack_rails.gemspec +23 -0
- data/lib/content_pack_rails/version.rb +3 -0
- data/lib/content_pack_rails.rb +28 -0
- metadata +86 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 01e54acea70e4117e3b4aa0eeb0abdd77e6dda0d66f3aba6adb03a7eeb10436b
|
4
|
+
data.tar.gz: 233bce3890613f9fd10988d2a3698d598fbba98cc973498a5d60098af2cac111
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 779ecc0ac8004761f8fee1022081521d2739be5c59c6c718c2d312d9ec3c86b2665a159b3ec7eff67f23a78a160d2574fa174138fa9ae436394f7cd4220374de
|
7
|
+
data.tar.gz: 4ba805bbcd91730f6df4061f1555043db1c4afae8a007063774608bfbea324337936fd2982bb5e914f9eb9ccd203d602a037c8fc67a953f56fd482ee903b2b17
|
data/.gitignore
ADDED
data/Gemfile
ADDED
data/MIT-LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright 2021 Andrew Bohush
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,145 @@
|
|
1
|
+
# ContentPackRails
|
2
|
+
A thin wrapper around [`ActionView::Helpers::CaptureHelper#content_for`](https://api.rubyonrails.org/classes/ActionView/Helpers/CaptureHelper.html#method-i-content_for).
|
3
|
+
Useful when you need to collect multiple content entries during page rendering, for example <script type="text/template"> templates, without woring about possible duplications and name clashes.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
Add this line to your application's Gemfile:
|
7
|
+
|
8
|
+
```ruby
|
9
|
+
gem 'content_pack_rails'
|
10
|
+
```
|
11
|
+
|
12
|
+
And then execute:
|
13
|
+
```bashn
|
14
|
+
$ bundle install
|
15
|
+
```
|
16
|
+
|
17
|
+
## Usage
|
18
|
+
|
19
|
+
Include `ContentPackRails` into your `ApplicationHelper`:
|
20
|
+
```ruby
|
21
|
+
module ApplicationHelper
|
22
|
+
include ContentPackRails
|
23
|
+
end
|
24
|
+
```
|
25
|
+
|
26
|
+
By default, this will add two view helpers `content_pack` and `provide_content_pack`.
|
27
|
+
To avoid collisions with a helper of the same name you might already have in your app, provide a different name:
|
28
|
+
|
29
|
+
```ruby
|
30
|
+
module ApplicationHelper
|
31
|
+
# This will add custom_named_pack and provide_custom_named_pack helpers respectively.
|
32
|
+
include ContentPackRails.init(pack_name: 'custom_named_pack')
|
33
|
+
end
|
34
|
+
```
|
35
|
+
|
36
|
+
Helper `content_pack` declares the same parameters as a standard rails `content_for` and can be used similarly to capture content like this:
|
37
|
+
|
38
|
+
```erb
|
39
|
+
<% content_pack 'content-id' do |id| %>
|
40
|
+
<!--
|
41
|
+
Your content goes here.
|
42
|
+
Additionally, the content name is passed as the first parameter.
|
43
|
+
So you may use it further, for example, to set it as the id attribute for an element.
|
44
|
+
<div id=<%= id %>></div>
|
45
|
+
-->
|
46
|
+
<% end %>
|
47
|
+
```
|
48
|
+
or pass content as a second argument:
|
49
|
+
```erb
|
50
|
+
<% content_pack 'content-id', 'Your content' %>
|
51
|
+
```
|
52
|
+
|
53
|
+
Unlike `content_for`, by default, `content_pack` will not concatenate content with the same name.
|
54
|
+
Multiple calls to `content_pack` with the same content name will result in only the first content being added to the pack.
|
55
|
+
If you would like to add additional content on a given name, you should pass an `append: true` option like so:
|
56
|
+
|
57
|
+
```erb
|
58
|
+
<% content_pack 'content-id', 'first content' %>
|
59
|
+
<!-- following will be concatenated with the first one -->
|
60
|
+
<% content_pack 'content-id', 'second content', append: true %>
|
61
|
+
```
|
62
|
+
|
63
|
+
In case you want to override content on a given name, provide a `flush: true` option:
|
64
|
+
```erb
|
65
|
+
<% content_pack 'content-id', 'first content' %>
|
66
|
+
<!-- following will override the first one -->
|
67
|
+
<% content_pack 'content-id', 'second content', flush: true %>
|
68
|
+
```
|
69
|
+
|
70
|
+
Every content passed to the `content_pack` is aggregated into one "pack," which you can access through the correspondent `provide_content_pack` helper.
|
71
|
+
So later on, you can render the whole pack somewhere in your view, like this:
|
72
|
+
```erb
|
73
|
+
<%= provide_content_pack %>
|
74
|
+
```
|
75
|
+
|
76
|
+
Should you want to have multiple packs, for example, to group content, you can set up those by including this module as many times as needed with different names:
|
77
|
+
```ruby
|
78
|
+
module ApplicationHelper
|
79
|
+
include ContentPackRails.init(pack_name: 'content_pack2')
|
80
|
+
include ContentPackRails.init(pack_name: 'content_pack3')
|
81
|
+
# etc.
|
82
|
+
end
|
83
|
+
```
|
84
|
+
|
85
|
+
### Common use-case example
|
86
|
+
|
87
|
+
You may have javascript components that render HTML using templates in the form of `<script type="text/template">`.
|
88
|
+
Those templates are usually included as a part of rendered HTML, and if you have enough of them, it is hard to track what has already been included.
|
89
|
+
|
90
|
+
Given that the same component may be a part of other components templates and so on, you may end up with multiple copies of the same template included in the final response.
|
91
|
+
`ContentPackRails` helpers tandem allow you to organize JS templates provisioning in a way somewhat similar to how it is done with javascript files.
|
92
|
+
|
93
|
+
For example, you can keep such templates as regular rails view partials in separate files somewhere under `app/views` directory:
|
94
|
+
```bash
|
95
|
+
# ...
|
96
|
+
app/views/comments/_collection_template.html.erb
|
97
|
+
app/views/comments/_item_template.html.erb
|
98
|
+
# ...
|
99
|
+
```
|
100
|
+
Both those templates can be exported using `content_pack` helper like this:
|
101
|
+
```erb
|
102
|
+
<!-- app/views/comments/_item_template.html.erb -->
|
103
|
+
<%= content_pack 'comments-item-template' do |id| %>
|
104
|
+
<script type="text/template" id=<%= id %>>
|
105
|
+
// item component template goes here
|
106
|
+
</script>
|
107
|
+
<% end %>
|
108
|
+
```
|
109
|
+
|
110
|
+
```erb
|
111
|
+
<!-- app/views/comments/_collection_template.html.erb -->
|
112
|
+
|
113
|
+
<% render 'item_template' %>
|
114
|
+
|
115
|
+
<%= content_pack 'comments-collection-template' do |id| %>
|
116
|
+
<script type="text/template" id=<%= id %>>
|
117
|
+
// somewhere here you will likely loop through comments using <comments-item-component> with some properties
|
118
|
+
</script>
|
119
|
+
<% end %>
|
120
|
+
```
|
121
|
+
Note that in `app/views/comments/_collection_template.html.erb` we called `render` for `item_template` because the collection template depends on it.
|
122
|
+
Calling `render` here may be thought of as `import` for the template, which in its turn tirgers `content_pack` (aka `export`) to include the template into a bundle.
|
123
|
+
Each template contains all the imports it depends on, so you can just grab any template and be sure its dependencies also end up in a final pack, like in the following example.
|
124
|
+
|
125
|
+
```erb
|
126
|
+
<!-- app/views/comments/index.html.erb -->
|
127
|
+
|
128
|
+
<% render 'collection_template' %> <!-- this will include collection and item templates -->
|
129
|
+
|
130
|
+
<!-- rest of the index.html -->
|
131
|
+
```
|
132
|
+
|
133
|
+
Resulting bundle of templates we can include in our `app/views/layouts/application.html.rb` like so:
|
134
|
+
```erb
|
135
|
+
<html>
|
136
|
+
<head></head>
|
137
|
+
<body>
|
138
|
+
<%= yield %>
|
139
|
+
</body>
|
140
|
+
<%= provide_content_pack %>
|
141
|
+
</html>
|
142
|
+
```
|
143
|
+
|
144
|
+
## License
|
145
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
data/Rakefile
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
require_relative "lib/content_pack_rails/version"
|
2
|
+
|
3
|
+
Gem::Specification.new do |spec|
|
4
|
+
spec.name = "content_pack_rails"
|
5
|
+
spec.version = ContentPackRails::VERSION
|
6
|
+
spec.authors = ["Andrew Bohush"]
|
7
|
+
spec.email = ["a.bohush01@gmail.com"]
|
8
|
+
spec.homepage = "https://github.com/a-bohush/content_pack_rails.git"
|
9
|
+
spec.summary = "Ruby on Rails helpers for capturing view content into bundles."
|
10
|
+
spec.license = "MIT"
|
11
|
+
|
12
|
+
spec.metadata["source_code_uri"] = spec.homepage
|
13
|
+
# spec.metadata["changelog_uri"] = "TODO: Put your gem's CHANGELOG.md URL here."
|
14
|
+
|
15
|
+
# Specify which files should be added to the gem when it is released.
|
16
|
+
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
|
17
|
+
spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
|
18
|
+
`git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
19
|
+
end
|
20
|
+
|
21
|
+
spec.add_development_dependency "rails", "~> 6.1.4", ">= 6.1.4.1"
|
22
|
+
spec.add_development_dependency "pry-byebug"
|
23
|
+
end
|
@@ -0,0 +1,28 @@
|
|
1
|
+
require "content_pack_rails/version"
|
2
|
+
|
3
|
+
module ContentPackRails
|
4
|
+
def self.init(configs = { pack_name: 'content_pack' })
|
5
|
+
Module.new do
|
6
|
+
silent = proc { |&block| v = $VERBOSE; $VERBOSE = nil; res = block.call; $VERBOSE = v; res }
|
7
|
+
|
8
|
+
define_method configs[:pack_name] do |id, content=nil, options={}, &block|
|
9
|
+
_id = "_id_#{configs[:pack_name]}_#{id}".to_sym
|
10
|
+
_block = proc { block.call(id) } if block
|
11
|
+
options[:append] || options[:flush] || content_for?(_id) && return
|
12
|
+
content_for(_id, content, options.slice(:flush), &_block)
|
13
|
+
(silent.call { instance_variable_get(:"@_#{configs[:pack_name]}_ids") } ||
|
14
|
+
instance_variable_set(:"@_#{configs[:pack_name]}_ids", Set.new)).add(_id)
|
15
|
+
end
|
16
|
+
|
17
|
+
define_method "provide_#{configs[:pack_name]}" do
|
18
|
+
content_for(configs[:pack_name], nil, flush: true)
|
19
|
+
instance_variable_get(:"@_#{configs[:pack_name]}_ids").each do |id|
|
20
|
+
content_for(configs[:pack_name]) { content_for(id) }
|
21
|
+
end
|
22
|
+
content_for(configs[:pack_name])
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
include init
|
28
|
+
end
|
metadata
ADDED
@@ -0,0 +1,86 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: content_pack_rails
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Andrew Bohush
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2021-12-02 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: rails
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: 6.1.4
|
20
|
+
- - ">="
|
21
|
+
- !ruby/object:Gem::Version
|
22
|
+
version: 6.1.4.1
|
23
|
+
type: :development
|
24
|
+
prerelease: false
|
25
|
+
version_requirements: !ruby/object:Gem::Requirement
|
26
|
+
requirements:
|
27
|
+
- - "~>"
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: 6.1.4
|
30
|
+
- - ">="
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: 6.1.4.1
|
33
|
+
- !ruby/object:Gem::Dependency
|
34
|
+
name: pry-byebug
|
35
|
+
requirement: !ruby/object:Gem::Requirement
|
36
|
+
requirements:
|
37
|
+
- - ">="
|
38
|
+
- !ruby/object:Gem::Version
|
39
|
+
version: '0'
|
40
|
+
type: :development
|
41
|
+
prerelease: false
|
42
|
+
version_requirements: !ruby/object:Gem::Requirement
|
43
|
+
requirements:
|
44
|
+
- - ">="
|
45
|
+
- !ruby/object:Gem::Version
|
46
|
+
version: '0'
|
47
|
+
description:
|
48
|
+
email:
|
49
|
+
- a.bohush01@gmail.com
|
50
|
+
executables: []
|
51
|
+
extensions: []
|
52
|
+
extra_rdoc_files: []
|
53
|
+
files:
|
54
|
+
- ".gitignore"
|
55
|
+
- Gemfile
|
56
|
+
- MIT-LICENSE
|
57
|
+
- README.md
|
58
|
+
- Rakefile
|
59
|
+
- content_pack_rails.gemspec
|
60
|
+
- lib/content_pack_rails.rb
|
61
|
+
- lib/content_pack_rails/version.rb
|
62
|
+
homepage: https://github.com/a-bohush/content_pack_rails.git
|
63
|
+
licenses:
|
64
|
+
- MIT
|
65
|
+
metadata:
|
66
|
+
source_code_uri: https://github.com/a-bohush/content_pack_rails.git
|
67
|
+
post_install_message:
|
68
|
+
rdoc_options: []
|
69
|
+
require_paths:
|
70
|
+
- lib
|
71
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - ">="
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '0'
|
76
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
77
|
+
requirements:
|
78
|
+
- - ">="
|
79
|
+
- !ruby/object:Gem::Version
|
80
|
+
version: '0'
|
81
|
+
requirements: []
|
82
|
+
rubygems_version: 3.1.6
|
83
|
+
signing_key:
|
84
|
+
specification_version: 4
|
85
|
+
summary: Ruby on Rails helpers for capturing view content into bundles.
|
86
|
+
test_files: []
|