uri_query_merger 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (4) hide show
  1. checksums.yaml +7 -0
  2. data/README.md +63 -0
  3. data/lib/uri_query_merger.rb +24 -0
  4. metadata +61 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 7dd1e01235f93b54b8de110e67c0e30f084ec198
4
+ data.tar.gz: 1125f9072f87cb77a226c6bf63ee9228bd31440f
5
+ SHA512:
6
+ metadata.gz: efaef9b30289bd3d832d91c7b8ee7efb5f149b429b5b5856cd29c1bc49832055a9a0747fce8148fff9d006381b74a4ed01b08172e36249bcb8cb65d00ebcbef3
7
+ data.tar.gz: c7c2ec2c08fd08d3c09660bad22e98d12572f8bbddb7b13b28dbfc6c0a527f018a36a480b4c14e252f15bb321021aa89394d62ede0dbc8ddc503d163c5db3184
data/README.md ADDED
@@ -0,0 +1,63 @@
1
+ # Rails Sort
2
+
3
+ This gem provides some view helpers and a model for performing basic sorts on ActiveRecord models.
4
+
5
+ This is just for consistency - so a consistent set of parameters are used for sorting. There's also a bunch of sorcery done so that the sort will remember search parameters and not clobber anything.
6
+
7
+ ## Usage
8
+
9
+ Include a reference in your Gemfile:
10
+
11
+ ```ruby
12
+ gem 'rails_sort', github: "thefrontiergroup/rails_sort"
13
+ ```
14
+
15
+ In your view you can create a sortable heading that will provide a link to toggle the sorting options. Sorting is in the following recurring order:
16
+
17
+ 1. None
18
+ 2. Asc
19
+ 3. Desc
20
+
21
+ The link that is returned will have a class attached to it indicating direction (none, "asc", or "desc"). You can use this to style the link appropriately (EG: up/down arrows)
22
+
23
+ If you were on the index action for Users and you wanted to sort by email:
24
+
25
+ ```haml
26
+ %table
27
+ %thead
28
+ %th= sortable_heading_for(:email, "Email")
29
+ ```
30
+
31
+ Which will generate the following link:
32
+
33
+ ```
34
+ <a href="http://localhost:3000?sort_attribute=email&sort_direction=asc">Email</a>
35
+ ```
36
+
37
+ Clicking that link will pass the sort parameters through to your index action.
38
+
39
+ You can use the RailsSort model to sort your collection. EG:
40
+
41
+ ```ruby
42
+ def index
43
+ @users = User.all
44
+ @users = RailSort.sort(@users, params)
45
+ end
46
+ ```
47
+
48
+ You can pass through some default sort options that will be used if no sorting has been indicated by the user.
49
+
50
+ ```ruby
51
+ def index
52
+ @users = User.all
53
+ @users = RailSort.sort(@users, params, {name: :asc})
54
+ end
55
+ ```
56
+
57
+ We also provide an ActiveRecord monkey patch that allows you to call sort directly on a collection:
58
+
59
+ ```ruby
60
+ def index
61
+ @users = User.sort(params)
62
+ end
63
+ ```
@@ -0,0 +1,24 @@
1
+ class UriQueryMerger
2
+
3
+ attr_reader :params, :uri
4
+
5
+ def initialize(uri, params)
6
+ @params = params
7
+ @uri = uri
8
+ end
9
+
10
+ def merge
11
+ new_uri = URI.parse(uri)
12
+ new_uri.query = merged_query
13
+ new_uri.to_s
14
+ end
15
+
16
+ private
17
+
18
+ def merged_query
19
+ new_query = params.stringify_keys
20
+ new_query = new_query.reverse_merge(CGI.parse(URI.parse(uri).query)) if URI.parse(uri).query.present?
21
+ URI.encode_www_form(new_query.to_a)
22
+ end
23
+
24
+ end
metadata ADDED
@@ -0,0 +1,61 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: uri_query_merger
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Jordan Maguire
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-12-17 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rails
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">"
18
+ - !ruby/object:Gem::Version
19
+ version: 3.0.0
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">"
25
+ - !ruby/object:Gem::Version
26
+ version: 3.0.0
27
+ description: Small class for merging a URL with existing params with a given set of
28
+ params
29
+ email:
30
+ - jordan@thefrontiergroup.com.au
31
+ executables: []
32
+ extensions: []
33
+ extra_rdoc_files: []
34
+ files:
35
+ - README.md
36
+ - lib/uri_query_merger.rb
37
+ homepage: https://github.com/jordanmaguire/uri_query_merger
38
+ licenses:
39
+ - WTFPL
40
+ metadata: {}
41
+ post_install_message:
42
+ rdoc_options: []
43
+ require_paths:
44
+ - lib
45
+ required_ruby_version: !ruby/object:Gem::Requirement
46
+ requirements:
47
+ - - ">="
48
+ - !ruby/object:Gem::Version
49
+ version: '0'
50
+ required_rubygems_version: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ requirements: []
56
+ rubyforge_project:
57
+ rubygems_version: 2.4.8
58
+ signing_key:
59
+ specification_version: 4
60
+ summary: Merge params into an existing URL easily
61
+ test_files: []