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
@@ -0,0 +1,80 @@
|
|
1
|
+
module Madmin
|
2
|
+
class ResourceBuilder
|
3
|
+
attr_reader :model
|
4
|
+
|
5
|
+
def initialize(model)
|
6
|
+
@model = model
|
7
|
+
end
|
8
|
+
|
9
|
+
def associations
|
10
|
+
model.reflections.reject { |name, association|
|
11
|
+
# Hide these special associations
|
12
|
+
name.starts_with?("rich_text") ||
|
13
|
+
name.ends_with?("_attachment") ||
|
14
|
+
name.ends_with?("_attachments") ||
|
15
|
+
name.ends_with?("_blob") ||
|
16
|
+
name.ends_with?("_blobs")
|
17
|
+
}.keys
|
18
|
+
end
|
19
|
+
|
20
|
+
def attributes
|
21
|
+
model.attribute_names + virtual_attributes - redundant_attributes
|
22
|
+
end
|
23
|
+
|
24
|
+
def store_accessors
|
25
|
+
model.stored_attributes.values.flatten
|
26
|
+
end
|
27
|
+
|
28
|
+
def virtual_attributes
|
29
|
+
virtual = []
|
30
|
+
|
31
|
+
# has_secure_password columns
|
32
|
+
password_attributes = model.attribute_types.keys.select { |k| k.ends_with?("_digest") }.map { |k| k.delete_suffix("_digest") }
|
33
|
+
virtual += password_attributes.map { |attr| [attr, "#{attr}_confirmation"] }.flatten
|
34
|
+
|
35
|
+
# ActiveRecord Store columns
|
36
|
+
virtual += store_accessors.map(&:to_s)
|
37
|
+
|
38
|
+
# Add virtual attributes for ActionText and ActiveStorage
|
39
|
+
model.reflections.each do |name, association|
|
40
|
+
if name.starts_with?("rich_text")
|
41
|
+
virtual << name.split("rich_text_").last
|
42
|
+
elsif name.ends_with?("_attachment")
|
43
|
+
virtual << name.split("_attachment").first
|
44
|
+
elsif name.ends_with?("_attachments")
|
45
|
+
virtual << name.split("_attachments").first
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
virtual
|
50
|
+
end
|
51
|
+
|
52
|
+
def redundant_attributes
|
53
|
+
redundant = []
|
54
|
+
|
55
|
+
# has_secure_password columns
|
56
|
+
redundant += model.attribute_types.keys.select { |k| k.ends_with?("_digest") }
|
57
|
+
|
58
|
+
# ActiveRecord Store columns
|
59
|
+
store_columns = model.stored_attributes.keys
|
60
|
+
redundant += store_columns.map(&:to_s)
|
61
|
+
|
62
|
+
model.reflections.each do |name, association|
|
63
|
+
if association.has_one?
|
64
|
+
next
|
65
|
+
elsif association.collection?
|
66
|
+
next
|
67
|
+
elsif association.polymorphic?
|
68
|
+
redundant << "#{name}_id"
|
69
|
+
redundant << "#{name}_type"
|
70
|
+
elsif name.starts_with?("rich_text")
|
71
|
+
redundant << name
|
72
|
+
else # belongs to
|
73
|
+
redundant << "#{name}_id"
|
74
|
+
end
|
75
|
+
end
|
76
|
+
|
77
|
+
redundant
|
78
|
+
end
|
79
|
+
end
|
80
|
+
end
|
@@ -0,0 +1,60 @@
|
|
1
|
+
# based on Administrate Search: https://github.com/thoughtbot/administrate/blob/main/lib/administrate/search.rb
|
2
|
+
|
3
|
+
module Madmin
|
4
|
+
class Search
|
5
|
+
attr_reader :query
|
6
|
+
|
7
|
+
def initialize(scoped_resource, resource, term)
|
8
|
+
@resource = resource
|
9
|
+
@scoped_resource = scoped_resource
|
10
|
+
@query = term
|
11
|
+
end
|
12
|
+
|
13
|
+
def run
|
14
|
+
if query.blank?
|
15
|
+
@scoped_resource.all
|
16
|
+
else
|
17
|
+
search_results(@scoped_resource)
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
private
|
22
|
+
|
23
|
+
def search_results(resources)
|
24
|
+
resources.where(query_template, *query_values)
|
25
|
+
end
|
26
|
+
|
27
|
+
def query_template
|
28
|
+
search_attributes.map do |attr|
|
29
|
+
table_name = query_table_name(attr)
|
30
|
+
searchable_fields(attr).map do |field|
|
31
|
+
column_name = column_to_query(field)
|
32
|
+
"LOWER(CAST(#{table_name}.#{column_name} AS CHAR(256))) LIKE ?"
|
33
|
+
end.join(" OR ")
|
34
|
+
end.join(" OR ")
|
35
|
+
end
|
36
|
+
|
37
|
+
def searchable_fields(attr)
|
38
|
+
[attr[:name]]
|
39
|
+
end
|
40
|
+
|
41
|
+
def query_values
|
42
|
+
fields_count = search_attributes.sum do |attr|
|
43
|
+
searchable_fields(attr).count
|
44
|
+
end
|
45
|
+
["%#{@query.mb_chars.downcase}%"] * fields_count
|
46
|
+
end
|
47
|
+
|
48
|
+
def search_attributes
|
49
|
+
@resource.searchable_attributes
|
50
|
+
end
|
51
|
+
|
52
|
+
def query_table_name(attr)
|
53
|
+
ActiveRecord::Base.connection.quote_column_name(@scoped_resource.table_name)
|
54
|
+
end
|
55
|
+
|
56
|
+
def column_to_query(attr)
|
57
|
+
ActiveRecord::Base.connection.quote_column_name(attr)
|
58
|
+
end
|
59
|
+
end
|
60
|
+
end
|
data/lib/madmin/version.rb
CHANGED
data/lib/madmin.rb
CHANGED
@@ -6,6 +6,8 @@ module Madmin
|
|
6
6
|
autoload :Field, "madmin/field"
|
7
7
|
autoload :GeneratorHelpers, "madmin/generator_helpers"
|
8
8
|
autoload :Resource, "madmin/resource"
|
9
|
+
autoload :ResourceBuilder, "madmin/resource_builder"
|
10
|
+
autoload :Search, "madmin/search"
|
9
11
|
|
10
12
|
module Fields
|
11
13
|
autoload :Attachment, "madmin/fields/attachment"
|
@@ -30,14 +32,27 @@ module Madmin
|
|
30
32
|
autoload :Time, "madmin/fields/time"
|
31
33
|
end
|
32
34
|
|
33
|
-
mattr_accessor :resources, default: []
|
34
|
-
|
35
35
|
class << self
|
36
36
|
def resource_for(object)
|
37
|
-
|
37
|
+
"#{object.class.name}Resource".constantize
|
38
|
+
end
|
39
|
+
|
40
|
+
def resource_by_name(name)
|
41
|
+
"#{name}Resource".constantize
|
42
|
+
end
|
43
|
+
|
44
|
+
def resources
|
45
|
+
@resources ||= resource_names.map(&:constantize)
|
46
|
+
end
|
47
|
+
|
48
|
+
def reset_resources!
|
49
|
+
@resources = nil
|
50
|
+
end
|
38
51
|
|
39
|
-
|
40
|
-
|
52
|
+
def resource_names
|
53
|
+
root = Rails.root.join("app/madmin/resources/")
|
54
|
+
files = Dir.glob(root.join("**/*.rb"))
|
55
|
+
files.sort!.map! { |f| f.split(root.to_s).last.delete_suffix(".rb").classify }
|
41
56
|
end
|
42
57
|
end
|
43
58
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: madmin
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: 1.2.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Chris Oliver
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2021-
|
12
|
+
date: 2021-09-24 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: rails
|
@@ -34,7 +34,7 @@ dependencies:
|
|
34
34
|
version: '3.5'
|
35
35
|
- - "<"
|
36
36
|
- !ruby/object:Gem::Version
|
37
|
-
version: '
|
37
|
+
version: '5.0'
|
38
38
|
type: :runtime
|
39
39
|
prerelease: false
|
40
40
|
version_requirements: !ruby/object:Gem::Requirement
|
@@ -44,7 +44,7 @@ dependencies:
|
|
44
44
|
version: '3.5'
|
45
45
|
- - "<"
|
46
46
|
- !ruby/object:Gem::Version
|
47
|
-
version: '
|
47
|
+
version: '5.0'
|
48
48
|
description: It's an admin, obviously.
|
49
49
|
email:
|
50
50
|
- excid3@gmail.com
|
@@ -63,6 +63,8 @@ files:
|
|
63
63
|
- app/controllers/madmin/dashboard_controller.rb
|
64
64
|
- app/controllers/madmin/resource_controller.rb
|
65
65
|
- app/helpers/madmin/application_helper.rb
|
66
|
+
- app/helpers/madmin/nav_helper.rb
|
67
|
+
- app/helpers/madmin/sort_helper.rb
|
66
68
|
- app/views/layouts/madmin/application.html.erb
|
67
69
|
- app/views/madmin/application/_form.html.erb
|
68
70
|
- app/views/madmin/application/_javascript.html.erb
|
@@ -133,8 +135,15 @@ files:
|
|
133
135
|
- app/views/madmin/fields/time/_form.html.erb
|
134
136
|
- app/views/madmin/fields/time/_index.html.erb
|
135
137
|
- app/views/madmin/fields/time/_show.html.erb
|
138
|
+
- app/views/madmin/shared/_label.html.erb
|
139
|
+
- lib/generators/madmin/field/field_generator.rb
|
140
|
+
- lib/generators/madmin/field/templates/_form.html.erb
|
141
|
+
- lib/generators/madmin/field/templates/_index.html.erb
|
142
|
+
- lib/generators/madmin/field/templates/_show.html.erb
|
143
|
+
- lib/generators/madmin/field/templates/field.rb.tt
|
136
144
|
- lib/generators/madmin/install/install_generator.rb
|
137
145
|
- lib/generators/madmin/install/templates/controller.rb.tt
|
146
|
+
- lib/generators/madmin/install/templates/routes.rb.tt
|
138
147
|
- lib/generators/madmin/resource/resource_generator.rb
|
139
148
|
- lib/generators/madmin/resource/templates/controller.rb.tt
|
140
149
|
- lib/generators/madmin/resource/templates/resource.rb.tt
|
@@ -173,6 +182,8 @@ files:
|
|
173
182
|
- lib/madmin/generator_helpers.rb
|
174
183
|
- lib/madmin/namespace.rb
|
175
184
|
- lib/madmin/resource.rb
|
185
|
+
- lib/madmin/resource_builder.rb
|
186
|
+
- lib/madmin/search.rb
|
176
187
|
- lib/madmin/version.rb
|
177
188
|
- lib/madmin/view_generator.rb
|
178
189
|
- lib/tasks/madmin_tasks.rake
|
@@ -195,7 +206,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
195
206
|
- !ruby/object:Gem::Version
|
196
207
|
version: '0'
|
197
208
|
requirements: []
|
198
|
-
rubygems_version: 3.2.
|
209
|
+
rubygems_version: 3.2.22
|
199
210
|
signing_key:
|
200
211
|
specification_version: 4
|
201
212
|
summary: A modern admin for Ruby on Rails apps
|