actionset 0.4.0 → 0.4.1

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
  SHA1:
3
- metadata.gz: a682e0da229e04c37aca7d4da4181a8725c837dd
4
- data.tar.gz: 98c6cd09dedad0b048cf11551f7290c388fef9c8
3
+ metadata.gz: 5a6420257cd324299c5517e5e47ba01f2213ce66
4
+ data.tar.gz: a505a2bb2c880fad25b8e1bfb4ec09627421fa25
5
5
  SHA512:
6
- metadata.gz: b6eeaedd38735cd6d63de62db3fc53cee81c3bf3350be60a91841a1c846a096ceee710a975a730192531ee7da2972b694de0933f0964b25ddaf4c91644426d4f
7
- data.tar.gz: 3ac5879e34de2d2a3b3d4b91e5ec62f78ccbc63cd34a969843589b1f6d152e11f55aec84013f509dccfdfbd470e75a40950e9d38734ab52ba2de74a55907c874
6
+ metadata.gz: 2e28535d83f7e8313ed211713c56338fb61c5f83e693092c55b22c99bea548d4f55b6f5dcf8d5d699a9d9bfd096b89b4c4e1f65582a980eb9fa0609c41ca0b6f
7
+ data.tar.gz: bcabe55d13513680c032a23cb640644d291e766c1867404d37597547e52dbb402b07b089db8a31bbdc8e3cd983c8292fb990d39e70330497142a88f784170fec
data/.gitignore CHANGED
@@ -12,3 +12,4 @@
12
12
  .rspec_status
13
13
  *.gem
14
14
  *.sqlite
15
+ .DS_Store
data/CHANGELOG CHANGED
@@ -1,3 +1,5 @@
1
+ v 0.4.1
2
+ - Fix the helper methods to work properly with the new version of ActiveSet
1
3
  v 0.4.0
2
4
  - Update to work with the new version of ActiveSet
3
5
  v 0.3.5
data/actionset.gemspec CHANGED
@@ -26,6 +26,7 @@ Gem::Specification.new do |spec|
26
26
  spec.add_dependency 'activesupport', '>= 4.0.2'
27
27
  spec.add_dependency 'railties'
28
28
 
29
+ spec.add_development_dependency 'rails', '~> 5.1.0'
29
30
  spec.add_development_dependency 'bundler', '~> 1.15'
30
31
  spec.add_development_dependency 'rake', '~> 10.0'
31
32
  spec.add_development_dependency 'rspec', '~> 3.0'
data/lib/action_set.rb CHANGED
@@ -7,7 +7,7 @@ require 'active_set'
7
7
  require 'ostruct'
8
8
 
9
9
  require 'action_set/version'
10
- require_relative './action_set/instructions/entry_value'
10
+ require_relative './action_set/instruction/value'
11
11
  require_relative './action_set/helpers/helper_methods'
12
12
 
13
13
  module ActionSet
@@ -68,8 +68,9 @@ module ActionSet
68
68
  instruction = ActiveSet::Instruction.new(keypath, value)
69
69
  item_with_value = set.find { |i| !instruction.value_for(item: i).nil? }
70
70
  item_value = instruction.value_for(item: item_with_value)
71
- typecast_value = ActionSet::Instructions::EntryValue.new(value)
72
- .cast(to: item_value.class)
71
+ typecast_value = Instruction::Value.new(value)
72
+ .cast(to: item_value.class)
73
+
73
74
  memo[keypath] = typecast_value
74
75
  end
75
76
  end
@@ -1,18 +1,24 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require_relative './sort_linker'
4
- require_relative './paginator'
3
+ require_relative './sort/current_direction_for_helper'
4
+ require_relative './sort/description_for_helper'
5
+ require_relative './sort/link_for_helper'
6
+ require_relative './sort/next_direction_for_helper'
7
+ require_relative './sort/path_for_helper'
8
+ require_relative './pagination/links_for_helper'
9
+ require_relative './pagination/path_for_helper'
5
10
 
6
11
  module ActionSet
