nice_partials 0.1.2 → 0.1.3

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
  SHA256:
3
- metadata.gz: d0a121209bad4d81ba1702d9b34f908e90da58b27f224d78714129654a2b4a94
4
- data.tar.gz: ee098d947c35bcf4cdb29d5aeb123856d6df88970686c310290c056d30502fc8
3
+ metadata.gz: af46913e68f4ca3d87579e5491ce53f040f11878d4671df03cfd7fc89723b66d
4
+ data.tar.gz: 1b4938352c280f79aed63af5caea1899737d4b8f8961e13cf0013c1790aeb88d
5
5
  SHA512:
6
- metadata.gz: 3f22e3ff0b5e24645c4f91025a02b08c2d3b018877caec5203d501483242f654364038d8f3fd7913cda2b5bceb6dc180d4d8846bd242999799146c3a68a3e37f
7
- data.tar.gz: 026a3b89e7471b8e45765abbe78ec9a86b62311a9f145bd740d046be80cb1236ee1363a7e9c662008394124c4b31db1a0c39d3ecfa0c9fdf6674315b4cf9bcbc
6
+ metadata.gz: 966d51762add5e871895c3eca4b8860fe3aff73a35e144095566b996ec7763a9df34e419cc4a1aa2d97078f3411e9ac0aeb6ac3da0c24f3b38db4ba08b6a5a2d
7
+ data.tar.gz: 2fedb3928eaa838b754d6bb7fe84638e0588a6dab2574200ad49c810bad1bbf360b872e535b5d62516ee02e468adfe801b6e90c136d899d8792cab18eb5b8ac5
data/README.md CHANGED
@@ -1,18 +1,18 @@
1
1
  # nice_partials [![[version]](https://badge.fury.io/rb/nice_partials.svg)](https://badge.fury.io/rb/nice_partials) [![[travis]](https://travis-ci.org/andrewculver/nice_partials.svg)](https://travis-ci.org/andrewculver/nice_partials)
2
2
 
