tramway 1.0.1.3 → 1.0.2.1

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 63be607c08e52f57b726d9f5c84f719febc22e35c0fb5d711f4c855268917203
4
- data.tar.gz: 004a821c356a9f2e0e9503ddef030b3502510b4d9d9d72fe44ac1725443ee4b6
3
+ metadata.gz: 581bdfc4cccc870402d66738f53acc44cb84782ea875afa0abbe6a6d4adb7717
4
+ data.tar.gz: 236f05a1f96eed1f559b7d9aabe0aae2bb01e58588f54d124ccc879a4ca6fc0f
5
5
  SHA512:
6
- metadata.gz: 0c53956b38f0110ea92c4d39e6c97e932534276a1d4d7cb11ca9087910ac4659861f3743c87416f480e57f5a3df3639434c7a5b7e921b80098e3efbf01b42c5a
7
- data.tar.gz: b179d2f0c46930bdab407ac94a99f0eb33f361eb0833850ede97a6fcbdccba9090c68edbbbd453f6b318bc69f9c965b732f9f1d0a58f985e720bfae520646d14
6
+ metadata.gz: 5b1a3f26325a610c59c58781046848ed3457969f5e9f1a0715a35915474549933186b089bd1a0238d16535140f844b891167deaeb7247601a972616c3514eed0
7
+ data.tar.gz: f5f1fe2b2d30bc74be63fd1e0b22f36468bd10e60c7910005db112c713bd397f9fc4bfe7783731da73749cf3333fea449674c01c9899d680c3f88e234047facf
data/README.md CHANGED
@@ -244,7 +244,7 @@ class UserDecorator < Tramway::BaseDecorator
244
244
 
245
245
  # you can provide representations with ViewComponent to avoid implementing views with Rails Helpers
246
246
  def posts_table
247
- render TableComponent.new(object.posts)
247
+ component 'table', object.posts
248
248
  end
249
249
  end
250
250
  ```
@@ -299,6 +299,39 @@ end
299
299
  With this configuration in place, the index page will render the `Create`
300
300
  button component above the table of projects.
301
301
 
302
+ #### Add header content to show pages
303
+
304
+ To inject custom content above a record's details, define an
305
+ object-level `show_header_content` method on its decorator. The method
306
+ can return any rendered content and has full access to the decorated
307
+ object's helpers and attributes.
308
+
309
+ #### Use `view_context` in show and index pages
310
+
311
+ Some helpers—such as `button_to` with non-`GET` methods—need access to the
312
+ live request context to include CSRF tokens. Pass the Rails `view_context`
313
+ into your decorated objects so their render calls execute inside the
314
+ current request.
315
+
316
+ ```ruby
317
+ def show
318
+ @project = tramway_decorate(Project.find(params[:id])).with(view_context:)
319
+ end
320
+ ```
321
+
322
+ For index pages, decorate each record with the current context before
323
+ rendering headers or per-row components:
324
+
325
+ ```ruby
326
+ def index
327
+ @projects = tramway_decorate(Project.all).map { |project| project.with(view_context:) }
328
+ end
329
+ ```
330
+
331
+ Passing the context this way ensures `show_header_content` and
332
+ `index_header_content` blocks can safely call helpers that require the
333
+ session-bound authenticity token.
334
+
302
335
  #### Decorate a single object
303
336
 
304
337
  You can use the same method to decorate a single object either
@@ -309,6 +342,11 @@ def show
309
342
  end
310
343
  ```
311
344
 
345
+ All objects returned from `tramway_decorate` respond to
346
+ `with(view_context:)`, so you can attach the current Rails `view_context`
347
+ when you need decorator-rendered content to use helpers that rely on the
348
+ active request (such as CSRF tokens).
349
+
312
350
  #### Decorate a collection of objects
313
351
 
314
352
  ```ruby
@@ -691,11 +729,14 @@ attributes, so you can pass things like `id`, `data` attributes, or additional c
691
729
  utility (e.g. a class that starts with `w-`), the component automatically appends `w-full` to keep the table responsive. This
692
730
  allows you to extend the default styling without losing the sensible defaults provided by the component.
693
731
 
732
+ Use the optional `href:` argument on `tramway_row` to turn an entire row into a link. Linked rows gain pointer and hover styles
733
+ (`cursor-pointer hover:bg-gray-100 dark:hover:bg-gray-700`) to indicate interactivity.
734
+
694
735
  ```erb
695
736
  <%= tramway_table class: 'max-w-3xl border border-gray-200', data: { controller: 'table' } do %>
696
737
  <%= tramway_header', headers: ['Name', 'Email'] %>
697
738
 
698
- <%= tramway_row do %>
739
+ <%= tramway_row href: user_path(user) do %>
699
740
  <%= tramway_cell do %>
