view_component_reflex 1.1.1 → 1.2.0

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: 163bc65c20bb1ac85bb9e15ee2ce4fbf7039a9d90cbe7c4cb1591d03699edc71
4
- data.tar.gz: fb80faac53511b705e0170da3cfae4c7c82c1a59c6bf773e8b86eea30463b9f5
3
+ metadata.gz: 4ed2f845769bba6d5142bef6d6fc601b44841080c49ee47abc0036801f62b145
4
+ data.tar.gz: 73e625d1948e5b477def8e098a4a4d55b9a02763c575e1d4bdd4c734e0f208b0
5
5
  SHA512:
6
- metadata.gz: 9936a4885f9b8e625eca11ac78af316f475764688a5fd2aca9f098aabd802c5e578bdca12266b7176c52c3ff6302dcf7c703e1dd902194bc2dd8c20a1071ab14
7
- data.tar.gz: e5fca59e3e33091ee7b53df0d0cf40007b476835f141cd3f717d7b86e14fde3cec8ffce960a4f23b7ffe5f12a48d10a6917843a9e33c67d911b76485187796c7
6
+ metadata.gz: 76c5b53b3acb0fa68084b762ad14fddbaa0aee1d0b9bbe67f33910f088e7b60062bf5dcf61c7703932b2cfa228f4bccfc768e6b9ab64253397e3483efd50cfb5
7
+ data.tar.gz: 077a34955920288651b183d77971d30eb4fbfeabe45aaa799d78f2ea242cb46f328d23223508d053e53210c00e9aeb570e9d234b571aba3005e57b6448afd5fc
data/README.md CHANGED
@@ -53,6 +53,13 @@ end
53
53
  <%= render(TodoComponent.with_collection(Todo.all)) %>
54
54
  ```
55
55
 
56
+ ## API
57
+
58
+ ### permit_parameter?(initial_param, new_params)
59
+ If a new parameter is passed to the component during rendering, it is used instead of what's in state.
60
+ If you're storing instances in state, you can use this to properly compare them.
61
+
62
+
56
63
  ## Custom State Adapters
57
64
 
58
65
  ViewComponentReflex uses session for its state by default. To change this, add
@@ -76,13 +83,13 @@ class YourAdapter
76
83
  end
77
84
 
78
85
  ##
79
- # set_state is used to modify the state. It accepts a reflex, which gives you
80
- # access to the request, as well as the controller and other useful objects.
86
+ # set_state is used to modify the state.
81
87
  #
82
- # reflex - The reflex instance that's trying to set the state
88
+ # request - a rails request object
89
+ # controller - the current controller
83
90
  # key - a unique string that identifies the component
84
91
  # new_state - the new state to set
85
- def self.set_state(reflex, key, new_state)
92
+ def self.set_state(request, controller, key, new_state)
86
93
  # update the state
87
94
  end
88
95
 
@@ -72,7 +72,7 @@ module ViewComponentReflex
72
72
  end
73
73
 
74
74
  def set_state(new_state = {})
75
- ViewComponentReflex::Engine.state_adapter.set_state(self, element.dataset[:key], new_state)
75
+ ViewComponentReflex::Engine.state_adapter.set_state(request, controller, element.dataset[:key], new_state)
76
76
  end
77
77
 
78
78
  def state
@@ -125,6 +125,10 @@ module ViewComponentReflex
125
125
  nil
126
126
  end
127
127
 
128
+ def permit_parameter?(initial_param, new_param)
129
+ initial_param != new_param
130
+ end
131
+
128
132
  def key
129
133
  # initialize session state
130
134
  if !stimulus_reflex? || session[@key].nil?
@@ -140,9 +144,13 @@ module ViewComponentReflex
140
144
  new_state[k] = instance_variable_get(k)
141
145
  end
142
146
  ViewComponentReflex::Engine.state_adapter.store_state(request, @key, new_state)
147
+ ViewComponentReflex::Engine.state_adapter.store_state(request, "#{@key}_initial", new_state)
143
148
  else
149
+ initial_state = ViewComponentReflex::Engine.state_adapter.state(request, "#{@key}_initial")
144
150
  ViewComponentReflex::Engine.state_adapter.state(request, @key).each do |k, v|
145
- instance_variable_set(k, v)
151
+ unless permit_parameter?(initial_state[k], instance_variable_get(k))
152
+ instance_variable_set(k, v)
153
+ end
146
154
  end
147
155
  end
148
156
  @key
@@ -5,12 +5,12 @@ module ViewComponentReflex
5
5
  request.session[key] ||= {}
6
6
  end
7
7
 
8
- def self.set_state(reflex, key, new_state)
8
+ def self.set_state(request, controller, key, new_state)
9
9
  new_state.each do |k, v|
10
- state(reflex.request, key)[k] = v
10
+ state(request, key)[k] = v
11
11
  end
12
- store = reflex.request.session.instance_variable_get("@by")
13
- store.commit_session reflex.request, reflex.controller.response
12
+ store = request.session.instance_variable_get("@by")
13
+ store.commit_session request, controller.response
14
14
  end
15
15
 
16
16
  def self.store_state(request, key, new_state = {})
@@ -1,3 +1,3 @@
1
1
  module ViewComponentReflex
2
- VERSION = '1.1.1'
2
+ VERSION = '1.2.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.1.1
4
+ version: 1.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Joshua LeBlanc