diff --git a/app/models/blog.rb b/app/models/blog.rb index 71e0501..e20bdb4 100644 --- a/app/models/blog.rb +++ b/app/models/blog.rb @@ -1,6 +1,7 @@ class Blog < Folder acts_as_having_posts + include PostsLimit #FIXME This should be used until there is a migration to fix all blogs that # already have folders inside them diff --git a/app/models/forum.rb b/app/models/forum.rb index 5a3c0aa..0298487 100644 --- a/app/models/forum.rb +++ b/app/models/forum.rb @@ -1,6 +1,7 @@ class Forum < Folder acts_as_having_posts :order => 'updated_at DESC' + include PostsLimit def self.type_name _('Forum') diff --git a/app/models/posts_limit.rb b/app/models/posts_limit.rb new file mode 100644 index 0000000..fb39348 --- /dev/null +++ b/app/models/posts_limit.rb @@ -0,0 +1,21 @@ +module PostsLimit + module ClassMethods + def posts_per_page_limit + 15 + end + + def posts_per_page_options + [5, 10, 15] + end + end + + def self.included(klass) + klass.send(:extend, PostsLimit::ClassMethods) + klass.class_eval do + def posts_per_page_with_limit + [self.class.posts_per_page_limit, posts_per_page_without_limit].min + end + alias_method_chain :posts_per_page, :limit + end + end +end diff --git a/app/views/cms/_blog.rhtml b/app/views/cms/_blog.rhtml index 0119d21..0202549 100644 --- a/app/views/cms/_blog.rhtml +++ b/app/views/cms/_blog.rhtml @@ -56,7 +56,7 @@ <%= labelled_form_field(_('How to display posts:'), f.select(:visualization_format, [ [ _('Full post'), 'full'], [ _('First paragraph'), 'short'] ])) %> -<%= labelled_form_field(_('Posts per page:'), f.select(:posts_per_page, [5, 10, 20, 50, 100])) %> +<%= labelled_form_field(_('Posts per page:'), f.select(:posts_per_page, Blog.posts_per_page_options)) %> <%= labelled_check_box(_("List only translated posts"), 'article[display_posts_in_current_language]', '1', @article.display_posts_in_current_language?) %> diff --git a/app/views/cms/_forum.rhtml b/app/views/cms/_forum.rhtml index cbd9e75..e876af5 100644 --- a/app/views/cms/_forum.rhtml +++ b/app/views/cms/_forum.rhtml @@ -10,4 +10,4 @@ <%= labelled_form_field(_('Description:'), text_area(:article, :body, :cols => 64, :rows => 10)) %> -<%= labelled_form_field(_('Posts per page:'), f.select(:posts_per_page, [5, 10, 20, 50, 100])) %> +<%= labelled_form_field(_('Posts per page:'), f.select(:posts_per_page, Forum.posts_per_page_options)) %> diff --git a/test/unit/posts_limit_test.rb b/test/unit/posts_limit_test.rb new file mode 100644 index 0000000..ed0a2e0 --- /dev/null +++ b/test/unit/posts_limit_test.rb @@ -0,0 +1,14 @@ +require File.dirname(__FILE__) + '/../test_helper' + +class PostsLimitTest < ActiveSupport::TestCase + + CLASSES = [Blog, Forum] + + should 'limit posts per_page_page' do + CLASSES.each do |klass| + object = klass.new + object.posts_per_page = klass.posts_per_page_limit + 1 + assert_equal klass.posts_per_page_limit, object.posts_per_page + end + end +end -- libgit2 0.21.2