7
12
  module Helpers
8
13
  module HelperMethods
9
- def sort_link_for(column, title = nil)
10
- Helpers::SortLinker.new(self).render(column, title)
11
- end
14
+ include Sort::CurrentDirectionForHelper
15
+ include Sort::DescriptionForHelper
16
+ include Sort::LinkForHelper
17
+ include Sort::NextDirectionForHelper
18
+ include Sort::PathForHelper
12
19
 
13
- def pagination_links_for(set)
14
- Helpers::Paginator.new(self).render(set)
15
- end
20
+ include Pagination::PathForHelper
21
+ include Pagination::LinksForHelper
16
22
  end
17
23
  end
18
24
  end
@@ -0,0 +1,79 @@
1
+ module Pagination
2
+ module LinksForHelper
3
+ def pagination_links_for(set)
4
+ content_tag(:nav, class: 'pagination', 'aria-label': 'Page navigation') do
5
+ safe_join([
6
+ link_to_first_page(set),
7
+ link_to_prev_page(set),
8
+ description_of_current_page(set),
9
+ link_to_next_page(set),
10
+ link_to_last_page(set)
11
+ ])
12
+ end
13
+ end
14
+
15
+ private
16
+
17
+ def description_of_current_page(set)
18
+ description = "Page&nbsp;<strong>#{current_page_for(set)}</strong>&nbsp;of&nbsp;<strong>#{total_pages_for(set)}</strong>".html_safe
19
+
20
+ content_tag(:span,
21
+ description,
22
+ class: 'page-current')
23
+ end
24
+
25
+ def link_to_first_page(set)
26
+ if current_page_for(set) > 1
27
+ link_to('« First', paginate_path_for(1), class: 'page-link page-first')
28
+ else
29
+ content_tag(:span, '« First', class: 'page-link page-first disabled')
30
+ end
31
+ end
32
+
33
+ def link_to_prev_page(set)
34
+ current_page = current_page_for(set)
35
+
36
+ if current_page > 1 && current_page <= total_pages_for(set)
37
+ link_to('‹ Prev', paginate_path_for(current_page - 1), rel: 'prev', class: 'page-link page-prev')
38
+ else
39
+ content_tag(:span, '‹ Prev', class: 'page-link page-prev disabled')
40
+ end
41
+ end
42
+
43
+ def link_to_last_page(set)
44
+ current_page = current_page_for(set)
45
+ total_pages = total_pages_for(set)
46
+
47
+ if current_page != total_pages && current_page <= total_pages
48
+ link_to('Last »', paginate_path_for(total_pages), class: 'page-link page-last')
49
+ else
50
+ content_tag(:span, 'Last »', class: 'page-link page-last disabled')
51
+ end
52
+ end
53
+
54
+ def link_to_next_page(set)
55
+ current_page = current_page_for(set)
56
+ total_pages = total_pages_for(set)
57
+
58
+ if current_page != total_pages && current_page <= total_pages
59
+ link_to('Next ›', paginate_path_for(current_page + 1), rel: 'next', class: 'page-link page-next')
60
+ else
61
+ content_tag(:span, 'Next ›', class: 'page-link page-next disabled')
62
+ end
63
+ end
64
+
65
+ def total_pages_for(set)
66
+ total_set_size = set.set.count
67
+
68
+ (total_set_size.to_f / page_size_for(set)).ceil
69
+ end
70
+
71
+ def current_page_for(set)
72
+ set.instructions.dig(:paginate, :page)
73
+ end
74
+
75
+ def page_size_for(set)
76
+ set.instructions.dig(:paginate, :size)
77
+ end
78
+ end
79
+ end
@@ -0,0 +1,16 @@
1
+ module Pagination
2
+ module PathForHelper
3
+ def paginate_path_for(page)
4
+ page ||= 1
5
+ url_for paginate_params_for(page)
6
+ end
7
+
8
+ private
9
+
10
+ def paginate_params_for(page)
11
+ current_params.merge(paginate: {
12
+ page: page
13
+ })
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,20 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Sort
4
+ module CurrentDirectionForHelper
5
+ def current_sort_direction_for(attribute, format: :short)
6
+ direction = current_params.dig(:sort, attribute).to_s.downcase
7
+
8
+ return _ascending(format) if direction.presence_in %w[asc ascending]
9
+ return _descending(format) if direction.presence_in %w[desc descending]
10
+
11
+ direction
12
+ end
13
+
14
+ def current_params
15
+ return params.to_unsafe_hash if params.respond_to? :to_unsafe_hash
16
+
17
+ params
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,9 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Sort
4
+ module DescriptionForHelper
5
+ def sort_description_for(attribute)
6
+ "sort by '#{attribute}' in #{next_sort_direction_for(attribute, format: :long)} order"
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,11 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Sort
4
+ module LinkForHelper
5
+ def sort_link_for(attribute, name = nil)
6
+ link_to(name || attribute.titleize,
7
+ sort_path_for(attribute),
8
+ 'aria-label': sort_description_for(attribute))
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,30 @@
1
+ # frozen_string_literal: true
2
+
3
+ # depends upon #current_sort_direction_for
4
+
5
+ module Sort
6
+ module NextDirectionForHelper
7
+ def next_sort_direction_for(attribute, format: :short)
8
+ direction = current_sort_direction_for(attribute)
9
+
10
+ return _ascending(format) unless direction
11
+ return _ascending(format) unless direction.presence_in %w[asc desc ascending descending]
12
+ return _ascending(format) if direction.presence_in %w[desc descending]
13
+ return _descending(format) if direction.presence_in %w[asc ascending]
14
+ end
15
+
16
+ private
17
+
18
+ def _ascending(format)
19
+ return 'asc' if format.to_s == 'short'
20
+
21
+ 'ascending'
22
+ end
23
+
24
+ def _descending(format)
25
+ return 'desc' if format.to_s == 'short'
26
+
27
+ 'descending'
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,18 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Sort
4
+ module PathForHelper
5
+ def sort_path_for(attribute)
6
+ url_for sort_params_for(attribute)
7
+ end
8
+
9
+ private
10
+
11
+ def sort_params_for(attribute)
12
+ current_params.merge(only_path: true,
13
+ sort: {
14
+ attribute => next_sort_direction_for(attribute)
15
+ })
16
+ end
17
+ end
18
+ end
@@ -1,8 +1,8 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module ActionSet
4
- module Instructions
5
- class EntryValue
4
+ module Instruction
5
+ class Value
6
6
  attr_reader :raw
