sort_out 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,3 @@
1
+ source "http://rubygems.org"
2
+
3
+ gem "rails", "~> 3.0.0"
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require 'bundler'
2
+ Bundler::GemHelper.install_tasks
@@ -0,0 +1,30 @@
1
+ module SortOut
2
+ module ActiveRecord
3
+ def sortable(options = {})
4
+ default_options = { :default_column => 'name', :after => [], :by => [] }
5
+ @sortable_options = default_options.merge(options) unless options.nil?
6
+ @sortable_options[:by] << [options[:default_column]]
7
+ end
8
+
9
+ def sort_out(column, direction)
10
+ sort_column = sortable_column(column)
11
+ sort_direction = direction.present? ? "desc" : "asc"
12
+ model = self.order "#{sort_column} #{sort_direction}"
13
+ @sortable_options[:after].each do |order|
14
+ model = model.order(order.join(' ')) if order[0].to_s != sort_column
15
+ end
16
+ model
17
+ end
18
+
19
+ def sortable_column(column)
20
+ @sortable_options[:by].each do |field|
21
+ if field.is_a? Array and field[0].to_s == column
22
+ return field[1]
23
+ elsif field.to_s == column
24
+ return column
25
+ end
26
+ end
27
+ @sortable_options[:default_column]
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,27 @@
1
+ module SortOut
2
+ module Helpers
3
+ def sortable(column, title = nil, options = {})
4
+ default_options = { :default => false }
5
+ options = default_options.merge(options) unless options.nil?
6
+ title ||= column.titleize
7
+ direction = nil
8
+ if column.to_s == params[:sort] || (options[:default] and params[:sort].blank?)
9
+ if params[:direction].blank?
10
+ direction = "desc"
11
+ title << '▼'
12
+ else
13
+ title << '▲'
14
+ end
15
+ end
16
+ #▴ ▾ ▼ ▲
17
+ link_to title, params.merge({:sort => column, :direction => direction})
18
+ end
19
+
20
+ def sortable_fields
21
+ html = "".html_safe
22
+ html << hidden_field_tag(:sort, params[:sort]) if params[:sort].present?
23
+ html << hidden_field_tag(:direction, params[:direction]) if params[:direction].present?
24
+ html
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,3 @@
1
+ module SortOut
2
+ VERSION = "0.1"
3
+ end
data/lib/sort_out.rb ADDED
@@ -0,0 +1,7 @@
1
+ require 'active_record'
2
+
3
+ require 'sort_out/helpers'
4
+ require 'sort_out/active_record'
5
+
6
+ ActiveRecord::Base.send :extend, SortOut::ActiveRecord
7
+ ActionView::Base.send :include, SortOut::Helpers
data/sort_out.gemspec ADDED
@@ -0,0 +1,21 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "sort_out/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "sort_out"
7
+ s.version = SortOut::VERSION
8
+ s.platform = Gem::Platform::RUBY
9
+ s.authors = ["Felipe Diesel"]
10
+ s.email = ["felipediesel@gmail.com"]
11
+ s.homepage = "http://felipediesel.com"
12
+ s.summary = %q{ActiveRecord extension that makes easy to order fields.}
13
+ s.description = %q{ActiveRecord extension that makes easy to order fields.}
14
+
15
+ s.rubyforge_project = "sort_out"
16
+
17
+ s.files = `git ls-files`.split("\n")
18
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
19
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
20
+ s.require_paths = ["lib"]
21
+ end
metadata ADDED
@@ -0,0 +1,74 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: sort_out
3
+ version: !ruby/object:Gem::Version
4
+ hash: 9
5
+ prerelease:
6
+ segments:
7
+ - 0
8
+ - 1
9
+ version: "0.1"
10
+ platform: ruby
11
+ authors:
12
+ - Felipe Diesel
13
+ autorequire:
14
+ bindir: bin
15
+ cert_chain: []
16
+
17
+ date: 2011-03-14 00:00:00 -03:00
18
+ default_executable:
19
+ dependencies: []
20
+
21
+ description: ActiveRecord extension that makes easy to order fields.
22
+ email:
23
+ - felipediesel@gmail.com
24
+ executables: []
25
+
26
+ extensions: []
27
+
28
+ extra_rdoc_files: []
29
+
30
+ files:
31
+ - .gitignore
32
+ - Gemfile
33
+ - Rakefile
34
+ - lib/sort_out.rb
35
+ - lib/sort_out/active_record.rb
36
+ - lib/sort_out/helpers.rb
37
+ - lib/sort_out/version.rb
38
+ - sort_out.gemspec
39
+ has_rdoc: true
40
+ homepage: http://felipediesel.com
41
+ licenses: []
42
+
43
+ post_install_message:
44
+ rdoc_options: []
45
+
46
+ require_paths:
47
+ - lib
48
+ required_ruby_version: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ">="
52
+ - !ruby/object:Gem::Version
53
+ hash: 3
54
+ segments:
55
+ - 0
56
+ version: "0"
57
+ required_rubygems_version: !ruby/object:Gem::Requirement
58
+ none: false
59
+ requirements:
60
+ - - ">="
61
+ - !ruby/object:Gem::Version
62
+ hash: 3
63
+ segments:
64
+ - 0
65
+ version: "0"
66
+ requirements: []
67
+
68
+ rubyforge_project: sort_out
69
+ rubygems_version: 1.5.0
70
+ signing_key:
71
+ specification_version: 3
72
+ summary: ActiveRecord extension that makes easy to order fields.
73
+ test_files: []
74
+