sort_out 0.1.1 → 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 8c6115fbb064045a3b68a41344fe7ab0b31db79b
4
+ data.tar.gz: 6c9f7ce3109c45498739b5187b31534496afd820
5
+ SHA512:
6
+ metadata.gz: 0a6a350f0c2c52f220d1e0af61b22e4f514a54eaeabc3db9c751abe6db37215507b5b8cfc62f718b731a436ab98e276017bfdc045b2a161dd4e182c51f18b3e6
7
+ data.tar.gz: 9aff6ac797738892b05e69142788f2e6c3f306632f9f16d089d83deec31069258b8df9b572fc4f1a52b014d6cd541cbd296a2acd3ff14379c06581c53aa8b20f
data/Gemfile CHANGED
@@ -1,3 +1,5 @@
1
1
  source "http://rubygems.org"
2
2
 
3
3
  gem "rails", "~> 3.0.0"
4
+
5
+
@@ -2,8 +2,21 @@
2
2
 
3
3
  require 'active_record'
4
4
 
5
+ require 'sort_out/action_controller'
5
6
  require 'sort_out/helpers'
6
7
  require 'sort_out/active_record'
7
8
 
8
9
  ActiveRecord::Base.send :extend, SortOut::ActiveRecord
9
- ActionView::Base.send :include, SortOut::Helpers
10
+ ActionController::Base.send :include, SortOut::ActionController
11
+ ActionView::Base.send :include, SortOut::Helpers
12
+
13
+ module SortOut
14
+ mattr_accessor :action_controller_options
15
+ @@action_controller_options = {}
16
+
17
+ mattr_accessor :sort
18
+ @@sort = nil
19
+
20
+ mattr_accessor :direction
21
+ @@direction = nil
22
+ end
@@ -0,0 +1,50 @@
1
+ module SortOut
2
+ module ActionController
3
+ def self.included(base)
4
+
5
+ base.instance_eval do
6
+ extend ClassMethods
7
+ end
8
+ end
9
+
10
+ module ClassMethods
11
+ def store_sortable(options = {})
12
+ SortOut.action_controller_options = options.reverse_merge get: :get_sortable, set: :set_sortable
13
+
14
+ define_method :store_sortable_before_filter do
15
+ if SortOut.sort.present? || SortOut.direction.present?
16
+ send SortOut.action_controller_options[:set], { sort: SortOut.sort, direction: SortOut.direction }
17
+ end
18
+ end
19
+
20
+ before_filter :get_sort_and_direction, only: [ :index ]
21
+ before_filter :store_sortable_before_filter, only: [ :index ]
22
+ end
23
+
24
+ def set_localize_sortable(*actions)
25
+ skip_before_filter :get_sort_and_direction, :store_sortable_before_filter
26
+ before_filter :get_sort_and_direction, only: actions.flatten
27
+ before_filter :store_sortable_before_filter, only: actions.flatten
28
+ end
29
+ end
30
+
31
+ def sortable_params
32
+ get_sortable = if SortOut.action_controller_options.any? and respond_to? SortOut.action_controller_options[:get]
33
+ send SortOut.action_controller_options[:get]
34
+ end
35
+
36
+ (get_sortable || {}).reverse_merge(sort: nil, direction: nil)
37
+ end
38
+
39
+ def get_sort_and_direction
40
+ if params[:sort].present?
41
+ SortOut.sort = params[:sort]
42
+ SortOut.direction = params[:direction]
43
+ else
44
+ SortOut.sort = sortable_params[:sort]
45
+ SortOut.direction = sortable_params[:direction]
46
+ end
47
+ end
48
+ end
49
+ end
50
+
@@ -1,15 +1,22 @@
1
1
  module SortOut
2
2
  module ActiveRecord
3
3
  def sortable(options = {})
4
- default_options = { :default_column => 'name', :after => [], :by => [] }
4
+ default_options = { :default_column => [:name, :asc], :after => [], :by => [] }
5
5
  @sortable_options = default_options.merge(options) unless options.nil?
6
- @sortable_options[:by] << [options[:default_column]]
6
+ @sortable_options[:default_column] = [@sortable_options[:default_column], :asc] unless @sortable_options[:default_column].is_a? Array
7
+ @sortable_options[:by] << @sortable_options[:default_column][0]
7
8
  end
8
9
 
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}"
10
+ def sort_out(column, direction = nil)
11
+ if column.is_a? Hash
12
+ direction = column[:direction]
13
+ column = column[:sort]
14
+ end
15
+
16
+ sort_column = sortable_column(column).to_s.downcase
17
+ sort_column << sortable_direction(column, direction)
18
+
19
+ model = self.order(sort_column)
13
20
  @sortable_options[:after].each do |order|
14
21
  model = model.order(order.join(' ')) if order[0].to_s != sort_column
15
22
  end
@@ -24,7 +31,18 @@ module SortOut
24
31
  return column
25
32
  end
26
33
  end
27
- @sortable_options[:default_column]
34
+ @sortable_options[:default_column][0]
35
+ end
36
+
37
+ def sortable_direction(column, direction)
38
+ if direction.present?
39
+ " desc"
40
+ elsif column.blank?
41
+ " #{@sortable_options[:default_column][1].to_s}"
42
+ else
43
+ ""
44
+ end
28
45
  end
29
46
  end
30
47
  end
48
+
@@ -2,27 +2,53 @@
2
2
  module SortOut
