Skip to content

Commit

Permalink
docs: use dataset.attribute over dataset[:attribute] syntax
Browse files Browse the repository at this point in the history
Also add examples for `Dataset#numeric` and `Dataset#boolean`
  • Loading branch information
marcoroth committed Mar 14, 2023
1 parent 09dc869 commit e225a1f
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 10 deletions.
2 changes: 1 addition & 1 deletion docs/guide/lifecycle.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ StimulusReflex gives you a set of callback events to control how your Reflex act
```ruby
class ExampleReflex < StimulusReflex::Reflex
# will run only if the element has the step attribute, can use "unless" instead of "if" for opposite condition
before_reflex :do_stuff, if: proc { |reflex| reflex.element.dataset[:step] }
before_reflex :do_stuff, if: proc { |reflex| reflex.element.dataset.step }

# will run only if the reflex instance has a url attribute, can use "unless" instead of "if" for opposite condition
before_reflex :do_stuff, if: :url
Expand Down
4 changes: 2 additions & 2 deletions docs/guide/morph-modes.md
Original file line number Diff line number Diff line change
Expand Up @@ -281,8 +281,8 @@ class PagyReflex < ApplicationReflex
include Pagy::Backend

def paginate
pagy, posts = pagy(Post.all, page: element.dataset[:page].to_i)
morph "#paginator", render(partial: "paginator", locals: {pagy: pagy})
pagy, posts = pagy(Post.all, page: element.dataset.page.to_i)
morph "#paginator", render(partial: "paginator", locals: { pagy: pagy })
morph "#posts", render(posts)
end
end
Expand Down
4 changes: 2 additions & 2 deletions docs/guide/patterns.md
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,7 @@ The `Current.user` accessor is now available in your Reflex action methods.
```ruby [app/reflexes/user_reflex.rb]
class UserReflex < ApplicationReflex
def follow
user = User.find(element.dataset[:user_id])
user = User.find(element.dataset.user_id)
Current.user.follow(user)

morph "#following", render(
Expand Down Expand Up @@ -336,7 +336,7 @@ CableReady - which is included and available for use in your Reflex classes - ex
```ruby [app/reflexes/user_reflex.rb]
class UserReflex < ApplicationReflex
def profile
user = User.find(element.dataset[:user_id])
user = User.find(element.dataset.user_id)
morph dom_id(user), render(
partial: "users/profile",
locals: { user: user }
Expand Down
22 changes: 21 additions & 1 deletion docs/guide/reflex-classes.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ You can get and set values on the `session` object, and if you're using the (def
```ruby [app/reflexes/example_reflex.rb]
class ExampleReflex < ApplicationReflex
def test
@id = element.dataset["id"] # @id will be available inside your controller action if you're doing a Page Morph
@id = element.dataset.id # @id will be available inside your controller action if you're doing a Page Morph
end
end
```
Expand Down Expand Up @@ -126,6 +126,7 @@ Here's an example that outlines how you can interact with the `element` property
checked
data-reflex="change->Example#accessors"
data-value="123"
data-enabled="true"
/>
```
:::
Expand Down Expand Up @@ -159,6 +160,25 @@ class ExampleReflex < ApplicationReflex
element.dataset[:value] # => "123"
element.dataset["value"] # => "123"

element.numeric[:value] # => 123.0
element.numeric["value"] # => 123.0
element.numeric[:"data-value"] # => 123.0
element.numeric["data-value"] # => 123.0

element.dataset.numeric[:value] # => 123.0
element.dataset.numeric["value"] # => 123.0
element.dataset.numeric[:"data-value"] # => 123.0
element.dataset.numeric["data-value"] # => 123.0

element.boolean[:enabled] # => true
element.boolean["enabled"] # => true
element.boolean[:"data-enabled"] # => true
element.boolean["data-enabled"] # => true

element.dataset.boolean[:enabled] # => true
element.dataset.boolean["enabled"] # => true
element.dataset.boolean[:"data-enabled"] # => true
element.dataset.boolean["data-enabled"] # => true
end
end
```
Expand Down
6 changes: 3 additions & 3 deletions docs/guide/reflexes.md
Original file line number Diff line number Diff line change
Expand Up @@ -95,13 +95,13 @@ You might design your interface such that you have a deeply nested structure of
</div>
```

This Reflex action will have `post-id` and `category-id` accessible:
This Reflex action will have `post_id` and `category_id` accessible:

```ruby
class CommentReflex < ApplicationReflex
def create
puts element.dataset["post-id"]
puts element.dataset["category-id"]
puts element.dataset.post_id
puts element.dataset.category_id
end
end
```
Expand Down
2 changes: 1 addition & 1 deletion docs/hello-world/quickstart.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ The other two attributes `data-step` and `data-count` are used to pass data to t
```ruby [app/reflexes/counter_reflex.rb]
class CounterReflex < ApplicationReflex
def increment
@count = element.dataset[:count].to_i + element.dataset[:step].to_i
@count = element.dataset.count.to_i + element.dataset.step.to_i
end
end
```
Expand Down

0 comments on commit e225a1f

Please sign in to comment.