administrate 0.12.0 → 0.15.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (105) hide show
  1. checksums.yaml +4 -4
  2. data/app/assets/javascripts/administrate/components/associative.js +5 -0
  3. data/app/assets/javascripts/administrate/components/date_time_picker.js +6 -2
  4. data/app/assets/javascripts/administrate/components/table.js +1 -1
  5. data/app/assets/stylesheets/administrate/base/_tables.scss +3 -0
  6. data/app/assets/stylesheets/administrate/components/_attributes.scss +4 -3
  7. data/app/assets/stylesheets/administrate/components/_buttons.scss +8 -0
  8. data/app/assets/stylesheets/administrate/components/_cells.scss +2 -0
  9. data/app/assets/stylesheets/administrate/components/_field-unit.scss +17 -4
  10. data/app/assets/stylesheets/administrate/components/_flashes.scss +0 -8
  11. data/app/assets/stylesheets/administrate/components/_main-content.scss +1 -0
  12. data/app/assets/stylesheets/administrate/components/_navigation.scss +2 -3
  13. data/app/assets/stylesheets/administrate/library/_variables.scss +10 -8
  14. data/app/controllers/administrate/application_controller.rb +29 -8
  15. data/app/helpers/administrate/application_helper.rb +32 -14
  16. data/app/views/administrate/application/_collection.html.erb +6 -4
  17. data/app/views/administrate/application/_form.html.erb +2 -2
  18. data/app/views/administrate/application/_navigation.html.erb +5 -3
  19. data/app/views/administrate/application/index.html.erb +2 -2
  20. data/app/views/administrate/application/show.html.erb +1 -1
  21. data/app/views/fields/belongs_to/_form.html.erb +3 -3
  22. data/app/views/fields/date/_form.html.erb +24 -0
  23. data/app/views/fields/date/_index.html.erb +21 -0
  24. data/app/views/fields/date/_show.html.erb +21 -0
  25. data/app/views/fields/has_one/_index.html.erb +1 -1
  26. data/app/views/fields/has_one/_show.html.erb +4 -4
  27. data/app/views/fields/number/_form.html.erb +1 -1
  28. data/app/views/fields/polymorphic/_show.html.erb +1 -1
  29. data/app/views/fields/select/_form.html.erb +21 -9
  30. data/app/views/fields/string/_show.html.erb +2 -2
  31. data/app/views/fields/text/_show.html.erb +2 -3
  32. data/app/views/fields/time/_form.html.erb +3 -2
  33. data/app/views/fields/time/_index.html.erb +3 -1
  34. data/app/views/fields/time/_show.html.erb +3 -1
  35. data/app/views/layouts/administrate/application.html.erb +1 -0
  36. data/config/locales/administrate.ar.yml +2 -0
  37. data/config/locales/administrate.bs.yml +2 -0
  38. data/config/locales/administrate.ca.yml +2 -0
  39. data/config/locales/administrate.da.yml +2 -0
  40. data/config/locales/administrate.de.yml +2 -0
  41. data/config/locales/administrate.en.yml +2 -0
  42. data/config/locales/administrate.es.yml +2 -0
  43. data/config/locales/administrate.fi.yml +30 -0
  44. data/config/locales/administrate.fr.yml +4 -2
  45. data/config/locales/administrate.id.yml +2 -0
  46. data/config/locales/administrate.it.yml +2 -0
  47. data/config/locales/administrate.ja.yml +2 -0
  48. data/config/locales/administrate.ko.yml +2 -0
  49. data/config/locales/administrate.nl.yml +7 -5
  50. data/config/locales/administrate.pl.yml +2 -0
  51. data/config/locales/administrate.pt-BR.yml +4 -2
  52. data/config/locales/administrate.pt.yml +4 -2
  53. data/config/locales/administrate.ru.yml +2 -0
  54. data/config/locales/{administrate.al.yml → administrate.sq.yml} +3 -1
  55. data/config/locales/administrate.sv.yml +2 -0
  56. data/config/locales/administrate.tr.yml +30 -0
  57. data/config/locales/administrate.uk.yml +2 -0
  58. data/config/locales/administrate.vi.yml +2 -0
  59. data/config/locales/administrate.zh-CN.yml +2 -0
  60. data/config/locales/administrate.zh-TW.yml +2 -0
  61. data/config/unicorn.rb +8 -13
  62. data/docs/adding_controllers_without_related_model.md +54 -0
  63. data/docs/adding_custom_field_types.md +3 -1
  64. data/docs/authentication.md +3 -1
  65. data/docs/authorization.md +5 -3
  66. data/docs/customizing_attribute_partials.md +4 -1
  67. data/docs/customizing_controller_actions.md +30 -1
  68. data/docs/customizing_dashboards.md +47 -35
  69. data/docs/customizing_page_views.md +18 -4
  70. data/docs/extending_administrate.md +27 -0
  71. data/docs/getting_started.md +35 -11
  72. data/docs/guides/hiding_dashboards_from_sidebar.md +19 -0
  73. data/docs/guides.md +5 -0
  74. data/docs/rails_api.md +5 -3
  75. data/lib/administrate/base_dashboard.rb +19 -10
  76. data/lib/administrate/custom_dashboard.rb +15 -0
  77. data/lib/administrate/engine.rb +7 -0
  78. data/lib/administrate/field/associative.rb +49 -5
  79. data/lib/administrate/field/base.rb +35 -9
  80. data/lib/administrate/field/belongs_to.rb +13 -3
  81. data/lib/administrate/field/date.rb +20 -0
  82. data/lib/administrate/field/deferred.rb +22 -3
  83. data/lib/administrate/field/has_many.rb +15 -2
  84. data/lib/administrate/field/has_one.rb +32 -12
  85. data/lib/administrate/field/number.rb +19 -2
  86. data/lib/administrate/field/polymorphic.rb +5 -5
  87. data/lib/administrate/field/select.rb +6 -1
  88. data/lib/administrate/namespace.rb +4 -0
  89. data/lib/administrate/order.rb +17 -7
  90. data/lib/administrate/page/base.rb +1 -3
  91. data/lib/administrate/page/form.rb +1 -1
  92. data/lib/administrate/resource_resolver.rb +1 -1
  93. data/lib/administrate/search.rb +26 -19
  94. data/lib/administrate/version.rb +1 -1
  95. data/lib/administrate/view_generator.rb +9 -3
  96. data/lib/administrate.rb +19 -0
  97. data/lib/generators/administrate/dashboard/dashboard_generator.rb +18 -14
  98. data/lib/generators/administrate/dashboard/templates/controller.rb.erb +20 -8
  99. data/lib/generators/administrate/dashboard/templates/dashboard.rb.erb +4 -4
  100. data/lib/generators/administrate/install/install_generator.rb +37 -1
  101. data/lib/generators/administrate/install/templates/application_controller.rb.erb +3 -3
  102. data/lib/generators/administrate/routes/routes_generator.rb +15 -25
  103. data/lib/generators/administrate/views/views_generator.rb +5 -4
  104. metadata +26 -29
  105. data/app/assets/javascripts/administrate/components/has_many_form.js +0 -3
