Skip to content

Commit

Permalink
[WIP] AC::Parameters is no longer inherited from Hash in Rails 5
Browse files Browse the repository at this point in the history
Rails commit rails/rails@14a3bd5 began a
series of changes that might affect many apps/gems and cause subtle
bugs.

This commit is a start to Ransack adapting to these changes.
  • Loading branch information
jonatack committed Sep 2, 2015
1 parent 456b75e commit ceafc05
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 2 deletions.
9 changes: 7 additions & 2 deletions lib/ransack/helpers/form_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ def url(routing_proxy, options_for_url)
class SortLink
def initialize(search, attribute, args, params)
@search = search
@params = params
@params = parameters_hash(params)
@field = attribute.to_s
@sort_fields = extract_sort_fields_and_mutate_args!(args).compact
@current_dir = existing_sort_direction
Expand Down Expand Up @@ -113,6 +113,11 @@ def html_options(args)

private

def parameters_hash(params)
return params unless params.instance_variable_defined?(:@parameters)
params.instance_variable_get(:@parameters)
end

def extract_sort_fields_and_mutate_args!(args)
return args.shift if args[0].is_a?(Array)
[@field]
Expand All @@ -133,7 +138,7 @@ def search_and_sort_params
end

def search_params
@params[@search.context.search_key].presence || {}
parameters_hash(@params[@search.context.search_key]).presence || {}
end

def sort_params
Expand Down
3 changes: 3 additions & 0 deletions lib/ransack/search.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ class Search
:translate, :to => :base

def initialize(object, params = {}, options = {})
if params.instance_variable_defined?(:@parameters)
params = params.instance_variable_get :@parameters
end
if params.is_a? Hash
params = params.dup
params.delete_if { |k, v| [*v].all?{ |i| i.blank? && i != false } }
Expand Down

2 comments on commit ceafc05

@jonatack
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If anyone is using Rails master targeting 5.0.0.alpha right now and needs pagination that works with Ransack... without Ransack's q params in the pagination links turning into broken ActionController#parameters (currently a problem with both kaminari and will_paginate):

I forked and updated the will_paginate gem here to work. I also optimized it for Ruby 2.2 by freezing strings to reduce object allocations and memory use, and by removing legacy code for Merb and Rails 2 + 3. I'm using this in production and it's working well.

In your gemfile:

gem 'will_paginate', github: 'jonatack/will_paginate'

@ryanswood
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jonatack Thank you for being prepared for Rails 5 and helping out all that depend on ransack and will_paginate. Your efforts are greatly appreciated.

Please sign in to comment.