Skip to content

Commit

Permalink
chore: add turbo 8 changes (#177)
Browse files Browse the repository at this point in the history
* chore: add turbo 8 changes

* add turbo
  • Loading branch information
Paul-Bob authored Feb 28, 2024
1 parent 5d532e4 commit 18af4aa
Show file tree
Hide file tree
Showing 3 changed files with 127 additions and 60 deletions.
125 changes: 70 additions & 55 deletions docs/3.0/actions.md
Original file line number Diff line number Diff line change
Expand Up @@ -204,59 +204,6 @@ def handle(**args)
end
```

You may want to redirect to another action. Here's an example of how to create a multi-step process, passing arguments from one action to another.
In this example the initial action prompts the user to select the fields they wish to update, and in the subsequent action, the chosen fields will be accessible for updating.

:::code-group
```ruby[PreUpdate]
class Avo::Actions::City::PreUpdate < Avo::BaseAction
self.name = "Update"
def fields
field :name, as: :boolean
field :population, as: :boolean
end
def handle(**args)
arguments = Base64.encode64 Avo::Services::EncryptionService.encrypt(
message: {
cities: args[:query].map(&:id),
render_name: args[:fields][:name],
render_population: args[:fields][:population]
},
purpose: :action_arguments
)
redirect_to "/admin/resources/city/actions?action_id=Avo::Actions::City::Update&arguments=#{arguments}", turbo_frame: "actions_show"
end
end
```

```ruby[Update]
class Avo::Actions::City::Update < Avo::BaseAction
self.name = "Update"
self.visible = -> { false }
def fields
field :name, as: :text if arguments[:render_name]
field :population, as: :number if arguments[:render_population]
end
def handle(**args)
City.find(arguments[:cities]).each do |city|
city.update! args[:fields]
end
succeed "City updated!"
end
end
```

:::info `turbo_frame`
Notice the `turbo_frame: "actions_show"` present on the redirect of `Avo::Actions::City::PreUpdate` action. That argument is essential to have a flawless redirect between the actions.
:::


:::option `turbo`
There are times when you don't want to perform the actions with Turbo. In such cases, turbo should be set to false.
:::
Expand Down Expand Up @@ -362,6 +309,54 @@ end
```
:::

:::option `navigate_to_action`
<VersionReq version="3.4.2" />

You may want to redirect to another action. Here's an example of how to create a multi-step process, passing arguments from one action to another.
In this example the initial action prompts the user to select the fields they wish to update, and in the subsequent action, the chosen fields will be accessible for updating.

:::code-group
```ruby[PreUpdate]
class Avo::Actions::City::PreUpdate < Avo::BaseAction
self.name = "Update"
def fields
field :name, as: :boolean
field :population, as: :boolean
end
def handle(**args)
navigate_to_action Avo::Actions::City::Update,
arguments: {
cities: args[:query].map(&:id),
render_name: args[:fields][:name],
render_population: args[:fields][:population]
}
end
end
```

```ruby[Update]
class Avo::Actions::City::Update < Avo::BaseAction
self.name = "Update"
self.visible = -> { false }
def fields
field :name, as: :text if arguments[:render_name]
field :population, as: :number if arguments[:render_population]
end
def handle(**args)
City.find(arguments[:cities]).each do |city|
city.update! args[:fields]
end
succeed "City updated!"
end
end
```
:::

## Customization

```ruby{2-6}
Expand Down Expand Up @@ -540,7 +535,27 @@ You may want to dynamically generate an action link. For that you need the actio

Let's see an example use case:

```ruby{15,16,17,18,20}
::: code-group
```ruby{7,8,9,10,11,12,13,15} [Current Version]
field :name,
as: :text,
filterable: true,
name: "name (click to edit)",
only_on: :index do
path, data = Avo::Actions::City::Update.link_arguments(
resource: resource,
arguments: {
cities: Array[resource.record.id],
render_name: true
}
)
link_to resource.record.name, path, data: data
end
```

```ruby{15,16,17,18,20} [< 3.4.2]
field :name,
as: :text,
filterable: true,
Expand All @@ -561,9 +576,9 @@ field :name,
)
link_to resource.record.name, path, data: data
end
```
:::

![actions link demo](/assets/img/actions/action_link.gif)

Expand Down
16 changes: 16 additions & 0 deletions docs/3.0/customization.md
Original file line number Diff line number Diff line change
Expand Up @@ -522,3 +522,19 @@ end

Now, when you visit `https://example.org/account/adrian/avo`, the `account_id` param is `adrian` and it will be appended to all path helpers.
::::

:::option `turbo`
You may want to configure how turbo behave on Avo.

You can configure it using `config.turbo` option on `avo.rb` initializer

Supported options with default values:

```ruby
config.turbo = -> do
{
instantclick: true
}
end
```
:::
46 changes: 41 additions & 5 deletions docs/3.0/upgrade.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ We'll update this page when we release new Avo 3 versions.

If you're looking for the Avo 2 to Avo 3 upgrade guide, please visit [the dedicated page](./avo-2-avo-3-upgrade).

## Upgrade from 3.4.1 to 3.5.0
## Upgrade from 3.4.1 to 3.4.2
:::option Basic Filters URL param changed to `encoded_filters`
When we added the [Dynamic Filters](./dynamic-filters) feature, by mistake we introduced a bug where you couldn't use the [Basic](./basic-filters) and [Dynamic Filters](./dynamic-filters) together because they are both using the `filters` URL param.

Expand All @@ -19,6 +19,12 @@ https://example.com/avo/resources/users?filters[first_name][contains][]=Jason&pa
# After
https://example.com/avo/resources/users?filters[first_name][contains][]=Jason&page=1&encoded_filters=eyJBdm86OkZpbHRlcnM6OklzQWRtaW4iOlsiYWRtaW5zIl19
```
### What to do?

If you have hardcoded links where you reference the `filters` param, change that to `encoded_filters`.
These links might be in Tools, Resource Tools, Menu Items, or regular view partials (yes, basically anywhere you might have added them 🫤).

A quick search through your codebase should reveal them.
:::

:::option Add `active_record_extended` gem to your `Gemfile`
Expand All @@ -29,12 +35,42 @@ This gem uses postgres and was breaking for those who use any other database lik
If you want to keep `Contained in` option on arrays and tags filters you should include the `active_record_extended` gem to your `Gemfile`.
:::

### What to do?
:::option Multiple action flux
First iteration of multiple action flux was using `redirect_to` with `turbo_frame: "actions_show"`. With the update to turbo 8 the redirect was giving some troubles and we decided that is time to improve this experience with a proper response type, [`navigate_to_action`](actions.html#navigate_to_action).

If you have hardcoded links where you reference the `filters` param, change that to `encoded_filters`.
These links might be in Tools, Resource Tools, Menu Items, or regular view partials (yes, basically anywhere you might have added them 🫤).
If you have a multiple action flux implemented with `redirect_to` you should change it to [`navigate_to_action`](actions.html#navigate_to_action).
:::

A quick search through your codebase should reveal them.
:::option Action `link_arguments` method
Action `link_arguments` method handles the `arguments` encoding and encryption internally now so you only need to pass the `arguments` as a hash and the returned `path` will already include the encoded arguments.

```ruby{20,21,22,23,25}
field :name,
as: :text,
filterable: true,
name: "name (click to edit)",
only_on: :index do
arguments = Base64.encode64 Avo::Services::EncryptionService.encrypt( # [!code --]
message: { # [!code --]
cities: Array[resource.record.id], # [!code --]
render_name: true # [!code --]
}, # [!code --]
purpose: :action_arguments # [!code --]
) # [!code --]
arguments = { # [!code ++]
cities: Array[resource.record.id], # [!code ++]
render_name: true # [!code ++]
} # [!code ++]
path, data = Avo::Actions::City::Update.link_arguments(
resource: resource,
arguments: arguments
)
link_to resource.record.name, path, data: data
end
:::
## Upgrade from 3.3.0 to 3.4.0
Expand Down

0 comments on commit 18af4aa

Please sign in to comment.