madmin 1.1.0 → 1.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 223251f99312a051f3fd6a6d926c6583347e2c78b255343bc66b3fb063ea8a54
4
- data.tar.gz: cfab141916431f52d137bd7704c33cc4cd3b1781735062e2a9d173f9d9d7bee4
3
+ metadata.gz: e868d17abd508729232e7ebeebd4b79a8e69545b853276a700c60e61e5d6348c
4
+ data.tar.gz: b19043a0a2e7dd0ad6392f995b76812b434e54f73682eb163ca28e1aaeb4ad34
5
5
  SHA512:
6
- metadata.gz: 7b1480b7df30ec0bba0f1937a7336bb8532f51cd8dc9c8de78fd73b686c132dd14083eec88a303938bfb4fdfa50827db570a3d899e40952799869dfe4795e756
7
- data.tar.gz: e362a3d85149b1f1074ea21eaf6a6659e6c8f002a929f668a21583a286fc1824a2f3ede02997eafe75feaa20e0172345c54210180b7c537b44d0de4bca1eb0b0
6
+ metadata.gz: '09b7ec920a1c8ae8a1429db77cff71ca45574e379563db5a06648d5cd5434950597b99eed5b836f7834cdc5c71e9d68411f064cc78d363da848efde9a0ef1b1a'
7
+ data.tar.gz: 51b2b25ddabe18e8777c9eab9e34dd1b8afd74a05aae5671f2f94c85cb404043bcde8423d996033a9dd4fa07ee672ad36240e65e195166586afabc02d756237a
@@ -1,5 +1,7 @@
1
1
  module Madmin
2
2
  class ResourceController < ApplicationController
3
+ include SortHelper
4
+
3
5
  before_action :set_record, except: [:index, :new, :create]
4
6
 
5
7
  def index
@@ -54,7 +56,7 @@ module Madmin
54
56
  end
55
57
 
56
58
  def scoped_resources
57
- resource.model.send(valid_scope)
59
+ resource.model.send(valid_scope).order(sort_column => sort_direction)
58
60
  end
59
61
 
60
62
  def valid_scope
@@ -0,0 +1,31 @@
1
+ module Madmin
2
+ module SortHelper
3
+ def sortable(column, title, options = {})
4
+ matching_column = (column.to_s == sort_column)
5
+
6
+ link_to request.params.merge(sort: column, direction: sort_direction), options do
7
+ concat title
8
+ if matching_column
9
+ concat " "
10
+ concat tag.i(sort_direction == "asc" ? "▲" : "▼")
11
+ end
12
+ end
13
+ end
14
+
15
+ def sort_column
16
+ resource.sortable_columns.include?(params[:sort]) ? params[:sort] : default_sort_column
17
+ end
18
+
19
+ def sort_direction
20
+ ["asc", "desc"].include?(params[:direction]) ? params[:direction] : default_sort_direction
21
+ end
22
+
23
+ def default_sort_column
24
+ resource.try(:default_sort_column) || "created_at"
25
+ end
26
+
27
+ def default_sort_direction
28
+ resource.try(:default_sort_direction) || "desc"
29
+ end
30
+ end
31
+ end
@@ -20,7 +20,7 @@
20
20
  <% next if attribute[:field].nil? %>
21
21
  <% next unless attribute[:field].visible?(action_name) %>
22
22
 
23
- <th><%= attribute[:name].to_s.titleize %></th>
23
+ <th><%= sortable attribute[:name], attribute[:name].to_s.titleize %></th>
24
24
  <% end %>
25
25
  <th>Actions</th>
26
26
  </tr>
@@ -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
@@ -70,7 +70,7 @@ module Madmin
70
70
  end
71
71
 
72
72
  def permitted_params
73
- attributes.map { |a| a[:field].to_param }
73
+ attributes.filter_map { |a| a[:field].to_param if a[:field].visible?(:form) }
74
74
  end
75
75
 
76
76
  def display_name(record)
@@ -81,6 +81,10 @@ module Madmin
81
81
  model.respond_to? :friendly
82
82
  end
83
83
 
84
+ def sortable_columns
85
+ model.column_names
86
+ end
87
+
84
88
  private
85
89
 
86
90
  def field_for_type(name, type)
@@ -1,3 +1,3 @@
1
1
  module Madmin
2
- VERSION = "1.1.0"
2
+ VERSION = "1.2.0"
3
3
  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.0
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-05-05 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rails
@@ -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/sort_helper.rb
66
67
  - app/views/layouts/madmin/application.html.erb
67
68
  - app/views/madmin/application/_form.html.erb
68
69
  - app/views/madmin/application/_javascript.html.erb