favourite_links_controller.rb
1.76 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
class FavouriteLinksController < ApplicationController
# The methods above are specific for noosfero application. I think
# this it not the correct way to get this method.
#
# We can create a method in the app/controllers/profile_admin folder
# the inherit this method and adds only the two lines above.
#
# With this way we can reuse this block on many others case and each case
# we follow the same way.
#
# Specific for app
needs_profile
design :holder => 'profile'
# End specific for app
acts_as_design_block
CONTROL_ACTION_OPTIONS = {
'manage_links' => _('Manage Links'),
'edit' => _('Edit'),
}
def index
get_favourite_links
design_render
end
def edit
design_render_on_edit
end
def save
if @design_block.update_attributes(params[:design_block])
get_favourite_links
design_render_on_edit :action => 'manage_links'
else
design_render_on_edit :nothing => true
end
end
def manage_links
get_favourite_links
design_render_on_edit
end
def add_link
design_render_on_edit
end
def remove_link
@design_block.delete_link(params[:link])
get_favourite_links
design_render_on_edit :action => 'manage_links'
end
def get_favourite_links
favourite_links = @design_block.favourite_links
@favourite_links_pages, @favourite_links = paginate_by_collection favourite_links
end
def paginate_by_collection(collection, options = {})
page = params[:page].blank? ? 1 : params[:page].to_i
items_per_page = @design_block.limit_number
offset = (page - 1) * items_per_page
link_pages = Paginator.new(self, collection.size, items_per_page, page)
collection = collection[offset..(offset + items_per_page - 1)]
return link_pages, collection
end
end