e9_rails 0.0.7 → 0.0.8
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/e9_rails/controllers/orderable.rb +34 -32
- data/lib/e9_rails/controllers/sortable.rb +46 -0
- data/lib/e9_rails/version.rb +1 -1
- data/lib/e9_rails.rb +1 -0
- metadata +5 -18
@@ -13,42 +13,44 @@ module E9Rails::Controllers
|
|
13
13
|
def ordered_if; params[:action] == 'index' end
|
14
14
|
|
15
15
|
included do
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
16
|
+
if respond_to?(:resource_class)
|
17
|
+
has_scope :ordered_on, :if => :ordered_if, :default => lambda {|c| c.send(:default_ordered_on) } do |controller, scope, val|
|
18
|
+
begin
|
19
|
+
# determine the dir from params or controller default
|
20
|
+
dir = case controller.params[:dir]
|
21
|
+
when /^desc$/i then 'DESC'
|
22
|
+
when /^asc$/i then 'ASC'
|
23
|
+
else controller.send(:default_ordered_dir) rescue ''
|
24
|
+
end
|
24
25
|
|
25
|
-
|
26
|
-
|
27
|
-
|
26
|
+
# split the ordered_param on commas and periods, the idea being that
|
27
|
+
# it can take multiple columns, and on assocation columns
|
28
|
+
val = val.split(',').map {|v| v.split('.') }
|
28
29
|
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
30
|
+
val = val.map {|v|
|
31
|
+
# if val split on '.', try to constantize the parsed class
|
32
|
+
if v.length > 1
|
33
|
+
klass = v.first.classify.constantize rescue nil
|
34
|
+
# and if it succeeds
|
35
|
+
if klass
|
36
|
+
# apply the join to the scope.
|
37
|
+
# NOTE there's no checking whatsoever here as to:
|
38
|
+
# A.) is this class an association?
|
39
|
+
# B.) does this class have the passed column?
|
40
|
+
scope = scope.joins(v.first.underscore.to_sym)
|
41
|
+
"#{klass.table_name}.#{v.last} #{dir}"
|
42
|
+
end
|
43
|
+
else
|
44
|
+
# else we're assuming the column is on the table
|
45
|
+
"#{resource_class.table_name}.#{v.last} #{dir}"
|
41
46
|
end
|
42
|
-
|
43
|
-
# else we're assuming the column is on the table
|
44
|
-
"#{resource_class.table_name}.#{v.last} #{dir}"
|
45
|
-
end
|
46
|
-
}.compact.join(', ')
|
47
|
+
}.compact.join(', ')
|
47
48
|
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
49
|
+
scope.order(val)
|
50
|
+
rescue => e
|
51
|
+
Rails.logger.error("Orderable ordered_on scope : #{e.message}")
|
52
|
+
scope
|
53
|
+
end
|
52
54
|
end
|
53
55
|
end
|
54
56
|
end
|
@@ -0,0 +1,46 @@
|
|
1
|
+
module E9Rails::Controllers
|
2
|
+
#
|
3
|
+
# Module to give a controller "sortable" functionality.
|
4
|
+
#
|
5
|
+
# To implement this there must exist a POST route to the collection url
|
6
|
+
# named update_order which passes an array, "ids", representing the new order, e.g.
|
7
|
+
#
|
8
|
+
# resources :sortable_things do
|
9
|
+
# collection { post :update_order }
|
10
|
+
# end
|
11
|
+
#
|
12
|
+
module Sortable
|
13
|
+
extend ActiveSupport::Concern
|
14
|
+
|
15
|
+
def update_order(options = {}, &block)
|
16
|
+
if params[:ids].is_a?(Array)
|
17
|
+
pos = 0
|
18
|
+
params[:ids].each {|id| pos += 1; _do_position_update(id, :position => pos) }
|
19
|
+
flash[:notice] = I18n.t(:notice, :scope => :"flash.actions.update_order")
|
20
|
+
else
|
21
|
+
flash[:alert] = I18n.t(:alert, :scope => :"flash.actions.update_order")
|
22
|
+
end
|
23
|
+
|
24
|
+
block ||= proc {|format| format.js { head :ok } }
|
25
|
+
|
26
|
+
respond_with(collection, &block)
|
27
|
+
end
|
28
|
+
|
29
|
+
alias :update_order! :update_order
|
30
|
+
|
31
|
+
#
|
32
|
+
# Override _do_position_update in your controller to define a different method to
|
33
|
+
# update a record's position.
|
34
|
+
#
|
35
|
+
# The out-of-the-box implementation assumes two things:
|
36
|
+
#
|
37
|
+
# 1. An InheritedResources controller
|
38
|
+
# 2. A vanilla (position column) acts_as_list install.
|
39
|
+
#
|
40
|
+
def _do_position_update(id, hash_with_position)
|
41
|
+
resource_class.update(id, hash_with_position)
|
42
|
+
end
|
43
|
+
|
44
|
+
private :_do_position_update
|
45
|
+
end
|
46
|
+
end
|
data/lib/e9_rails/version.rb
CHANGED
data/lib/e9_rails.rb
CHANGED
metadata
CHANGED
@@ -1,12 +1,8 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: e9_rails
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
prerelease:
|
5
|
-
|
6
|
-
- 0
|
7
|
-
- 0
|
8
|
-
- 7
|
9
|
-
version: 0.0.7
|
4
|
+
prerelease:
|
5
|
+
version: 0.0.8
|
10
6
|
platform: ruby
|
11
7
|
authors:
|
12
8
|
- Travis Cox
|
@@ -14,7 +10,7 @@ autorequire:
|
|
14
10
|
bindir: bin
|
15
11
|
cert_chain: []
|
16
12
|
|
17
|
-
date: 2011-04-
|
13
|
+
date: 2011-04-18 00:00:00 -04:00
|
18
14
|
default_executable:
|
19
15
|
dependencies:
|
20
16
|
- !ruby/object:Gem::Dependency
|
@@ -25,10 +21,6 @@ dependencies:
|
|
25
21
|
requirements:
|
26
22
|
- - ~>
|
27
23
|
- !ruby/object:Gem::Version
|
28
|
-
segments:
|
29
|
-
- 3
|
30
|
-
- 0
|
31
|
-
- 0
|
32
24
|
version: 3.0.0
|
33
25
|
type: :runtime
|
34
26
|
version_requirements: *id001
|
@@ -40,8 +32,6 @@ dependencies:
|
|
40
32
|
requirements:
|
41
33
|
- - ">="
|
42
34
|
- !ruby/object:Gem::Version
|
43
|
-
segments:
|
44
|
-
- 0
|
45
35
|
version: "0"
|
46
36
|
type: :runtime
|
47
37
|
version_requirements: *id002
|
@@ -65,6 +55,7 @@ files:
|
|
65
55
|
- lib/e9_rails/active_record/inheritable_options.rb
|
66
56
|
- lib/e9_rails/active_record/sti.rb
|
67
57
|
- lib/e9_rails/controllers/orderable.rb
|
58
|
+
- lib/e9_rails/controllers/sortable.rb
|
68
59
|
- lib/e9_rails/helpers/pagination.rb
|
69
60
|
- lib/e9_rails/helpers/resource_error_messages.rb
|
70
61
|
- lib/e9_rails/helpers/title.rb
|
@@ -84,21 +75,17 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
84
75
|
requirements:
|
85
76
|
- - ">="
|
86
77
|
- !ruby/object:Gem::Version
|
87
|
-
segments:
|
88
|
-
- 0
|
89
78
|
version: "0"
|
90
79
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
91
80
|
none: false
|
92
81
|
requirements:
|
93
82
|
- - ">="
|
94
83
|
- !ruby/object:Gem::Version
|
95
|
-
segments:
|
96
|
-
- 0
|
97
84
|
version: "0"
|
98
85
|
requirements: []
|
99
86
|
|
100
87
|
rubyforge_project: e9_rails
|
101
|
-
rubygems_version: 1.
|
88
|
+
rubygems_version: 1.6.2
|
102
89
|
signing_key:
|
103
90
|
specification_version: 3
|
104
91
|
summary: A collection of helpers and extensions used in e9 Rails 3 projects
|