madmin 1.1.0 → 1.2.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +25 -0
- data/app/controllers/madmin/base_controller.rb +0 -5
- data/app/controllers/madmin/resource_controller.rb +22 -1
- data/app/helpers/madmin/application_helper.rb +4 -0
- data/app/helpers/madmin/nav_helper.rb +30 -0
- data/app/helpers/madmin/sort_helper.rb +32 -0
- data/app/views/layouts/madmin/application.html.erb +5 -5
- data/app/views/madmin/application/_form.html.erb +6 -8
- data/app/views/madmin/application/_javascript.html.erb +35 -4
- data/app/views/madmin/application/_navigation.html.erb +31 -5
- data/app/views/madmin/application/edit.html.erb +5 -1
- data/app/views/madmin/application/index.html.erb +48 -36
- data/app/views/madmin/application/new.html.erb +5 -1
- data/app/views/madmin/application/show.html.erb +24 -17
- data/app/views/madmin/fields/attachment/_form.html.erb +3 -1
- data/app/views/madmin/fields/attachment/_show.html.erb +7 -1
- data/app/views/madmin/fields/attachments/_form.html.erb +3 -1
- data/app/views/madmin/fields/attachments/_show.html.erb +7 -3
- data/app/views/madmin/fields/belongs_to/_form.html.erb +4 -2
- data/app/views/madmin/fields/belongs_to/_show.html.erb +1 -1
- data/app/views/madmin/fields/boolean/_form.html.erb +3 -1
- data/app/views/madmin/fields/date/_form.html.erb +3 -1
- data/app/views/madmin/fields/date_time/_form.html.erb +3 -1
- data/app/views/madmin/fields/decimal/_form.html.erb +3 -1
- data/app/views/madmin/fields/enum/_form.html.erb +4 -2
- data/app/views/madmin/fields/float/_form.html.erb +3 -1
- data/app/views/madmin/fields/has_many/_form.html.erb +4 -2
- data/app/views/madmin/fields/has_many/_show.html.erb +1 -1
- data/app/views/madmin/fields/has_one/_form.html.erb +3 -2
- data/app/views/madmin/fields/integer/_form.html.erb +3 -1
- data/app/views/madmin/fields/json/_form.html.erb +3 -1
- data/app/views/madmin/fields/nested_has_many/_fields.html.erb +2 -3
- data/app/views/madmin/fields/nested_has_many/_form.html.erb +3 -1
- data/app/views/madmin/fields/nested_has_many/_show.html.erb +1 -1
- data/app/views/madmin/fields/password/_form.html.erb +3 -1
- data/app/views/madmin/fields/polymorphic/_form.html.erb +3 -1
- data/app/views/madmin/fields/polymorphic/_show.html.erb +1 -1
- data/app/views/madmin/fields/rich_text/_form.html.erb +3 -1
- data/app/views/madmin/fields/string/_form.html.erb +3 -1
- data/app/views/madmin/fields/text/_form.html.erb +3 -1
- data/app/views/madmin/fields/time/_form.html.erb +3 -1
- data/app/views/madmin/shared/_label.html.erb +4 -0
- data/lib/generators/madmin/field/field_generator.rb +31 -0
- data/lib/generators/madmin/field/templates/_form.html.erb +2 -0
- data/lib/generators/madmin/field/templates/_index.html.erb +1 -0
- data/lib/generators/madmin/field/templates/_show.html.erb +1 -0
- data/lib/generators/madmin/field/templates/field.rb.tt +26 -0
- data/lib/generators/madmin/install/install_generator.rb +6 -1
- data/lib/generators/madmin/install/templates/routes.rb.tt +3 -0
- data/lib/generators/madmin/resource/resource_generator.rb +9 -59
- data/lib/generators/madmin/resource/templates/controller.rb.tt +6 -0
- data/lib/generators/madmin/resource/templates/resource.rb.tt +10 -1
- data/lib/madmin/engine.rb +4 -4
- data/lib/madmin/field.rb +4 -0
- data/lib/madmin/fields/belongs_to.rb +9 -6
- data/lib/madmin/fields/has_many.rb +10 -6
- data/lib/madmin/fields/nested_has_many.rb +1 -1
- data/lib/madmin/fields/string.rb +3 -0
- data/lib/madmin/fields/text.rb +3 -0
- data/lib/madmin/generator_helpers.rb +22 -4
- data/lib/madmin/resource.rb +46 -22
- data/lib/madmin/resource_builder.rb +80 -0
- data/lib/madmin/search.rb +60 -0
- data/lib/madmin/version.rb +1 -1
- data/lib/madmin.rb +20 -5
- metadata +16 -5
@@ -1,2 +1,4 @@
|
|
1
|
-
|
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
|
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
|
-
|
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
|
-
|
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
|
-
|
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
|
-
|
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
|
-
|
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
|
-
|
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
|
-
|
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,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 @@
|
|
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
|
@@ -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
|
32
|
-
model
|
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
|
43
|
-
|
35
|
+
def resource_builder
|
36
|
+
@resource_builder ||= ResourceBuilder.new(model)
|
44
37
|
end
|
45
38
|
|
46
|
-
def
|
47
|
-
|
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
|
-
|
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,6 +1,6 @@
|
|
1
1
|
class <%= class_name %>Resource < Madmin::Resource
|
2
2
|
# Attributes
|
3
|
-
<%
|
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
|
-
|
4
|
-
app.config.
|
5
|
-
app.config.
|
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.
|
9
|
+
Madmin.reset_resources!
|
10
10
|
end
|
11
11
|
end
|
12
12
|
end
|
data/lib/madmin/field.rb
CHANGED
@@ -2,18 +2,21 @@ module Madmin
|
|
2
2
|
module Fields
|
3
3
|
class BelongsTo < Field
|
4
4
|
def options_for_select(record)
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
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
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
[
|
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 { |
|
6
|
+
resource.attributes.reject { |name, attribute| skipped_fields.include?(name) }
|
7
7
|
end
|
8
8
|
|
9
9
|
def resource
|
data/lib/madmin/fields/string.rb
CHANGED
data/lib/madmin/fields/text.rb
CHANGED
@@ -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(
|
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 ||=
|
24
|
+
sentinel ||= default_sentinel(file)
|
19
25
|
|
20
26
|
in_root do
|
21
|
-
inject_into_file
|
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
|
data/lib/madmin/resource.rb
CHANGED
@@ -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
|
-
|
34
|
+
type ||= infer_type(name)
|
35
|
+
field = options[:field] || field_for_type(type)
|
36
|
+
|
37
|
+
attributes[name] = OpenStruct.new(
|
35
38
|
name: name,
|
36
|
-
|
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.
|
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.
|
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
|
-
|
101
|
+
def sortable_columns
|
102
|
+
model.column_names
|
103
|
+
end
|
85
104
|
|
86
|
-
def
|
87
|
-
|
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
|