@@ -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 permitted_attribute(attr, _options = nil)
33
- options.fetch(:foreign_key,
34
- deferred_class.permitted_attribute(attr, options))
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,7 +7,17 @@ module Administrate
7
7
  class HasMany < Associative
8
8
  DEFAULT_LIMIT = 5
9
9
 
10
- def self.permitted_attribute(attr, _options = nil)
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
 
@@ -36,7 +46,10 @@ module Administrate
36
46
  end
37
47
 
38
48
  def permitted_attribute
39
- self.class.permitted_attribute(attribute)
49
+ self.class.permitted_attribute(
50
+ attribute,
51
+ resource_class: resource.class,
52
+ )
40
53
  end
41
54
 
42
55
  def resources(page = 1, order = self.order)
@@ -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 self.permitted_attribute(attr, options = nil)
14
- associated_class_name =
15
- if options
16
- options.fetch(:class_name, attr.to_s.singularize.camelcase)
17
- else
18
- attr
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
- { "#{attr}_attributes": related_dashboard_attributes }
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/#{associated_class_name}")
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
- prefix + "%.#{decimals}f" + suffix
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
- protected
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
- @collection ||= options.fetch(:collection, [])
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
@@ -18,6 +18,10 @@ module Administrate
18
18
  end
19
19
  end
20
20
 
21
+ def resources_with_index_route
22
+ routes.select { |_resource, route| route == "index" }.map(&:first).uniq
23
+ end
24
+
21
25
  private
22
26
 
23
27
  attr_reader :namespace
