kadmin 1.0.9 → 1.1.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
- SHA1:
3
- metadata.gz: eff92699a130e18fba90ad39a18ace224525efff
4
- data.tar.gz: 3202fc802e89c02a72d4c8b50d676bce010a5e15
2
+ SHA256:
3
+ metadata.gz: bc941c839a9a08a0ee49c52d60fd634d7a2b09bec14a5a2ded93ed5c2ae12d1e
4
+ data.tar.gz: 43b77a2767b748968f69b03d949c9bdc93de856914a61568233e5899482b1c7b
5
5
  SHA512:
6
- metadata.gz: 95b1476d0e4a4cf78376f85a7b131b07cd77a960fe2eb33f2f5c3567f6218aeda3ba10bc5f5180ef6843b7482e9f20f118de1f7dce821765404f7dde8368129e
7
- data.tar.gz: 3ca05a7b9230563ac965287cb47873277c881867024d73de77a04275c130803f95ea0d4eb292749df2dd916777cb51fe6dd44e1063a1a5d03dece7e19672f968
6
+ metadata.gz: c7377a3142c16a114d6ee4d0c4c17eac612db6d2b0805c0ccd7a1be79bb0da58096a0b5435286afd9f5ff40d6746e19942a0e10c84cfc190d68d8e1e9cba880d
7
+ data.tar.gz: 01bc90cdffb34e6996f82d0878d446f52c1501db32c6f8fa609abe5f2bcb0b70474e78b7ec5154f7e9af3e9ad3cdc3672a39df2843cc5d844e0fc92b0718fb01
@@ -1,3 +1,54 @@
1
1
  //= require modular
2
2
  //= require jquery_ujs
3
3
  //= require_tree .
4
+
5
+ // sortable tables
6
+
7
+ $( document ).ready(function() {
8
+ //commit=Filter&filter_name=test&page_offset=100&page_size=500&sort_asc=true&sort_column=
9
+ const urlParams = new URLSearchParams(window.location.search);
10
+ var params = {
11
+ commit: urlParams.get('commit'),
12
+ };
13
+
14
+ if (urlParams.get('page_size')) {
15
+ params['page_size'] = urlParams.get('page_size');
16
+ }
17
+
18
+ // adding filter(s)
19
+ for (let param of urlParams) {
20
+ if (param[0].startsWith('filter_')) {
21
+ params[param[0]] = param[1];
22
+ }
23
+ }
24
+
25
+ var path = window.location.pathname+'?';
26
+
27
+ $('th[sort-column]').each(function() {
28
+ var currentColumnName = $(this).attr('sort-column');
29
+ params['sort_column'] = currentColumnName;
30
+
31
+ params['sort_asc'] = 'true';
32
+ var sortIndicator = "";
33
+ if (urlParams.get('sort_column') === currentColumnName) {
34
+ //reverse sort order
35
+ if (urlParams.get('sort_asc') === 'true') {
36
+ params['sort_asc'] = 'false';
37
+ sortIndicator = ' ▴';
38
+ }
39
+ else {
40
+ params['sort_asc'] = 'true';
41
+ sortIndicator = ' ▾';
42
+ }
43
+ }
44
+
45
+ var linkElement = $( "<a/>", {
46
+ style: "padding:0;",
47
+ html: $(this).html() + sortIndicator,
48
+ "class": "btn btn-default",
49
+ href: path + $.param(params)
50
+ });
51
+
52
+ $(this).html(linkElement);
53
+ });
54
+ });
@@ -13,6 +13,12 @@ module Kadmin
13
13
  # @return [ActiveRecord::Relation] the base relation to find items from
14
14
  attr_reader :scope
15
15
 
16
+ # @return [String] name of column that is used in ORDER_BY clause
17
+ attr_reader :sort_column
18
+
19
+ # @return [bool] true if sort order is ASC, false if DESC
20
+ attr_reader :sort_asc
21
+
16
22
  # @param [ActiveRecord::Relation] scope base relation to page/filter on
17
23
  def initialize(scope)
18
24
  @scope = scope
@@ -20,6 +26,7 @@ module Kadmin
20
26
  @filters = {}
