madmin 1.1.0 → 1.2.3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (67) 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 +22 -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 +32 -0
  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 +35 -4
  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 +48 -36
  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/_fields.html.erb +2 -3
  34. data/app/views/madmin/fields/nested_has_many/_form.html.erb +3 -1
  35. data/app/views/madmin/fields/nested_has_many/_show.html.erb +1 -1
  36. data/app/views/madmin/fields/password/_form.html.erb +3 -1
  37. data/app/views/madmin/fields/polymorphic/_form.html.erb +3 -1
  38. data/app/views/madmin/fields/polymorphic/_show.html.erb +1 -1
  39. data/app/views/madmin/fields/rich_text/_form.html.erb +3 -1
  40. data/app/views/madmin/fields/string/_form.html.erb +3 -1
  41. data/app/views/madmin/fields/text/_form.html.erb +3 -1
  42. data/app/views/madmin/fields/time/_form.html.erb +3 -1
  43. data/app/views/madmin/shared/_label.html.erb +4 -0
  44. data/lib/generators/madmin/field/field_generator.rb +31 -0
  45. data/lib/generators/madmin/field/templates/_form.html.erb +2 -0
  46. data/lib/generators/madmin/field/templates/_index.html.erb +1 -0
  47. data/lib/generators/madmin/field/templates/_show.html.erb +1 -0
  48. data/lib/generators/madmin/field/templates/field.rb.tt +26 -0
  49. data/lib/generators/madmin/install/install_generator.rb +6 -1
  50. data/lib/generators/madmin/install/templates/routes.rb.tt +3 -0
  51. data/lib/generators/madmin/resource/resource_generator.rb +9 -59
  52. data/lib/generators/madmin/resource/templates/controller.rb.tt +6 -0
  53. data/lib/generators/madmin/resource/templates/resource.rb.tt +10 -1
  54. data/lib/madmin/engine.rb +4 -4
  55. data/lib/madmin/field.rb +4 -0
  56. data/lib/madmin/fields/belongs_to.rb +9 -6
  57. data/lib/madmin/fields/has_many.rb +10 -6
  58. data/lib/madmin/fields/nested_has_many.rb +1 -1
  59. data/lib/madmin/fields/string.rb +3 -0
  60. data/lib/madmin/fields/text.rb +3 -0
  61. data/lib/madmin/generator_helpers.rb +22 -4
  62. data/lib/madmin/resource.rb +46 -22
  63. data/lib/madmin/resource_builder.rb +80 -0
  64. data/lib/madmin/search.rb +60 -0
  65. data/lib/madmin/version.rb +1 -1
  66. data/lib/madmin.rb +20 -5
  67. metadata +16 -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.text_area field.attribute_name, class: "form-input" %>
@@ -1,5 +1,5 @@
1
- <%= content_tag :div, class: "nested-fields bg-gray-100 rounded-t-xl p-5", data: { new_record: f.object.new_record? } do %>
2
- <% field.nested_attributes.each do |nested_attribute| %>
1
+ <%= content_tag :div, class: "nested-fields border border-gray-200 rounded-lg p-5", data: { new_record: f.object.new_record? } do %>
2
+ <% field.nested_attributes.each do |name, nested_attribute| %>
3
3
  <% next if nested_attribute[:field].nil? %>
4
4
  <% next unless nested_attribute[:field].visible?(action_name) %>
5
5
  <% next unless nested_attribute[:field].visible?(:form) %>
@@ -14,5 +14,4 @@
14
14
  <small><%= link_to "Remove", "#", data: { action: "click->nested-form#remove_association" } %></small>
15
15
 
16
16
  <%= f.hidden_field :_destroy %>
17
-
18
17
  <% 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 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
@@ -28,69 +28,19 @@ module Madmin
28
28
 
29
29
  private
30
30
 
31
- def associations
32
- model.reflections.reject { |name, association|
33
- # Hide these special associations
34
- name.starts_with?("rich_text") ||
35
- name.ends_with?("_attachment") ||
36
- name.ends_with?("_attachments") ||
37
- name.ends_with?("_blob") ||
38
- name.ends_with?("_blobs")
39
- }.keys
31
+ def model
32
+ @model ||= class_name.constantize
40
33
  end
41
34
 
42
- def attributes
43
- model.attribute_names + virtual_attributes - redundant_attributes
35
+ def resource_builder
36
+ @resource_builder ||= ResourceBuilder.new(model)
44
37
  end
