diff --git a/app/design_blocks/favorite_links/controllers/favorite_links_controller.rb b/app/design_blocks/favorite_links/controllers/favorite_links_controller.rb new file mode 100644 index 0000000..0ad52b5 --- /dev/null +++ b/app/design_blocks/favorite_links/controllers/favorite_links_controller.rb @@ -0,0 +1,73 @@ +class FavoriteLinksController < 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_favorite_links + design_render + end + + def edit + design_render_on_edit + end + + def save + if @design_block.update_attributes(params[:design_block]) + get_favorite_links + design_render_on_edit :action => 'manage_links' + else + design_render_on_edit :nothing => true + end + end + + def manage_links + get_favorite_links + design_render_on_edit + end + + def add_link + design_render_on_edit + end + + def remove_link + @design_block.delete_link(params[:link]) + get_favorite_links + design_render_on_edit :action => 'manage_links' + end + + def get_favorite_links + favorite_links = @design_block.favorite_links + @favorite_links_pages, @favorite_links = paginate_by_collection favorite_links + end + + def paginate_by_collection(collection, options = {}) + page = ( 1).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 diff --git a/app/design_blocks/favorite_links/models/favorite_links.rb b/app/design_blocks/favorite_links/models/favorite_links.rb new file mode 100644 index 0000000..dcfcc9c --- /dev/null +++ b/app/design_blocks/favorite_links/models/favorite_links.rb @@ -0,0 +1,37 @@ +class FavoriteLinks < Design::Block + + def self.description + _('Favorite Links') + end + + def limit_number= value + self.settings[:limit_number] = value.to_i + end + + def limit_number + self.settings[:limit_number] || 5 + end + + def favorite_links_limited + self.favorite_links.first(self.limit_number) + end + + def favorite_links + self.settings[:favorite_links] ||= [] + end + + def delete_link link + self.settings[:favorite_links].reject!{ |item| item == link } + self.save + end + + def favorite_link + nil + end + + def favorite_link= link + self.favorite_links.push(link) + self.favorite_links.uniq! + end + +end diff --git a/app/design_blocks/favorite_links/views/add_link.rhtml b/app/design_blocks/favorite_links/views/add_link.rhtml new file mode 100644 index 0000000..dce29c9 --- /dev/null +++ b/app/design_blocks/favorite_links/views/add_link.rhtml @@ -0,0 +1,13 @@ +

<%= _('Editing Favorite Links') %>

+ +<% design_form_remote_tag( :url => {:action => 'save'}) do %> + +

+ + <%= text_field 'design_block', 'favorite_link'%> +

+ + <%= submit_tag _('Save') %> + +<% end %> + diff --git a/app/design_blocks/favorite_links/views/edit.rhtml b/app/design_blocks/favorite_links/views/edit.rhtml new file mode 100644 index 0000000..e2e9c97 --- /dev/null +++ b/app/design_blocks/favorite_links/views/edit.rhtml @@ -0,0 +1,29 @@ + +

<%= _('Editing Favorite Links') %>

+ + <% design_form_remote_tag( :url => {:action => 'save'}) do %> + +

+ + <%= text_field 'design_block', 'title'%> +

+ +

+ + <%= check_box 'design_block', 'display_header', {}, 'true', 'false' %> +

+ +

+ + <%= check_box 'design_block', 'display_title', {}, 'true', 'false' %> +

+ +

+ + <%= text_field 'design_block', 'limit_number'%> +

+ + <%= submit_tag _('Save') %> + +<% end %> + diff --git a/app/design_blocks/favorite_links/views/index.rhtml b/app/design_blocks/favorite_links/views/index.rhtml new file mode 100644 index 0000000..e0e50c2 --- /dev/null +++ b/app/design_blocks/favorite_links/views/index.rhtml @@ -0,0 +1,16 @@ + +<% if @favorite_links_pages.current.previous%> + <%= design_link_to_remote(_('Previous'), :url => {:action => 'index', :page => @favorite_links_pages.current.previous}) %> +<% end %> +<% if @favorite_links_pages.current.next %> + <%= design_link_to_remote(_('Next'), :url => {:action => 'index', :page => @favorite_links_pages.current.next}) %> +<% end %> + +<% @favorite_links.each do |link| %> + +<% end %> + diff --git a/app/design_blocks/favorite_links/views/manage_links.rhtml b/app/design_blocks/favorite_links/views/manage_links.rhtml new file mode 100644 index 0000000..6e5a10b --- /dev/null +++ b/app/design_blocks/favorite_links/views/manage_links.rhtml @@ -0,0 +1,21 @@ + +

+ <%= _('Favorite Links') %> + <%= design_link_to_remote(_('Add Link'), :url => {:action => 'add_link'} )%> + +

+<% if @favorite_links_pages.current.previous%> + <%= design_link_to_remote(_('Previous'), :url => {:action => 'manage_links', :page => @favorite_links_pages.current.previous}) %> +<% end %> +<% if @favorite_links_pages.current.next %> + <%= design_link_to_remote(_('Next'), :url => {:action => 'manage_links', :page => @favorite_links_pages.current.next}) %> +<% end %> + +<% @favorite_links.each do |link| %> + +<% end %> diff --git a/test/fixtures/design_blocks.yml b/test/fixtures/design_blocks.yml index e2d3642..0693676 100644 --- a/test/fixtures/design_blocks.yml +++ b/test/fixtures/design_blocks.yml @@ -5,42 +5,40 @@ one: box_id: 2 position: 1 type: 'MainBlock' - helper: 'plain_content' - name: 'Main Content' -# Link Blocks + title: 'Main Content' +# FavoriteLinks Blocks two: id: 2 box_id: 1 position: 2 - type: 'LinkBlock' - helper: 'list_content' - name: 'List of Links 1' + type: 'FavoriteLinks' + title: 'List of Links 1' three: id: 3 box_id: 1 position: 3 - type: 'LinkBlock' - helper: 'plain_content' - name: 'List of Link 2' + type: 'FavoriteLinks' + title: 'List of Link 2' #List Blocks -four: - id: 4 - box_id: 1 - position: 1 - type: 'ListBlock' - helper: 'list_content' - name: 'List of Names 1' -five: - id: 5 - box_id: 3 - position: 1 - type: 'ListBlock' - helper: 'list_content' - name: 'List of Names 2' -six: - id: 6 - box_id: 3 - position: 2 - type: 'ListBlock' - helper: 'list_content' - name: 'List of Names 3' +#FIXME Put other blocks to works +#four: +# id: 4 +# box_id: 1 +# position: 1 +# type: 'ListBlock' +# helper: 'list_content' +# title: 'List of titles 1' +#five: +# id: 5 +# box_id: 3 +# position: 1 +# type: 'ListBlock' +# helper: 'list_content' +# title: 'List of titles 2' +#six: +# id: 6 +# box_id: 3 +# position: 2 +# type: 'ListBlock' +# helper: 'list_content' +# title: 'List of titles 3' -- libgit2 0.21.2