Class: ATDIS::Models::Pagination

Inherits:
ATDIS::Model show all
Defined in:
lib/atdis/models/pagination.rb

Instance Attribute Summary

Attributes inherited from ATDIS::Model

#attributes, #attributes_before_type_cast, #json_left_overs, #json_load_error, #timezone, #url

Instance Method Summary collapse

Methods inherited from ATDIS::Model

attribute_keys, attribute_names, cast, cast_datetime, cast_geojson, cast_integer, cast_string, cast_uri, hash_symbols_to_string, #initialize, interpret, #json_errors, #json_errors_in_children, #json_errors_local, #json_left_overs_is_empty, #json_loaded_correctly!, partition_by_used, read_json, read_url, read_url_raw, #used_attribute?

Constructor Details

This class inherits a constructor from ATDIS::Model

Instance Method Details

#all_pagination_is_presentObject

If some of the pagination fields are present all of the required ones should be present



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/atdis/models/pagination.rb', line 20

def all_pagination_is_present
  if current.nil?
    errors.add(
      :current,
      ErrorMessage["should be present if pagination is being used", "6.4"]
    )
  end
  if per_page.nil?
    errors.add(
      :per_page,
      ErrorMessage["should be present if pagination is being used", "6.4"]
    )
  end
  if count.nil?
    errors.add(
      :count,
      ErrorMessage["should be present if pagination is being used", "6.4"]
    )
  end
  return unless pages.nil?

  errors.add(
    :pages,
    ErrorMessage["should be present if pagination is being used", "6.4"]
  )
end

#count_is_consistentObject



107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/atdis/models/pagination.rb', line 107

def count_is_consistent
  if pages && per_page && count && count > pages * per_page
    errors.add(
      :count,
      ErrorMessage["is larger than can be retrieved through paging", "6.4"]
    )
  end
  return unless pages &&
                per_page &&
                count &&
                count.positive? &&
                count <= (pages - 1) * per_page

  errors.add(
    :count,
    ErrorMessage["could fit into a smaller number of pages", "6.4"]
  )
end

#current_is_consistentObject



92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/atdis/models/pagination.rb', line 92

def current_is_consistent
  if current && pages && current > pages
    errors.add(
      :current,
      ErrorMessage["is larger than the number of pages", "6.4"]
    )
  end
  return unless current && current < 1

  errors.add(
    :current,
    ErrorMessage["can not be less than 1", "6.4"]
  )
end

#next_is_consistentObject



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/atdis/models/pagination.rb', line 68

def next_is_consistent
  if self.next && current && self.next != current + 1
    errors.add(
      :next,
      ErrorMessage[
        "should be one greater than current page number or null if last page",
        "6.4"
      ]
    )
  end
  if self.next.nil? && current != pages
    errors.add(
      :next,
      ErrorMessage["can't be null if not on the last page", "6.4"]
    )
  end
  return unless self.next && current == pages

  errors.add(
    :next,
    ErrorMessage["should be null if on the last page", "6.4"]
  )
end

#previous_is_consistentObject



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/atdis/models/pagination.rb', line 47

def previous_is_consistent
  if previous && current && previous != current - 1
    errors.add(
      :previous,
      ErrorMessage["should be one less than current page number or null if first page", "6.4"]
    )
  end
  if previous && current && current == 1
    errors.add(
      :previous,
      ErrorMessage["should be null if on the first page", "6.4"]
    )
  end
  return unless previous.nil? && current && current > 1

  errors.add(
    :previous,
    ErrorMessage["can't be null if not on the first page", "6.4"]
  )
end