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

Support nil as a meaningful param value #18

Open
emolayi opened this issue Jan 2, 2019 · 0 comments
Open

Support nil as a meaningful param value #18

emolayi opened this issue Jan 2, 2019 · 0 comments

Comments

@emolayi
Copy link
Contributor

emolayi commented Jan 2, 2019

To support a boolean field that is used as having a trinary value, I have had to override the filter_instructions_for method (see below). This is because a nil value is discarded.

# Override ActionSet filter_instructions_for method to modify filtering behavior.
  def filter_instructions_for(set)
    filter_params_hash = super

    # Allow `nil` values to be passed as meaningful filters on boolean fields
    filter_params.select { |_, v| v.include? 'nil' }.each do |k, v|
      modified_nil_value = nil if v.instance_of?(String) && v == 'nil'
      modified_nil_value = manipulate_trinary_boolean_array(v) if v.instance_of?(Array)
      filter_params_hash[k] = modified_nil_value if modified_nil_value
    end

    filter_params_hash
  end

  def manipulate_trinary_boolean_array(array)
    array.map do |b|
      next nil if b == 'nil'

      ActiveModel::Type::Boolean.new.cast(b)
    end
  end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant