orden 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 45437893eb15d9e3966e3b9275b80503b52a163a
4
+ data.tar.gz: 97eeaf7ed7448abae3912d3c6fa466623a8f8f65
5
+ SHA512:
6
+ metadata.gz: 6cba4fb2a2728569ab9d4fccbebdd03daa1f58ead935968f02620a2e7bd6d8c66fc47ff7d84752cb7ffdb04dfecf93356ee9bbe7cbbde52f5c81857bf61a089a
7
+ data.tar.gz: f30491fa3939427b7677a4ed96dfd3db05bc13825b98c1527f57e2de788c004e7a005e9eaf71e74e9a3a7b010931850bdc7d4ad81bae381e1fd0a6b193a9a162
data/.gitignore ADDED
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --require spec_helper
data/.travis.yml ADDED
@@ -0,0 +1,4 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.2.2
4
+ before_install: gem install bundler -v 1.10.5
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in orden.gemspec
4
+ gemspec
data/README.md ADDED
@@ -0,0 +1,90 @@
1
+ # Orden
2
+
3
+ A simple library (~ 40 LOC) to generate sorting links via
4
+ query strings for example: `http://www.example.com/?sort_attr=id&sort_dir=asc`.
5
+
6
+ The only dependency of this library is *Rack* so it should work in your Rack
7
+ compatible framework of choice (Cuba, Rails, Roda, Sinatra, etc).
8
+
9
+ The typical use case for the library is column sorting on html tables.
10
+
11
+ ## Installation
12
+
13
+ Add this line to your application's Gemfile:
14
+
15
+ ```ruby
16
+ gem 'orden'
17
+ ```
18
+
19
+ And then execute:
20
+
21
+ $ bundle
22
+
23
+ Or install it yourself as:
24
+
25
+ $ gem install orden
26
+
27
+ ## Usage
28
+
29
+ You need to instantiate an `Orden` object in the context of the current
30
+ request, for this you need to pass a Rack::Request or similiar (Roda typical '`r`'
31
+ object, `request` object inside a Rails controller, etc).
32
+
33
+ ```ruby
34
+ Orden.new([request_object], [default sort attr], [default order (asc/desc)])
35
+ ```
36
+
37
+ For example:
38
+
39
+ ```ruby
40
+ @sorter = Orden.new(r, "id", "desc")
41
+ ```
42
+
43
+ In your views you can now call:
44
+
45
+ ```ruby
46
+ @sorter.sort_path([attr])
47
+ ```
48
+
49
+ For example in a table header:
50
+
51
+ ```eruby
52
+ <th><a href="<%= @sorter.sort_path "id" %>">ID</a></th>
53
+ <th><a href="<%= @sorter.sort_path "name" %>">Name</a></th>
54
+ ```
55
+
56
+ and it will generate the expected path to sort your results using that
57
+ attribute for example (/users?sort_attr=id&sort_dir=asc).
58
+
59
+ ## Security
60
+
61
+ Take into account that this library does not apply any type of sanitation to
62
+ the received parameters. Typically sorting attributes should be filtered or
63
+ white listed someway before applying them to an SQL query or equivalent.
64
+
65
+ For example you can create a helper such as:
66
+
67
+ ```ruby
68
+ module SortHelper
69
+ def sort_sql(sorter, attr_whitelist)
70
+ if attr_whitelist.include?(sorter.current_attribute)
71
+ "#{sorter.current_attribute} #{sorter.current_direction}"
72
+ else
73
+ "#{sorter.default_attr} #{sorter.default_dir}"
74
+ end
75
+ end
76
+ end
77
+ ```
78
+
79
+ and the use it in an ActiveRecord query:
80
+
81
+ ```ruby
82
+ @sorter = Orden.new(req, "id", "desc")
83
+ @users = User.order(sort_sql(@sorter, User::SORTABLE_ATTRIBUTES)).
84
+ ```
85
+
86
+ Please take this as an example, this code may not be secure.
87
+
88
+ ## Contributing
89
+
90
+ Bug reports and pull requests are welcome on GitHub at https://github.com/manuca/orden.
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
data/lib/orden.rb ADDED
@@ -0,0 +1,43 @@
1
+ require "orden/version"
2
+ require 'rack/utils'
3
+
4
+ class Orden
5
+ attr_reader :default_attr, :default_dir
6
+
7
+ def initialize(request, default_attr, default_dir = "asc")
8
+ @request = request
9
+ @default_attr = default_attr
10
+ @default_dir = default_dir
11
+ end
12
+
13
+ def current_attribute
14
+ query_hash["sort_attr"] ? query_hash["sort_attr"] : default_attr
15
+ end
16
+
17
+ def current_direction
18
+ query_hash["sort_dir"] ? query_hash["sort_dir"] : default_dir
19
+ end
20
+
21
+ def sort_path(attr)
22
+ h = query_hash.dup
23
+ h["sort_attr"] = attr
24
+
25
+ if attr == current_attribute
26
+ h["sort_dir"] = opposite(current_direction)
27
+ else
28
+ h["sort_dir"] = "asc"
29
+ end
30
+
31
+ "#{@request.path}?#{Rack::Utils.build_query(h)}"
32
+ end
33
+
34
+ private
35
+
36
+ def opposite(direction)
37
+ direction == "asc" ? "desc" : "asc"
38
+ end
39
+
40
+ def query_hash
41
+ @_query_hash ||= Rack::Utils.parse_query(@request.query_string)
42
+ end
43
+ end
@@ -0,0 +1,3 @@
1
+ class Orden
2
+ VERSION = "0.1.1"
3
+ end
data/orden.gemspec ADDED
@@ -0,0 +1,26 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'orden/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "orden"
8
+ spec.version = Orden::VERSION
9
+ spec.authors = ["Manuel Barros Reyes"]
10
+ spec.email = ["manuca@gmail.com"]
11
+
12
+ spec.description = %q{A tiny helper to sort columns in Rack apps}
13
+ spec.summary = %q{It relies exclusively on Rack to parse and rewrite query strings so no extra dependencies are required}
14
+ spec.homepage = "https://github.com/manuca/orden"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
17
+ spec.bindir = "exe"
18
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_dependency "rack", "~> 1.6"
22
+
23
+ spec.add_development_dependency "bundler", "~> 1.10"
24
+ spec.add_development_dependency "rake", "~> 10.0"
25
+ spec.add_development_dependency "rspec", "~> 3.3"
26
+ end
metadata ADDED
@@ -0,0 +1,109 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: orden
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.1
5
+ platform: ruby
6
+ authors:
7
+ - Manuel Barros Reyes
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2015-09-02 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rack
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.6'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.6'
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.10'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.10'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '10.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '10.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '3.3'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '3.3'
69
+ description: A tiny helper to sort columns in Rack apps
70
+ email:
71
+ - manuca@gmail.com
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - ".gitignore"
77
+ - ".rspec"
78
+ - ".travis.yml"
79
+ - Gemfile
80
+ - README.md
81
+ - Rakefile
82
+ - lib/orden.rb
83
+ - lib/orden/version.rb
84
+ - orden.gemspec
85
+ homepage: https://github.com/manuca/orden
86
+ licenses: []
87
+ metadata: {}
88
+ post_install_message:
89
+ rdoc_options: []
90
+ require_paths:
91
+ - lib
92
+ required_ruby_version: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ required_rubygems_version: !ruby/object:Gem::Requirement
98
+ requirements:
99
+ - - ">="
100
+ - !ruby/object:Gem::Version
101
+ version: '0'
102
+ requirements: []
103
+ rubyforge_project:
104
+ rubygems_version: 2.4.5
105
+ signing_key:
106
+ specification_version: 4
107
+ summary: It relies exclusively on Rack to parse and rewrite query strings so no extra
108
+ dependencies are required
109
+ test_files: []