sorty 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,4 @@
1
+ *.gem
2
+ .bundle
3
+ Gemfile.lock
4
+ pkg/*
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in sorty.gemspec
4
+ gemspec
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
data/lib/sorty.rb ADDED
@@ -0,0 +1,17 @@
1
+ require "sorty/version"
2
+ require "sorty/sort"
3
+ require "sorty/helpers"
4
+
5
+ module Sorty
6
+
7
+ end
8
+
9
+ module ActiveRecord
10
+ class Base
11
+ include Sorty::Sort
12
+ end
13
+ end
14
+
15
+ module ApplicationHelper
16
+ include Sorty::Helpers
17
+ end
@@ -0,0 +1,18 @@
1
+ module Sorty
2
+ module Helpers
3
+ def sorty(column, options = {})
4
+ options = { :label => column.to_s.humanize.titleize }.merge(options)
5
+
6
+ query = params.merge({
7
+ column: column,
8
+ direction: (params[:direction].eql?('asc') ? 'desc' : 'asc')
9
+ })
10
+
11
+ if params[:column].eql?(column.to_s)
12
+ options[:label] << (params[:direction].eql?('asc') ? ' &uarr;' : ' &darr;')
13
+ end
14
+
15
+ link_to(raw(options[:label]), query)
16
+ end
17
+ end
18
+ end
data/lib/sorty/sort.rb ADDED
@@ -0,0 +1,25 @@
1
+ module Sorty
2
+ module Sort
3
+ extend ActiveSupport::Concern
4
+
5
+ module ClassMethods
6
+ def sorty(params)
7
+ return scoped if params[:column].blank?
8
+
9
+ column = params[:column].dup
10
+ direction = params[:direction] || 'asc'
11
+ relation = column[/^(.*)\./, 1]
12
+
13
+ if relation && self.reflections.has_key?(relation.to_sym)
14
+ column.gsub!(/#{relation}\./, "#{self.reflections[relation.to_sym].klass.table_name}.")
15
+ query = includes(relation.to_sym)
16
+ else
17
+ column = "#{table_name}.#{column}"
18
+ query = scoped
19
+ end
20
+
21
+ query.order("#{column} #{direction}")
22
+ end
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,3 @@
1
+ module Sorty
2
+ VERSION = "0.0.1"
3
+ end
data/sorty.gemspec ADDED
@@ -0,0 +1,24 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "sorty/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "sorty"
7
+ s.version = Sorty::VERSION
8
+ s.authors = ["Jim Neath"]
9
+ s.email = ["jimneath@googlemail.com"]
10
+ s.homepage = ""
11
+ s.summary = %q{Sorting tables}
12
+ s.description = %q{Sorting tables}
13
+
14
+ s.rubyforge_project = "sorty"
15
+
16
+ s.files = `git ls-files`.split("\n")
17
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
18
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
19
+ s.require_paths = ["lib"]
20
+
21
+ # specify any dependencies here; for example:
22
+ # s.add_development_dependency "rspec"
23
+ # s.add_runtime_dependency "rest-client"
24
+ end
metadata ADDED
@@ -0,0 +1,73 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: sorty
3
+ version: !ruby/object:Gem::Version
4
+ hash: 29
5
+ prerelease:
6
+ segments:
7
+ - 0
8
+ - 0
9
+ - 1
10
+ version: 0.0.1
11
+ platform: ruby
12
+ authors:
13
+ - Jim Neath
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2012-03-21 00:00:00 Z
19
+ dependencies: []
20
+
21
+ description: Sorting tables
22
+ email:
23
+ - jimneath@googlemail.com
24
+ executables: []
25
+
26
+ extensions: []
27
+
28
+ extra_rdoc_files: []
29
+
30
+ files:
31
+ - .gitignore
32
+ - Gemfile
33
+ - Rakefile
34
+ - lib/sorty.rb
35
+ - lib/sorty/helpers.rb
36
+ - lib/sorty/sort.rb
37
+ - lib/sorty/version.rb
38
+ - sorty.gemspec
39
+ homepage: ""
40
+ licenses: []
41
+
42
+ post_install_message:
43
+ rdoc_options: []
44
+
45
+ require_paths:
46
+ - lib
47
+ required_ruby_version: !ruby/object:Gem::Requirement
48
+ none: false
49
+ requirements:
50
+ - - ">="
51
+ - !ruby/object:Gem::Version
52
+ hash: 3
53
+ segments:
54
+ - 0
55
+ version: "0"
56
+ required_rubygems_version: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ hash: 3
62
+ segments:
63
+ - 0
64
+ version: "0"
65
+ requirements: []
66
+
67
+ rubyforge_project: sorty
68
+ rubygems_version: 1.8.17
69
+ signing_key:
70
+ specification_version: 3
71
+ summary: Sorting tables
72
+ test_files: []
73
+