alphabetical_paginate 1.0.2 → 1.0.3

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,72 @@
1
+ module AlphabeticalPaginate
2
+ module ControllerHelpers
3
+ class Relation
4
+ def alpha_paginate current_field, params = {enumerate:false, default_field: "a",
5
+ paginate_all: false, numbers: true,
6
+ others: true, pagination_class: "pagination-centered",
7
+ batch_size: 500, db_mode: false,
8
+ db_field: "id"}
9
+ params[:paginate_all] ||= false
10
+ params[:numbers] = true if !params.has_key? :numbers
11
+ params[:others] = true if !params.has_key? :numbers
12
+ params[:pagination_class] ||= "pagination-centered"
13
+ params[:batch_size] ||= 500
14
+ params[:db_mode] ||= false
15
+ params[:field] ||= "id"
16
+
17
+ if params[:db_mode]
18
+ if !ActiveRecord::Base.connection.adapter_name.downcase.include? "mysql"
19
+ raise "You need a mysql database to ues db_mode with alphabetical_paginate"
20
+ end
21
+ params[:paginate_all] = true
22
+ end
23
+
24
+ output = []
25
+
26
+ if current_field == nil
27
+ current_field = params[:default_field]
28
+ end
29
+
30
+ if params[:db_mode]
31
+ case field_letter
32
+ when /[a-z]/
33
+ output = self.where("%s REGEXP '^%s.*'" % [params[:db_field], current_field])
34
+ when /[0-9]/
35
+ if params[:enumerate]
36
+ output = self.where("%s REGEXP '^%s.*'" % [params[:db_field], current_field])
37
+ else
38
+ output = self.where("%s REGEXP '^[0-9].*'" % [params[:db_field], current_field])
39
+ end
40
+ else
41
+ output = self.where("%s REGEXP '^[a-z0-9].*'" % [params[:db_field], current_field])
42
+ end
43
+ else
44
+ availableLetters = {}
45
+ self.find_each({batch_size: params[:batch_size]}) do |x|
46
+ field_val = block_given? ? yield(x).to_s : x.id.to_s
47
+ field_letter = field_val[0].downcase
48
+ case field_letter
49
+ when /[a-z]/
50
+ availableLetters[field_letter] = true if !availableLetters.has_key? field_letter
51
+ output << x if current_field =~ /[a-z]/ && field_letter == current_field
52
+ when /[0-9]/
53
+ if params[:enumerate]
54
+ availableLetters[field_letter] = true if !availableLetters.has_key? field_letter
55
+ output << x if current_field =~ /[0-9]/ && field_letter == current_field
56
+ else
57
+ availableLetters['0'] = true if !availableLetters.has_key? 'numbers'
58
+ output << x if current_field == "0"
59
+ end
60
+ else
61
+ availableLetters['*'] = true if !availableLetters.has_key? 'other'
62
+ output << x if current_field == "*"
63
+ end
64
+ end
65
+ params[:availableLetters] = availableLetters.collect{|k,v| k.to_s}
66
+ end
67
+ params[:currentField] = current_field
68
+ return output, params
69
+ end
70
+ end
71
+ end
72
+ end
@@ -4,5 +4,8 @@ module AlphabeticalPaginate
4
4
  initializer "alphabetical_paginate.view_helpers" do
5
5
  ActionView::Base.send :include, ViewHelpers
6
6
  end
7
+ initializer "alphabetical_paginate.controller_helpers" do
8
+ ActiveRecord::Relation.send :include, ControllerHelpers
9
+ end
7
10
  end
8
11
  end
@@ -1,3 +1,3 @@
1
1
  module AlphabeticalPaginate
2
- VERSION = "1.0.2"
2
+ VERSION = "1.0.3"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: alphabetical_paginate
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.2
4
+ version: 1.0.3
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-07-20 00:00:00.000000000 Z
12
+ date: 2013-07-22 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: bundler
@@ -90,6 +90,7 @@ files:
90
90
  - alphabetical_paginate.gemspec
91
91
  - lib/alphabetical_paginate.rb
92
92
  - lib/alphabetical_paginate/array.rb
93
+ - lib/alphabetical_paginate/controller.rb
93
94
  - lib/alphabetical_paginate/engine.rb
94
95
  - lib/alphabetical_paginate/railtie.rb
95
96
  - lib/alphabetical_paginate/version.rb