File: jellyswitch.md

package info (click to toggle)
ruby-view-component 2.74.1-1
  • links: PTS, VCS
  • area: contrib
  • in suites: bookworm
  • size: 3,156 kB
  • sloc: ruby: 6,731; sh: 163; javascript: 10; makefile: 4
file content (82 lines) | stat: -rw-r--r-- 3,011 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
---
nav_exclude: true
---

# Jellyswitch

Name: Dave Paola

Github Handle: [@dpaola2](https://github.com/dpaola2)

[Jellyswitch](https://www.jellyswitch.com) is a coworking space management platform

In response to [this tweet](https://twitter.com/joelhawksley/status/1232674647327547394):

I recently began refactoring many of my partials into components. Along the way I discovered an interesting use case, which was to componentize the various bootstrap components I was already using.

Some examples:

- I've componentized the specific layout that I've designed using the grid system. I have defined a `FullWidthLayout` component that wraps its contents in the correct classes to give my layout a good design on both mobile and desktop. (On desktop, there is a sidebar, and on mobile, the sidebar floats on top in a collapsed fashion.)
- [Modals](https://getbootstrap.com/docs/4.4/components/modal/) are now componentized and accept arguments. I had them as partials before, but having ruby classes is an upgrade.
- [Breadcrumbs](https://getbootstrap.com/docs/4.4/components/breadcrumb/) same as above.

Here's one that I use a LOT: `OnOffSwitch`:

```ruby
class OnOffSwitch < ApplicationComponent
  def initialize(predicate:, path:, disabled: false, label: nil)
    @predicate = predicate
    @path = path
    @disabled = disabled
    @label = label
  end

  private

  attr_reader :predicate, :path, :disabled, :label

  def icon_class
    if predicate
      "fas fa-toggle-on"
    else
      "fas fa-toggle-off"
    end
  end
end
```

```erb
<div class="d-flex align-items-center">
  <% if !disabled %>
    <%= link_to path, class: "text-body", remote: true do %>
      <span style="font-size: 20pt">
        <% if predicate %>
          <span class="text-success">
            <i class="<%= icon_class %>"></i>
          </span>
        <% else %>
          <span class="text-danger">
            <i class="<%= icon_class %>"></i>
          </span>
        <% end %>
      </span>
    <% end %>
  <% else %>
    <span style="font-size: 20pt">
      <span class="text-muted">
        <i class="<%= icon_class %>"></i>
      </span>
    </span>
  <% end %>
  &nbsp;
  <%= content %>
</div>
```

Here is an example of how this looks:

<img width="653" src="https://user-images.githubusercontent.com/150509/75365920-cbfb9500-5872-11ea-8234-f1343629a462.png">

I have found that refactoring complex views is made easier and faster by first putting them into a component, extracting the conditionals and other logic into private methods and proceeding from there. And I wind up with a nice set of well-factored components and sub-components, with argument lists and validations. I think the rails community is going to benefit from this library, and I'm hugely appreciative of y'all's efforts on it!

I plan to release an early version of the bootstrap components we've developed at some point in the near future. I would love to collaborate and learn the most appropriate way to structure that contribution.