madmin 1.2.0 → 1.2.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (63) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +25 -0
  3. data/app/controllers/madmin/base_controller.rb +0 -5
  4. data/app/controllers/madmin/resource_controller.rb +20 -1
  5. data/app/helpers/madmin/application_helper.rb +4 -0
  6. data/app/helpers/madmin/nav_helper.rb +30 -0
  7. data/app/helpers/madmin/sort_helper.rb +3 -2
  8. data/app/views/layouts/madmin/application.html.erb +5 -5
  9. data/app/views/madmin/application/_form.html.erb +6 -8
  10. data/app/views/madmin/application/_javascript.html.erb +34 -3
  11. data/app/views/madmin/application/_navigation.html.erb +31 -5
  12. data/app/views/madmin/application/edit.html.erb +5 -1
  13. data/app/views/madmin/application/index.html.erb +34 -22
  14. data/app/views/madmin/application/new.html.erb +5 -1
  15. data/app/views/madmin/application/show.html.erb +24 -17
  16. data/app/views/madmin/fields/attachment/_form.html.erb +3 -1
  17. data/app/views/madmin/fields/attachment/_show.html.erb +7 -1
  18. data/app/views/madmin/fields/attachments/_form.html.erb +3 -1
  19. data/app/views/madmin/fields/attachments/_show.html.erb +7 -3
  20. data/app/views/madmin/fields/belongs_to/_form.html.erb +4 -2
  21. data/app/views/madmin/fields/belongs_to/_show.html.erb +1 -1
  22. data/app/views/madmin/fields/boolean/_form.html.erb +3 -1
  23. data/app/views/madmin/fields/date/_form.html.erb +3 -1
  24. data/app/views/madmin/fields/date_time/_form.html.erb +3 -1
  25. data/app/views/madmin/fields/decimal/_form.html.erb +3 -1
  26. data/app/views/madmin/fields/enum/_form.html.erb +4 -2
  27. data/app/views/madmin/fields/float/_form.html.erb +3 -1
  28. data/app/views/madmin/fields/has_many/_form.html.erb +4 -2
  29. data/app/views/madmin/fields/has_many/_show.html.erb +1 -1
  30. data/app/views/madmin/fields/has_one/_form.html.erb +3 -2
  31. data/app/views/madmin/fields/integer/_form.html.erb +3 -1
  32. data/app/views/madmin/fields/json/_form.html.erb +3 -1
  33. data/app/views/madmin/fields/nested_has_many/_form.html.erb +3 -1
  34. data/app/views/madmin/fields/nested_has_many/_show.html.erb +1 -1
  35. data/app/views/madmin/fields/password/_form.html.erb +3 -1
  36. data/app/views/madmin/fields/polymorphic/_form.html.erb +3 -1
  37. data/app/views/madmin/fields/polymorphic/_show.html.erb +1 -1
  38. data/app/views/madmin/fields/rich_text/_form.html.erb +3 -1
  39. data/app/views/madmin/fields/string/_form.html.erb +3 -1
  40. data/app/views/madmin/fields/text/_form.html.erb +3 -1
  41. data/app/views/madmin/fields/time/_form.html.erb +3 -1
  42. data/app/views/madmin/shared/_label.html.erb +4 -0
  43. data/lib/generators/madmin/field/field_generator.rb +31 -0
  44. data/lib/generators/madmin/field/templates/_form.html.erb +2 -0
  45. data/lib/generators/madmin/field/templates/_index.html.erb +1 -0
  46. data/lib/generators/madmin/field/templates/_show.html.erb +1 -0
  47. data/lib/generators/madmin/field/templates/field.rb.tt +26 -0
  48. data/lib/generators/madmin/install/install_generator.rb +6 -1
  49. data/lib/generators/madmin/install/templates/routes.rb.tt +3 -0
  50. data/lib/generators/madmin/resource/resource_generator.rb +1 -1
  51. data/lib/generators/madmin/resource/templates/controller.rb.tt +6 -0
  52. data/lib/madmin/engine.rb +1 -1
  53. data/lib/madmin/field.rb +4 -0
  54. data/lib/madmin/fields/belongs_to.rb +9 -6
  55. data/lib/madmin/fields/has_many.rb +10 -6
  56. data/lib/madmin/fields/string.rb +3 -0
  57. data/lib/madmin/fields/text.rb +3 -0
  58. data/lib/madmin/generator_helpers.rb +22 -4
  59. data/lib/madmin/resource.rb +19 -13
  60. data/lib/madmin/search.rb +60 -0
  61. data/lib/madmin/version.rb +1 -1
  62. data/lib/madmin.rb +19 -5
  63. metadata +14 -5