@@ -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 || :asc
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.to_sym == :asc ? :desc : :asc
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
- left_joins(attribute.to_sym).
60
- group(:id).
61
- reorder("COUNT(#{attribute}.id) #{direction}")
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("#{attribute}_id #{direction}")
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
@@ -23,7 +23,7 @@ module Administrate
23
23
  dashboard.try(:item_includes) || []
24
24
  end
25
25
 
26
- protected
26
+ private
27
27
 
28
28
  def attribute_field(dashboard, resource, attribute_name, page)
29
29
  value = get_attribute_value(resource, attribute_name)
@@ -33,8 +33,6 @@ module Administrate
33
33
 
34
34
  def get_attribute_value(resource, attribute_name)
35
35
  resource.public_send(attribute_name)
36
- rescue NameError
37
- nil
38
36
  end
39
37
 
40
38
  attr_reader :dashboard, :options
@@ -20,7 +20,7 @@ module Administrate
20
20
  dashboard.display_resource(resource)
21
21
  end
22
22
 
23
- protected
23
+ private
24
24
 
25
25
  attr_reader :dashboard
26
26
  end
@@ -21,7 +21,7 @@ module Administrate
21
21
  end
22
22
 
23
23
  def resource_title
24
- model_path_parts.join(" ")
24
+ resource_class.model_name.human
25
25
  end
26
26
 
27
27
  private
@@ -81,14 +81,24 @@ module Administrate
81
81
  def query_template
82
82
  search_attributes.map do |attr|
83
83
  table_name = query_table_name(attr)
84
- attr_name = column_to_query(attr)
85
-
86
- "LOWER(CAST(#{table_name}.#{attr_name} AS CHAR(256))) LIKE ?"
84
+ searchable_fields(attr).map do |field|
85
+ column_name = column_to_query(field)
86
+ "LOWER(CAST(#{table_name}.#{column_name} AS CHAR(256))) LIKE ?"
87
+ end.join(" OR ")
87
88
  end.join(" OR ")
88
89
  end
89
90
 
91
+ def searchable_fields(attr)
92
+ return [attr] unless association_search?(attr)
93
+
94
+ attribute_types[attr].searchable_fields
95
+ end
96
+
90
97
  def query_values
91
- ["%#{term.mb_chars.downcase}%"] * search_attributes.count
98
+ fields_count = search_attributes.sum do |attr|
99
+ searchable_fields(attr).count
100
+ end
101
+ ["%#{term.mb_chars.downcase}%"] * fields_count
92
102
  end
93
103
 
94
104
  def search_attributes
@@ -99,7 +109,7 @@ module Administrate
99
109
 
100
110
  def search_results(resources)
101
111
  resources.
102
- joins(tables_to_join).
112
+ left_joins(tables_to_join).
103
113
  where(query_template, *query_values)
104
114
  end
105
115
 
@@ -117,7 +127,15 @@ module Administrate
117
127
 
118
128
  def query_table_name(attr)
119
129
  if association_search?(attr)
120
- ActiveRecord::Base.connection.quote_table_name(attr.to_s.pluralize)
130
+ provided_class_name = attribute_types[attr].options[:class_name]
131
+ unquoted_table_name =
132
+ if provided_class_name
133
+ Administrate.warn_of_deprecated_option(:class_name)
134
+ provided_class_name.constantize.table_name
135
+ else
136
+ @scoped_resource.reflect_on_association(attr).klass.table_name
137
+ end
138
+ ActiveRecord::Base.connection.quote_table_name(unquoted_table_name)
121
139
  else
122
140
  ActiveRecord::Base.connection.
123
141
  quote_table_name(@scoped_resource.table_name)
@@ -125,12 +143,7 @@ module Administrate
125
143
  end
126
144
 
127
145
  def column_to_query(attr)
128
- if association_search?(attr)
129
- ActiveRecord::Base.connection.
130
- quote_column_name(attribute_types[attr].searchable_field)
131
- else
132
- ActiveRecord::Base.connection.quote_column_name(attr)
133
- end
146
+ ActiveRecord::Base.connection.quote_column_name(attr)
134
147
  end
135
148
 
136
149
  def tables_to_join
@@ -140,13 +153,7 @@ module Administrate
140
153
  end
141
154
 
142
155
  def association_search?(attribute)