700
741
  <%= user.name %>
701
742
  <% end %>
@@ -731,8 +772,14 @@ component applies.
731
772
  Tramway ships with helpers for common UI patterns built on top of Tailwind components.
732
773
 
733
774
  * `tramway_button` renders a button-styled form submit by default and accepts `path`, optional `text`, HTTP `method`, and styling
734
- options such as `color`, `type`, and `size`. Pass `link: true` to render a button-styled link instead. All additional keyword
735
- arguments are forwarded to the underlying component as HTML attributes.
775
+ options such as `color`, `type`, and `size`. It uses Rails' `button_to` helper by default (or when `link: false` is passed),
776
+ and switches to `link_to` when you set `link: true`.
777
+
778
+ ```erb
779
+ <%= tramway_button path: user_path(user), text: 'Open profile', link: true %>
780
+ ```
781
+
782
+ All additional keyword arguments are forwarded to the underlying component as HTML attributes.
736
783
 
737
784
  The `type` option maps semantic intent to Tailwind color families. The full set of supported values is:
738
785
 
@@ -964,7 +1011,6 @@ end
964
1011
  * [Behave as ActiveRecord. Why do we want objects to be AR lookalikes?](https://kalashnikovisme.medium.com/behave-as-activerecord-why-do-we-want-objects-to-be-ar-lookalikes-d494d692e1d3)
965
1012
  * [Decorating associations in Rails with Tramway](https://kalashnikovisme.medium.com/decorating-associations-in-rails-with-tramway-b46a28392f9e)
966
1013
  * [Easy-to-use Tailwind-styled multi-select built with Stimulus](https://medium.com/@kalashnikovisme/easy-to-use-tailwind-styled-multi-select-built-with-stimulus-b3daa9e307aa)
967
- *
968
1014
 
969
1015
  ## Contributing
970
1016
 
@@ -23,7 +23,10 @@ module Tramway
23
23
  end
24
24
 
25
25
  def show
26
- @entity = tramway_decorate model_class.find(params[:id]), namespace: entity.namespace
26
+ @entity = tramway_decorate(
27
+ model_class.find(params[:id]),
28
+ namespace: entity.namespace
29
+ ).with(view_context:)
27
30
  end
28
31
 
29
32
  private
@@ -1,12 +1,18 @@
1
1
  = tramway_container do
2
- %h1.text-4xl.font-bold.mb-4
3
- = @entity.title
2
+ .flex.justify-between.items-center
3
+ %h1.text-4xl.font-bold.mb-4
4
+ = @entity.title
5
+
6
+ .flex.justify-end.mt-2
7
+ - if @entity.show_header_content.present?
8
+ = @entity.show_header_content
4
9
 
5
10
  - if @entity.show_attributes.empty?
6
11
  %p.text-center.mt-10
7
12
  You should fill object-level method `show_attributes` inside your
8
13
  = @entity.class.name
9
- = tramway_table do
14
+
15
+ = tramway_table class: 'mt-4' do
10
16
  - @entity.show_attributes.each do |attribute|
11
17
  = tramway_row do
12
18
  = tramway_cell do
@@ -18,12 +18,18 @@ module Tramway
18
18
  include Tramway::Helpers::DecorateHelper
19
19
  include Tramway::Helpers::ComponentHelper
20
20
 
21
- attr_reader :object
21
+ attr_reader :object, :view_context
22
22
 
23
23
  def initialize(object)
24
24
  @object = object
25
25
  end
26
26
 
27
+ def with(view_context:)
28
+ @view_context = view_context
29
+
30
+ self
31
+ end
32
+
27
33
  class << self
28
34
  include Tramway::Helpers::ComponentHelper
29
35
  include Tramway::Utils::Render
@@ -71,6 +77,10 @@ module Tramway
71
77
  []
72
78
  end
73
79
 
80
+ def show_header_content
81
+ nil
82
+ end
83
+
74
84
  # :reek:ManualDispatch { enabled: false } because there is the idea to manual dispatch
75
85
  def method_missing(method_name, *, &)
76
86
  url_helpers = Rails.application.routes.url_helpers
@@ -6,6 +6,10 @@ module Tramway
6
6
  #
7
7
  module Render
8
8
  def render(*, &)
9
+ render_context = defined?(@view_context) && @view_context
10
+
11
+ return render_context.render(*, &) if render_context
12
+
9
13
  ActionController::Base.render(*, &)
10
14
  end
11
15
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Tramway
4
- VERSION = '1.0.1.3'
4
+ VERSION = '1.0.2.1'
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: tramway
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.1.3
4
+ version: 1.0.2.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - kalashnikovisme