view_component_reflex 1.0.1 → 1.1.0

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: 82d4e40f0f58fd47d6c163e9054b3497eb46ec96a1e963037d96793dbea35530
4
- data.tar.gz: d169f2e5a7816ee41f56cf033037c61ebb4085b201c85185c8df1cccadbbadb6
3
+ metadata.gz: 42797141d9ed8ecfb013f8f68c9e073b9ee6db4fe4eb04f1c7484babbd2c4e06
4
+ data.tar.gz: 9dfb73b083e7e1ac3417a676f37962c1861435310e0f04934d16cea5824f6eb3
5
5
  SHA512:
6
- metadata.gz: 61562e94b88bc140382c480f61e0b9e6afb7024177378978b14fd02a7d2658042fc530798ccafb65cea2092d0438a968b32361afde80b0fb23deea73b2475f5a
7
- data.tar.gz: b7b4d73cca02d7a2b70d4b43570e09a2a7acede221d9516c29aa68232d421f05cb27da0a743f532369f0db068e01c30c7ed207c426e48a9b2e1fd7857383efab
6
+ metadata.gz: 4505fedd6b66cf2e5b31fe3cdeb8ec3b337a0c6b8de79082926d637c39dd74770b562280ae6f61bc79f434b2184e993c706dcd7fac088d436b185e56228f7d28
7
+ data.tar.gz: ed2b650393469281ecd997a7589f6e9999a1aeda25e624193c54c61c1d7e7b78b012718945c409c5db8db86588a7ea64e1be547a4d351e92b377329eef1d7e18
data/README.md CHANGED
@@ -34,6 +34,25 @@ end
34
34
  <% end %>
35
35
  ```
36
36
 
37
+ ## Collections
38
+
39
+ In order to reconcile state to components in collections, you can specify a `collection_key` method that returns some
40
+ value unique to that component.
41
+
42
+ ```
43
+ class TodoComponent < ViewComponentReflex::Component
44
+ def initialize(todo:)
45
+ @todo = todo
46
+ end
47
+
48
+ def collection_key
49
+ @todo.id
50
+ end
51
+ end
52
+ #
53
+ <%= render(TodoComponent.with_collection(Todo.all)) %>
54
+ ```
55
+
37
56
  ## Custom State Adapters
38
57
 
39
58
  ViewComponentReflex uses session for its state by default. To change this, add
@@ -4,7 +4,7 @@ module ViewComponentReflex
4
4
  def init_stimulus_reflex
5
5
  klass = self
6
6
  @stimulus_reflex ||= Object.const_set(name + "Reflex", Class.new(StimulusReflex::Reflex) {
7
- def refresh!(primary_selector = "[data-controller=\"#{stimulus_controller}\"][data-key=\"#{element.dataset[:key]}\"]", *selectors)
7
+ def refresh!(primary_selector = "[data-controller~=\"#{stimulus_controller}\"][data-key=\"#{element.dataset[:key]}\"]", *selectors)
8
8
  save_state
9
9
  @channel.send :render_page_and_broadcast_morph, self, [primary_selector, *selectors], {
10
10
  "dataset" => element.dataset.to_h,
@@ -98,9 +98,18 @@ module ViewComponentReflex
98
98
  helpers.controller.instance_variable_get(:@stimulus_reflex)
99
99
  end
100
100
 
101
- def component_controller(&blk)
102
- opts = {data: {controller: self.class.stimulus_controller, key: key}}
103
- view_context.content_tag :div, capture(&blk), opts
101
+ def component_controller(opts = {}, &blk)
102
+ self.class.init_stimulus_reflex
103
+ opts[:data] = {
104
+ controller: self.class.stimulus_controller,
105
+ key: key,
106
+ **(opts[:data] || {})
107
+ }
108
+ content_tag :div, capture(&blk), opts
109
+ end
110
+
111
+ def collection_key
112
+ nil
104
113
  end
105
114
 
106
115
  # key is required if you're using state
@@ -108,10 +117,10 @@ module ViewComponentReflex
108
117
  # because it doesn't have a view_context yet
109
118
  # This is the next best place to do it
110
119
  def key
111
- self.class.init_stimulus_reflex
112
120
  # we want the erb file that renders the component. `caller` gives the file name,
113
121
  # and line number, which should be unique. We hash it to make it a nice number
114
122
  key = caller.select { |p| p.include? ".html.erb" }[1]&.hash.to_s
123
+ key += collection_key.to_s if collection_key
115
124
  if @key.nil? || @key.empty?
116
125
  @key = key
117
126
  end
@@ -1,3 +1,3 @@
1
1
  module ViewComponentReflex
2
- VERSION = '1.0.1'
2
+ VERSION = '1.1.0'
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: view_component_reflex
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.1
4
+ version: 1.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Joshua LeBlanc