7
7
 
8
8
  def initialize(value)
@@ -81,6 +81,7 @@ module ActionSet
81
81
  @possible_typecasters ||= type_class.constants
82
82
  .map(&:to_s)
83
83
  .select { |t| can_typecast?(t) }
84
+ .reject { |t| t == 'Time' }
84
85
  .map { |t| init_typecaster(t) }
85
86
  .compact
86
87
  end
@@ -142,6 +143,7 @@ module ActionSet
142
143
  return if @raw.is_a? @target
143
144
  return unless @target.eql?(ActiveSupport::TimeWithZone)
144
145
  time_value = ActiveModelAdapter.new(@raw, Time).process
146
+
145
147
  return unless time_value.is_a?(Time)
146
148
  return time_value unless time_value.respond_to?(:in_time_zone)
147
149
  time_value.in_time_zone
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module ActionSet
4
- VERSION = '0.4.0'
4
+ VERSION = '0.4.1'
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: actionset
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.0
4
+ version: 0.4.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Stephen Margheim
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2018-08-20 00:00:00.000000000 Z
11
+ date: 2018-08-29 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activeset
@@ -52,6 +52,20 @@ dependencies:
52
52
  - - ">="
53
53
  - !ruby/object:Gem::Version
54
54
  version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rails
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: 5.1.0
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: 5.1.0
55
69
  - !ruby/object:Gem::Dependency
56
70
  name: bundler
57
71
  requirement: !ruby/object:Gem::Requirement