@@ -1,2 +1,4 @@
1
- <%= form.label field.attribute_name, class: "inline-block w-32 flex-shrink-0" %>
1
+ <div class="block md:inline-block md:w-32 flex-shrink-0 text-gray-700">
2
+ <%= render "madmin/shared/label", form: form, field: field %>
3
+ </div>
2
4
  <%= form.file_field field.to_param %>
@@ -1,3 +1,9 @@
1
1
  <% if (attachment = field.value(record)) && attachment.attached? %>
2
- <%= link_to attachment.filename, attachment, target: :_blank %>
2
+ <% if attachment.variable? %>
3
+ <%= link_to attachment, target: :_blank do %>
4
+ <%= image_tag attachment, class: "max-h-32" %>
5
+ <% end %>
6
+ <% else %>
7
+ <%= link_to attachment.filename, attachment, target: :_blank, class: "text-indigo-500 underline" %>
8
+ <% end %>
3
9
  <% end %>
@@ -1,2 +1,4 @@
1
- <%= form.label field.attribute_name, class: "inline-block w-32 flex-shrink-0" %>
1
+ <div class="block md:inline-block md:w-32 flex-shrink-0 text-gray-700">
2
+ <%= render "madmin/shared/label", form: form, field: field %>
3
+ </div>
2
4
  <%= form.file_field field.attribute_name, multiple: true %>
@@ -1,7 +1,11 @@
1
1
  <% if (attachments = field.value(record)) && attachments.attached? %>
2
2
  <% attachments.each do |attachment| %>
3
- <div>
4
- <%= link_to attachment.filename, attachment, target: :_blank %>
5
- </div>
3
+ <% if attachment.variable? %>
4
+ <%= link_to attachment, target: :_blank do %>
5
+ <%= image_tag attachment, class: "max-h-32" %>
6
+ <% end %>
7
+ <% else %>
8
+ <%= link_to attachment.filename, attachment, target: :_blank, class: "text-indigo-500 underline" %>
9
+ <% end %>
6
10
  <% end %>
7
11
  <% end %>
@@ -1,2 +1,4 @@
1
- <%= form.label field.attribute_name, class: "inline-block w-32 flex-shrink-0" %>
2
- <%= form.select field.to_param, field.options_for_select(record), { prompt: true }, { class: "form-select", data: { controller: "slimselect", data: { controller: "slimselect" } } } %>
1
+ <div class="block md:inline-block md:w-32 flex-shrink-0 text-gray-700">
2
+ <%= render "madmin/shared/label", form: form, field: field %>
3
+ </div>
4
+ <%= form.select field.to_param, field.options_for_select(record), { prompt: true }, { class: "form-select", data: { controller: "select", select_url_value: field.index_path } } %>
@@ -1,3 +1,3 @@
1
1
  <% if (object = field.value(record)) %>
2
- <%= link_to Madmin.resource_for(object).display_name(object), Madmin.resource_for(object).show_path(object) %>
2
+ <%= link_to Madmin.resource_for(object).display_name(object), Madmin.resource_for(object).show_path(object), class: "text-indigo-500 underline" %>
3
3
  <% end %>
