Sha256: 0791e5002b1578c8cf910dcb7e1bedfce09f32b01a722229a740a7e6bb13ee54

Contents?: true

Size: 1.32 KB

Versions: 13

Compression:

Stored size: 1.32 KB

Contents

# Custom Filter Method

## Overview

We can add a custom filter method to filter records based on specific criteria. This is useful when the default filtering options do not meet the requirements. The custom filter method can be defined using the `filter_with` option.

## Usage

The custom filter method is implemented using the `filter` dsl_method with the `filter_with` option. Below are the key elements and their usage:

### Syntax

```ruby
filter :filter_name, :filter_type, filter_with: :custom_filter_method
```

### Example

**1. For Search and Select:**

Define the custom filter method in your model:

```ruby
class YourModel < ApplicationRecord
  scope :custom_filter_method, ->(value) { where('column_name LIKE ?', "%#{value}%") }
end
```

Use the custom filter method in your cm admin:

```ruby
filter :filter_name, :search, filter_with: :custom_filter_method
```

```ruby
filter :filter_name, :single_select, helper_method: :options_for_select, filter_with: :custom_filter_method
```

**2. For Date and Range:**

Define the custom filter method in your model:

```ruby
class YourModel < ApplicationRecord
  scope :custom_filter_method, ->(from, to) { where('column_name BETWEEN ? AND ?', from, to) }
end
```

Use the custom filter method in your cm admin:

```ruby
filter :filter_name, :date, filter_with: :custom_filter_method
```

Version data entries

13 entries across 13 versions & 1 rubygems

Version Path
cm-admin-4.6.5 docs/CustomFilterMethod.md
cm-admin-4.4.8 docs/CustomFilterMethod.md
cm-admin-4.4.6 docs/CustomFilterMethod.md
cm-admin-4.4.1 docs/CustomFilterMethod.md
cm-admin-4.4.0 docs/CustomFilterMethod.md
cm-admin-4.3.0 docs/CustomFilterMethod.md
cm-admin-4.2.1 docs/CustomFilterMethod.md
cm-admin-4.2.0 docs/CustomFilterMethod.md
cm-admin-4.1.2 docs/CustomFilterMethod.md
cm-admin-4.1.1 docs/CustomFilterMethod.md
cm-admin-4.1.0 docs/CustomFilterMethod.md
cm-admin-4.0.0 docs/CustomFilterMethod.md
cm-admin-3.0.16 docs/CustomFilterMethod.md