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
@@ -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
@@ -1,3 +1,3 @@
1
1
  module Madmin
2
- VERSION = "1.1.0"
2
+ VERSION = "1.2.3"
3
3
  end
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
- Rails.application.eager_load!
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
- klass_name = object.class.name
40
- Madmin.resources.find { |r| r.model_name == klass_name }
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.1.0
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-04-22 00:00:00.000000000 Z
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: '4.0'
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: '4.0'
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.3
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