Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature flexible resource filters #2091

Merged

Commits on Jul 5, 2021

  1. Make resource filtering more flexible

    this commit introduces a breaking change for apps using the class method `alchemy_resource_filters` in their models used in custom modules.
    
    The change introduces a new data structure for defining filters. It allows to set up multiple filters in two different ways:
    
    * where the filter name is assumed to be a scope on the model and the values are passed as the argument
    * where the filter values are assumed to be scopes on the model (basically as it was until now)
    
    Example:
    
    ```
    class Mymodel < ApplicationRecord
      scope :by_language, ->(lang) { where(language: lang) }
      scope :published, -> { where(published: true) }
      scope :drafts, -> { not.published }
    
      def self.alchemy_resource_filters
        [
          {
            name: :by_language,
            values: distinct.pluck(:language)
          },
          {
            name: :status,
            values: %w[published drafts]
          }
        ]
      end
    end
    ```
    
    Both, the name and the values can be translated. Since the filter values are passed into the `options_for_select` helper, they can be provided in two ways: array of strings/symbols, or as an array of arrays. In the first case Alchemy will lookup a translation in `alchemy.filters.<name_of_filter>.values` whereas in the second case the provided values will just be passed to into `options_for_select` directly.
    
    There are two security measures in place against filter param tempering:
    
    1. Arriving filter values must match the values defined in the model. If the value doesn't exist, the parameter gets rejected and the value won't be send to the model.
    
    2. Rails' strong parameters is used for the case where someone tempered with the parameter names. If a name and doesn't match one of the defined filter names in the model it won't get the permission to pass.
    robinboening committed Jul 5, 2021
    Configuration menu
    Copy the full SHA
    161c792 View commit details
    Browse the repository at this point in the history
  2. Configuration menu
    Copy the full SHA
    1d60447 View commit details
    Browse the repository at this point in the history
  3. Configuration menu
    Copy the full SHA
    46705ff View commit details
    Browse the repository at this point in the history

Commits on Jul 7, 2021

  1. Configuration menu
    Copy the full SHA
    b3c69ac View commit details
    Browse the repository at this point in the history
  2. Configuration menu
    Copy the full SHA
    f7e3d9a View commit details
    Browse the repository at this point in the history
  3. Configuration menu
    Copy the full SHA
    b9bc437 View commit details
    Browse the repository at this point in the history
  4. Configuration menu
    Copy the full SHA
    8296e47 View commit details
    Browse the repository at this point in the history
  5. Configuration menu
    Copy the full SHA
    2e0a870 View commit details
    Browse the repository at this point in the history
  6. Configuration menu
    Copy the full SHA
    7b58c6e View commit details
    Browse the repository at this point in the history
  7. Configuration menu
    Copy the full SHA
    20aad20 View commit details
    Browse the repository at this point in the history

Commits on Jul 8, 2021

  1. support but deprecate legacy alchemy_resource_filters data structure

    This commit is supposed to be reverted after Alchemy v6.0 is released.
    robinboening committed Jul 8, 2021
    Configuration menu
    Copy the full SHA
    4af2611 View commit details
    Browse the repository at this point in the history

Commits on Jul 9, 2021

  1. Configuration menu
    Copy the full SHA
    762c6a6 View commit details
    Browse the repository at this point in the history
  2. Configuration menu
    Copy the full SHA
    46a501c View commit details
    Browse the repository at this point in the history