@@ -1,2 +1,4 @@
1
- <%= form.label field.attribute_name, class: "inline-block w-32 flex-shrink-0" %>
1
+ <div class="block md:inline-block md:w-32 flex-shrink-0 text-gray-700">
2
+ <%= render "madmin/shared/label", form: form, field: field %>
3
+ </div>
2
4
  <%= form.check_box field.attribute_name, class: "form-input" %>
@@ -1,2 +1,4 @@
1
- <%= form.label field.attribute_name, class: "inline-block w-32 flex-shrink-0" %>
1
+ <div class="block md:inline-block md:w-32 flex-shrink-0 text-gray-700">
2
+ <%= render "madmin/shared/label", form: form, field: field %>
3
+ </div>
2
4
  <%= form.text_field field.attribute_name, { class: "form-select", data: { controller: "flatpickr" } } %>
@@ -1,2 +1,4 @@
1
- <%= form.label field.attribute_name, class: "inline-block w-32 flex-shrink-0" %>
1
+ <div class="block md:inline-block md:w-32 flex-shrink-0 text-gray-700">
2
+ <%= render "madmin/shared/label", form: form, field: field %>
3
+ </div>
2
4
  <%= form.text_field field.attribute_name, data: { controller: "flatpickr", flatpickr_enable_time: true } %>
@@ -1,2 +1,4 @@
1
- <%= form.label field.attribute_name, class: "inline-block w-32 flex-shrink-0" %>
1
+ <div class="block md:inline-block md:w-32 flex-shrink-0 text-gray-700">
2
+ <%= render "madmin/shared/label", form: form, field: field %>
3
+ </div>
2
4
  <%= form.number_field field.attribute_name, step: :any, class: "form-input" %>
@@ -1,2 +1,4 @@
1
- <%= form.label field.attribute_name, class: "inline-block w-32 flex-shrink-0" %>
2
- <%= form.select field.attribute_name, field.options_for_select(record), { prompt: true }, { class: "form-select" } %>
1
+ <div class="block md:inline-block md:w-32 flex-shrink-0 text-gray-700">
2
+ <%= render "madmin/shared/label", form: form, field: field %>
3
+ </div>
4
+ <%= form.select field.attribute_name, field.options_for_select(record), { prompt: true }, { class: "form-select", data: { controller: "select" } } %>
@@ -1,2 +1,4 @@
1
- <%= form.label field.attribute_name, class: "inline-block w-32 flex-shrink-0" %>
1
+ <div class="block md:inline-block md:w-32 flex-shrink-0 text-gray-700">
2
+ <%= render "madmin/shared/label", form: form, field: field %>
3
+ </div>
2
4
  <%= form.number_field field.attribute_name, step: :any, class: "form-input" %>
@@ -1,2 +1,4 @@
1
- <%= form.label field.attribute_name, class: "inline-block w-32 flex-shrink-0" %>
2
- <%= form.select "#{field.attribute_name.to_s.singularize}_ids", field.options_for_select(record), { prompt: true }, { multiple: true, class: "form-select", data: { controller: "slimselect" } } %>
1
+ <div class="block md:inline-block md:w-32 flex-shrink-0 text-gray-700">
2
+ <%= render "madmin/shared/label", form: form, field: field %>
3
+ </div>
4
+ <%= form.select "#{field.attribute_name.to_s.singularize}_ids", field.options_for_select(record), { prompt: true }, { multiple: true, class: "form-select", data: { controller: "select", select_url_value: field.index_path } } %>
@@ -1,5 +1,5 @@
1
1
  <% field.value(record).each do |object| %>
2
2
  <div>
3
- <%= link_to Madmin.resource_for(object).display_name(object), Madmin.resource_for(object).show_path(object) %>
3
+ <%= link_to Madmin.resource_for(object).display_name(object), Madmin.resource_for(object).show_path(object), class: "text-indigo-500 underline" %>
4
4
  </div>