3
3
  module Helpers
4
4
  def sortable(column, title = nil, options = {})
5
- default_options = { :default => false }
6
- options = default_options.merge(options) unless options.nil?
5
+ options.reverse_merge! default: false, direction: true, params: {}
7
6
  title ||= column.titleize
8
7
  direction = nil
9
- if column.to_s == params[:sort] || (options[:default] and params[:sort].blank?)
10
- if params[:direction].blank?
11
- direction = "desc"
12
- title << ''
8
+ if column.to_s == SortOut.sort || (options[:default] and SortOut.sort.blank?)
9
+ if SortOut.direction.blank?
10
+ if options[:default].to_s == 'desc' and SortOut.sort.blank?
11
+ title << '' if options[:direction]
12
+ else
13
+ direction = "desc"
14
+ title << '▼' if options[:direction]
15
+ end
13
16
  else
14
- title << '▲'
17
+ title << '▲' if options[:direction]
15
18
  end
16
19
  end
17
20
  #▴ ▾ ▼ ▲
18
- link_to title, params.merge({:sort => column, :direction => direction})
21
+ link_to title, params.merge({ sort: column, direction: direction }.merge(options[:params]))
19
22
  end
20
23
 
21
24
  def sortable_fields
22
25
  html = "".html_safe
23
- html << hidden_field_tag(:sort, params[:sort]) if params[:sort].present?
24
- html << hidden_field_tag(:direction, params[:direction]) if params[:direction].present?
26
+ html << hidden_field_tag(:sort, SortOut.sort) if SortOut.sort.present?
27
+ html << hidden_field_tag(:direction, SortOut.direction) if SortOut.direction.present?
25
28
  html
26
29
  end
30
+
31
+ def sortable_current(columns, default, options)
32
+ options.reverse_merge! model_name: controller_name.singularize
33
+
34
+ column = if SortOut.sort.present? and columns.include? SortOut.sort.to_sym
35
+ SortOut.sort.to_sym
36
+ else
37
+ default.first
38
+ end
39
+
40
+ title = tl("#{options[:model_name]}.#{column.to_s}")
41
+
42
+ if SortOut.direction.blank?
43
+ if default[1].to_s == 'desc' and SortOut.sort.blank?
44
+ title << '▲'
45
+ else
46
+ title << '▼'
47
+ end
48
+ else
49
+ title << '▲'
50
+ end
51
+ end
27
52
  end
28
53
  end
54
+
@@ -1,3 +1,3 @@
1
1
  module SortOut
2
- VERSION = "0.1.1"
2
+ VERSION = "1.0.0"
3
3
  end
metadata CHANGED
@@ -1,65 +1,53 @@
1
- --- !ruby/object:Gem::Specification
1
+ --- !ruby/object:Gem::Specification
2
2
  name: sort_out
3
- version: !ruby/object:Gem::Version
4
- prerelease:
5
- version: 0.1.1
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
6
5
  platform: ruby
7
- authors:
6
+ authors:
8
7
  - Felipe Diesel
9
8
  autorequire:
10
9
  bindir: bin
11
10
  cert_chain: []
12
-
13
- date: 2011-04-01 00:00:00 -03:00
14
- default_executable:
11
+ date: 2013-11-14 00:00:00.000000000 Z
15
12
  dependencies: []
16
-
17
13
  description: ActiveRecord extension that makes easy to order fields.
18
- email:
14
+ email:
19
15
  - felipediesel@gmail.com
20
16
  executables: []
21
-
22
17
  extensions: []
23
-
24
18
  extra_rdoc_files: []
25
-
26
- files:
19
+ files:
27
20
  - .gitignore
28
21
  - Gemfile
29
22
  - README.markdown
30
23
  - Rakefile
31
24
  - lib/sort_out.rb
25
+ - lib/sort_out/action_controller.rb
32
26
  - lib/sort_out/active_record.rb
33
27
  - lib/sort_out/helpers.rb
34
28
  - lib/sort_out/version.rb
35
29
  - sort_out.gemspec
36
- has_rdoc: true
37
30
  homepage: http://felipediesel.com
38
31
  licenses: []
39
-
32
+ metadata: {}
40
33
  post_install_message:
41
34
  rdoc_options: []
42
-
43
- require_paths:
35
+ require_paths:
44
36
  - lib
45
- required_ruby_version: !ruby/object:Gem::Requirement
46
- none: false
47
- requirements:
48
- - - ">="
49
- - !ruby/object:Gem::Version
50
- version: "0"
51
- required_rubygems_version: !ruby/object:Gem::Requirement
52
- none: false
53
- requirements:
54
- - - ">="
55
- - !ruby/object:Gem::Version
56
- version: "0"
37
+ required_ruby_version: !ruby/object:Gem::Requirement
38
+ requirements:
39
+ - - '>='
40
+ - !ruby/object:Gem::Version
41
+ version: '0'
42
+ required_rubygems_version: !ruby/object:Gem::Requirement
43
+ requirements:
44
+ - - '>='
45
+ - !ruby/object:Gem::Version
46
+ version: '0'
57
47
  requirements: []
58
-
59
48
  rubyforge_project: sort_out
60
- rubygems_version: 1.6.2
49
+ rubygems_version: 2.0.6
61
50
  signing_key:
62
- specification_version: 3
51
+ specification_version: 4
63
52
  summary: ActiveRecord extension that makes easy to order fields.
64
53
  test_files: []
65
-