@@ -228,9 +242,14 @@ files:
228
242
  - config.ru
229
243
  - lib/action_set.rb
230
244
  - lib/action_set/helpers/helper_methods.rb
231
- - lib/action_set/helpers/paginator.rb
232
- - lib/action_set/helpers/sort_linker.rb
233
- - lib/action_set/instructions/entry_value.rb
245
+ - lib/action_set/helpers/pagination/links_for_helper.rb
246
+ - lib/action_set/helpers/pagination/path_for_helper.rb
247
+ - lib/action_set/helpers/sort/current_direction_for_helper.rb
248
+ - lib/action_set/helpers/sort/description_for_helper.rb
249
+ - lib/action_set/helpers/sort/link_for_helper.rb
250
+ - lib/action_set/helpers/sort/next_direction_for_helper.rb
251
+ - lib/action_set/helpers/sort/path_for_helper.rb
252
+ - lib/action_set/instruction/value.rb
234
253
  - lib/action_set/version.rb
235
254
  homepage: https://github.com/fractaledmind/actionset
236
255
  licenses:
@@ -1,165 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module ActionSet
4
- module Helpers
5
- class Paginator
6
- def initialize(template)
7
- @template = template
8
- @info = {}
9
- end
10
-
11
- def render(set)
12
- @info = info_for_set(set)
13
-
14
- content_tag(:ul, class: 'pagination') do
15
- safe_join([
16
- link_to_first_page,
17
- link_to_prev_page,
18
- link_to_prev_gap,
19
- safe_join(relevant_page_numbers.map do |num|
20
- page = PageInterface.new(num, @info)
21
- link = link_to(page, page_link_url(page), rel: page.rel)
22
- content_tag(:li, link, class: ['page', (page.current? ? 'active' : nil)].compact)
23
- end),
24
- link_to_next_gap,
25
- link_to_next_page,
26
- link_to_last_page
27
- ].compact)
28
- end
29
- end
30
-
31
- private
32
-
33
- def info_for_set(set)
34
- {
35
- current_page: set.instructions.dig(:paginate, :page),
36
- page_size: set.instructions.dig(:paginate, :size),
37
- total_pages: (set.total_count.to_f / set.instructions.dig(:paginate, :size)).ceil,
38
- left_window_size: 2,
39
- right_window_size: 2
40
- }
41
- end
42
-
43
- def relevant_page_numbers
44
- current_page = [@info[:current_page]]
45
- left_window = [*(@info[:current_page] - @info[:left_window_size])..@info[:current_page]]
46
- right_window = [*@info[:current_page]..(@info[:current_page] + @info[:right_window_size])]
47
-
48
- (left_window | current_page | right_window).sort.reject { |x| (x < 1) || (x > @info[:total_pages]) }
49
- end
50
-
51
- def page_link_url(page_number)
52
- request.query_parameters
53
- .deep_merge(controller: controller.controller_path,
54
- action: controller.action_name,
55
- paginate: {
56
- page: page_number
57
- })
58
- end
59
-
60
- def link_to_prev_gap
61
- return unless @info[:current_page] > 1
62
- return unless @info[:current_page] <= @info[:total_pages]
63
- return unless (@info[:current_page] - 1) > (@info[:left_window_size] + 1)
64
-
65
- link = link_to('…', '#', onclick: 'return false;')
66
- content_tag(:li, link, class: 'page prev_gap disabled')
67
- end
68
-
69
- def link_to_next_gap
70
- return unless @info[:current_page] >= 1
71
- return unless @info[:current_page] <= @info[:total_pages]
72
- return unless (@info[:total_pages] - @info[:current_page]) > (@info[:right_window_size] + 1)
73
-
74
- link = link_to('…', '#', onclick: 'return false;')
75
- content_tag(:li, link, class: 'page next_gap disabled')
76
- end
77
-
78
- def link_to_first_page
79
- return unless @info[:current_page] > 1
80
-
81
- link = link_to('« First', page_link_url(1))
82
- content_tag(:li, link, class: 'page first')
83
- end
84
-
85
- def link_to_prev_page
86
- return unless @info[:current_page] > 1
87
- return unless @info[:current_page] <= @info[:total_pages]
88
-
89
- link = link_to('‹ Prev', page_link_url(@info[:current_page] - 1), rel: 'prev')
90
- content_tag(:li, link, class: 'page prev')
91
- end
92
-
93
- def link_to_last_page
94
- return unless @info[:current_page] != @info[:total_pages]
95
- return unless @info[:current_page] <= @info[:total_pages]
96
-
97
- link = link_to('Last »', page_link_url(@info[:total_pages]))
98
- content_tag(:li, link, class: 'page last')
99
- end
100
-
101
- def link_to_next_page
102
- return unless @info[:current_page] != @info[:total_pages]
103
- return unless @info[:current_page] <= @info[:total_pages]
104
-
105
- link = link_to('Next ›', page_link_url(@info[:current_page] + 1), rel: 'next')
106
- content_tag(:li, link, class: 'page next')
107
- end
108
-
109
- # delegates view helper methods to @template
110
- def method_missing(name, *args, &block)
111
- @template.respond_to?(name) ? @template.send(name, *args, &block) : super
112
- end
113
-
114
- # Wraps a "page number" and provides some utility methods
115
- class PageInterface
116
- def initialize(page, info)
117
- @page = page
118
- @info = info
119
- end
120
-
121
- def number
122
- @page
123
- end
124
-
125
- def current?
126
- @page == @info[:current_page]
127
- end
128
-
129
- def first?
130
- @page == 1
131
- end
132
-
133
- def last?
134
- @page == @info[:total_pages]
135
- end
136
-
137
- def prev?
138
- @page == @info[:current_page] - 1
139
- end
140
-
141
- def next?
142
- @page == @info[:current_page] + 1
143
- end
144
-
145
- def rel
146
- return 'next' if next?
147
- return 'prev' if prev?
148
- nil
149
- end
150
-
151
- def out_of_range?
152
- @page > @info[:total_pages]
153
- end
154
-
155
- def to_i
156
- number
157
- end
158
-
159
- def to_s
160
- number.to_s
161
- end
162
- end
163
- end
164
- end
165
- end
@@ -1,60 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module ActionSet
4
- module Helpers
5
- class SortLinker
6
- def initialize(template)
7
- @template = template
8
- end
9
-
10
- def render(column, title = nil)
11
- column = column.to_s
12
- title ||= sort_link_title(column)
13
-
14
- @template.link_to title, sort_link_url(column)
15
- end
16
-
17
- private
18
-
19
- def sort_link_title(column)
20
- I18n.t(column,
21
- scope: :attributes,
22
- default: default_sort_link_title(column))
23
- end
24
-
25
- def sort_link_url(column)
26
- @template.request
27
- .query_parameters
28
- .merge(controller: @template.controller.controller_path,
29
- action: @template.controller.action_name,
30
- sort: {
31
- column => sort_link_direction(column)
32
- })
33
- end
34
-
35
- def sort_link_direction(column)
36
- return 'asc' unless sort_params_include_column?(column)
37
- return 'asc' unless sort_params_only_include_valid_directions?
38
-
39
- sort_direction_inverses = { 'asc' => 'desc', 'desc' => 'asc' }
40
- sort_direction_inverses[@template.params.dig(:sort, column).to_s]
41
- end
42
-
43
- def default_sort_link_title(column)
44
- ActiveSet::Instructions::Entry::KeyPath.new(column).titleized
45
- end
46
-
47
- def sort_params_include_column?(column)
48
- @template.params[:sort]&.keys&.include?(column.to_s)
49
- end
50
-
51
- def sort_params_only_include_valid_directions?
52
- (sort_params_directions - %w[asc desc]).empty?
53
- end
54
-
55
- def sort_params_directions
56
- @template.params[:sort]&.values&.map(&:to_s)&.uniq
57
- end
58
- end
59
- end
60
- end