5
5
  <% end %>
@@ -1,3 +1,4 @@
1
- HAS ONE
2
- <%= form.label field.attribute_name, class: "inline-block w-32 flex-shrink-0" %>
1
+ <div class="block md:inline-block md:w-32 flex-shrink-0 text-gray-700">
2
+ <%= render "madmin/shared/label", form: form, field: field %>
3
+ </div>
3
4
  <%= field.value(record) %>
@@ -1,2 +1,4 @@
1
- <%= form.label field.attribute_name, class: "inline-block w-32 flex-shrink-0" %>
1
+ <div class="block md:inline-block md:w-32 flex-shrink-0 text-gray-700">
2
+ <%= render "madmin/shared/label", form: form, field: field %>
3
+ </div>
2
4
  <%= form.number_field field.attribute_name, class: "form-input" %>
@@ -1,2 +1,4 @@
1
- <%= form.label field.attribute_name, class: "inline-block w-32 flex-shrink-0" %>
1
+ <div class="block md:inline-block md:w-32 flex-shrink-0 text-gray-700">
2
+ <%= render "madmin/shared/label", form: form, field: field %>
3
+ </div>
2
4
  <%= form.text_area field.attribute_name, class: "form-input" %>
@@ -1,4 +1,6 @@
1
- <%= form.label field.attribute_name, class: "inline-block w-32 flex-shrink-0" %>
1
+ <div class="block md:inline-block w-32 flex-shrink-0">
2
+ <%= render "madmin/shared/label", form: form, field: field %>
3
+ </div>
2
4
 
3
5
  <div class="container space-y-8" data-controller="nested-form">
4
6
  <template data-target="nested-form.template">
@@ -1,5 +1,5 @@
1
1
  <% field.value(record).each do |object| %>
2
2
  <div>
3
- <%= link_to Madmin.resource_for(object).display_name(object), Madmin.resource_for(object).show_path(object) %>
3
+ <%= link_to Madmin.resource_for(object).display_name(object), Madmin.resource_for(object).show_path(object), class: "text-indigo-500 underline" %>
4
4
  </div>
5
5
  <% end %>
@@ -1,2 +1,4 @@
1
- <%= form.label field.attribute_name, class: "inline-block w-32 flex-shrink-0" %>
1
+ <div class="block md:inline-block md:w-32 flex-shrink-0 text-gray-700">
2
+ <%= render "madmin/shared/label", form: form, field: field %>
3
+ </div>
2
4
  <%= form.password_field field.attribute_name, class: "form-input" %>
@@ -1,5 +1,7 @@
1
1
  <%= form.fields_for field.attribute_name do |pf| %>
2
- <%= pf.label field.attribute_name, class: "inline-block w-32 flex-shrink-0" %>
2
+ <div class="block md:inline-block md:w-32 flex-shrink-0 text-gray-700">
3
+ <%= render "madmin/shared/label", form: pf, field: field %>
4
+ </div>
3
5
  <%= pf.select :value, field.options_for_select(record).map(&:to_global_id), { selected: field.value(record)&.to_global_id, prompt: true }, { class: "form-select", data: { controller: "slimselect" } } %>
4
6
  <%= pf.hidden_field :type, value: "polymorphic" %>
5
7
  <% end %>
@@ -1,3 +1,3 @@
1
1
  <% if (object = field.value(record)) %>
2
- <%= link_to Madmin.resource_for(object).display_name(object), Madmin.resource_for(object).show_path(object) %>
2
+ <%= link_to Madmin.resource_for(object).display_name(object), Madmin.resource_for(object).show_path(object), class: "text-indigo-500 underline" %>
3
3
  <% end %>