45
38
 
46
- def virtual_attributes
47
- virtual = []
48
-
49
- # has_secure_password columns
50
- password_attributes = model.attribute_types.keys.select { |k| k.ends_with?("_digest") }.map { |k| k.delete_suffix("_digest") }
51
- virtual += password_attributes.map { |attr| [attr, "#{attr}_confirmation"] }.flatten
52
-
53
- # Add virtual attributes for ActionText and ActiveStorage
54
- model.reflections.each do |name, association|
55
- if name.starts_with?("rich_text")
56
- virtual << name.split("rich_text_").last
57
- elsif name.ends_with?("_attachment")
58
- virtual << name.split("_attachment").first
59
- elsif name.ends_with?("_attachments")
60
- virtual << name.split("_attachments").first
61
- end
62
- end
63
-
64
- virtual
39
+ def model_attributes
40
+ resource_builder.attributes
65
41
  end
66
42
 
67
- def redundant_attributes
68
- redundant = []
69
-
70
- # has_secure_password columns
71
- redundant += model.attribute_types.keys.select { |k| k.ends_with?("_digest") }
72
-
73
- model.reflections.each do |name, association|
74
- if association.has_one?
75
- next
76
- elsif association.collection?
77
- next
78
- elsif association.polymorphic?
79
- redundant << "#{name}_id"
80
- redundant << "#{name}_type"
81
- elsif name.starts_with?("rich_text")
82
- redundant << name
83
- else # belongs to
84
- redundant << "#{name}_id"
85
- end
86
- end
87
-
88
- redundant
89
- end
90
-
91
- def model
92
- @model ||= class_name.constantize
93
- end
43
+ delegate :associations, :virtual_attributes, :store_accessors, to: :resource_builder
94
44
 
95
45
  def formatted_options_for_attribute(name)
96
46
  options = options_for_attribute(name)
@@ -114,7 +64,7 @@ module Madmin
114
64
  {form: false}
115
65
 
116
66
  # Attributes without a database column
117
- elsif !model.column_names.include?(name)
67
+ elsif !model.column_names.include?(name) && !store_accessors.map(&:to_s).include?(name)
118
68
  {index: false}
119
69
  end
120
70
  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
@@ -1,6 +1,6 @@
1
1
  class <%= class_name %>Resource < Madmin::Resource
2
2
  # Attributes
3
- <% attributes.each do |attribute_name| -%>
3
+ <% model_attributes.each do |attribute_name| -%>
4
4
  attribute :<%= attribute_name %><%= formatted_options_for_attribute(attribute_name) %>
5
5
  <% end -%>
6
6
 
@@ -13,4 +13,13 @@ class <%= class_name %>Resource < Madmin::Resource
13
13
  # def self.display_name(record)
14
14
  # record.name
15
15
  # end
16
+
17
+ # Uncomment this to customize the default sort column and direction.
18
+ # def self.default_sort_column
19
+ # "created_at"
20
+ # end
21
+ #
22
+ # def self.default_sort_direction
23
+ # "desc"
24
+ # end
16
25
  end
data/lib/madmin/engine.rb CHANGED
@@ -1,12 +1,12 @@
1
1
  module Madmin
2
2
  class Engine < ::Rails::Engine
3
- initializer "madmin.autoload", before: :set_autoload_paths do |app|
4
- app.config.paths.add "app/madmin/resources", eager_load: true
5
- app.config.paths.add "app/madmin/fields", eager_load: true
3
+ config.before_configuration do |app|
4
+ app.config.autoload_paths << File.expand_path("app/madmin/resources", Rails.root)
5
+ app.config.autoload_paths << File.expand_path("app/madmin/fields", Rails.root)
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
@@ -3,7 +3,7 @@ module Madmin
3
3
  class NestedHasMany < Field
4
4
  DEFAULT_ATTRIBUTES = %w[_destroy id].freeze
5
5
  def nested_attributes
6
- resource.attributes.reject { |i| skipped_fields.include?(i[:name]) }
6
+ resource.attributes.reject { |name, attribute| skipped_fields.include?(name) }
7
7
  end
8
8
 
9
9
  def resource
@@ -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
@@ -1,14 +1,10 @@
1
1
  module Madmin
2
2
  class Resource
3
- class_attribute :attributes, default: []
3
+ class_attribute :attributes, default: ActiveSupport::OrderedHash.new
4
4
  class_attribute :scopes, default: []
