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

Add sort url #706

Merged
merged 2 commits into from
Jul 29, 2016
Merged
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 @@ -222,6 +222,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
4 changes: 4 additions & 0 deletions lib/ransack/helpers/form_builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,10 @@ def sort_link(attribute, *args)
@template.sort_link @object, attribute, *args
end

def sort_url(attribute, *args)
@template.sort_url @object, attribute, *args
end

def condition_fields(*args, &block)
search_fields(:c, args, block)
end
Expand Down
12 changes: 12 additions & 0 deletions lib/ransack/helpers/form_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,18 @@ def sort_link(search_object, attribute, *args, &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)
unless Search === search
raise TypeError, 'First argument must be a Ransack::Search!'
end
s = SortLink.new(search, attribute, args, params)
url(routing_proxy, s.url_options)
end

private

def options_for(record)
Expand Down
Loading