diff --git a/plugins/profile_images/lib/profile_images_plugin.rb b/plugins/profile_images/lib/profile_images_plugin.rb new file mode 100644 index 0000000..aa461f8 --- /dev/null +++ b/plugins/profile_images/lib/profile_images_plugin.rb @@ -0,0 +1,23 @@ +class ProfileImagesPlugin < Noosfero::Plugin + def self.plugin_name + 'ProfileImagesPlugin' + end + + def self.plugin_description + _('Adds a block that lists all images inside a profile.') + end + + def self.extra_blocks + { + ProfileImagesPlugin::ProfileImagesBlock => { type: [Person, Community, Enterprise] } + } + end + + def self.has_admin_url? + false + end + + def stylesheet? + true + end +end diff --git a/plugins/profile_images/lib/profile_images_plugin/profile_images_block.rb b/plugins/profile_images/lib/profile_images_plugin/profile_images_block.rb new file mode 100644 index 0000000..5a7753c --- /dev/null +++ b/plugins/profile_images/lib/profile_images_plugin/profile_images_block.rb @@ -0,0 +1,49 @@ +class ProfileImagesPlugin::ProfileImagesBlock < Block + attr_accessible :limit + settings_items :limit, type: :integer, default: 6 + + def view_title + self.default_title + end + + def images + images = owner.articles.images + self.limit.nil? ? images : images.first(self.get_limit) + end + + def extra_option + { } + end + + def self.description + _('Display the images inside the context where the block is available.') + end + + def help + _('This block lists the images inside this profile.') + end + + def default_title + _('Profile images') + end + + def api_content + content = [] + images.each do |image| + content << { title: image.title, view_url: image.view_url, path: image.public_filename(:thumb), id: image.id } + end + { images: content } + end + + def display_api_content_by_default? + false + end + + def timeout + 4.hours + end + + def self.expire_on + { profile: [:article] } + end +end diff --git a/plugins/profile_images/public/style.css b/plugins/profile_images/public/style.css new file mode 100644 index 0000000..1d94b08 --- /dev/null +++ b/plugins/profile_images/public/style.css @@ -0,0 +1,28 @@ +.profile-images-block ul { + padding: 0; + display: block; + width: 100%; +} + +.profile-images-block li { + list-style: none; +} + +.profile-images-block a { + display: block; + width: 53px; + height: 53px; + float: left; + padding: 1px; + border: 1px solid #ccc; + margin: 3px; + background-size: cover; +} + +.profile-images-block a:hover { + border: 1px solid #000; +} + +.profile-images-block a span { + display: none; +} diff --git a/plugins/profile_images/test/test_helper.rb b/plugins/profile_images/test/test_helper.rb new file mode 100644 index 0000000..70322cf --- /dev/null +++ b/plugins/profile_images/test/test_helper.rb @@ -0,0 +1 @@ +require_relative '../../../test/test_helper' diff --git a/plugins/profile_images/test/unit/profile_images_block_test.rb b/plugins/profile_images/test/unit/profile_images_block_test.rb new file mode 100644 index 0000000..fd52e84 --- /dev/null +++ b/plugins/profile_images/test/unit/profile_images_block_test.rb @@ -0,0 +1,69 @@ +require_relative '../test_helper' + +class ProfileImagesBlockTest < ActiveSupport::TestCase + should 'describe itself' do + assert_not_equal Block.description, ProfileImagesPlugin::ProfileImagesBlock.description + end + + should 'is editable' do + block = ProfileImagesPlugin::ProfileImagesBlock.new + assert block.editable? + end + + should 'return images' do + # profile + # |- image1 + # |- file + # |- folder1/ + # |--- image2 + # |--- folder2/ + # |------ image3 + profile = create(Profile, name: 'Test') + image1 = create(UploadedFile, uploaded_data: fixture_file_upload('/files/rails.png', 'image/png'), profile: profile) + file = fast_create(UploadedFile, profile_id: profile.id) + folder1 = fast_create(Folder, profile_id: profile.id) + image2 = create(UploadedFile, uploaded_data: fixture_file_upload('/files/rails.png', 'image/png'), profile: profile, parent: folder1) + folder2 = fast_create(Folder, parent_id: folder1.id) + image3 = create(UploadedFile, uploaded_data: fixture_file_upload('/files/rails.png', 'image/png'), profile: profile, parent: folder2) + + block = ProfileImagesPlugin::ProfileImagesBlock.new + block.stubs(:owner).returns(profile) + + assert_equal [image1, image2, image3].map(&:id), block.images.map(&:id) + end + + should 'return images with limit' do + # profile + # |- image1 + # |- image2 + profile = create(Profile, name: 'Test') + image1 = create(UploadedFile, uploaded_data: fixture_file_upload('/files/rails.png', 'image/png'), profile: profile) + image2 = create(UploadedFile, uploaded_data: fixture_file_upload('/files/rails.png', 'image/png'), profile: profile) + + block = ProfileImagesPlugin::ProfileImagesBlock.new + block.stubs(:owner).returns(profile) + block.limit = 1 + + assert_equal [image1.id], block.images.map(&:id) + end +end + +require 'boxes_helper' + +class ProfileImagesBlockViewTest < ActionView::TestCase + include BoxesHelper + + should 'return images in api_content' do + # profile + # |- image1 + # |- image2 + profile = create(Profile, name: 'Test') + image1 = create(UploadedFile, uploaded_data: fixture_file_upload('/files/rails.png', 'image/png'), profile: profile) + image2 = create(UploadedFile, uploaded_data: fixture_file_upload('/files/rails.png', 'image/png'), profile: profile) + + block = ProfileImagesPlugin::ProfileImagesBlock.new + block.stubs(:owner).returns(profile) + + assert_equal [image1.id, image2.id], block.api_content[:images].map{ |a| a[:id] } + end +end diff --git a/plugins/profile_images/views/blocks/profile_images.html.erb b/plugins/profile_images/views/blocks/profile_images.html.erb new file mode 100644 index 0000000..54ae68f --- /dev/null +++ b/plugins/profile_images/views/blocks/profile_images.html.erb @@ -0,0 +1,14 @@ +<%= block_title(block.view_title, block.subtitle) %> + +