@@ -1,4 +1,6 @@
1
- <%= form.label field.attribute_name, class: "inline-block w-32 flex-shrink-0" %>
1
+ <div class="block md:inline-block md:w-32 flex-shrink-0 text-gray-700">
2
+ <%= render "madmin/shared/label", form: form, field: field %>
3
+ </div>
2
4
  <div class="flex-1">
3
5
  <%= form.rich_text_area field.attribute_name, class: "form-input block" %>
4
6
  </div>
@@ -1,2 +1,4 @@
1
- <%= form.label field.attribute_name, class: "inline-block w-32 flex-shrink-0" %>
1
+ <div class="block md:inline-block md:w-32 flex-shrink-0 text-gray-700">
2
+ <%= render "madmin/shared/label", form: form, field: field %>
3
+ </div>
2
4
  <%= form.text_field field.attribute_name, class: "form-input" %>
@@ -1,2 +1,4 @@
1
- <%= form.label field.attribute_name, class: "inline-block w-32 flex-shrink-0" %>
1
+ <div class="block md:inline-block md:w-32 flex-shrink-0 text-gray-700">
2
+ <%= render "madmin/shared/label", form: form, field: field %>
3
+ </div>
2
4
  <%= form.text_area field.attribute_name, class: "form-input" %>
@@ -1,2 +1,4 @@
1
- <%= form.label field.attribute_name, class: "inline-block w-32 flex-shrink-0" %>
1
+ <div class="block md:inline-block md:w-32 flex-shrink-0 text-gray-700">
2
+ <%= render "madmin/shared/label", form: form, field: field %>
3
+ </div>
2
4
  <%= form.time_select field.attribute_name, {}, { class: "form-select" } %>
@@ -0,0 +1,4 @@
1
+ <%= form.label field.attribute_name %>
2
+ <% if field.required? %>
3
+ <span class="text-red-500">*</span>
4
+ <% end %>
@@ -0,0 +1,31 @@
1
+ module Madmin
2
+ module Generators
3
+ class FieldGenerator < Rails::Generators::NamedBase
4
+ include Madmin::GeneratorHelpers
5
+
6
+ source_root File.expand_path("../templates", __FILE__)
7
+
8
+ def eager_load
9
+ Rails.application.eager_load!
10
+ end
11
+
12
+ def generate_field
13
+ template "field.rb", "app/madmin/fields/#{file_path}_field.rb"
14
+ copy_resource_template "_form"
15
+ copy_resource_template "_index"
16
+ copy_resource_template "_show"
17
+ end
18
+
19
+ private
20
+
21
+ def copy_resource_template(template_name)
22
+ template_file = "#{template_name}.html.erb"
23
+
24
+ copy_file(
25
+ template_file,
26
+ "app/views/madmin/fields/#{file_path}_field/#{template_file}"
27
+ )
28
+ end
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,2 @@
1
+ <%= form.label field.attribute_name, class: "inline-block w-32 flex-shrink-0" %>
2
+ <%= form.text_field field.attribute_name, class: "form-input" %>
@@ -0,0 +1 @@
1
+ <%= field.value(record) %>
@@ -0,0 +1 @@
1
+ <%= field.value(record) %>
@@ -0,0 +1,26 @@
1
+ class <%= class_name %>Field < Madmin::Field
2
+ # def value(record)
3
+ # record.public_send(attribute_name)
4
+ # end
5
+
6
+ # def to_partial_path(name)
7
+ # unless %w[index show form].include? name
8
+ # raise ArgumentError, "`partial` must be 'index', 'show', or 'form'"
9
+ # end
10
+ #
11
+ # "/madmin/fields/#{self.class.field_type}/#{name}"
12
+ # end
13
+
14
+ # def to_param
15
+ # attribute_name
16
+ # end
17
+
18
+ # # Used for checking visibility of attribute on an view
19
+ # def visible?(action, default: true)
20
+ # options.fetch(action.to_sym, default)
21
+ # end
22
+
23
+ # def required?
24
+ # model.validators_on(attribute_name).any? { |v| v.is_a? ActiveModel::Validations::PresenceValidator }
25
+ # end
26
+ end
@@ -16,8 +16,13 @@ module Madmin
16
16
  end
