Skip to content

Commit

Permalink
Merge pull request #129 from avo-hq/add_filters_decode_for_v3
Browse files Browse the repository at this point in the history
add encoded URLs to filters
  • Loading branch information
Paul-Bob authored Nov 3, 2023
2 parents 0db8ea4 + 6ffaaa3 commit 4cfe4d7
Showing 1 changed file with 95 additions and 0 deletions.
95 changes: 95 additions & 0 deletions docs/3.0/basic-filters.md
Original file line number Diff line number Diff line change
Expand Up @@ -700,3 +700,98 @@ class Avo::Filters::Name < Avo::Filters::TextFilter
end
```

## Manually create encoded URLs

You may want to redirect users to filtered states of the <Index /> view from other places in your app. In order to create those filtered states you may use these helpers functions or Rails helpers.


### Rails helpers

:::option `decode_filter_params`

Decodes the `filters` param. This Rails helper can be used anywhere in a view or off the `view_context`.

#### Usage

```ruby
# in a view
decode_filter_params params[:filters] # {"NameFilter"=>"Apple"}

# Or somewhere in an Avo configuration file

class Avo::Actions::DummyAction < Avo::BaseAction
self.name = "Dummy action"

def handle(**args)
filters = view_context.decode_filter_params(params[:filters])

do_something_important_with_the_filters filters
end
end
```
:::

:::option `encode_filter_params`

Encodes a `filters` object into a serialized state that Avo understands. This Rails helper can be used anywhere in a view or off the `view_context`.

#### Usage

```ruby
# in a view
filters = {"NameFilter"=>"Apple"}
encode_filter_params filters # eyJOYW1lRmlsdGVyIjoiQXBwbGUifQ==

# Or somewhere in an Avo configuration file

class Avo::Actions::DummyAction < Avo::BaseAction
self.name = "Dummy action"

def handle(**args)
do_something_important

redirect_to avo.resources_users_path(filters: view_context.decode_filter_params({"NameFilter"=>"Apple"}))
end
end
```
:::

### Standalone helpers

:::option `Avo::Filters::BaseFilter.decode_filters`

Decodes the `filters` param. This standalone method can be used anywhere.

#### Usage

```ruby
class Avo::Actions::DummyAction < Avo::BaseAction
self.name = "Dummy action"

def handle(**args)
filters = Avo::Filters::BaseFilter.decode_filters(params[:filters])

do_something_important_with_the_filters filters
end
end
```
:::

:::option `Avo::Filters::BaseFilter.encode_filters`

Encodes a `filters` object into a serialized state that Avo understands. This standalone method can be used anywhere.

#### Usage

```ruby
class Avo::Actions::DummyAction < Avo::BaseAction
self.name = "Dummy action"

def handle(**args)
do_something_important

redirect_to avo.resources_users_path(filters: Avo::Filters::BaseFilter.encode_filters({"NameFilter"=>"Apple"}))
end
end
```
:::

0 comments on commit 4cfe4d7

Please sign in to comment.