143
- return unless attribute_types[attribute].respond_to?(:deferred_class)
144
-
145
- [
146
- Administrate::Field::BelongsTo,
147
- Administrate::Field::HasMany,
148
- Administrate::Field::HasOne,
149
- ].include?(attribute_types[attribute].deferred_class)
156
+ attribute_types[attribute].associative?
150
157
  end
151
158
 
152
159
  def term
@@ -1,3 +1,3 @@
1
1
  module Administrate
2
- VERSION = "0.12.0".freeze
2
+ VERSION = "0.15.0".freeze
3
3
  end
@@ -1,11 +1,11 @@
1
1
  require "rails/generators/base"
2
2
  require "administrate/generator_helpers"
3
+ require "administrate/namespace"
3
4
 
4
5
  module Administrate
5
6
  class ViewGenerator < Rails::Generators::Base
6
7
  include Administrate::GeneratorHelpers
7
-
8
- private
8
+ class_option :namespace, type: :string, default: "admin"
9
9
 
10
10
  def self.template_source_path
11
11
  File.expand_path(
@@ -14,12 +14,18 @@ module Administrate
14
14
  )
15
15
  end
16
16
 
17
+ private
18
+
19
+ def namespace
20
+ options[:namespace]
21
+ end
22
+
17
23
  def copy_resource_template(template_name)
18
24
  template_file = "#{template_name}.html.erb"
19
25
 
20
26
  copy_file(
21
27
  template_file,
22
- "app/views/admin/#{resource_path}/#{template_file}",
28
+ "app/views/#{namespace}/#{resource_path}/#{template_file}",
23
29
  )
24
30
  end
25
31
 
data/lib/administrate.rb CHANGED
@@ -1,4 +1,23 @@
1
1
  require "administrate/engine"
2
2
 
3
3
  module Administrate
4
+ def self.warn_of_missing_resource_class
5
+ ActiveSupport::Deprecation.warn(
6
+ "Calling Field::Base.permitted_attribute without the option " +
7
+ ":resource_class is deprecated. If you are seeing this " +
8
+ "message, you are probably using a custom field type that" +
9
+ "does this. Please make sure to update it to a version that " +
10
+ "does not use a deprecated API",
11
+ )
12
+ end
13
+
14
+ def self.warn_of_deprecated_option(name)
15
+ ActiveSupport::Deprecation.warn(
16
+ "The option :#{name} is deprecated. " +
17
+ "Administrate should detect it automatically. " +
18
+ "Please file an issue at " +
19
+ "https://github.com/thoughtbot/administrate/issues " +
20
+ "if you think otherwise.",
21
+ )
22
+ end
4
23
  end
@@ -5,18 +5,21 @@ module Administrate
5
5
  class DashboardGenerator < Rails::Generators::NamedBase
6
6
  ATTRIBUTE_TYPE_MAPPING = {
7
7
  boolean: "Field::Boolean",
8
- date: "Field::DateTime",
8
+ date: "Field::Date",
9
9
  datetime: "Field::DateTime",
10
- enum: "Field::String",
10
+ enum: "Field::Select",
11
11
  float: "Field::Number",
12
12
  integer: "Field::Number",
13
13
  time: "Field::Time",
14
14
  text: "Field::Text",
15
15
  string: "Field::String",
16
+ uuid: "Field::String",
16
17
  }
17
18
 
18
19
  ATTRIBUTE_OPTIONS_MAPPING = {
19
- enum: { searchable: false },
20
+ # procs must be defined in one line!
21
+ enum: { searchable: false,
22
+ collection: ->(field) { field.resource.class.send(field.attribute.to_s.pluralize).keys } },
20
23
  float: { decimals: 2 },
21
24
  }
22
25
 
@@ -107,11 +110,11 @@ module Administrate
107
110
  if relationship.has_one?
108
111
  "Field::HasOne"
109
112
  elsif relationship.collection?
110
- "Field::HasMany" + relationship_options_string(relationship)
113
+ "Field::HasMany"
111
114
  elsif relationship.polymorphic?
112
115
  "Field::Polymorphic"
113
116
  else
114
- "Field::BelongsTo" + relationship_options_string(relationship)
117
+ "Field::BelongsTo"
115
118
  end
116
119
  end
117
120
 
@@ -119,14 +122,6 @@ module Administrate
119
122
  @klass ||= Object.const_get(class_name)
120
123
  end
121
124
 