17
17
 
18
18
  def generate_routes
19
+ if rails6_1_and_up?
20
+ route "draw :madmin", file: ROUTES_FILE[:default]
21
+ template("routes.rb.tt", "config/routes/madmin.rb")
22
+ end
23
+
19
24
  if route_namespace_exists?
20
- route "root to: \"dashboard#show\"", indentation: 4, sentinel: /namespace :madmin do\s*\n/m
25
+ route "root to: \"dashboard#show\"", indentation: separated_routes_file? ? 2 : 4, sentinel: /namespace :madmin do\s*\n/m
21
26
  else
22
27
  route "root to: \"dashboard#show\"", namespace: [:madmin]
23
28
  end
@@ -0,0 +1,3 @@
1
+ # Below are the routes for madmin
2
+ namespace :madmin do
3
+ end
@@ -20,7 +20,7 @@ module Madmin
20
20
 
21
21
  def generate_route
22
22
  if route_namespace_exists?
23
- route "resources :#{plural_name}", namespace: class_path, indentation: 4, sentinel: /namespace :madmin do\s*\n/m
23
+ route "resources :#{plural_name}", namespace: class_path, indentation: separated_routes_file? ? 2 : 4, sentinel: /namespace :madmin do\s*\n/m
24
24
  else
25
25
  route "resources :#{plural_name}", namespace: [:madmin] + class_path
26
26
  end
@@ -1,4 +1,10 @@
1
1
  module Madmin
2
2
  class <%= class_name.pluralize %>Controller < Madmin::ResourceController
3
+ <% if class_name == "ActiveStorage::Blob" -%>
4
+ def new
5
+ super
6
+ @record.assign_attributes(filename: "")
7
+ end
8
+ <% end -%>
3
9
  end
4
10
  end
data/lib/madmin/engine.rb CHANGED
@@ -6,7 +6,7 @@ module Madmin
6
6
  end
7
7
 
8
8
  config.to_prepare do
9
- Madmin.resources = []
9
+ Madmin.reset_resources!
10
10
  end
11
11
  end
12
12
  end
data/lib/madmin/field.rb CHANGED
@@ -36,5 +36,9 @@ module Madmin
36
36
  def required?
37
37
  model.validators_on(attribute_name).any? { |v| v.is_a? ActiveModel::Validations::PresenceValidator }
38
38
  end
39
+
40
+ def searchable?
41
+ false
42
+ end
39
43
  end
40
44
  end
@@ -2,18 +2,21 @@ module Madmin
2
2
  module Fields
3
3
  class BelongsTo < Field
4
4
  def options_for_select(record)
5
- association = record.class.reflect_on_association(attribute_name)
6
- klass = association.klass
7
- resource = nil
8
- klass.all.map do |r|
9
- resource ||= Madmin.resource_for(r)
10
- [resource.display_name(r), r.id]
5
+ if (record = record.send(attribute_name))
6
+ resource = Madmin.resource_for(record)
7
+ [[resource.display_name(record), record.id]]
8
+ else
9
+ []
11
10
  end
12
11
  end
13
12
 
14
13
  def to_param
15
14
  "#{attribute_name}_id"
16
15
  end
16
+
17
+ def index_path
18
+ Madmin.resource_by_name(model.reflect_on_association(attribute_name).klass).index_path(format: :json)
19
+ end
17
20
  end
18
21
  end
19
22
  end
@@ -2,18 +2,22 @@ module Madmin
2
2
  module Fields
3
3
  class HasMany < Field
4
4
  def options_for_select(record)
