Skip to content

Commit

Permalink
Merge pull request #706 from yumenosora/add_sort_url
Browse files Browse the repository at this point in the history
Add `sort_url` view helper to return the URL only
  • Loading branch information
jonatack authored Jul 29, 2016
2 parents 0e17fff + 69af45a commit 26cb9e4
Show file tree
Hide file tree
Showing 4 changed files with 314 additions and 0 deletions.
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
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

0 comments on commit 26cb9e4

Please sign in to comment.