122
- def relationship_options_string(relationship)
123
- if relationship.class_name != relationship.name.to_s.classify
124
- options_string(class_name: relationship.class_name)
125
- else
126
- ""
127
- end
128
- end
129
-
130
125
  def options_string(options)
131
126
  if options.any?
132
127
  ".with_options(#{inspect_hash_as_ruby(options)})"
@@ -136,7 +131,16 @@ module Administrate
136
131
  end
137
132
 
138
133
  def inspect_hash_as_ruby(hash)
139
- hash.map { |key, value| "#{key}: #{value.inspect}" }.join(", ")
134
+ hash.map do |key, value|
135
+ v_str = value.respond_to?(:call) ? proc_string(value) : value.inspect
136
+ "#{key}: #{v_str}"
137
+ end.join(", ")
138
+ end
139
+
140
+ def proc_string(value)
141
+ source = value.source_location
142
+ proc_string = IO.readlines(source.first)[source.second - 1]
143
+ proc_string[/->[^}]*} | (lambda|proc).*end/x]
140
144
  end
141
145
  end
142
146
  end
@@ -4,9 +4,8 @@ module <%= namespace.classify %>
4
4
  # For example, you may want to send an email after a foo is updated.
5
5
  #
6
6
  # def update
7
- # foo = Foo.find(params[:id])
8
- # foo.update(params[:foo])
9
- # send_foo_updated_email
7
+ # super
8
+ # send_foo_updated_email(requested_resource)
10
9
  # end
11
10
 
12
11
  # Override this method to specify custom lookup behavior.
@@ -17,15 +16,28 @@ module <%= namespace.classify %>
17
16
  # Foo.find_by!(slug: param)
18
17
  # end
19
18
 
19
+ # The result of this lookup will be available as `requested_resource`
20
+
20
21
  # Override this if you have certain roles that require a subset
21
22
  # this will be used to set the records shown on the `index` action.
22
23
  #
23
24
  # def scoped_resource
24
- # if current_user.super_admin?
25
- # resource_class
26
- # else
27
- # resource_class.with_less_stuff
28
- # end
25
+ # if current_user.super_admin?
26
+ # resource_class
27
+ # else
28
+ # resource_class.with_less_stuff
29
+ # end
30
+ # end
31
+
32
+ # Override `resource_params` if you want to transform the submitted
33
+ # data before it's persisted. For example, the following would turn all
34
+ # empty values into nil values. It uses other APIs such as `resource_class`
35
+ # and `dashboard`:
36
+ #
37
+ # def resource_params
38
+ # params.require(resource_class.model_name.param_key).
39
+ # permit(dashboard.permitted_attributes).
40
+ # transform_values { |value| value == "" ? nil : value }
29
41
  # end
30
42
 
31
43
  # See https://administrate-prototype.herokuapp.com/customizing_controller_actions
@@ -21,7 +21,7 @@ class <%= class_name %>Dashboard < Administrate::BaseDashboard
21
21
  COLLECTION_ATTRIBUTES = %i[
22
22
  <%=
23
23
  attributes.first(COLLECTION_ATTRIBUTE_LIMIT).map do |attr|
24
- " #{attr}"
24
+ " #{attr}"
25
25
  end.join("\n")
26
26
  %>
27
27
  ].freeze
@@ -31,7 +31,7 @@ class <%= class_name %>Dashboard < Administrate::BaseDashboard
31
31
  SHOW_PAGE_ATTRIBUTES = %i[
32
32
  <%=
33
33
  attributes.map do |attr|
34
- " #{attr}"
34
+ " #{attr}"
35
35
  end.join("\n")
36
36
  %>
37
37
  ].freeze
@@ -42,7 +42,7 @@ class <%= class_name %>Dashboard < Administrate::BaseDashboard
42
42
  FORM_ATTRIBUTES = %i[
43
43
  <%=
44
44
  form_attributes.map do |attr|
45
- " #{attr}"
45
+ " #{attr}"
46
46
  end.join("\n")
47
47
  %>
48
48
  ].freeze
@@ -55,7 +55,7 @@ class <%= class_name %>Dashboard < Administrate::BaseDashboard
55
55
  # in the search field:
56
56
  #
57
57
  # COLLECTION_FILTERS = {
58
- # open: ->(resources) { where(open: true) }
58
+ # open: ->(resources) { resources.where(open: true) }
59
59
  # }.freeze
60
60
  COLLECTION_FILTERS = {}.freeze
61
61