Class: ThemeCheck::PaginationSize

Inherits:
LiquidCheck show all
Defined in:
lib/theme_check/checks/pagination_size.rb

Constant Summary

Constants inherited from Check

Check::CATEGORIES, Check::SEVERITIES, Check::SEVERITY_VALUES

Instance Attribute Summary collapse

Attributes inherited from Check

#ignored_patterns, #offenses, #options, #theme

Instance Method Summary collapse

Methods included from ChecksTracking

#inherited

Methods included from ParsingHelpers

#outside_of_strings

Methods inherited from Check

#==, #add_offense, all, can_disable, #can_disable?, categories, #categories, category, #code_name, doc, #doc, docs_url, #ignore!, #ignored?, #severity, severity, #severity=, #severity_value, severity_value, single_file, #single_file?, #to_s, #whole_theme?

Methods included from JsonHelpers

#format_json_parse_error

Constructor Details

#initialize(min_size: 1, max_size: 50) ⇒ PaginationSize

Returns a new instance of PaginationSize.



11
12
13
14
# File 'lib/theme_check/checks/pagination_size.rb', line 11

def initialize(min_size: 1, max_size: 50)
  @min_size = min_size
  @max_size = max_size
end

Instance Attribute Details

#max_sizeObject (readonly)

Returns the value of attribute max_size.



9
10
11
# File 'lib/theme_check/checks/pagination_size.rb', line 9

def max_size
  @max_size
end

#min_sizeObject (readonly)

Returns the value of attribute min_size.



8
9
10
# File 'lib/theme_check/checks/pagination_size.rb', line 8

def min_size
  @min_size
end

Instance Method Details

#after_document(_node) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/theme_check/checks/pagination_size.rb', line 39

def after_document(_node)
  @paginations.each_pair do |size, nodes|
    numerical_size = if size.is_a?(Numeric)
      size
    else
      get_setting_default_value(size.lookups.last)
    end
    if numerical_size.nil?
      nodes.each { |node| add_offense("Default pagination size should be defined in the section settings", node: node) }
    elsif numerical_size > @max_size || numerical_size < @min_size || !numerical_size.is_a?(Integer)
      nodes.each { |node| add_offense("Pagination size must be a positive integer between #{@min_size} and #{@max_size}", node: node) }
    end
  end
end

#on_document(_node) ⇒ Object



16
17
18
19
# File 'lib/theme_check/checks/pagination_size.rb', line 16

def on_document(_node)
  @paginations = {}
  @schema_settings = {}
end

#on_paginate(node) ⇒ Object



21
22
23
24
25
26
27
# File 'lib/theme_check/checks/pagination_size.rb', line 21

def on_paginate(node)
  size = node.value.page_size
  unless @paginations.key?(size)
    @paginations[size] = []
  end
  @paginations[size].push(node)
end

#on_schema(node) ⇒ Object



29
30
31
32
33
34
35
36
37
# File 'lib/theme_check/checks/pagination_size.rb', line 29

def on_schema(node)
  schema = JSON.parse(node.value.nodelist.join)

  if (settings = schema["settings"])
    @schema_settings = settings
  end
rescue JSON::ParserError
  # Ignored, handled in ValidSchema.
end