21
27
  @results = nil
22
28
  @filtering = false
29
+ @sort_asc = true
23
30
  end
24
31
 
25
32
  # @param [String] name the filter name (should be unique)
@@ -35,6 +42,15 @@ module Kadmin
35
42
  return self
36
43
  end
37
44
 
45
+ # sets sort order for scope, any existing order is overwritten
46
+ # @param [String] name of column to sort
47
+ # @param [bool] true for ASC sort, false otherwise
48
+ def order(sort_column, sort_asc)
49
+ @sort_column = sort_column
50
+ @sort_asc = sort_asc
51
+ @scope = @scope.reorder("#{sort_column} #{sort_asc ? 'ASC' : 'DESC'}")
52
+ end
53
+
38
54
  def filtering?
39
55
  return @filtering
40
56
  end
@@ -14,13 +14,18 @@ module Kadmin
14
14
  def resources_finder(scope, filters = [])
15
15
  filters = Array.wrap(filters)
16
16
  filter_params = filters.map { |hash| hash[:param] }
17
- permitted = params.permit(:page_size, :page_offset, :format, *filter_params)
17
+ permitted = params.permit(:page_size, :page_offset, :format, *filter_params, :sort_column, :sort_asc)
18
18
 
19
19
  page_size = permitted.fetch(:page_size, DEFAULT_FINDER_PAGE_SIZE).to_i
20
20
  page_offset = permitted.fetch(:page_offset, 0).to_i
21
21
 
22
22
  finder = Finder.new(scope)
23
23
  finder.paginate(size: page_size, offset: page_offset)
24
+
25
+ if permitted[:sort_column].present?
26
+ finder.order(permitted[:sort_column], ActiveRecord::Type::Boolean.new.deserialize(permitted.fetch(:sort_asc, true)))
27
+ end
28
+
24
29
  filters.each do |hash|
25
30
  value = permitted[hash[:param]]
26
31
  filter = if hash[:filter].present?
@@ -43,13 +48,16 @@ module Kadmin
43
48
  # allowing you to reference joined tables as well.
44
49
  def resources_filter_matches(columns)
45
50
  return lambda do |v|
46
- pattern = "%#{v}%"
47
51
  conditions = Array.wrap(columns).reduce(nil) do |acc, column|
48
52
  column = arel_table[column] unless column.is_a?(Arel::Attributes::Attribute)
49
- matcher = column.matches(pattern)
53
+ matcher = if type_for_attribute(column.name).type == :integer
54
+ column.eq(v)
55
+ else
56
+ pattern = "%#{v}%"
57
+ column.matches(pattern)
58
+ end
50
59
  acc.nil? ? matcher : acc.or(matcher)
51
60
  end
52
-
53
61
  where(conditions)
54
62
  end
55
63
  end
@@ -1,4 +1,6 @@
1
1
  <%= form_tag(request.fullpath, method: 'get', class: 'form-inline pull-right') do %>
2
+ <%= hidden_field_tag 'sort_column', finder&.sort_column %>
3
+ <%= hidden_field_tag 'sort_asc', finder&.sort_asc %>
2
4
  <div class='form-group'>
3
5
  <% unless local_assigns.fetch(:hide_create_button, false) %>
4
6
  <%= link_to(t('kadmin.components.finder.create'), url_for(action: :new), class: 'btn btn-primary', style: 'margin-right: 20px') %>
@@ -1,3 +1,3 @@
1
1
  module Kadmin
2
- VERSION = '1.0.9'.freeze
2
+ VERSION = '1.1.0'.freeze
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: kadmin
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.9
4
+ version: 1.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Nicolas Pepin-Perreault
@@ -10,7 +10,7 @@ authors:
10
10
  autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
- date: 2019-03-06 00:00:00.000000000 Z
13
+ date: 2019-03-14 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: rails
@@ -196,7 +196,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
196
196
  version: '0'
197
197
  requirements: []
198
198
  rubyforge_project:
199
- rubygems_version: 2.6.14.3
199
+ rubygems_version: 2.7.8
200
200
  signing_key:
201
201
  specification_version: 4
202
202
  summary: Collection of utility, configuration, etc., for admin areas