nice_partials 0.1.0 → 0.1.1

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: 80f4ce71d7a0d70adfd7f7c6775178df6705bd74fc520d7d81e1f28ab2b4b831
4
- data.tar.gz: 37bfd17903477aad2e7cdfc2a0eb8a42a36749851e445be88c1172a013b58fba
3
+ metadata.gz: ace03ad5f6583df5b8545c3045f72c606a1cfbef879913633d9e2ef4f38a3fb0
4
+ data.tar.gz: e7284461cae24a294fb95b5517f0b639793e4802307903dea3a8014a9c1f6ebc
5
5
  SHA512:
6
- metadata.gz: 44aeee28bb6aaa22fcb7f3021c49142d6c463ab7f72e083a73c0f0465a7dd96296e5a13271ea74e711ae4905d2f7deadfe89e1b12886c5af6332a3d75b8ce595
7
- data.tar.gz: 90262c8cb7eafae7d7f0b88b4a36ac2e4e41acd1d3e78957e4a6016136b4f788de9d0cd274480774f0f63e9a81cc43e8b040ac980ce91bc0b343dfcd417387e2
6
+ metadata.gz: 60b93b1de86b5031f2b91f7d9c2f9461bba4511ebf7a33fb6d6b6d7f32f07d49793b1e6aa55543013fb3fb2ad88a8e5d915797e25605609560b409b9786cb9e1
7
+ data.tar.gz: 93a6f1a3db7ed2221fdf0f343e612872c3dda91de4115210d67352fd216fd7eb404b9daa5c34ee398cc6c332b4f173c5d6803be07b8696e0df4250358340f28f
data/README.md CHANGED
@@ -1,6 +1,16 @@
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
- TODO
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. Nice Partials is specifically designed to be a lightweight and more Rails-native alternative to [ViewComponent](http://viewcomponent.org) that hopefully provides many of the same benefits while requiring less ceremony. This specific approach originated with [Bullet Train](https://bullettrain.co)'s "Field Partials" and was later reimagined and reimplemented by Dom Christie.
4
+
5
+
6
+ ## Benefits
7
+
8
+ - They're just partials like you're used to, with a few extra features.
9
+ - Less context switching. Your components are all just rendering in the standard view context.
10
+ - You don't have to upgrade your existing partials. You can still nest them in a Nice Partials content area.
11
+ - Less ceremony. You _can_ spin up a custom class to back your partial if you want to, but you don't have to by default, and we don't suggest it.
12
+ - Instead, skip the component class entirely! You can define appropriately scoped helpers right inline with your partial.
13
+ - It's still testable. There's no reason why these can't be as testable as ViewComponents.
4
14
 
5
15
 
6
16
  ## Setup
@@ -14,9 +24,74 @@ gem "nice_partials"
14
24
 
15
25
  ## Usage
16
26
 
27
+ ### Defining a Nice Partial
28
+
29
+ We'll define an example Nice Partial in `app/views/partials/card`. We start by invoking Nice Partials like so:
30
+
31
+ ```
32
+ <% yield p = np %>
33
+ ```
34
+
35
+ We can explain what each thing is doing there later, but just trust us that it's the minimum viable invocation that we're aware of at the moment.
36
+
37
+ After that, you can define your partial content and define your content areas:
38
+
39
+ ```
40
+ <div class="card">
41
+ <%= p.yield :image %>
42
+ <div class="card-body">
43
+ <h5 class="card-title"><%= title %></h5>
44
+ <p class="card-text">
45
+ <%= p.yield :body %>
46
+ </p>
47
+ </div>
48
+ </div>
49
+ ```
50
+
51
+ That's it!
52
+
53
+ ### Utilizing a Nice Partial
54
+
55
+ To use a Nice Partial, just render it like you would any other partial, but also pass a block that accepts a parameter:
56
+
17
57
  ```ruby
58
+ <%= render 'partials/card', title: 'Some Title' do |p| %>
59
+ <% p.content_for :body %>
60
+ Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
61
+ Ut enim ad minim veniam, <strong>quis nostrud exercitation ullamco laboris</strong> nisi ut aliquip ex ea commodo consequat.
62
+ <% end %>
63
+
64
+ <% p.content_for :image %>
65
+ <%= image_tag image_path('example.jpg'), alt: 'An example image' %>
66
+ <% end %>
67
+ <% end %>
68
+ ```
69
+
70
+ ### Defining and Using Well Isolated Helper Methods
71
+
72
+ To minimize the amount of pollution in the global helper namespace, you can define helper methods specifically for your partials _within your partial_ like so:
73
+
18
74
  ```
75
+ <% p.helpers do
19
76
 
77
+ # references should be a link if the user can drill down, otherwise just a text label.
78
+ # (this method has access to the scope of the entire view context and all the other helpers that come with it.)
79
+ def reference_to(user)
80
+ if can? :show, user
81
+ link_to user.name, user
82
+ else
83
+ object.name
84
+ end
85
+ end
86
+
87
+ end %>
88
+ ```
89
+
90
+ Then later in the partial you can use the helper method like so:
91
+
92
+ ```
93
+ <td><%= p.reference_to(user) %></td>
94
+ ```
20
95
 
21
96
  ## MIT License
22
97
 
@@ -2,6 +2,7 @@
2
2
 
3
3
  require_relative "nice_partials/version"
4
4
  require_relative "nice_partials/helper"
5
+ require_relative "partials"
5
6
 
6
7
  module NicePartials
7
8
  end
@@ -1,23 +1,7 @@
1
+ require_relative "partial"
2
+
1
3
  module NicePartials::Helper
2
4
  def np
3
- # self in this context is the context of the view partial we're being called from.
4
- DecoratedViewContext.new(self)
5
- end
6
-
7
- private
8
- class DecoratedViewContext
9
- def initialize(view_context)
10
- @view_context = view_context
11
- @key = SecureRandom.uuid
12
- end
13
-
14
- def yield(name = nil)
15
- raise "You can only use Nice Partial's yield method to retrieve the content of named content area blocks. If you're not trying to fetch the content of a named content area block, you don't need Nice Partials! You can just call the vanilla Rails `yield`." unless name
16
- content_for(name)
17
- end
18
-
19
- def content_for(name, content = nil, options = {}, &block)
20
- @view_context.content_for("#{name}_#{@key}".to_sym, content, options, &block)
21
- end
5
+ NicePartials::Partial.new(self)
22
6
  end
23
7
  end
@@ -0,0 +1,21 @@
1
+ module NicePartials
2
+ class Partial
3
+ def initialize(view_context)
4
+ @view_context = view_context
5
+ @key = SecureRandom.uuid
6
+ end
7
+
8
+ def yield(name = nil)
9
+ raise "You can only use Nice Partial's yield method to retrieve the content of named content area blocks. If you're not trying to fetch the content of a named content area block, you don't need Nice Partials! You can just call the vanilla Rails `yield`." unless name
10
+ content_for(name)
11
+ end
12
+
13
+ def helpers(&block)
14
+ class_eval &block
15
+ end
16
+
17
+ def content_for(name, content = nil, options = {}, &block)
18
+ @view_context.content_for("#{name}_#{@key}".to_sym, content, options, &block)
19
+ end
20
+ end
21
+ end
@@ -1,6 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module NicePartials
4
- VERSION = "0.1.0"
4
+ VERSION = "0.1.1"
5
5
  end
6
-
@@ -0,0 +1,2 @@
1
+ module Partials
2
+ 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.0
4
+ version: 0.1.1
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-01 00:00:00.000000000 Z
12
+ date: 2020-12-02 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,7 +29,9 @@ files:
29
29
  - Rakefile
30
30
  - lib/nice_partials.rb
31
31
  - lib/nice_partials/helper.rb
32
+ - lib/nice_partials/partial.rb
32
33
  - lib/nice_partials/version.rb
34
+ - lib/partials.rb
33
35
  - nice_partials.gemspec
34
36
  - spec/nice_partials_spec.rb
35
37
  homepage: https://github.com/bullet-train-co/nice_partials