e9_rails 0.0.5 → 0.0.7
Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore
CHANGED
@@ -19,11 +19,14 @@ module E9Rails::ActiveRecord
|
|
19
19
|
end
|
20
20
|
|
21
21
|
def options=(hash={})
|
22
|
-
write_attribute(:options, hash)
|
22
|
+
write_attribute(:options, hash.stringify_keys)
|
23
23
|
end
|
24
24
|
|
25
25
|
def options
|
26
|
-
|
26
|
+
opts = read_attribute(:options) || {}
|
27
|
+
opts.reverse_merge! Hash[options_parameters.map(&:to_s).zip([nil])]
|
28
|
+
|
29
|
+
options_class.new(opts, self)
|
27
30
|
end
|
28
31
|
|
29
32
|
class Options < HashWithIndifferentAccess
|
@@ -39,6 +42,13 @@ module E9Rails::ActiveRecord
|
|
39
42
|
def i18n_scope; :activerecord end
|
40
43
|
end
|
41
44
|
|
45
|
+
# This is for active_support, signifying that this class shouldn't be
|
46
|
+
# extracted from *args as options via extract_options. If this is NOT set,
|
47
|
+
# we'll get some odd errors when trying to build the form.
|
48
|
+
def extractable_options?
|
49
|
+
false
|
50
|
+
end
|
51
|
+
|
42
52
|
def initialize(hash, base)
|
43
53
|
merge!(hash)
|
44
54
|
@base = base
|
@@ -0,0 +1,56 @@
|
|
1
|
+
module E9Rails::Controllers
|
2
|
+
# A hackish has_scope mixin to allow ordering by database column.
|
3
|
+
#
|
4
|
+
# Requires has_scope and inherited_resources.
|
5
|
+
#
|
6
|
+
module Orderable
|
7
|
+
extend ActiveSupport::Concern
|
8
|
+
|
9
|
+
def default_ordered_on; 'created_at' end
|
10
|
+
|
11
|
+
def default_ordered_dir; 'DESC' end
|
12
|
+
|
13
|
+
def ordered_if; params[:action] == 'index' end
|
14
|
+
|
15
|
+
included do
|
16
|
+
has_scope :ordered_on, :if => :ordered_if, :default => lambda {|c| c.send(:default_ordered_on) } do |controller, scope, val|
|
17
|
+
begin
|
18
|
+
# determine the dir from params or controller default
|
19
|
+
dir = case controller.params[:dir]
|
20
|
+
when /^desc$/i then 'DESC'
|
21
|
+
when /^asc$/i then 'ASC'
|
22
|
+
else controller.send(:default_ordered_dir) rescue ''
|
23
|
+
end
|
24
|
+
|
25
|
+
# split the ordered_param on commas and periods, the idea being that
|
26
|
+
# it can take multiple columns, and on assocation columns
|
27
|
+
val = val.split(',').map {|v| v.split('.') }
|
28
|
+
|
29
|
+
val = val.map {|v|
|
30
|
+
# if val split on '.', try to constantize the parsed class
|
31
|
+
if v.length > 1
|
32
|
+
klass = v.first.classify.constantize rescue nil
|
33
|
+
# and if it succeeds
|
34
|
+
if klass
|
35
|
+
# apply the join to the scope.
|
36
|
+
# NOTE there's no checking whatsoever here as to:
|
37
|
+
# A.) is this class an association?
|
38
|
+
# B.) does this class have the passed column?
|
39
|
+
scope = scope.joins(v.first.underscore.to_sym)
|
40
|
+
"#{klass.table_name}.#{v.last} #{dir}"
|
41
|
+
end
|
42
|
+
else
|
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
|
+
|
48
|
+
scope.order(val)
|
49
|
+
rescue => e
|
50
|
+
raise e
|
51
|
+
scope
|
52
|
+
end
|
53
|
+
end
|
54
|
+
end
|
55
|
+
end
|
56
|
+
end
|
@@ -23,14 +23,13 @@ module E9Rails::Helpers
|
|
23
23
|
return ''
|
24
24
|
end
|
25
25
|
|
26
|
-
|
27
|
-
html = <<-HTML
|
26
|
+
<<-HTML.html_safe
|
28
27
|
<div id="errorExplanation">
|
29
|
-
<ul
|
28
|
+
<ul>
|
29
|
+
#{ object.errors.map {|attribute, msg| "<li>#{msg}</li>" }.join }
|
30
|
+
</ul>
|
30
31
|
</div>
|
31
32
|
HTML
|
32
|
-
|
33
|
-
html.html_safe
|
34
33
|
rescue => e
|
35
34
|
''
|
36
35
|
end
|
data/lib/e9_rails/version.rb
CHANGED
data/lib/e9_rails.rb
CHANGED
@@ -3,16 +3,20 @@ require 'rails'
|
|
3
3
|
module E9Rails
|
4
4
|
autoload :Version, 'e9_rails/version'
|
5
5
|
|
6
|
+
module ActiveRecord
|
7
|
+
autoload :STI, 'e9_rails/active_record/sti'
|
8
|
+
autoload :AttributeSearchable, 'e9_rails/active_record/attribute_searchable'
|
9
|
+
autoload :InheritableOptions, 'e9_rails/active_record/inheritable_options'
|
10
|
+
end
|
11
|
+
|
12
|
+
module Controllers
|
13
|
+
autoload :Orderable, 'e9_rails/controllers/orderable'
|
14
|
+
end
|
15
|
+
|
6
16
|
module Helpers
|
7
17
|
autoload :Translation, 'e9_rails/helpers/translation'
|
8
18
|
autoload :ResourceErrorMessages, 'e9_rails/helpers/resource_error_messages'
|
9
19
|
autoload :Title, 'e9_rails/helpers/title'
|
10
20
|
autoload :Pagination, 'e9_rails/helpers/pagination'
|
11
21
|
end
|
12
|
-
|
13
|
-
module ActiveRecord
|
14
|
-
autoload :STI, 'e9_rails/active_record/sti'
|
15
|
-
autoload :AttributeSearchable, 'e9_rails/active_record/attribute_searchable'
|
16
|
-
autoload :InheritableOptions, 'e9_rails/active_record/inheritable_options'
|
17
|
-
end
|
18
22
|
end
|
metadata
CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
|
|
5
5
|
segments:
|
6
6
|
- 0
|
7
7
|
- 0
|
8
|
-
-
|
9
|
-
version: 0.0.
|
8
|
+
- 7
|
9
|
+
version: 0.0.7
|
10
10
|
platform: ruby
|
11
11
|
authors:
|
12
12
|
- Travis Cox
|
@@ -14,7 +14,7 @@ autorequire:
|
|
14
14
|
bindir: bin
|
15
15
|
cert_chain: []
|
16
16
|
|
17
|
-
date: 2011-
|
17
|
+
date: 2011-04-15 00:00:00 -04:00
|
18
18
|
default_executable:
|
19
19
|
dependencies:
|
20
20
|
- !ruby/object:Gem::Dependency
|
@@ -64,6 +64,7 @@ files:
|
|
64
64
|
- lib/e9_rails/active_record/attribute_searchable.rb
|
65
65
|
- lib/e9_rails/active_record/inheritable_options.rb
|
66
66
|
- lib/e9_rails/active_record/sti.rb
|
67
|
+
- lib/e9_rails/controllers/orderable.rb
|
67
68
|
- lib/e9_rails/helpers/pagination.rb
|
68
69
|
- lib/e9_rails/helpers/resource_error_messages.rb
|
69
70
|
- lib/e9_rails/helpers/title.rb
|