sorting_table_for 0.1.0 → 0.1.2
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGELOG.mdown +14 -0
- data/README.mdown +11 -4
- data/TODO +4 -0
- data/lib/model/sorting_table_model_scope.rb +24 -5
- data/lib/sorting_table_for.rb +18 -0
- data/lib/sorting_table_for/i18n.rb +5 -1
- data/lib/sorting_table_for/table_builder.rb +365 -24
- data/spec/helpers/header_spec.rb +2 -2
- metadata +9 -10
data/CHANGELOG.mdown
ADDED
data/README.mdown
CHANGED
@@ -12,11 +12,18 @@ SortingTableFor is a Rails TableBuilder made to easily create table or sort a ta
|
|
12
12
|
|
13
13
|
## Installation
|
14
14
|
|
15
|
-
|
15
|
+
In Rails 3, add this to your Gemfile.
|
16
|
+
|
17
|
+
gem "sorting_table_for"
|
18
|
+
|
19
|
+
In Rails 2, add this to your environment.rb file.
|
20
|
+
|
21
|
+
config.gem "sorting_table_for"
|
22
|
+
|
23
|
+
Alternatively, you can install it as a plugin.
|
24
|
+
|
25
|
+
rails plugin install git://github.com/arkownz/sorting_table_for.git
|
16
26
|
|
17
|
-
cd your_rails_app
|
18
|
-
cd vendor/plugins
|
19
|
-
git clone git://github.com/arkownz/sorting_table_for.git
|
20
27
|
|
21
28
|
## Usage
|
22
29
|
|
data/TODO
ADDED
@@ -2,29 +2,46 @@
|
|
2
2
|
|
3
3
|
module SortingTableModelScope
|
4
4
|
|
5
|
+
# Include the methods in models
|
5
6
|
def self.included(base)
|
6
7
|
base.extend(SingletonMethods)
|
7
8
|
end
|
8
9
|
|
9
10
|
module SingletonMethods
|
10
|
-
|
11
|
+
|
12
|
+
# Return a scope of the object with an order
|
13
|
+
#
|
14
|
+
# === Usage
|
15
|
+
#
|
16
|
+
# sorting_table(the params) - Sorting by the given parameters
|
17
|
+
# sorting_table(the params, column name) - Sort by the column name with direction ascending, if no parameters
|
18
|
+
# sorting_table(the params, column name, direction) - Sort by the column name with the given direction, if no parameters
|
19
|
+
#
|
20
|
+
# === Exemples
|
21
|
+
#
|
22
|
+
# User.sorting_table(params)
|
23
|
+
# User.sorting_table(params, :username)
|
24
|
+
# User.sorting_table(params, :username, :desc)
|
25
|
+
#
|
11
26
|
def sorting_table(*args)
|
12
27
|
raise ArgumentError, 'sorting_table: Too many arguments (max : 3)' if args.size > 3
|
13
28
|
sort_table_param = get_sorting_table_params(args)
|
14
29
|
return scoped({}) if !sort_table_param and args.size == 1
|
15
30
|
sort, direction = get_sort_and_direction(sort_table_param, args)
|
16
31
|
return scoped({}) if !sort or !valid_column?(sort) or !valid_direction?(direction)
|
17
|
-
|
32
|
+
scoped({ :order => "#{sort} #{direction}" })
|
18
33
|
end
|
19
34
|
|
20
35
|
private
|
21
|
-
|
36
|
+
|
37
|
+
# Return the params for sorting table
|
22
38
|
def get_sorting_table_params(args)
|
23
|
-
return nil unless args.first.is_a? Hash
|
39
|
+
return nil unless args.first.is_a? Hash
|
24
40
|
return nil unless args.first.has_key? SortingTableFor::TableBuilder.params_sort_table.to_s
|
25
41
|
args.first[SortingTableFor::TableBuilder.params_sort_table.to_s]
|
26
42
|
end
|
27
|
-
|
43
|
+
|
44
|
+
# Parse the params and return the column name and the direction
|
28
45
|
def get_sort_and_direction(sort_table_param, args)
|
29
46
|
if sort_table_param
|
30
47
|
key = sort_table_param.keys.first rescue nil
|
@@ -37,10 +54,12 @@ module SortingTableModelScope
|
|
37
54
|
return args[1], args[2]
|
38
55
|
end
|
39
56
|
|
57
|
+
# Return true if the column name exist
|
40
58
|
def valid_column?(column)
|
41
59
|
column_names.include? column.to_s.downcase
|
42
60
|
end
|
43
61
|
|
62
|
+
# Return true if the direction exist
|
44
63
|
def valid_direction?(direction)
|
45
64
|
%[asc desc].include? direction.to_s.downcase
|
46
65
|
end
|
data/lib/sorting_table_for.rb
CHANGED
@@ -6,6 +6,24 @@ require 'sorting_table_for/tools'
|
|
6
6
|
|
7
7
|
module SortingTableFor
|
8
8
|
|
9
|
+
# Method sorting_table_for with builder => SortingTableFor::TableBuilder
|
10
|
+
#
|
11
|
+
# # Exemples :
|
12
|
+
# <% sorting_table_for @users do |table| %>
|
13
|
+
# <% end %>
|
14
|
+
#
|
15
|
+
# <% sorting_table_for @users do |table| %>
|
16
|
+
# <%= table.headers %>
|
17
|
+
# <%= table.columns %>
|
18
|
+
# <% end %>
|
19
|
+
#
|
20
|
+
# === Options
|
21
|
+
#
|
22
|
+
# * :builder - Set a table builder (default: SortingTableFor::TableBuilder)
|
23
|
+
# * :html - Set html options (id, class, ...)
|
24
|
+
# * :remote_link - To set actions link with ajax (true or false)
|
25
|
+
# * :remote_sort - To set link for sorting with ajax (true of false)
|
26
|
+
#
|
9
27
|
def sorting_table_for(object_or_array, *args)
|
10
28
|
raise ArgumentError, 'Missing block' unless block_given?
|
11
29
|
options = args.extract_options!
|
@@ -4,10 +4,13 @@ module SortingTableFor
|
|
4
4
|
module I18n
|
5
5
|
class << self
|
6
6
|
|
7
|
+
# Set options to create a default scope
|
7
8
|
def set_options(params, model_name, namespace)
|
8
9
|
@params, @model_name, @namespace = params, model_name, namespace
|
9
10
|
end
|
10
11
|
|
12
|
+
# Translate
|
13
|
+
# Add a default scope if option scope isn't defined
|
11
14
|
def translate(attribute, options = {}, action_header = false)
|
12
15
|
if !options.has_key? :scope
|
13
16
|
options[:scope] = create_scope
|
@@ -18,7 +21,8 @@ module SortingTableFor
|
|
18
21
|
alias :t :translate
|
19
22
|
|
20
23
|
private
|
21
|
-
|
24
|
+
|
25
|
+
# Return an array with the scope based on option: SortingTableFor::TableBuilder.i18n_default_scope
|
22
26
|
def create_scope
|
23
27
|
return TableBuilder.i18n_default_scope.collect do |scope_value|
|
24
28
|
case scope_value.to_sym
|
@@ -33,8 +33,101 @@ module SortingTableFor
|
|
33
33
|
@lines = []
|
34
34
|
end
|
35
35
|
|
36
|
-
|
37
|
-
|
36
|
+
# Create a header with the name of the columns (th) around thead and tr.
|
37
|
+
# It can be called with or without a block, or with a list of columns.
|
38
|
+
# By default it will add the default actions (edit, delete).
|
39
|
+
# Also by default it will add a link to sort to the column.
|
40
|
+
# These two exemples are equivalent:
|
41
|
+
#
|
42
|
+
# # With a block:
|
43
|
+
# <% sorting_table_for @users do |table| %>
|
44
|
+
# <%= table.headers do %>
|
45
|
+
# <%= table.header :username %>
|
46
|
+
# <%= table.header :firstname %>
|
47
|
+
# <% end %>
|
48
|
+
# <% end %>
|
49
|
+
#
|
50
|
+
# # With a list:
|
51
|
+
# <% sorting_table_for @users do |table| %>
|
52
|
+
# <%= table.headers :username, :firstname %>
|
53
|
+
# <% end %>
|
54
|
+
#
|
55
|
+
# # Output:
|
56
|
+
# <table class='sorting_table_for'>
|
57
|
+
# <thead>
|
58
|
+
# <tr>
|
59
|
+
# <th class='cur-sort-not'><a href='/my_link?table_sort[username]=asc'>...</a></th>
|
60
|
+
# <th class='cur-sort-not'><a href='/my_link?table_sort[firstname]=asc'>...</a></th>
|
61
|
+
# <th>Edit</th>
|
62
|
+
# <th>Delete</th>
|
63
|
+
# </tr>
|
64
|
+
# </thead>
|
65
|
+
# </table>
|
66
|
+
#
|
67
|
+
# === Quick Headers
|
68
|
+
#
|
69
|
+
# When called without a block or a list, the headers are rendered for each column in
|
70
|
+
# the model's database table and the default actions (edit, delete), and the links to sort.
|
71
|
+
#
|
72
|
+
# <% sorting_table_for @users do |table| %>
|
73
|
+
# <%= table.headers %>
|
74
|
+
# <% end %>
|
75
|
+
#
|
76
|
+
# === Options
|
77
|
+
#
|
78
|
+
# All options are passed down to the fieldser HTML attribtues (id, class, title, ...) expect
|
79
|
+
# for option sort.
|
80
|
+
#
|
81
|
+
# # Html option:
|
82
|
+
# <% sorting_table_for @users do |table| do %>
|
83
|
+
# <%= table.headers :username, :firstname, :html => { :class => 'my_class', :id => 'my_id' }
|
84
|
+
# <% end %>
|
85
|
+
#
|
86
|
+
# # Sort option:
|
87
|
+
# <% sorting_table_for @users do |table| do %>
|
88
|
+
# <%= table.headers :sort => false %>
|
89
|
+
# <% end %>
|
90
|
+
#
|
91
|
+
# === I18n
|
92
|
+
#
|
93
|
+
# For each column contained in the model's database table, the name is set with the I18n translation.
|
94
|
+
# The translation are scoped by option 'i18n_default_scope' defined in your options.
|
95
|
+
#
|
96
|
+
# # Exemple of I18n options for header:
|
97
|
+
# SortingTableFor::TableBuilder.i18n_default_scope = [:controller, :action]
|
98
|
+
#
|
99
|
+
# # Ouput:
|
100
|
+
# I18n.t(:username, :scope => [:current_controller, :current_action]) => en.current_controller.current_action.username
|
101
|
+
#
|
102
|
+
# === Actions
|
103
|
+
#
|
104
|
+
# The option 'default_actions' contained the actions to add by default to the table. The header's actions
|
105
|
+
# are not sortable. The name of action is set with I18n translation.
|
106
|
+
# It's possible to add a value in the scope for header by option 'i18n_add_header_action_scope'
|
107
|
+
#
|
108
|
+
# # Exemple of default_actions:
|
109
|
+
# SortingTableFor::TableBuilder.default_actions = [:edit]
|
110
|
+
# SortingTableFor::TableBuilder.i18n_add_header_action_scope = :header
|
111
|
+
#
|
112
|
+
# # Ouput:
|
113
|
+
# I18n.t(:edit, :scope => [:current_controller, :current_action, :header]) => en.current_controller.current_action.header.edit
|
114
|
+
#
|
115
|
+
# === Sorting
|
116
|
+
#
|
117
|
+
# The link for sorting is set if the column is contained in the model's database table. the link for sorting
|
118
|
+
# is set with the current url, the builder adds a param 'table_sort'.
|
119
|
+
#
|
120
|
+
# # Exemple for column username:
|
121
|
+
# current url: /my_link
|
122
|
+
# param sort: /my_link?table_sort[username]=asc
|
123
|
+
#
|
124
|
+
# the name of the param is set by option: TableSortingFor::TableBuilder.params_sort_table
|
125
|
+
#
|
126
|
+
# === Values
|
127
|
+
#
|
128
|
+
# the values given to headers could be anything. Only the symbols are set with I18n translation.
|
129
|
+
# If you give other types (string, image, ...) there won't have sorting and I18n translation.
|
130
|
+
#
|
38
131
|
def headers(*args, &block)
|
39
132
|
column_options, html_options = get_column_and_html_options( args.extract_options! )
|
40
133
|
if block_given?
|
@@ -46,6 +139,45 @@ module SortingTableFor
|
|
46
139
|
render_thead
|
47
140
|
end
|
48
141
|
|
142
|
+
# Create a cell of header, to have more control.
|
143
|
+
# It can be called with or without a block.
|
144
|
+
# The three exemples are equivalent:
|
145
|
+
#
|
146
|
+
# # Without block (I18n and sorting)
|
147
|
+
# <% sorting_table_for @users do |table| %>
|
148
|
+
# <%= table.headers do %>
|
149
|
+
# <%= table.header :username %>
|
150
|
+
# <% end %>
|
151
|
+
# <% end %>
|
152
|
+
#
|
153
|
+
# # With string (no I18n and no sorting)
|
154
|
+
# <% sorting_table_for @users do |table| %>
|
155
|
+
# <%= table.headers do %>
|
156
|
+
# <%= table.header 'hello_my_header' %>
|
157
|
+
# <% end %>
|
158
|
+
# <% end %>
|
159
|
+
#
|
160
|
+
# # With a block and image (no I18n and no sorting)
|
161
|
+
# <% sorting_table_for @users do |table| %>
|
162
|
+
# <%= table.headers do %>
|
163
|
+
# <%= table.header do %>
|
164
|
+
# <%= image_tag('rails.png') %>
|
165
|
+
# <% end %>
|
166
|
+
# <% end %>
|
167
|
+
# <% end %>
|
168
|
+
#
|
169
|
+
# === Options
|
170
|
+
#
|
171
|
+
# * :sort - true of false to add or not sorting
|
172
|
+
# * :html - Hash options: class, id, ...
|
173
|
+
#
|
174
|
+
# === Values
|
175
|
+
#
|
176
|
+
# the values given to header can be anything. Only the symbols are set with I18n translation.
|
177
|
+
# If you give other types (string, image, ...) there won't have sorting and I18n translation.
|
178
|
+
#
|
179
|
+
# With block there won't have sorting and translation.
|
180
|
+
#
|
49
181
|
def header(*args, &block)
|
50
182
|
if block_given?
|
51
183
|
block = @@template.capture(&block)
|
@@ -56,8 +188,137 @@ module SortingTableFor
|
|
56
188
|
nil
|
57
189
|
end
|
58
190
|
|
59
|
-
|
60
|
-
|
191
|
+
# Create the content of table with the values of collection (td) around tbody and tr.
|
192
|
+
# It can be called with or without a block, or with a list of columns.
|
193
|
+
# By default it will add the default actions (edit, delete).
|
194
|
+
# The method columns return the current object of the collection.
|
195
|
+
# These three exemples are equivalent:
|
196
|
+
#
|
197
|
+
# # With a list:
|
198
|
+
# <% sorting_table_for @users do |table| %>
|
199
|
+
# <%= table.columns :username, :firstname %>
|
200
|
+
# <% end %>
|
201
|
+
#
|
202
|
+
# # With a block:
|
203
|
+
# <% sorting_table_for @users do |table| %>
|
204
|
+
# <%= table.columns do %>
|
205
|
+
# <%= table.column :username %>
|
206
|
+
# <%= table.column :firstname %>
|
207
|
+
# <% end %>
|
208
|
+
# <% end %>
|
209
|
+
#
|
210
|
+
# # With a block and current object:
|
211
|
+
# <% sorting_table_for @users do |table| %>
|
212
|
+
# <%= table.columns do |user| %>
|
213
|
+
# <%= table.column user.username %>
|
214
|
+
# <%= table.column user.firstname %>
|
215
|
+
# <% end %>
|
216
|
+
# <% end %>
|
217
|
+
#
|
218
|
+
# # Output:
|
219
|
+
# <table class='sorting_table_for'>
|
220
|
+
# <tbody>
|
221
|
+
# <tr>
|
222
|
+
# <td>...</td>
|
223
|
+
# <td>...</td>
|
224
|
+
# <td><a href='/users/1/edit'>Edit<a></td>
|
225
|
+
# <td><a href='/users/1'>Delete</a></td>
|
226
|
+
# </tr>
|
227
|
+
# ...
|
228
|
+
# </tbody>
|
229
|
+
# </table>
|
230
|
+
#
|
231
|
+
# === Quick Columns
|
232
|
+
#
|
233
|
+
# When called without a block or a list, the columns are rendered for each column in
|
234
|
+
# the model's database table and the default actions (edit, delete).
|
235
|
+
#
|
236
|
+
# <% sorting_table_for @users do |table| %>
|
237
|
+
# <%= table.columns %>
|
238
|
+
# <% end %>
|
239
|
+
#
|
240
|
+
# === Options
|
241
|
+
#
|
242
|
+
# * :html - Hash options: class, id, ...
|
243
|
+
# * :actions - Set actions to render
|
244
|
+
# * :only - Columns only to render
|
245
|
+
# * :except - Columns to not render
|
246
|
+
#
|
247
|
+
# # Exemples:
|
248
|
+
# <% sorting_table_for @users do |table| %>
|
249
|
+
# <%= table.columns :username, :actions => [:edit, :delete] %>
|
250
|
+
# <% end %>
|
251
|
+
#
|
252
|
+
# <% sorting_table for @users do |table| %>
|
253
|
+
# <%= table.columns :except => [:created_at, :updated_at] %>
|
254
|
+
# <% end %>
|
255
|
+
#
|
256
|
+
# === I18n
|
257
|
+
#
|
258
|
+
# For each action, the name is set with the I18n translation.
|
259
|
+
#
|
260
|
+
# # Exemple of i18n_default_scope:
|
261
|
+
# SortingTableFor::TableBuilder.i18n_default_scope = [:controller, :action]
|
262
|
+
#
|
263
|
+
# # Ouput:
|
264
|
+
# I18n.t(:edit, :scope => [:current_controller, :current_action]) => en.current_controller.current_action.edit
|
265
|
+
#
|
266
|
+
# === Actions
|
267
|
+
#
|
268
|
+
# The option 'default_actions' contains the actions to add by default to the table. The link to actions
|
269
|
+
# is set by the collection sent to the method sorting_table_for.
|
270
|
+
# The name of action is set with I18n translation.
|
271
|
+
#
|
272
|
+
# # Exemple of default_actions:
|
273
|
+
# SortingTableFor::TableBuilder.default_actions = [:edit]
|
274
|
+
#
|
275
|
+
# # Ouput:
|
276
|
+
# link_to( I18n.t(:edit, :scope => [:current_controller, :current_action]), [:edit, user]) => /users/1/edit
|
277
|
+
#
|
278
|
+
# For action :delete the builder set a confirm message with I18n translation.
|
279
|
+
#
|
280
|
+
# # the I18n confirm message:
|
281
|
+
# I18n.t(:confirm_delete, :scope => [:current_controller, :current_action])
|
282
|
+
#
|
283
|
+
# === Total entries
|
284
|
+
#
|
285
|
+
# The first tr of the columns are the number of results in the collection. The line appears if
|
286
|
+
# option 'show_total_entries' is set to true.
|
287
|
+
#
|
288
|
+
# # Exemple of option:
|
289
|
+
# SortingTableFor::TableBuilder.show_total_entries = true
|
290
|
+
#
|
291
|
+
# # Exemple:
|
292
|
+
# <% sorting_table_for @users do |table| %>
|
293
|
+
# <%= table.columns :username, :firstname %>
|
294
|
+
# <% end %>
|
295
|
+
#
|
296
|
+
# # Ouput:
|
297
|
+
# <table class='sorting_table_for'>
|
298
|
+
# <tbody>
|
299
|
+
# <tr>
|
300
|
+
# <td colspan='2' class='total-entries'>Total Entries: 42</td>
|
301
|
+
# </tr>
|
302
|
+
# <tr>
|
303
|
+
# <td>...</td>
|
304
|
+
# <td>...</td>
|
305
|
+
# </tr>
|
306
|
+
# ...
|
307
|
+
# </tbody>
|
308
|
+
# </table>
|
309
|
+
#
|
310
|
+
# A colspan is added by default with the number of columns.
|
311
|
+
# Total entries are compatible with the plugin 'will_paginate'.
|
312
|
+
# The total entries text if defined by I18n translation.
|
313
|
+
#
|
314
|
+
# # I18n translation:
|
315
|
+
# I18n.t(:total_entries, :scope => :sorting_table_for, :value => total_entries)
|
316
|
+
#
|
317
|
+
# === Values
|
318
|
+
#
|
319
|
+
# the values given to columns can be anything. Only the symbols are the values of the collection.
|
320
|
+
# If you give other types (string, image, ...) there won't be interpreted.
|
321
|
+
#
|
61
322
|
def columns(*args, &block)
|
62
323
|
column_options, html_options = get_column_and_html_options( args.extract_options! )
|
63
324
|
@collection.each do |object|
|
@@ -71,7 +332,58 @@ module SortingTableFor
|
|
71
332
|
end
|
72
333
|
render_tbody
|
73
334
|
end
|
74
|
-
|
335
|
+
|
336
|
+
# Create a cell of column, to have more control.
|
337
|
+
# It can be called with or without a block.
|
338
|
+
# The three exemples are equivalent:
|
339
|
+
#
|
340
|
+
# # Without block
|
341
|
+
# <% sorting_table_for @users do |table| %>
|
342
|
+
# <%= table.columns do %>
|
343
|
+
# <%= table.column :username %>
|
344
|
+
# <% end %>
|
345
|
+
# <% end %>
|
346
|
+
#
|
347
|
+
# # With current object
|
348
|
+
# <% sorting_table_for @users do |table| %>
|
349
|
+
# <%= table.columns do |user| %>
|
350
|
+
# <%= table.column user.username %>
|
351
|
+
# <% end %>
|
352
|
+
# <% end %>
|
353
|
+
#
|
354
|
+
# # With a block and current object
|
355
|
+
# <% sorting_table_for @users do |table| %>
|
356
|
+
# <%= table.columns do |user| %>
|
357
|
+
# <%= table.column do %>
|
358
|
+
# <%= user.username %>
|
359
|
+
# <% end %>
|
360
|
+
# <% end %>
|
361
|
+
# <% end %>
|
362
|
+
#
|
363
|
+
# === Options
|
364
|
+
#
|
365
|
+
# * :html - Hash options: class, id, ...
|
366
|
+
# * :as - Force to render a type (:date, :time, :currency)
|
367
|
+
# * :format - Set the I18n localization format for :date or :time (:default, :short, ...)
|
368
|
+
# * :action - Set an action
|
369
|
+
#
|
370
|
+
# # Exemple:
|
371
|
+
# <% sorting_table_for @users do |table| %>
|
372
|
+
# <%= table.colums do |user| %>
|
373
|
+
# <%= table.column :username, :html => { :class => 'my_class', :id => 'my_id' }
|
374
|
+
# <%= table.column user.username, :action => :show %>
|
375
|
+
# <%= table.column user.created_at, :as => :date, :format => :short %>
|
376
|
+
# <%= table.column :action => :edit %>
|
377
|
+
# <% end %>
|
378
|
+
# <% end %>
|
379
|
+
#
|
380
|
+
# === Values
|
381
|
+
#
|
382
|
+
# the values given to column can be anything. Only the symbols are set with I18n translation.
|
383
|
+
# If you give other types (string, image, ...) there won't be interpreted.
|
384
|
+
#
|
385
|
+
# With block the value won't be interpreted.
|
386
|
+
#
|
75
387
|
def column(*args, &block)
|
76
388
|
if block_given?
|
77
389
|
block = @@template.capture(&block)
|
@@ -84,20 +396,22 @@ module SortingTableFor
|
|
84
396
|
|
85
397
|
protected
|
86
398
|
|
87
|
-
#
|
399
|
+
# Return the name of the model
|
88
400
|
def model_name(object)
|
89
401
|
object.present? ? object.class.name : object.to_s.classify
|
90
402
|
end
|
91
403
|
|
92
404
|
private
|
93
|
-
|
405
|
+
|
406
|
+
# Return the balise thead and its content
|
94
407
|
def render_thead
|
95
408
|
if @header_line
|
96
409
|
return Tools::html_safe(content_tag(:thead, @header_line.render_line))
|
97
410
|
end
|
98
411
|
''
|
99
412
|
end
|
100
|
-
|
413
|
+
|
414
|
+
# Return the balise tbody and its content
|
101
415
|
def render_tbody
|
102
416
|
if @lines and @lines.size > 0
|
103
417
|
return Tools::html_safe(content_tag(:tbody, render_total_entries + Tools::html_safe(@lines.collect { |line| line.render_line }.join)))
|
@@ -105,10 +419,14 @@ module SortingTableFor
|
|
105
419
|
''
|
106
420
|
end
|
107
421
|
|
422
|
+
# Set default global options
|
423
|
+
# Set sort to true if the value isn't defined in options
|
108
424
|
def set_default_global_options
|
109
|
-
@@options[:sort] = true
|
425
|
+
@@options[:sort] = true if !defined?(@@options[:sort]) or !@@options.has_key? :sort
|
110
426
|
end
|
111
427
|
|
428
|
+
# Calculate the total entries
|
429
|
+
# Return a tr and td with a colspan of total entries
|
112
430
|
def render_total_entries
|
113
431
|
if self.show_total_entries
|
114
432
|
total_entries = @collection.total_entries rescue @collection.size
|
@@ -117,18 +435,13 @@ module SortingTableFor
|
|
117
435
|
''
|
118
436
|
end
|
119
437
|
|
438
|
+
# Return options and html options
|
120
439
|
def get_column_and_html_options(options)
|
121
440
|
return options, options.delete(:html) || {}
|
122
441
|
end
|
123
442
|
|
124
443
|
end
|
125
444
|
|
126
|
-
##
|
127
|
-
## Format Lines
|
128
|
-
##
|
129
|
-
##
|
130
|
-
##
|
131
|
-
|
132
445
|
class FormatLine < TableBuilder
|
133
446
|
|
134
447
|
def initialize(args, column_options = {}, html_options = {}, object = nil, type = nil)
|
@@ -140,10 +453,13 @@ module SortingTableFor
|
|
140
453
|
end
|
141
454
|
end
|
142
455
|
|
143
|
-
|
456
|
+
# Create a new cell with the class FormatCell
|
457
|
+
# Add the object in @cells
|
458
|
+
def add_cell(object, args, type = nil, block = nil)
|
144
459
|
@cells << FormatCell.new(object, args, type, block)
|
145
460
|
end
|
146
461
|
|
462
|
+
# Return a tr line based on the type (:thead or :tbody)
|
147
463
|
def render_line
|
148
464
|
if @type == :thead
|
149
465
|
header = content_tag(:tr, Tools::html_safe(@cells.collect { |cell| cell.render_cell_thead }.join), @html_options)
|
@@ -152,17 +468,19 @@ module SortingTableFor
|
|
152
468
|
end
|
153
469
|
end
|
154
470
|
|
471
|
+
# Return a string with the total of cells
|
155
472
|
def total_cells
|
156
473
|
@cells.size.to_s
|
157
474
|
end
|
158
475
|
|
159
476
|
protected
|
160
477
|
|
161
|
-
#
|
478
|
+
# Return each column in the model's database table
|
162
479
|
def content_columns
|
163
480
|
model_name(@object).constantize.content_columns.collect { |c| c.name.to_sym }.compact rescue []
|
164
481
|
end
|
165
482
|
|
483
|
+
# Return true if the column is in the model's database table
|
166
484
|
def model_have_column?(column)
|
167
485
|
model_name(@object).constantize.content_columns.each do |model_column|
|
168
486
|
return true if model_column.name == column.to_s
|
@@ -170,12 +488,16 @@ module SortingTableFor
|
|
170
488
|
false
|
171
489
|
end
|
172
490
|
|
491
|
+
# Return true if the column is in the model's database table
|
173
492
|
def can_sort_column?(column)
|
174
493
|
model_have_column?(column)
|
175
494
|
end
|
176
495
|
|
177
496
|
private
|
178
497
|
|
498
|
+
# Call after headers or columns with no attributes (table.headers)
|
499
|
+
# Create all the cells based on each column in the model's database table
|
500
|
+
# Create cell's actions based on option default_actions or on actions given (:actions => [:edit])
|
179
501
|
def create_cells
|
180
502
|
@attributes.each { |ask| add_cell(@object, ask) }
|
181
503
|
if @args.empty?
|
@@ -185,6 +507,7 @@ module SortingTableFor
|
|
185
507
|
end
|
186
508
|
end
|
187
509
|
|
510
|
+
# Return an Array of all actions given to headers or columns (:actions => [:edit, :delete])
|
188
511
|
def get_column_actions
|
189
512
|
if @column_options.has_key? :actions
|
190
513
|
if @column_options[:actions].is_a?(Array)
|
@@ -196,6 +519,7 @@ module SortingTableFor
|
|
196
519
|
[]
|
197
520
|
end
|
198
521
|
|
522
|
+
# Return an Array of the columns based on options :only or :except
|
199
523
|
def get_columns
|
200
524
|
if @column_options.has_key? :only
|
201
525
|
return @column_options[:only] if @column_options[:only].is_a?(Array)
|
@@ -210,12 +534,6 @@ module SortingTableFor
|
|
210
534
|
|
211
535
|
end
|
212
536
|
|
213
|
-
##
|
214
|
-
## Format Cells
|
215
|
-
##
|
216
|
-
##
|
217
|
-
##
|
218
|
-
|
219
537
|
class FormatCell < FormatLine
|
220
538
|
|
221
539
|
def initialize(object, args, type = nil, block = nil)
|
@@ -234,6 +552,7 @@ module SortingTableFor
|
|
234
552
|
@can_sort = true if @options and @options[:sort] and can_sort_column?(@ask)
|
235
553
|
end
|
236
554
|
|
555
|
+
# Return a td with the formated value or action for columns
|
237
556
|
def render_cell_tbody
|
238
557
|
if @type == :action
|
239
558
|
cell_value = action_link_to(@ask)
|
@@ -246,6 +565,7 @@ module SortingTableFor
|
|
246
565
|
content_tag(:td, cell_value, @html_options)
|
247
566
|
end
|
248
567
|
|
568
|
+
# Return a td with the formated value or action for headers
|
249
569
|
def render_cell_thead
|
250
570
|
if @ask
|
251
571
|
cell_value = (@ask.is_a?(Symbol)) ? I18n.t(@ask, {}, true) : @ask
|
@@ -262,16 +582,23 @@ module SortingTableFor
|
|
262
582
|
|
263
583
|
private
|
264
584
|
|
585
|
+
# Return options and html options for a cell
|
265
586
|
def get_cell_and_html_options(options)
|
266
587
|
return options, options.delete(:html) || {}
|
267
588
|
end
|
268
589
|
|
590
|
+
# Set default options for cell
|
591
|
+
# Set an empty hash if no html options
|
592
|
+
# Set an empty hash if no options
|
593
|
+
# Set sort to true if no options sort
|
269
594
|
def set_default_options
|
270
595
|
@html_options = {} unless defined? @html_options
|
271
596
|
@options = {} unless defined? @options
|
272
|
-
@options[:sort] = @@options[:sort]
|
597
|
+
@options[:sort] = @@options[:sort] if !@options.has_key? :sort
|
273
598
|
end
|
274
599
|
|
600
|
+
# Create the link for actions
|
601
|
+
# Set the I18n translation or the given block for the link's name
|
275
602
|
def action_link_to(action, block = nil)
|
276
603
|
object_or_array = @@object_or_array.clone
|
277
604
|
object_or_array.push @object
|
@@ -286,10 +613,14 @@ module SortingTableFor
|
|
286
613
|
end
|
287
614
|
end
|
288
615
|
|
616
|
+
# Create sorting link
|
289
617
|
def sort_link_to(name)
|
290
618
|
create_link_to(name, sort_url, @@options[:sort_remote])
|
291
619
|
end
|
292
620
|
|
621
|
+
# Create the link based on object
|
622
|
+
# Set an ajax link if option link_remote is set to true
|
623
|
+
# Compatible with rails 2 and 3.
|
293
624
|
def create_link_to(block, url, remote, method = nil, confirm = nil)
|
294
625
|
if remote and Tools::rails3?
|
295
626
|
return @@template.link_to(block, url, :method => method, :confirm => confirm, :remote => true)
|
@@ -300,12 +631,17 @@ module SortingTableFor
|
|
300
631
|
@@template.link_to(block, url, :method => method, :confirm => confirm)
|
301
632
|
end
|
302
633
|
|
634
|
+
# Return a string with html class of sorting for headers
|
635
|
+
# The html class is based on option: SortingTableFor::TableBuilder.html_sorting_class
|
303
636
|
def sorting_html_class
|
304
637
|
return TableBuilder.html_sorting_class.first if current_sorting.nil?
|
305
638
|
return TableBuilder.html_sorting_class.second if current_sorting == :asc
|
306
639
|
TableBuilder.html_sorting_class.third
|
307
640
|
end
|
308
641
|
|
642
|
+
# Return an url for sorting
|
643
|
+
# Add the param sorting_table[name]=direction to the url
|
644
|
+
# Add the default direction: :asc
|
309
645
|
def sort_url
|
310
646
|
url_params = @@params.clone
|
311
647
|
if url_params.has_key? TableBuilder.params_sort_table
|
@@ -319,6 +655,7 @@ module SortingTableFor
|
|
319
655
|
@@template.url_for(url_params)
|
320
656
|
end
|
321
657
|
|
658
|
+
# Return a symbol of the current sorting (:asc, :desc, nil)
|
322
659
|
def current_sorting
|
323
660
|
if @@params.has_key? TableBuilder.params_sort_table and @@params[TableBuilder.params_sort_table].has_key? @ask
|
324
661
|
return @@params[TableBuilder.params_sort_table][@ask].to_sym
|
@@ -326,12 +663,14 @@ module SortingTableFor
|
|
326
663
|
nil
|
327
664
|
end
|
328
665
|
|
666
|
+
# Return a symbol, the inverse of the current sorting
|
329
667
|
def inverse_sorting
|
330
668
|
return :asc if current_sorting.nil?
|
331
669
|
return :desc if current_sorting == :asc
|
332
670
|
:asc
|
333
671
|
end
|
334
672
|
|
673
|
+
# Return the formated cell's value
|
335
674
|
def format_cell_value(value, attribute = nil)
|
336
675
|
unless (ret_value = format_cell_value_as_ask(value)).nil?
|
337
676
|
return ret_value
|
@@ -339,6 +678,7 @@ module SortingTableFor
|
|
339
678
|
format_cell_value_as_type(value, attribute)
|
340
679
|
end
|
341
680
|
|
681
|
+
# Format the value if option :as is set
|
342
682
|
def format_cell_value_as_ask(value)
|
343
683
|
return nil if !@options or @options.empty? or !@options.has_key?(:as)
|
344
684
|
return case @options[:as]
|
@@ -349,6 +689,7 @@ module SortingTableFor
|
|
349
689
|
end
|
350
690
|
end
|
351
691
|
|
692
|
+
# Format the value based on value's type
|
352
693
|
def format_cell_value_as_type(value, attribute)
|
353
694
|
if value.is_a?(Time) or value.is_a?(Date)
|
354
695
|
return ::I18n.l(value, :format => @options[:format] || TableBuilder.i18n_default_format_date)
|
data/spec/helpers/header_spec.rb
CHANGED
@@ -26,7 +26,7 @@ describe SortingTableFor, :type => :helper do
|
|
26
26
|
html.should have_comp_tag("th", :count => 11)
|
27
27
|
end
|
28
28
|
end
|
29
|
-
|
29
|
+
|
30
30
|
it "should add sorting class on th" do
|
31
31
|
helper.sorting_table_for(@users) do |table|
|
32
32
|
table.headers.should have_comp_tag("th[class='cur-sort-not']", :count => 9)
|
@@ -181,7 +181,7 @@ describe SortingTableFor, :type => :helper do
|
|
181
181
|
table.header :price
|
182
182
|
end
|
183
183
|
html.should_not have_comp_tag("th[class=cur-sort-not]")
|
184
|
-
end
|
184
|
+
end
|
185
185
|
end
|
186
186
|
|
187
187
|
it "should not sort on select columns" do
|
metadata
CHANGED
@@ -1,13 +1,12 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: sorting_table_for
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash: 27
|
5
4
|
prerelease: false
|
6
5
|
segments:
|
7
6
|
- 0
|
8
7
|
- 1
|
9
|
-
-
|
10
|
-
version: 0.1.
|
8
|
+
- 2
|
9
|
+
version: 0.1.2
|
11
10
|
platform: ruby
|
12
11
|
authors:
|
13
12
|
- Thomas Floch
|
@@ -15,7 +14,7 @@ autorequire:
|
|
15
14
|
bindir: bin
|
16
15
|
cert_chain: []
|
17
16
|
|
18
|
-
date: 2010-
|
17
|
+
date: 2010-11-02 00:00:00 +01:00
|
19
18
|
default_executable:
|
20
19
|
dependencies: []
|
21
20
|
|
@@ -25,8 +24,8 @@ executables: []
|
|
25
24
|
|
26
25
|
extensions: []
|
27
26
|
|
28
|
-
extra_rdoc_files:
|
29
|
-
|
27
|
+
extra_rdoc_files:
|
28
|
+
- README.mdown
|
30
29
|
files:
|
31
30
|
- lib/model/sorting_table_model_scope.rb
|
32
31
|
- lib/sorting_table_for/i18n.rb
|
@@ -45,17 +44,19 @@ files:
|
|
45
44
|
- spec/locales/test_rails3.yml
|
46
45
|
- spec/model/sorting_table_model_scope_spec.rb
|
47
46
|
- spec/spec_helper.rb
|
47
|
+
- CHANGELOG.mdown
|
48
48
|
- MIT-LICENSE
|
49
49
|
- Rakefile
|
50
50
|
- README.mdown
|
51
|
+
- TODO
|
51
52
|
- init.rb
|
52
53
|
has_rdoc: true
|
53
54
|
homepage: http://github.com/arkownz/sorting_table_for
|
54
55
|
licenses: []
|
55
56
|
|
56
57
|
post_install_message:
|
57
|
-
rdoc_options:
|
58
|
-
|
58
|
+
rdoc_options:
|
59
|
+
- --charset=UTF-8
|
59
60
|
require_paths:
|
60
61
|
- lib
|
61
62
|
required_ruby_version: !ruby/object:Gem::Requirement
|
@@ -63,7 +64,6 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
63
64
|
requirements:
|
64
65
|
- - ">="
|
65
66
|
- !ruby/object:Gem::Version
|
66
|
-
hash: 3
|
67
67
|
segments:
|
68
68
|
- 0
|
69
69
|
version: "0"
|
@@ -72,7 +72,6 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
72
72
|
requirements:
|
73
73
|
- - ">="
|
74
74
|
- !ruby/object:Gem::Version
|
75
|
-
hash: 19
|
76
75
|
segments:
|
77
76
|
- 1
|
78
77
|
- 3
|