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,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,19 @@ 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
+ )
38
42
  end
39
43
 
40
44
  def friendly_name
@@ -42,7 +46,7 @@ module Madmin
42
46
  end
43
47
 
44
48
  def index_path(options = {})
45
- route_name = "madmin_#{model.table_name}_path"
49
+ route_name = "madmin_#{model.model_name.plural}_path"
46
50
 
47
51
  url_helpers.send(route_name, options)
48
52
  end
@@ -70,7 +74,7 @@ module Madmin
70
74
  end
71
75
 
72
76
  def permitted_params
73
- attributes.filter_map { |a| a[:field].to_param if a[:field].visible?(:form) }
77
+ attributes.values.filter_map { |a| a.field.to_param if a.field.visible?(:form) }
74
78
  end
75
79
 
76
80
  def display_name(record)
@@ -85,11 +89,13 @@ module Madmin
85
89
  model.column_names
86
90
  end
87
91
 
88
- private
92
+ def searchable_attributes
93
+ attributes.values.select { |a| a.field.searchable? }
94
+ end
89
95
 
90
- def field_for_type(name, type)
91
- type ||= infer_type(name)
96
+ private
92
97
 
98
+ def field_for_type(type)
93
99
  {
94
100
  binary: Fields::String,
95
101
  blob: Fields::Text,
@@ -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.2.0"
2
+ VERSION = "1.2.1"
3
3
  end
data/lib/madmin.rb CHANGED
@@ -6,6 +6,7 @@ module Madmin
6
6
  autoload :Field, "madmin/field"
7
7
  autoload :GeneratorHelpers, "madmin/generator_helpers"
8
8
  autoload :Resource, "madmin/resource"
9
+ autoload :Search, "madmin/search"
9
10
 
10
11
  module Fields
11
12
  autoload :Attachment, "madmin/fields/attachment"
@@ -30,14 +31,27 @@ module Madmin
30
31
  autoload :Time, "madmin/fields/time"
31
32
  end
32
33
 
33
- mattr_accessor :resources, default: []
34
-
35
34
  class << self
36
35
  def resource_for(object)
37
- Rails.application.eager_load!
36
+ "#{object.class.name}Resource".constantize
37
+ end
38
+
39
+ def resource_by_name(name)
40
+ "#{name}Resource".constantize
41
+ end
42
+
43
+ def resources
44
+ @resources ||= resource_names.map(&:constantize)
45
+ end
46
+
47
+ def reset_resources!
48
+ @resources = nil
49
+ end
38
50
 
39
- klass_name = object.class.name
40
- Madmin.resources.find { |r| r.model_name == klass_name }
51
+ def resource_names
52
+ root = Rails.root.join("app/madmin/resources/")
53
+ files = Dir.glob(root.join("**/*.rb"))
54
+ files.sort!.map! { |f| f.split(root.to_s).last.delete_suffix(".rb").classify }
41
55
  end
42
56
  end
43
57
  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.2.0
4
+ version: 1.2.1
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-05-05 00:00:00.000000000 Z
12
+ date: 2021-09-09 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,7 @@ 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
66
67
  - app/helpers/madmin/sort_helper.rb
67
68
  - app/views/layouts/madmin/application.html.erb
68
69
  - app/views/madmin/application/_form.html.erb
@@ -134,8 +135,15 @@ files:
134
135
  - app/views/madmin/fields/time/_form.html.erb
135
136
  - app/views/madmin/fields/time/_index.html.erb
136
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
137
144
  - lib/generators/madmin/install/install_generator.rb
138
145
  - lib/generators/madmin/install/templates/controller.rb.tt
146
+ - lib/generators/madmin/install/templates/routes.rb.tt
139
147
  - lib/generators/madmin/resource/resource_generator.rb
140
148
  - lib/generators/madmin/resource/templates/controller.rb.tt
141
149
  - lib/generators/madmin/resource/templates/resource.rb.tt
@@ -174,6 +182,7 @@ files:
174
182
  - lib/madmin/generator_helpers.rb
175
183
  - lib/madmin/namespace.rb
176
184
  - lib/madmin/resource.rb
185
+ - lib/madmin/search.rb
177
186
  - lib/madmin/version.rb
178
187
  - lib/madmin/view_generator.rb
179
188
  - lib/tasks/madmin_tasks.rake
@@ -196,7 +205,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
196
205
  - !ruby/object:Gem::Version
197
206
  version: '0'
198
207
  requirements: []
199
- rubygems_version: 3.2.3
208
+ rubygems_version: 3.2.22
200
209
  signing_key:
201
210
  specification_version: 4
202
211
  summary: A modern admin for Ruby on Rails apps