terrazzo 0.5.3 → 0.5.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 6976f4987f794484e19de73da573e1b7d65dfc75a0852133c1eebf005d22167e
4
- data.tar.gz: 4dc5597241838c5523ed2a5a50d23185a0e21199b18ae9dd07128d7687e836e6
3
+ metadata.gz: 100223520cc1a1dbedef6413a2ba4e1b45691f4773abddd9ee27d123e735c64d
4
+ data.tar.gz: eeee21e64365ca82bc7c6f11c2f80dd813b2960d96e1e3aa80839fd7acb2b2cf
5
5
  SHA512:
6
- metadata.gz: 673cd873e8c295de85c4bc2d798a33d4879e37cfded9357d01c08b1617e050931502193c3be3c445f0577f4bc29372bc15867b3e4fafe519161f6209d894ca4f
7
- data.tar.gz: ae15ddf2abd934407d97f3e17d4344e68f5ff93e7fce588f9d520998d017404f980c7f75d07709ba2e984ae49490d65d9a573d4f169949c8b03cce849402f46b
6
+ metadata.gz: f44edde4138e7348cfaa6abd5c790c8d295f40ed57fd7391c6e1af6a97a2f334bf73e147e4322e37138b5a72ef4743be7c0346448defef4c8d0becaf1daf8d17
7
+ data.tar.gz: 344144b82879ebe42857dbf048b9737e429fba63d06a06aa5ef5b2aa1ff3accef8633efb11c12835fca3a384ef06b162cf5c78892db4ffbc8e046598b2eaf311
@@ -44,7 +44,10 @@ module Terrazzo
44
44
 
45
45
  def show
46
46
  @resource = find_resource(params[:id])
47
- @page = Terrazzo::Page::Show.new(dashboard, @resource)
47
+ @page = Terrazzo::Page::Show.new(
48
+ dashboard, @resource,
49
+ has_many_params: Terrazzo::HasManyPagination.extract(params)
50
+ )
48
51
  end
49
52
 
50
53
  def new
@@ -9,6 +9,21 @@ module Terrazzo
9
9
  end
10
10
  end
11
11
 
12
+ def has_many_pagination_paths(field, resource)
13
+ param_key = Terrazzo::HasManyPagination.param_key(field.attribute)
14
+ base = request.query_parameters.merge(
15
+ only_path: true,
16
+ controller: controller_path,
17
+ action: :show,
18
+ id: resource.to_param,
19
+ format: nil
20
+ )
21
+ {
22
+ prevPagePath: (field.current_page > 1 ? url_for(base.merge(param_key => field.current_page - 1)) : nil),
23
+ nextPagePath: (field.current_page < field.total_pages ? url_for(base.merge(param_key => field.current_page + 1)) : nil)
24
+ }
25
+ end
26
+
12
27
  private
13
28
 
14
29
  def default_collection_item_actions(resource)
@@ -60,12 +60,12 @@ json.pagination do
60
60
  if @resources.next_page
61
61
  json.nextPagePath url_for(
62
62
  _page: @resources.next_page,
63
+ per_page: params[:per_page],
63
64
  search: @search_term,
64
65
  order: params[:order],
65
66
  direction: params[:direction],
66
67
  filter: @active_filter,
67
68
  filter_value: @filter_value,
68
- props_at: "data.pagination",
69
69
  only_path: true
70
70
  )
71
71
  else
@@ -74,12 +74,12 @@ json.pagination do
74
74
  if @resources.prev_page
75
75
  json.prevPagePath url_for(
76
76
  _page: @resources.prev_page,
77
+ per_page: params[:per_page],
77
78
  search: @search_term,
78
79
  order: params[:order],
79
80
  direction: params[:direction],
80
81
  filter: @active_filter,
81
82
  filter_value: @filter_value,
82
- props_at: "data.pagination",
83
83
  only_path: true
84
84
  )
85
85
  else
@@ -10,11 +10,14 @@ show_field_json = ->(json, field) do
10
10
  if field.class.associative? && field.data.present?
11
11
  if field.is_a?(Terrazzo::Field::HasMany)
12
12
  render_actions = field.options.fetch(:render_actions, true)
