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

Fix insecure params error and Add sort_url function. #672

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,23 @@ Ransack.configure do |c|
end
```

####`sort_url` is return only url of `sort_link`

It can be used like `sort_link`.

```erb
<%= sort_url(@q, :name, default_order: :desc) %>
```

```erb
<%= sort_url(@q, :last_name, [:last_name, 'first_name asc']) %>
```

```erb
<%= sort_url(@q, :last_name, %i(last_name first_name),
default_order: { last_name: 'asc', first_name: 'desc' }) %>
```

### Advanced Mode

"Advanced" searches (ab)use Rails' nested attributes functionality in order to
Expand Down
24 changes: 20 additions & 4 deletions lib/ransack/helpers/form_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,14 +46,22 @@ def search_form_for(record, options = {}, &proc)
#
def sort_link(search_object, attribute, *args, &block)
search, routing_proxy = extract_search_and_routing_proxy(search_object)
unless Search === search
raise TypeError, 'First argument must be a Ransack::Search!'
end
raise TypeError, 'First argument must be a Ransack::Search!' unless Search === search
args.unshift(capture(&block)) if block_given?
s = SortLink.new(search, attribute, args, params, &block)
s = SortLink.new(search, attribute, args, search_params(search), &block)
link_to(s.name, url(routing_proxy, s.url_options), s.html_options(args))
end

# +sort_url+
# <%= sort_url(@q, :created_at, default_order: :desc) %>
#
def sort_url(search_object, attribute, *args)
search, routing_proxy = extract_search_and_routing_proxy(search_object)
raise TypeError, 'First argument must be a Ransack::Search!' unless Search === search
s = SortLink.new(search, attribute, args, search_params(search))
url(routing_proxy, s.url_options)
end

private

def options_for(record)
Expand All @@ -75,6 +83,14 @@ def extract_search_and_routing_proxy(search)
[search, nil]
end

def search_params(search)
begin
params.permit.tap {|whitelisted| whitelisted[search.context.search_key] = params[search.context.search_key] }
rescue
params
end
end

def url(routing_proxy, options_for_url)
if routing_proxy && respond_to?(routing_proxy)
send(routing_proxy).url_for(options_for_url)
Expand Down