3
- Nice Partials provides a light layer of magic on top of traditional Rails view partials to try and make them an even better fit for extracting and reusing components in your views.
3
+ Nice Partials extends the concept of [`content_for` blocks and `yield`](https://guides.rubyonrails.org/layouts_and_rendering.html#using-the-content-for-method) for those times when a partial needs to provide one or more named "content areas" or "slots". This thin, optional layer of magic helps make traditional Rails view partials an even better fit for extracting components from your views, like so:
4
4
 
5
- It allows your partials to define named content areas like this:
6
-
7
- `app/views/partials/_card.html.erb`:
5
+ `app/views/components/_card.html.erb`:
8
6
  ```html+erb
9
7
  <div class="card">
10
8
  <%= p.yield :image %>
11
9
  <div class="card-body">
12
10
  <h5 class="card-title"><%= title %></h5>
13
- <p class="card-text">
14
- <%= p.yield :body %>
15
- </p>
11
+ <% if p.content_for? :body %>
12
+ <p class="card-text">
13
+ <%= p.yield :body %>
14
+ </p>
15
+ <% end %>
16
16
  </div>
17
17
  </div>
18
18
  ```
@@ -20,7 +20,7 @@ It allows your partials to define named content areas like this:
20
20
  These partials can still be utilized with a standard `render` call, but you can specify how to populate the content areas like so:
21
21
 
22
22
  ```html+erb
23
- <%= render 'partials/card', title: 'Some Title' do |p| %>
23
+ <%= render 'components/card', title: 'Some Title' do |p| %>
24
24
  <% p.content_for :body do %>
25
25
  Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod
26
26
  tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam,
@@ -50,11 +50,7 @@ Compared to something more heavy-handed, Nice Partials:
50
50
  - are still testable!
51
51
 
52
52
 
53
- ## How does it work?
54
-
55
- Nice Partials slightly extends the concept of [`content_for` blocks and `yield`](https://guides.rubyonrails.org/layouts_and_rendering.html#using-the-content-for-method) so they can be properly used to define and utilize "content areas" or "slots" in simple ERB partials.
56
-
57
- ### Can't I do the same thing without Nice Partials?
53
+ ## Can't I do the same thing without Nice Partials?
58
54
 
59
55
  You can almost accomplish the same thing without Nice Partials, but in practice you end up having to flush the content buffers after using them, leading to something like this:
60
56
 
@@ -64,10 +60,12 @@ You can almost accomplish the same thing without Nice Partials, but in practice
64
60
  <% content_for :image, flush: true do '' end %>
65
61
  <div class="card-body">
66
62
  <h5 class="card-title"><%= title %></h5>
67
- <p class="card-text">
68
- <%= yield :body %>
69
- <% content_for :body, flush: true do '' end %>
70
- </p>
63
+ <% if content_for? :body %>
64
+ <p class="card-text">
65
+ <%= yield :body %>
66
+ <% content_for :body, flush: true do '' end %>
67
+ </p>
68
+ <% end %>
71
69
  </div>
72
70
  </div>
73
71
  ```
@@ -91,7 +89,7 @@ gem "nice_partials"
91
89
 
92
90
  You only need to use Nice Partials when:
93
91
 
94
- - you want to define multiple named content areas in your partial. If you don't have multiple named content areas in your partial, you could just pass your content into the partial using the standard block and `yield` approach.
92
+ - you want to define one or more named content areas in your partial. If you don't have multiple named content areas in your partial, you could just pass your content into the partial using the standard block and `yield` approach.
95
93
 
96
94
  - you want to specifically isolate your helper methods for a specific partial.
97
95
 
@@ -115,7 +113,7 @@ Here's what is happening here:
115
113
  Once you've done this at the top of your partial file, you can then use `<%= p.yield :some_section %>` to render whatever content areas you want to be passed into your partial.
116
114
 
117
115
 
118
- ### Extra Credit: Defining and Using Well Isolated Helper Methods
116
+ ### Defining and using well isolated helper methods
119
117
 
120
118
  To minimize the amount of pollution in the global helper namespace, you can use the shared context object to define helper methods specifically for your partials _within your partial_ like so:
121
119
 
data/lib/nice_partials.rb CHANGED
@@ -2,9 +2,18 @@
2
2
 
3
3
  require_relative "nice_partials/version"
4
4
  require_relative "nice_partials/helper"
5
+ require_relative "nice_partials/monkey_patch"
5
6
  require_relative "partials"
6
7
 
7
8
  module NicePartials
8
9
  end
9
10
 
11
+ # TODO Is there somewhere better we can put this?
12
+ def nice_partials_locale_prefix_from_view_context_and_block(context, block)
13
+ root_paths = context.view_renderer.lookup_context.view_paths.map(&:path)
14
+ partial_location = block.source_location.first.dup
15
+ root_paths.each { |path| partial_location.gsub!(/^#{path}\//, '') }
16
+ partial_location.split('.').first.gsub('/_', '/').gsub('/', '.')
17
+ end
18
+
10
19
  ActionView::Base.send :include, NicePartials::Helper
@@ -4,4 +4,22 @@ module NicePartials::Helper
4
4
  def np
5
5
  NicePartials::Partial.new(self)
6
6
  end
7
+
8
+ def nice_partials_push_t_prefix(prefix)
9
+ @_nice_partials_t_prefixes ||= []
10
+ @_nice_partials_t_prefixes << prefix
11
+ end
12
+
13
+ def nice_partials_pop_t_prefix
14
+ @_nice_partials_t_prefixes ||= []
15
+ @_nice_partials_t_prefixes.pop
16
+ end
17
+
18
+ def t(key, options = {})
19
+ if @_nice_partials_t_prefixes&.any? && key.first == '.'
20
+ key = "#{@_nice_partials_t_prefixes.last}#{key}"
21
+ end
22
+
23
+ super(key, options)
24
+ end
7
25
  end
@@ -0,0 +1,23 @@
1
+ # Monkey patch required to make `t` work as expected. Is this evil?
2
+ # TODO Do we need to monkey patch other types of renderers as well?
3
+ class ActionView::PartialRenderer
4
+ alias_method :original_render, :render
5
+
6
+ # See `content_for` in `lib/nice_partials/partial.rb` for something similar.
7
+ def render(partial, context, block)
8
+ if block
9
+ partial_prefix = nice_partials_locale_prefix_from_view_context_and_block(context, block)
10
+ context.nice_partials_push_t_prefix partial_prefix
11
+ else
12
+ # Render partial calls with no block should disable any prefix magic.
13
+ context.nice_partials_push_t_prefix ''
14
+ end
15
+
16
+ result = original_render(partial, context, block)
17
+
18
+ # Whether there was a block or not, pop off whatever we put on the stack.
19
+ context.nice_partials_pop_t_prefix
20
+
21
+ return result
22
+ end
23
+ end
@@ -1,5 +1,7 @@
1
1
  module NicePartials
2
2
  class Partial
3
+ delegate_missing_to :@view_context
4
+
3
5
  def initialize(view_context)
4
6
  @view_context = view_context
5
7
  @key = SecureRandom.uuid
@@ -14,8 +16,20 @@ module NicePartials
14
16
  class_eval &block
15
17
  end
16
18
 
19
+ # See the `ActionView::PartialRenderer` monkey patch in `lib/nice_partials/monkey_patch.rb` for something similar.
17
20
  def content_for(name, content = nil, options = {}, &block)
18
- @view_context.content_for("#{name}_#{@key}".to_sym, content, options, &block)
21
+ if block_given?
22
+ partial_prefix = nice_partials_locale_prefix_from_view_context_and_block(@view_context, block)
23
+ @view_context.nice_partials_push_t_prefix(partial_prefix)
24
+ end
25
+
26
+ result = @view_context.content_for("#{name}_#{@key}".to_sym, content, options, &block)
27
+
28
+ if block_given?
29
+ @view_context.nice_partials_pop_t_prefix
30
+ end
31
+
32
+ return result
19
33
  end
20
34
 
21
35
  def content_for?(name)
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module NicePartials
4
- VERSION = "0.1.2"
4
+ VERSION = "0.1.3"
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: nice_partials
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.1.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Andrew Culver
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2020-12-02 00:00:00.000000000 Z
12
+ date: 2021-02-11 00:00:00.000000000 Z
13
13
  dependencies: []
14
14
  description: A little bit of magic to make partials perfect for components.
15
15
  email:
@@ -29,6 +29,7 @@ files:
29
29
  - Rakefile
30
30
  - lib/nice_partials.rb
31
31
  - lib/nice_partials/helper.rb
32
+ - lib/nice_partials/monkey_patch.rb
32
33
  - lib/nice_partials/partial.rb
33
34
  - lib/nice_partials/version.rb
34
35
  - lib/partials.rb
@@ -53,7 +54,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
53
54
  - !ruby/object:Gem::Version
54
55
  version: '0'
55
56
  requirements: []
56
- rubygems_version: 3.0.8
57
+ rubygems_version: 3.1.4
57
58
  signing_key:
58
59
  specification_version: 4
59
60
  summary: A little bit of magic to make partials perfect for components.