13
- json.hasManyRowExtras(field.data.each_with_object({}) do |record, hash|
13
+
14
+ json.hasManyRowExtras(field.page_records.each_with_object({}) do |record, hash|
14
15
  extras = { showPath: (polymorphic_path([namespace, record]) rescue nil) }
15
16
  extras[:collectionItemActions] = collection_item_actions(record) if render_actions
16
17
  hash[record.id.to_s] = extras
17
18
  end)
19
+
20
+ json.paginationPaths has_many_pagination_paths(field, @resource)
18
21
  else
19
22
  json.showPath polymorphic_path([namespace, field.data]) rescue nil
20
23
  end
@@ -2,6 +2,14 @@ module Terrazzo
2
2
  class Engine < ::Rails::Engine
3
3
  isolate_namespace Terrazzo
4
4
 
5
+ initializer "terrazzo.props_searcher_partial" do
6
+ Props::Searcher.class_eval do
7
+ def partial!(**options)
8
+ @context.render options
9
+ end
10
+ end
11
+ end
12
+
5
13
  initializer "terrazzo.i18n" do
6
14
  Terrazzo::Engine.root.glob("config/locales/**/*.yml").each do |locale|
7
15
  I18n.load_path << locale unless I18n.load_path.include?(locale)
@@ -36,7 +36,7 @@ module Terrazzo
36
36
  end
37
37
 
38
38
  def default_options
39
- { limit: 5 }
39
+ {}
40
40
  end
41
41
 
42
42
  def permitted_attribute(attr, _options = {})
@@ -44,22 +44,53 @@ module Terrazzo
44
44
  end
45
45
  end
46
46
 
47
+ def per_page
48
+ (options[:per_page] || options[:limit] || 5).to_i
49
+ end
50
+
51
+ def current_page
52
+ p = options[:_page].to_i
53
+ p < 1 ? 1 : p
54
+ end
55
+
56
+ def total_count
57
+ paginated.total_count
58
+ end
59
+
60
+ def total_pages
61
+ [paginated.total_pages, 1].max
62
+ end
63
+
64
+ def page_records
65
+ paginated.to_a
66
+ end
67
+
47
68
  private
48
69
 
70
+ def paginated
71
+ @paginated ||= begin
72
+ scope = apply_sorting(data)
73
+ scope = Kaminari.paginate_array(scope.to_a) unless scope.respond_to?(:page)
74
+ scope.page(current_page).per(per_page)
75
+ end
76
+ end
77
+
49
78
  def serialize_show_value
50
- limit = options.fetch(:limit, 5)
51
- records = apply_sorting(data)
52
- all_records = records.to_a
53
- total = all_records.size
54
79
  col_attrs = options[:collection_attributes] || resolve_default_collection_attributes
55
80
 
81
+ meta = {
82
+ total: total_count,
83
+ perPage: per_page,
84
+ currentPage: current_page,
85
+ totalPages: total_pages,
86
+ }
87
+
56
88
  if col_attrs
57
- serialize_with_collection_attributes(all_records, col_attrs, total, limit)
89
+ { **serialize_with_collection_attributes(page_records, col_attrs), **meta }
58
90
  else
59
91
  {
60
- items: all_records.map { |r| { id: r.id.to_s, display: display_name(r) } },
61
- total: total,
62
- initialLimit: limit
92
+ items: page_records.map { |r| { id: r.id.to_s, display: display_name(r) } },
93
+ **meta,
63
94
  }
64
95
  end
65
96
  end
@@ -71,7 +102,7 @@ module Terrazzo
71
102
  nil
72
103
  end
73
104
 
74
- def serialize_with_collection_attributes(records, col_attrs, total, limit)
105
+ def serialize_with_collection_attributes(records, col_attrs)
75
106
  dashboard_class = find_associated_dashboard
76
107
 
77
108
  headers = col_attrs.map do |attr|
@@ -90,12 +121,7 @@ module Terrazzo
90
121
  { id: record.id.to_s, cells: cells }
91
122
  end
92
123
 
93
- {
94
- headers: headers,
95
- rows: rows,
96
- total: total,
97
- initialLimit: limit
98
- }
124
+ { headers: headers, rows: rows }
99
125
  end
100
126
 
101
127
  def resource_options
@@ -0,0 +1,24 @@
1
+ module Terrazzo
2
+ module HasManyPagination
3
+ PARAM_PREFIX = "hm_".freeze
4
+ PARAM_SUFFIX = "_page".freeze
5
+
6
+ module_function
7
+
8
+ def extract(params)
9
+ result = {}
10
+ params.each do |key, value|
11
+ key_s = key.to_s
12
+ next unless key_s.start_with?(PARAM_PREFIX) && key_s.end_with?(PARAM_SUFFIX)
13
+ attr = key_s[PARAM_PREFIX.length...-PARAM_SUFFIX.length]
14
+ next if attr.empty?
15
+ result[attr.to_sym] = { _page: value.to_i }
16
+ end
17
+ result
18
+ end
19
+
20
+ def param_key(attribute)
21
+ "#{PARAM_PREFIX}#{attribute}#{PARAM_SUFFIX}"
22
+ end
23
+ end
24
+ end
@@ -3,9 +3,10 @@ module Terrazzo
3
3
  class Show < Base
4
4
  attr_reader :resource
5
5
 
6
- def initialize(dashboard, resource)
6
+ def initialize(dashboard, resource, has_many_params: {})
7
7
  super(dashboard, resource.class)
8
8
  @resource = resource
9
+ @has_many_params = has_many_params || {}
9
10
  end
10
11
 
11
12
  def page_title
@@ -20,24 +21,29 @@ module Terrazzo
20
21
  def attributes
21
22
  attrs = dashboard.show_page_attributes
22
23
  dashboard.flatten_attributes(attrs).map do |attr|
23
- dashboard.attribute_type_for(attr).new(attr, nil, :show, resource: resource)
24
+ build_field(attr, :show)
24
25
  end
25
26
  end
26
27
 
27
28
  private
28
29
 
30
+ def build_field(attr, mode)
31
+ extra = @has_many_params[attr.to_sym] || {}
32
+ dashboard.attribute_type_for(attr).new(attr, nil, mode, resource: resource, options: extra)
33
+ end
34
+
29
35
  def normalize_groups(attrs, mode)
30
36
  if attrs.is_a?(Hash)
31
37
  attrs.map do |group_name, fields|
32
38
  {
33
39
  name: group_name,
34
- fields: fields.map { |attr| dashboard.attribute_type_for(attr).new(attr, nil, mode, resource: resource) }
40
+ fields: fields.map { |attr| build_field(attr, mode) }
35
41
  }
36
42
  end
37
43
  else
38
44
  [{
39
45
  name: "",
40
- fields: attrs.map { |attr| dashboard.attribute_type_for(attr).new(attr, nil, mode, resource: resource) }
46
+ fields: attrs.map { |attr| build_field(attr, mode) }
41
47
  }]
42
48
  end
43
49
  end
@@ -1,3 +1,3 @@
1
1
  module Terrazzo
2
- VERSION = "0.5.3"
2
+ VERSION = "0.5.4"
3
3
  end
data/lib/terrazzo.rb CHANGED
@@ -13,6 +13,7 @@ module Terrazzo
13
13
  autoload :Filter, "terrazzo/filter"
14
14
  autoload :Namespace, "terrazzo/namespace"
15
15
  autoload :GeneratorHelpers, "terrazzo/generator_helpers"
16
+ autoload :HasManyPagination, "terrazzo/has_many_pagination"
16
17
  autoload :UsesSuperglue, "terrazzo/uses_superglue"
17
18
 
18
19
  module Field
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: terrazzo
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.3
4
+ version: 0.5.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Terrazzo Contributors
@@ -273,6 +273,7 @@ files:
273
273
  - lib/terrazzo/field/url.rb
274
274
  - lib/terrazzo/filter.rb
275
275
  - lib/terrazzo/generator_helpers.rb
276
+ - lib/terrazzo/has_many_pagination.rb
276
277
  - lib/terrazzo/namespace.rb
277
278
  - lib/terrazzo/namespace/resource.rb
278
279
  - lib/terrazzo/not_authorized_error.rb