administrate 0.10.0 → 0.15.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/app/assets/javascripts/administrate/components/associative.js +5 -0
- data/app/assets/javascripts/administrate/components/date_time_picker.js +6 -2
- data/app/assets/javascripts/administrate/components/table.js +1 -1
- data/app/assets/stylesheets/administrate/base/_tables.scss +3 -0
- data/app/assets/stylesheets/administrate/base/_typography.scss +1 -1
- data/app/assets/stylesheets/administrate/components/_attributes.scss +4 -3
- data/app/assets/stylesheets/administrate/components/_buttons.scss +8 -0
- data/app/assets/stylesheets/administrate/components/_cells.scss +2 -0
- data/app/assets/stylesheets/administrate/components/_field-unit.scss +17 -4
- data/app/assets/stylesheets/administrate/components/_flashes.scss +0 -8
- data/app/assets/stylesheets/administrate/components/_main-content.scss +1 -0
- data/app/assets/stylesheets/administrate/components/_navigation.scss +2 -3
- data/app/assets/stylesheets/administrate/library/_variables.scss +10 -8
- data/app/controllers/administrate/application_controller.rb +33 -9
- data/app/controllers/concerns/administrate/punditize.rb +3 -3
- data/app/helpers/administrate/application_helper.rb +47 -15
- data/app/views/administrate/application/_collection.html.erb +8 -7
- data/app/views/administrate/application/_form.html.erb +2 -2
- data/app/views/administrate/application/{_icons.erb → _icons.html.erb} +0 -0
- data/app/views/administrate/application/_navigation.html.erb +5 -3
- data/app/views/administrate/application/index.html.erb +4 -2
- data/app/views/administrate/application/show.html.erb +2 -2
- data/app/views/fields/belongs_to/_form.html.erb +3 -3
- data/app/views/fields/date/_form.html.erb +24 -0
- data/app/views/fields/date/_index.html.erb +21 -0
- data/app/views/fields/date/_show.html.erb +21 -0
- data/app/views/fields/has_many/_form.html.erb +2 -2
- data/app/views/fields/has_many/_show.html.erb +8 -5
- data/app/views/fields/has_one/_form.html.erb +1 -1
- data/app/views/fields/has_one/_index.html.erb +1 -1
- data/app/views/fields/has_one/_show.html.erb +4 -4
- data/app/views/fields/number/_form.html.erb +1 -1
- data/app/views/fields/polymorphic/_form.html.erb +0 -3
- data/app/views/fields/polymorphic/_show.html.erb +1 -1
- data/app/views/fields/select/_form.html.erb +22 -10
- data/app/views/fields/string/_show.html.erb +2 -2
- data/app/views/fields/text/_show.html.erb +2 -3
- data/app/views/fields/time/_form.html.erb +3 -2
- data/app/views/fields/time/_index.html.erb +3 -1
- data/app/views/fields/time/_show.html.erb +3 -1
- data/app/views/fields/url/_form.html.erb +23 -0
- data/app/views/fields/url/_index.html.erb +20 -0
- data/app/views/fields/url/_show.html.erb +20 -0
- data/app/views/layouts/administrate/application.html.erb +2 -1
- data/config/locales/administrate.ar.yml +2 -0
- data/config/locales/administrate.bs.yml +2 -0
- data/config/locales/administrate.ca.yml +2 -0
- data/config/locales/administrate.da.yml +2 -0
- data/config/locales/administrate.de.yml +2 -0
- data/config/locales/administrate.en.yml +2 -0
- data/config/locales/administrate.es.yml +3 -1
- data/config/locales/administrate.fi.yml +30 -0
- data/config/locales/administrate.fr.yml +4 -2
- data/config/locales/administrate.id.yml +30 -0
- data/config/locales/administrate.it.yml +2 -0
- data/config/locales/administrate.ja.yml +2 -0
- data/config/locales/administrate.ko.yml +12 -10
- data/config/locales/administrate.nl.yml +7 -5
- data/config/locales/administrate.pl.yml +2 -0
- data/config/locales/administrate.pt-BR.yml +4 -2
- data/config/locales/administrate.pt.yml +4 -2
- data/config/locales/administrate.ru.yml +2 -0
- data/config/locales/{administrate.al.yml → administrate.sq.yml} +3 -1
- data/config/locales/administrate.sv.yml +2 -0
- data/config/locales/administrate.tr.yml +30 -0
- data/config/locales/administrate.uk.yml +2 -0
- data/config/locales/administrate.vi.yml +2 -0
- data/config/locales/administrate.zh-CN.yml +4 -2
- data/config/locales/administrate.zh-TW.yml +6 -4
- data/config/unicorn.rb +8 -13
- data/docs/adding_controllers_without_related_model.md +54 -0
- data/docs/adding_custom_field_types.md +3 -1
- data/docs/authentication.md +3 -1
- data/docs/authorization.md +5 -3
- data/docs/customizing_attribute_partials.md +5 -2
- data/docs/customizing_controller_actions.md +32 -2
- data/docs/customizing_dashboards.md +74 -34
- data/docs/customizing_page_views.md +18 -4
- data/docs/extending_administrate.md +27 -0
- data/docs/getting_started.md +49 -56
- data/docs/guides.md +5 -0
- data/docs/guides/hiding_dashboards_from_sidebar.md +19 -0
- data/docs/rails_api.md +45 -0
- data/lib/administrate.rb +19 -0
- data/lib/administrate/base_dashboard.rb +35 -9
- data/lib/administrate/custom_dashboard.rb +15 -0
- data/lib/administrate/engine.rb +8 -1
- data/lib/administrate/field/associative.rb +49 -5
- data/lib/administrate/field/base.rb +35 -9
- data/lib/administrate/field/belongs_to.rb +13 -3
- data/lib/administrate/field/date.rb +20 -0
- data/lib/administrate/field/deferred.rb +22 -3
- data/lib/administrate/field/has_many.rb +31 -11
- data/lib/administrate/field/has_one.rb +32 -12
- data/lib/administrate/field/number.rb +19 -2
- data/lib/administrate/field/polymorphic.rb +5 -5
- data/lib/administrate/field/select.rb +6 -1
- data/lib/administrate/field/url.rb +21 -0
- data/lib/administrate/namespace.rb +4 -0
- data/lib/administrate/order.rb +17 -7
- data/lib/administrate/page/base.rb +9 -3
- data/lib/administrate/page/collection.rb +5 -1
- data/lib/administrate/page/form.rb +1 -1
- data/lib/administrate/resource_resolver.rb +1 -1
- data/lib/administrate/search.rb +107 -23
- data/lib/administrate/version.rb +1 -1
- data/lib/administrate/view_generator.rb +9 -3
- data/lib/generators/administrate/dashboard/dashboard_generator.rb +21 -15
- data/lib/generators/administrate/dashboard/templates/controller.rb.erb +33 -8
- data/lib/generators/administrate/dashboard/templates/dashboard.rb.erb +18 -6
- data/lib/generators/administrate/install/install_generator.rb +37 -1
- data/lib/generators/administrate/install/templates/application_controller.rb.erb +3 -3
- data/lib/generators/administrate/routes/routes_generator.rb +21 -26
- data/lib/generators/administrate/views/layout_generator.rb +1 -0
- data/lib/generators/administrate/views/views_generator.rb +5 -4
- metadata +50 -53
- data/app/assets/javascripts/administrate/components/has_many_form.js +0 -3
- data/config/secrets.yml +0 -14
@@ -12,10 +12,22 @@ module Administrate
|
|
12
12
|
field_type.dasherize
|
13
13
|
end
|
14
14
|
|
15
|
+
def self.associative?
|
16
|
+
self < Associative
|
17
|
+
end
|
18
|
+
|
15
19
|
def self.searchable?
|
16
20
|
false
|
17
21
|
end
|
18
22
|
|
23
|
+
def self.field_type
|
24
|
+
to_s.split("::").last.underscore
|
25
|
+
end
|
26
|
+
|
27
|
+
def self.permitted_attribute(attr, _options = nil)
|
28
|
+
attr
|
29
|
+
end
|
30
|
+
|
19
31
|
def initialize(attribute, data, page, options = {})
|
20
32
|
@attribute = attribute
|
21
33
|
@data = data
|
@@ -24,10 +36,6 @@ module Administrate
|
|
24
36
|
@options = options
|
25
37
|
end
|
26
38
|
|
27
|
-
def self.permitted_attribute(attr, _options = nil)
|
28
|
-
attr
|
29
|
-
end
|
30
|
-
|
31
39
|
def html_class
|
32
40
|
self.class.html_class
|
33
41
|
end
|
@@ -40,15 +48,33 @@ module Administrate
|
|
40
48
|
"/fields/#{self.class.field_type}/#{page}"
|
41
49
|
end
|
42
50
|
|
43
|
-
|
51
|
+
def required?
|
52
|
+
return false unless resource.class.respond_to?(:validators_on)
|
44
53
|
|
45
|
-
|
54
|
+
resource.class.validators_on(attribute).any? do |v|
|
55
|
+
next false unless v.class == ActiveRecord::Validations::PresenceValidator
|
46
56
|
|
47
|
-
|
57
|
+
options = v.options
|
58
|
+
next false if options.include?(:if)
|
59
|
+
next false if options.include?(:unless)
|
48
60
|
|
49
|
-
|
50
|
-
|
61
|
+
if on_option = options[:on]
|
62
|
+
if on_option == :create && !resource.persisted?
|
63
|
+
next true
|
64
|
+
end
|
65
|
+
|
66
|
+
if on_option == :update && resource.persisted?
|
67
|
+
next true
|
68
|
+
end
|
69
|
+
|
70
|
+
next false
|
71
|
+
end
|
72
|
+
|
73
|
+
true
|
74
|
+
end
|
51
75
|
end
|
76
|
+
|
77
|
+
attr_reader :attribute, :data, :options, :page, :resource
|
52
78
|
end
|
53
79
|
end
|
54
80
|
end
|
@@ -3,8 +3,14 @@ require_relative "associative"
|
|
3
3
|
module Administrate
|
4
4
|
module Field
|
5
5
|
class BelongsTo < Associative
|
6
|
-
def self.permitted_attribute(attr,
|
7
|
-
:
|
6
|
+
def self.permitted_attribute(attr, options = {})
|
7
|
+
resource_class = options[:resource_class]
|
8
|
+
if resource_class
|
9
|
+
foreign_key_for(resource_class, attr)
|
10
|
+
else
|
11
|
+
Administrate.warn_of_missing_resource_class
|
12
|
+
:"#{attr}_id"
|
13
|
+
end
|
8
14
|
end
|
9
15
|
|
10
16
|
def permitted_attribute
|
@@ -12,7 +18,7 @@ module Administrate
|
|
12
18
|
end
|
13
19
|
|
14
20
|
def associated_resource_options
|
15
|
-
|
21
|
+
candidate_resources.map do |resource|
|
16
22
|
[display_candidate_resource(resource), resource.send(primary_key)]
|
17
23
|
end
|
18
24
|
end
|
@@ -21,6 +27,10 @@ module Administrate
|
|
21
27
|
data && data.send(primary_key)
|
22
28
|
end
|
23
29
|
|
30
|
+
def include_blank_option
|
31
|
+
options.fetch(:include_blank, true)
|
32
|
+
end
|
33
|
+
|
24
34
|
private
|
25
35
|
|
26
36
|
def candidate_resources
|
@@ -0,0 +1,20 @@
|
|
1
|
+
require_relative "base"
|
2
|
+
|
3
|
+
module Administrate
|
4
|
+
module Field
|
5
|
+
class Date < Base
|
6
|
+
def date
|
7
|
+
I18n.localize(
|
8
|
+
data.to_date,
|
9
|
+
format: format,
|
10
|
+
)
|
11
|
+
end
|
12
|
+
|
13
|
+
private
|
14
|
+
|
15
|
+
def format
|
16
|
+
options.fetch(:format, :default)
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
@@ -21,17 +21,36 @@ module Administrate
|
|
21
21
|
options == other.options
|
22
22
|
end
|
23
23
|
|
24
|
+
def associative?
|
25
|
+
deferred_class.associative?
|
26
|
+
end
|
27
|
+
|
24
28
|
def searchable?
|
25
29
|
options.fetch(:searchable, deferred_class.searchable?)
|
26
30
|
end
|
27
31
|
|
28
32
|
def searchable_field
|
33
|
+
ActiveSupport::Deprecation.warn(
|
34
|
+
"searchable_field is deprecated, use searchable_fields instead",
|
35
|
+
)
|
29
36
|
options.fetch(:searchable_field)
|
30
37
|
end
|
31
38
|
|
32
|
-
def
|
33
|
-
options.
|
34
|
-
|
39
|
+
def searchable_fields
|
40
|
+
if options.key?(:searchable_field)
|
41
|
+
[searchable_field]
|
42
|
+
else
|
43
|
+
options.fetch(:searchable_fields)
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
def permitted_attribute(attr, opts = {})
|
48
|
+
if options.key?(:foreign_key)
|
49
|
+
Administrate.warn_of_deprecated_option(:foreign_key)
|
50
|
+
options.fetch(:foreign_key)
|
51
|
+
else
|
52
|
+
deferred_class.permitted_attribute(attr, options.merge(opts))
|
53
|
+
end
|
35
54
|
end
|
36
55
|
|
37
56
|
delegate :html_class, to: :deferred_class
|
@@ -7,12 +7,22 @@ module Administrate
|
|
7
7
|
class HasMany < Associative
|
8
8
|
DEFAULT_LIMIT = 5
|
9
9
|
|
10
|
-
def self.permitted_attribute(attr, _options =
|
10
|
+
def self.permitted_attribute(attr, _options = {})
|
11
|
+
# This may seem arbitrary, and improvable by using reflection.
|
12
|
+
# Worry not: here we do exactly what Rails does. Regardless of the name
|
13
|
+
# of the foreign key, has_many associations use the suffix `_ids`
|
14
|
+
# for this.
|
15
|
+
#
|
16
|
+
# Eg: if the associated table and primary key are `countries.code`,
|
17
|
+
# you may expect `country_codes` as attribute here, but it will
|
18
|
+
# be `country_ids` instead.
|
19
|
+
#
|
20
|
+
# See https://github.com/rails/rails/blob/b30a23f53b52e59d31358f7b80385ee5c2ba3afe/activerecord/lib/active_record/associations/builder/collection_association.rb#L48
|
11
21
|
{ "#{attr.to_s.singularize}_ids".to_sym => [] }
|
12
22
|
end
|
13
23
|
|
14
|
-
def associated_collection
|
15
|
-
Administrate::Page::Collection.new(associated_dashboard)
|
24
|
+
def associated_collection(order = self.order)
|
25
|
+
Administrate::Page::Collection.new(associated_dashboard, order: order)
|
16
26
|
end
|
17
27
|
|
18
28
|
def attribute_key
|
@@ -36,10 +46,13 @@ module Administrate
|
|
36
46
|
end
|
37
47
|
|
38
48
|
def permitted_attribute
|
39
|
-
self.class.permitted_attribute(
|
49
|
+
self.class.permitted_attribute(
|
50
|
+
attribute,
|
51
|
+
resource_class: resource.class,
|
52
|
+
)
|
40
53
|
end
|
41
54
|
|
42
|
-
def resources(page = 1)
|
55
|
+
def resources(page = 1, order = self.order)
|
43
56
|
resources = order.apply(data).page(page).per(limit)
|
44
57
|
includes.any? ? resources.includes(*includes) : resources
|
45
58
|
end
|
@@ -49,13 +62,24 @@ module Administrate
|
|
49
62
|
end
|
50
63
|
|
51
64
|
def data
|
52
|
-
@data ||= associated_class.none
|
65
|
+
@data ||= associated_class.none
|
66
|
+
end
|
67
|
+
|
68
|
+
def order_from_params(params)
|
69
|
+
Administrate::Order.new(
|
70
|
+
params.fetch(:order, sort_by),
|
71
|
+
params.fetch(:direction, direction),
|
72
|
+
)
|
73
|
+
end
|
74
|
+
|
75
|
+
def order
|
76
|
+
@order ||= Administrate::Order.new(sort_by, direction)
|
53
77
|
end
|
54
78
|
|
55
79
|
private
|
56
80
|
|
57
81
|
def includes
|
58
|
-
associated_dashboard.
|
82
|
+
associated_dashboard.collection_includes
|
59
83
|
end
|
60
84
|
|
61
85
|
def candidate_resources
|
@@ -71,10 +95,6 @@ module Administrate
|
|
71
95
|
associated_dashboard.display_resource(resource)
|
72
96
|
end
|
73
97
|
|
74
|
-
def order
|
75
|
-
@_order ||= Administrate::Order.new(sort_by, direction)
|
76
|
-
end
|
77
|
-
|
78
98
|
def sort_by
|
79
99
|
options[:sort_by]
|
80
100
|
end
|
@@ -3,6 +3,29 @@ require_relative "associative"
|
|
3
3
|
module Administrate
|
4
4
|
module Field
|
5
5
|
class HasOne < Associative
|
6
|
+
def self.permitted_attribute(attr, options = {})
|
7
|
+
resource_class = options[:resource_class]
|
8
|
+
final_associated_class_name =
|
9
|
+
if options.key?(:class_name)
|
10
|
+
Administrate.warn_of_deprecated_option(:class_name)
|
11
|
+
options.fetch(:class_name)
|
12
|
+
elsif resource_class
|
13
|
+
associated_class_name(resource_class, attr)
|
14
|
+
else
|
15
|
+
Administrate.warn_of_missing_resource_class
|
16
|
+
if options
|
17
|
+
attr.to_s.singularize.camelcase
|
18
|
+
else
|
19
|
+
attr
|
20
|
+
end
|
21
|
+
end
|
22
|
+
related_dashboard_attributes =
|
23
|
+
Administrate::ResourceResolver.
|
24
|
+
new("admin/#{final_associated_class_name}").
|
25
|
+
dashboard_class.new.permitted_attributes + [:id]
|
26
|
+
{ "#{attr}_attributes": related_dashboard_attributes }
|
27
|
+
end
|
28
|
+
|
6
29
|
def nested_form
|
7
30
|
@nested_form ||= Administrate::Page::Form.new(
|
8
31
|
resolver.dashboard_class.new,
|
@@ -10,25 +33,22 @@ module Administrate
|
|
10
33
|
)
|
11
34
|
end
|
12
35
|
|
13
|
-
def
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
end
|
20
|
-
related_dashboard_attributes =
|
21
|
-
Administrate::ResourceResolver.new("admin/#{associated_class_name}").
|
22
|
-
dashboard_class.new.permitted_attributes + [:id]
|
36
|
+
def nested_show
|
37
|
+
@nested_show ||= Administrate::Page::Show.new(
|
38
|
+
resolver.dashboard_class.new,
|
39
|
+
data || resolver.resource_class.new,
|
40
|
+
)
|
41
|
+
end
|
23
42
|
|
24
|
-
|
43
|
+
def linkable?
|
44
|
+
data.try(:persisted?)
|
25
45
|
end
|
26
46
|
|
27
47
|
private
|
28
48
|
|
29
49
|
def resolver
|
30
50
|
@resolver ||=
|
31
|
-
Administrate::ResourceResolver.new("admin/#{
|
51
|
+
Administrate::ResourceResolver.new("admin/#{associated_class.name}")
|
32
52
|
end
|
33
53
|
end
|
34
54
|
end
|
@@ -1,16 +1,19 @@
|
|
1
1
|
require_relative "base"
|
2
|
+
require "active_support/number_helper"
|
2
3
|
|
3
4
|
module Administrate
|
4
5
|
module Field
|
5
6
|
class Number < Field::Base
|
6
7
|
def to_s
|
7
|
-
data.nil? ? "-" : format_string % value
|
8
|
+
result = data.nil? ? "-" : format_string % value
|
9
|
+
result = format(result) if options[:format]
|
10
|
+
prefix + result + suffix
|
8
11
|
end
|
9
12
|
|
10
13
|
private
|
11
14
|
|
12
15
|
def format_string
|
13
|
-
|
16
|
+
"%.#{decimals}f"
|
14
17
|
end
|
15
18
|
|
16
19
|
def prefix
|
@@ -30,6 +33,20 @@ module Administrate
|
|
30
33
|
def value
|
31
34
|
data * options.fetch(:multiplier, 1)
|
32
35
|
end
|
36
|
+
|
37
|
+
def format(result)
|
38
|
+
formatter = options[:format][:formatter]
|
39
|
+
formatter_options = options[:format][:formatter_options].to_h
|
40
|
+
|
41
|
+
case formatter
|
42
|
+
when :number_to_delimited
|
43
|
+
ActiveSupport::NumberHelper.number_to_delimited(
|
44
|
+
result, **formatter_options
|
45
|
+
)
|
46
|
+
else
|
47
|
+
result
|
48
|
+
end
|
49
|
+
end
|
33
50
|
end
|
34
51
|
end
|
35
52
|
end
|
@@ -3,6 +3,10 @@ require_relative "associative"
|
|
3
3
|
module Administrate
|
4
4
|
module Field
|
5
5
|
class Polymorphic < BelongsTo
|
6
|
+
def self.permitted_attribute(attr, _options = {})
|
7
|
+
{ attr => %i{type value} }
|
8
|
+
end
|
9
|
+
|
6
10
|
def associated_resource_grouped_options
|
7
11
|
classes.map do |klass|
|
8
12
|
[klass.to_s, candidate_resources_for(klass).map do |resource|
|
@@ -11,10 +15,6 @@ module Administrate
|
|
11
15
|
end
|
12
16
|
end
|
13
17
|
|
14
|
-
def self.permitted_attribute(attr, _options = nil)
|
15
|
-
{ attr => %i{type value} }
|
16
|
-
end
|
17
|
-
|
18
18
|
def permitted_attribute
|
19
19
|
{ attribute => %i{type value} }
|
20
20
|
end
|
@@ -23,7 +23,7 @@ module Administrate
|
|
23
23
|
data ? data.to_global_id : nil
|
24
24
|
end
|
25
25
|
|
26
|
-
|
26
|
+
private
|
27
27
|
|
28
28
|
def associated_dashboard(klass = data.class)
|
29
29
|
"#{klass.name}Dashboard".constantize.new
|
@@ -14,7 +14,12 @@ module Administrate
|
|
14
14
|
private
|
15
15
|
|
16
16
|
def collection
|
17
|
-
|
17
|
+
values = options.fetch(:collection, [])
|
18
|
+
if values.respond_to? :call
|
19
|
+
return values.arity.positive? ? values.call(self) : values.call
|
20
|
+
end
|
21
|
+
|
22
|
+
@collection ||= values
|
18
23
|
end
|
19
24
|
end
|
20
25
|
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
require_relative "base"
|
2
|
+
|
3
|
+
module Administrate
|
4
|
+
module Field
|
5
|
+
class Url < Field::Base
|
6
|
+
def self.searchable?
|
7
|
+
true
|
8
|
+
end
|
9
|
+
|
10
|
+
def truncate
|
11
|
+
data.to_s[0...truncation_length]
|
12
|
+
end
|
13
|
+
|
14
|
+
private
|
15
|
+
|
16
|
+
def truncation_length
|
17
|
+
options.fetch(:truncate, 50)
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
data/lib/administrate/order.rb
CHANGED
@@ -2,7 +2,7 @@ module Administrate
|
|
2
2
|
class Order
|
3
3
|
def initialize(attribute = nil, direction = nil)
|
4
4
|
@attribute = attribute
|
5
|
-
@direction = direction
|
5
|
+
@direction = sanitize_direction(direction)
|
6
6
|
end
|
7
7
|
|
8
8
|
def apply(relation)
|
@@ -11,7 +11,7 @@ module Administrate
|
|
11
11
|
|
12
12
|
order = "#{relation.table_name}.#{attribute} #{direction}"
|
13
13
|
|
14
|
-
return relation.reorder(order) if
|
14
|
+
return relation.reorder(Arel.sql(order)) if
|
15
15
|
relation.columns_hash.keys.include?(attribute.to_s)
|
16
16
|
|
17
17
|
relation
|
@@ -34,6 +34,10 @@ module Administrate
|
|
34
34
|
|
35
35
|
attr_reader :attribute
|
36
36
|
|
37
|
+
def sanitize_direction(direction)
|
38
|
+
%w[asc desc].include?(direction.to_s) ? direction.to_sym : :asc
|
39
|
+
end
|
40
|
+
|
37
41
|
def reversed_direction_param_for(attr)
|
38
42
|
if ordered_by?(attr)
|
39
43
|
opposite_direction
|
@@ -43,7 +47,7 @@ module Administrate
|
|
43
47
|
end
|
44
48
|
|
45
49
|
def opposite_direction
|
46
|
-
direction
|
50
|
+
direction == :asc ? :desc : :asc
|
47
51
|
end
|
48
52
|
|
49
53
|
def order_by_association(relation)
|
@@ -55,14 +59,16 @@ module Administrate
|
|
55
59
|
end
|
56
60
|
|
57
61
|
def order_by_count(relation)
|
62
|
+
klass = reflect_association(relation).klass
|
63
|
+
query = "COUNT(#{klass.table_name}.#{klass.primary_key}) #{direction}"
|
58
64
|
relation.
|
59
|
-
|
60
|
-
|
61
|
-
|
65
|
+
left_joins(attribute.to_sym).
|
66
|
+
group(:id).
|
67
|
+
reorder(Arel.sql(query))
|
62
68
|
end
|
63
69
|
|
64
70
|
def order_by_id(relation)
|
65
|
-
relation.reorder("#{
|
71
|
+
relation.reorder("#{foreign_key(relation)} #{direction}")
|
66
72
|
end
|
67
73
|
|
68
74
|
def has_many_attribute?(relation)
|
@@ -76,5 +82,9 @@ module Administrate
|
|
76
82
|
def reflect_association(relation)
|
77
83
|
relation.klass.reflect_on_association(attribute.to_s)
|
78
84
|
end
|
85
|
+
|
86
|
+
def foreign_key(relation)
|
87
|
+
reflect_association(relation).foreign_key
|
88
|
+
end
|
79
89
|
end
|
80
90
|
end
|