5
5
 
6
6
  class << self
7
7
  def inherited(base)
8
- # Remove any old references
9
- Madmin.resources.delete(base)
10
- Madmin.resources << base
11
-
12
8
  base.attributes = attributes.dup
13
9
  base.scopes = scopes.dup
14
10
  super
@@ -30,11 +26,32 @@ module Madmin
30
26
  scopes << name
31
27
  end
32
28
 
29
+ def get_attribute(name)
30
+ attributes[name]
31
+ end
32
+
33
33
  def attribute(name, type = nil, **options)
34
- attributes << {
34
+ type ||= infer_type(name)
35
+ field = options[:field] || field_for_type(type)
36
+
37
+ attributes[name] = OpenStruct.new(
35
38
  name: name,
36
- field: field_for_type(name, type).new(**options.merge(attribute_name: name, model: model))
37
- }
39
+ type: type,
40
+ field: field.new(**options.merge(attribute_name: name, model: model))
41
+ )
42
+ rescue => e
43
+ builder = ResourceBuilder.new(model)
44
+ raise ArgumentError, <<~MESSAGE
45
+ Madmin couldn't find attribute or association '#{name}' on #{model} model.
46
+
47
+ We searched these attributes and associations:
48
+ #{(builder.attributes + builder.associations).join(", ")}
49
+
50
+ This attribute is defined in a Madmin resource at:
51
+ #{e.backtrace.find { |l| l =~ /_resource.rb/ }}
52
+
53
+ Either add the missing attribute or assocation, or remove this line from your Madmin resource.
54
+ MESSAGE
38
55
  end
39
56
 
40
57
  def friendly_name
@@ -42,7 +59,7 @@ module Madmin
42
59
  end
43
60
 
44
61
  def index_path(options = {})
45
- route_name = "madmin_#{model.table_name}_path"
62
+ route_name = "madmin_#{model.model_name.plural}_path"
46
63
 
47
64
  url_helpers.send(route_name, options)
48
65
  end
@@ -70,7 +87,7 @@ module Madmin
70
87
  end
71
88
 
72
89
  def permitted_params
73
- attributes.map { |a| a[:field].to_param }
90
+ attributes.values.filter_map { |a| a.field.to_param if a.field.visible?(:form) }
74
91
  end
75
92
 
76
93
  def display_name(record)
@@ -81,11 +98,17 @@ module Madmin
81
98
  model.respond_to? :friendly
82
99
  end
83
100
 
84
- private
101
+ def sortable_columns
102
+ model.column_names
103
+ end
85
104
 
86
- def field_for_type(name, type)
87
- type ||= infer_type(name)
105
+ def searchable_attributes
106
+ attributes.values.select { |a| a.field.searchable? }
107
+ end
108
+
109
+ private
88
110
 
111
+ def field_for_type(type)
89
112
  {
90
113
  binary: Fields::String,
91
114
  blob: Fields::Text,
@@ -144,15 +167,6 @@ module Madmin
144
167
  rich_text: Fields::RichText,
145
168
  nested_has_many: Fields::NestedHasMany
146
169
  }.fetch(type)
147
- rescue
148
- raise ArgumentError, <<~MESSAGE
149
- Couldn't find attribute or association '#{name}' with type '#{type}' on #{model} model
150
-
151
- To fix this, either:
152
-
153
- 1. Remove 'attribute #{name}' from app/madmin/resources/#{model.to_s.underscore}_resource.rb
154
- 2. Or add the missing attribute or association to the #{model} model
155
- MESSAGE
156
170
  end
157
171
 
158
172
  def infer_type(name)
@@ -177,6 +191,10 @@ module Madmin
177
191
  # has_secure_password
178
192
  elsif model.attribute_types.include?("#{name_string}_digest") || name_string.ends_with?("_confirmation")
179
193
  :password
194
+
195
+ # ActiveRecord Store
196
+ elsif model_store_accessors.include?(name)
197
+ :string
180
198
  end
181
199
  end
182
200
 
@@ -195,6 +213,12 @@ module Madmin
195
213
  def url_helpers
196
214
  @url_helpers ||= Rails.application.routes.url_helpers
197
215
  end
216
+
217
+ def model_store_accessors
218
+ store_accessors = model.stored_attributes.values
219
+
220
+ store_accessors.flatten
221
+ end
198
222
  end
199
223
  end
200
224
  end