tramway 1.0.2 → 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: f347c8016c4bf4a0098ad6075235e573194ad4a1f06c99646d82686187d47524
4
- data.tar.gz: 9cde483c80bdba5a855780d93c169b9b6c9b74739b029f476a0356243e1396fd
3
+ metadata.gz: 581bdfc4cccc870402d66738f53acc44cb84782ea875afa0abbe6a6d4adb7717
4
+ data.tar.gz: 236f05a1f96eed1f559b7d9aabe0aae2bb01e58588f54d124ccc879a4ca6fc0f
5
5
  SHA512:
6
- metadata.gz: 2acee51cfa114f2324809a506d276d482e166e520d40a68d7ca3f5b49cad17a6441f04e097ab20ead0bf1c4e42c2e042cf335aa6fc3889c8f265fe52e11c7ea8
7
- data.tar.gz: 443e3cc99f4e3777f9090e6c23a4b575086d58237a948e7d2b19e6cfff602941c0c9f1fa0907ceb33251d7732c8050450ff2cb1f016e42534ccffae3379eff41
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
  ```
@@ -306,6 +306,32 @@ object-level `show_header_content` method on its decorator. The method
306
306
  can return any rendered content and has full access to the decorated
307
307
  object's helpers and attributes.
308
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
+
309
335
  #### Decorate a single object
310
336
 
311
337
  You can use the same method to decorate a single object either
@@ -316,6 +342,11 @@ def show
316
342
  end
317
343
  ```
318
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
+
319
350
  #### Decorate a collection of objects
320
351
 
321
352
  ```ruby
@@ -698,11 +729,14 @@ attributes, so you can pass things like `id`, `data` attributes, or additional c
698
729
  utility (e.g. a class that starts with `w-`), the component automatically appends `w-full` to keep the table responsive. This
699
730
  allows you to extend the default styling without losing the sensible defaults provided by the component.
700
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
+
701
735
  ```erb
702
736
  <%= tramway_table class: 'max-w-3xl border border-gray-200', data: { controller: 'table' } do %>
703
737
  <%= tramway_header', headers: ['Name', 'Email'] %>
704
738
 
705
- <%= tramway_row do %>
739
+ <%= tramway_row href: user_path(user) do %>
706
740
  <%= tramway_cell do %>
707
741
  <%= user.name %>
708
742
  <% end %>
@@ -738,8 +772,14 @@ component applies.
738
772
  Tramway ships with helpers for common UI patterns built on top of Tailwind components.
739
773
 
740
774
  * `tramway_button` renders a button-styled form submit by default and accepts `path`, optional `text`, HTTP `method`, and styling
741
- options such as `color`, `type`, and `size`. Pass `link: true` to render a button-styled link instead. All additional keyword
742
- 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.
743
783
 
744
784
  The `type` option maps semantic intent to Tailwind color families. The full set of supported values is:
745
785
 
@@ -971,7 +1011,6 @@ end
971
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)
972
1012
  * [Decorating associations in Rails with Tramway](https://kalashnikovisme.medium.com/decorating-associations-in-rails-with-tramway-b46a28392f9e)
973
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)
974
- *
975
1014
 
976
1015
  ## Contributing
977
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,22 +1,17 @@
1
- - @show_header_content = @entity.show_header_content
2
-
3
1
  = tramway_container do
4
-
5
2
  .flex.justify-between.items-center
6
3
  %h1.text-4xl.font-bold.mb-4
7
4
  = @entity.title
8
5
 
9
6
  .flex.justify-end.mt-2
10
- - if @show_header_content.present?
11
- - if @show_header_content.respond_to?(:call)
12
- = @show_header_content.call(@entity)
13
- - else
14
- = @show_header_content
7
+ - if @entity.show_header_content.present?
8
+ = @entity.show_header_content
15
9
 
16
10
  - if @entity.show_attributes.empty?
17
11
  %p.text-center.mt-10
18
12
  You should fill object-level method `show_attributes` inside your
19
13
  = @entity.class.name
14
+
20
15
  = tramway_table class: 'mt-4' do
21
16
  - @entity.show_attributes.each do |attribute|
22
17
  = tramway_row 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
@@ -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.2'
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.2
4
+ version: 1.0.2.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - kalashnikovisme