Class: Arrest::HasManyCollection
- Inherits:
-
Object
- Object
- Arrest::HasManyCollection
show all
- Defined in:
- lib/arrest/helper/has_many_collection.rb
Overview
Instance Method Summary
collapse
Constructor Details
#initialize(parent, has_many_attribute) ⇒ HasManyCollection
Returns a new instance of HasManyCollection.
4
5
6
7
8
9
10
11
12
13
14
15
|
# File 'lib/arrest/helper/has_many_collection.rb', line 4
def initialize(parent, has_many_attribute)
@parent = parent
@clazz_name = (StringUtils.classify(has_many_attribute.clazz_name.to_s))
@url_part = has_many_attribute.url_part
@children = nil
@foreign_key_name = (StringUtils.underscore(@parent.class.name).gsub(/^.*\//, '') + '_id').to_sym
define_filters
@attribute = has_many_attribute
@page = 1
@page_size = nil
@per_page = 5
end
|
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(*args, &block) ⇒ Object
22
23
24
|
# File 'lib/arrest/helper/has_many_collection.rb', line 22
def method_missing(*args, &block)
children.send(*args, &block)
end
|
Instance Method Details
#build(attributes = {}) ⇒ Object
17
18
19
20
|
# File 'lib/arrest/helper/has_many_collection.rb', line 17
def build attributes = {}
extended_attrs = attributes.merge({@foreign_key_name => @parent.id})
resolved_class.new(@parent.context, extended_attrs)
end
|
#current_page ⇒ Object
67
68
69
|
# File 'lib/arrest/helper/has_many_collection.rb', line 67
def current_page
@page
end
|
#current_page_count ⇒ Object
87
88
89
|
# File 'lib/arrest/helper/has_many_collection.rb', line 87
def current_page_count @page
end
|
#first_page? ⇒ Boolean
79
80
81
|
# File 'lib/arrest/helper/has_many_collection.rb', line 79
def first_page?
current_page == 1
end
|
#inspect ⇒ Object
26
27
28
|
# File 'lib/arrest/helper/has_many_collection.rb', line 26
def inspect
children.inspect
end
|
83
84
85
|
# File 'lib/arrest/helper/has_many_collection.rb', line 83
def last_page?
current_page >= num_pages
end
|
#limit(count) ⇒ Object
30
31
32
33
34
35
36
|
# File 'lib/arrest/helper/has_many_collection.rb', line 30
def limit(count)
if count != @page_size
@children = nil
end
@page_size = count
self
end
|
#limit_value ⇒ Object
for kaminari
TODO: move to external module
59
60
61
|
# File 'lib/arrest/helper/has_many_collection.rb', line 59
def limit_value @page_size || 0
end
|
#num_pages ⇒ Object
71
72
73
74
75
76
77
|
# File 'lib/arrest/helper/has_many_collection.rb', line 71
def num_pages
if @page_size
(total_count.to_f / @page_size).ceil
else
1
end
end
|
#offset(count) ⇒ Object
38
39
40
41
42
43
44
45
46
47
48
49
|
# File 'lib/arrest/helper/has_many_collection.rb', line 38
def offset(count)
if @page_size
new_page = count / @page_size
else
new_page = 1
end
if new_page != @page
@children = nil
end
@page = new_page
self
end
|
#offset_value ⇒ Object
63
64
65
|
# File 'lib/arrest/helper/has_many_collection.rb', line 63
def offset_value ((@page_size || 0) * (@page - 1)) || 0
end
|
#page(num) ⇒ Object
96
97
98
99
100
101
102
103
104
|
# File 'lib/arrest/helper/has_many_collection.rb', line 96
def page(num)
num ||= 1
if @page_size != @per_page || @page != num
@children = nil
end
@page_size = @per_page
@page = num.to_i
self
end
|
#per(num) ⇒ Object
91
92
93
94
|
# File 'lib/arrest/helper/has_many_collection.rb', line 91
def per(num)
@per_page = num.to_i
self
end
|
#total_count ⇒ Object
51
52
53
54
|
# File 'lib/arrest/helper/has_many_collection.rb', line 51
def total_count
children() @total_count
end
|