5
- association = record.class.reflect_on_association(attribute_name)
6
- klass = association.klass
7
- resource = nil
8
- klass.all.map do |r|
9
- resource ||= Madmin.resource_for(r)
10
- [resource.display_name(r), r.id]
5
+ if (records = record.send(attribute_name))
6
+ return [] unless records.first
7
+ resource = Madmin.resource_for(records.first)
8
+ records.map { |record| [resource.display_name(record), record.id] }
9
+ else
10
+ []
11
11
  end
12
12
  end
13
13
 
14
14
  def to_param
15
15
  {"#{attribute_name.to_s.singularize}_ids".to_sym => []}
16
16
  end
17
+
18
+ def index_path
19
+ Madmin.resource_by_name(model.reflect_on_association(attribute_name).klass).index_path(format: :json)
20
+ end
17
21
  end
18
22
  end
19
23
  end
@@ -1,6 +1,9 @@
1
1
  module Madmin
2
2
  module Fields
3
3
  class String < Field
4
+ def searchable?
5
+ options.fetch(:searchable, model.column_names.include?(attribute_name.to_s))
6
+ end
4
7
  end
5
8
  end
6
9
  end
@@ -1,6 +1,9 @@
1
1
  module Madmin
2
2
  module Fields
3
3
  class Text < Field
4
+ def searchable?
5
+ options.fetch(:searchable, model.column_names.include?(attribute_name.to_s))
6
+ end
4
7
  end
5
8
  end
6
9
  end
@@ -1,24 +1,30 @@
1
1
  module Madmin
2
2
  module GeneratorHelpers
3
+ ROUTES_FILE = {default: "config/routes.rb", separated: "config/routes/madmin.rb"}.freeze
4
+
3
5
  def call_generator(generator, *args)
4
6
  Rails::Generators.invoke(generator, args, generator_options)
5
7
  end
6
8
 
7
9
  def route_namespace_exists?
8
- File.readlines(Rails.root.join("config/routes.rb")).grep(/namespace :madmin/).size > 0
10
+ File.readlines(Rails.root.join(default_routes_file)).grep(/namespace :madmin/).size > 0
11
+ end
12
+
13
+ def rails6_1_and_up?
14
+ Gem.loaded_specs["rails"].version >= Gem::Version.new(6.1)
9
15
  end
10
16
 
11
17
  # Method copied from Rails 6.1 master
12
- def route(routing_code, namespace: nil, sentinel: nil, indentation: 2)
18
+ def route(routing_code, namespace: nil, sentinel: nil, indentation: 2, file: default_routes_file)
13
19
  routing_code = Array(namespace).reverse.reduce(routing_code) { |code, ns|
14
20
  "namespace :#{ns} do\n#{indent(code, 2)}\nend"
15
21
  }
16
22
 
17
23
  log :route, routing_code
18
- sentinel ||= /\.routes\.draw do\s*\n/m
24
+ sentinel ||= default_sentinel(file)
19
25
 
20
26
  in_root do
21
- inject_into_file "config/routes.rb", optimize_indentation(routing_code, indentation), after: sentinel, verbose: false, force: false
27
+ inject_into_file file, optimize_indentation(routing_code, indentation), after: sentinel, verbose: false, force: false
22
28
  end
23
29
  end
24
30
 
@@ -30,6 +36,18 @@ module Madmin
30
36
 
31
37
  private
32
38
 
39
+ def separated_routes_file?
40
+ default_routes_file.eql?(ROUTES_FILE[:separated])
41
+ end
42
+
43
+ def default_sentinel(file)
44
+ file.eql?(ROUTES_FILE[:default]) ? /\.routes\.draw do\s*\n/m : /namespace :madmin do\s*\n/m
45
+ end
46
+
47
+ def default_routes_file
48
+ rails6_1_and_up? ? ROUTES_FILE[:separated] : ROUTES_FILE[:default]
49
+ end
50
+
33
51
  def generator_options
34
52
  {behavior: behavior}
35
53
  end