diff --git a/app/helpers/content_viewer_helper.rb b/app/helpers/content_viewer_helper.rb index 4a64d29..e51841b 100644 --- a/app/helpers/content_viewer_helper.rb +++ b/app/helpers/content_viewer_helper.rb @@ -2,6 +2,7 @@ module ContentViewerHelper include BlogHelper include ForumHelper + include DatesHelper def display_number_of_comments(n) base_str = "#{n}" @@ -24,8 +25,9 @@ module ContentViewerHelper unless args[:no_comments] || !article.accept_comments comments = (" - %s") % link_to_comments(article) end + date_format = show_with_right_format_date article title << content_tag('span', - content_tag('span', show_date(article.published_at), :class => 'date') + + date_format + content_tag('span', _(", by %s") % (article.author ? link_to(article.author_name, article.author_url) : article.author_name), :class => 'author') + content_tag('span', comments, :class => 'comments'), :class => 'created-at' @@ -34,6 +36,24 @@ module ContentViewerHelper title end + def show_with_right_format_date article + date_format = article.environment.date_format + use_numbers = false + year = true + left_time = false + if date_format == 'numbers_with_year' + use_numbers = true + elsif date_format == 'numbers' + use_numbers = true + year = false + elsif date_format == 'month_name' + year = false + elsif date_format == 'past_time' + left_time = true + end + content_tag('span', show_date(article.published_at, use_numbers , year, left_time), :class => 'date') + end + def link_to_comments(article, args = {}) return '' unless article.accept_comments? reference_to_article number_of_comments(article), article, 'comments_list' diff --git a/app/helpers/dates_helper.rb b/app/helpers/dates_helper.rb index 1a9b246..87dc349 100644 --- a/app/helpers/dates_helper.rb +++ b/app/helpers/dates_helper.rb @@ -2,6 +2,7 @@ require 'noosfero/i18n' module DatesHelper + include ActionView::Helpers::DateHelper def months I18n.t('date.month_names') end @@ -15,10 +16,12 @@ module DatesHelper end # formats a date for displaying. - def show_date(date, use_numbers = false, year=true) + def show_date(date, use_numbers = false, year = true, left_time = false) if date && use_numbers date_format = year ? _('%{month}/%{day}/%{year}') : _('%{month}/%{day}') date_format % { :day => date.day, :month => date.month, :year => date.year } + elsif date && left_time + date_format = time_ago_in_words(date) elsif date date_format = year ? _('%{month_name} %{day}, %{year}') : _('%{month_name} %{day}') date_format % { :day => date.day, :month_name => month_name(date.month), :year => date.year } diff --git a/app/models/environment.rb b/app/models/environment.rb index 502d07b..321551d 100644 --- a/app/models/environment.rb +++ b/app/models/environment.rb @@ -3,7 +3,17 @@ # domains. class Environment < ActiveRecord::Base - attr_accessible :name, :is_default, :signup_welcome_text_subject, :signup_welcome_text_body, :terms_of_use, :message_for_disabled_enterprise, :news_amount_by_folder, :default_language, :languages, :description, :organization_approval_method, :enabled_plugins, :enabled_features, :redirection_after_login, :redirection_after_signup, :contact_email, :theme, :reports_lower_bound, :noreply_email, :signup_welcome_screen_body, :members_whitelist_enabled, :members_whitelist, :highlighted_news_amount, :portal_news_amount + attr_accessible :name, :is_default, :signup_welcome_text_subject, + :signup_welcome_text_body, :terms_of_use, + :message_for_disabled_enterprise, :news_amount_by_folder, + :default_language, :languages, :description, + :organization_approval_method, :enabled_plugins, + :enabled_features, :redirection_after_login, + :redirection_after_signup, :contact_email, :theme, + :reports_lower_bound, :noreply_email, + :signup_welcome_screen_body, :members_whitelist_enabled, + :members_whitelist, :highlighted_news_amount, + :portal_news_amount, :date_format has_many :users @@ -14,6 +24,12 @@ class Environment < ActiveRecord::Base IDENTIFY_SCRIPTS = /(php[0-9s]?|[sp]htm[l]?|pl|py|cgi|rb)/ + validates_inclusion_of :date_format, + :in => [ 'numbers_with_year', 'numbers', + 'month_name_with_year', 'month_name', + 'past_time'], + :if => :date_format + def self.verify_filename(filename) filename += '.txt' if File.extname(filename) =~ IDENTIFY_SCRIPTS filename diff --git a/app/views/admin_panel/_site_info.html.erb b/app/views/admin_panel/_site_info.html.erb index 062b062..8641313 100644 --- a/app/views/admin_panel/_site_info.html.erb +++ b/app/views/admin_panel/_site_info.html.erb @@ -3,6 +3,21 @@ <%= labelled_form_field(_('No reply email'), text_field(:environment, :noreply_email)) %> <% themes_options = Theme.system_themes.map {|theme| [theme.name, theme.id] }.sort %> <%= labelled_form_field(_('Theme'), select(:environment, :theme, options_for_select(themes_options, environment.theme))) %> + +<%= labelled_form_field( + _("Article's date format"), + select(:environment, :date_format, + options_for_select([ + [ _('mm/dd/yyyy'), 'numbers_with_year'], + [ _('mm/dd'), 'numbers'], + [ _('Month dd, yyyy'), 'month_name_with_year'], + [ _('Month dd'), 'month_name'], + [ _('X minutes/hours/days/months/years ago'), 'past_time'] + ], environment.date_format + ) + ) +) %> + <%= required f.text_field(:reports_lower_bound, :size => 3) %> <%= labelled_form_field(_('Default language'), select(:environment, :default_language, environment.locales.invert, { :selected => environment.default_locale, :include_blank => true })) %> <%= label_tag :languages, _('Available languages') %> diff --git a/db/migrate/20150529180110_add_date_format_to_environment.rb b/db/migrate/20150529180110_add_date_format_to_environment.rb new file mode 100644 index 0000000..594818e --- /dev/null +++ b/db/migrate/20150529180110_add_date_format_to_environment.rb @@ -0,0 +1,9 @@ +class AddDateFormatToEnvironment < ActiveRecord::Migration + def up + add_column :environments, :date_format, :string, :default => 'month_name_with_year' + end + + def down + remove_column :environments, :date_format + end +end diff --git a/test/functional/admin_panel_controller_test.rb b/test/functional/admin_panel_controller_test.rb index d25dca7..4e652db 100644 --- a/test/functional/admin_panel_controller_test.rb +++ b/test/functional/admin_panel_controller_test.rb @@ -130,6 +130,20 @@ class AdminPanelControllerTest < ActionController::TestCase assert_equal "This is my new environment", Environment.default.message_for_disabled_enterprise end + should 'save site article date format option' do + post :site_info, :environment => { :date_format => "numbers_with_year" } + assert_redirected_to :action => 'index' + + assert_equal "numbers_with_year", Environment.default.date_format + end + + should 'dont save site article date format option when a invalid option is passed' do + post :site_info, :environment => { :date_format => "invalid_format" } + + assert_match "Date format is not included in the list", @response.body + assert_equal "month_name_with_year", Environment.default.date_format + end + should 'set portal community' do e = Environment.default @controller.stubs(:environment).returns(e) diff --git a/test/test_helper.rb b/test/test_helper.rb index b64836b..96366a5 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -262,10 +262,6 @@ module NoosferoTestHelper arg end - def show_date(date) - date.to_s - end - def strip_tags(html) html.gsub(/<[^>]+>/, '') end diff --git a/test/unit/content_viewer_helper_test.rb b/test/unit/content_viewer_helper_test.rb index 6dbb90b..9f8e0d6 100644 --- a/test/unit/content_viewer_helper_test.rb +++ b/test/unit/content_viewer_helper_test.rb @@ -18,7 +18,7 @@ class ContentViewerHelperTest < ActionView::TestCase result = article_title(post) assert_tag_in_string result, :tag => 'span', :content => show_date(post.published_at) end - + should 'display published-at for forum posts' do forum = fast_create(Forum, :name => 'Forum test', :profile_id => profile.id) post = TextileArticle.create!(:name => 'post test', :profile => profile, :parent => forum) @@ -112,6 +112,42 @@ class ContentViewerHelperTest < ActionView::TestCase assert_match 'bt-bookmark.gif', addthis_image_tag end + should 'show date with mm/dd/yyyy' do + Environment.any_instance.stubs(:date_format).returns('numbers_with_year') + article = TextileArticle.new(:name => 'post for test', :body => 'post for test', :profile => profile) + article.published_at = Time.zone.local(2007, 2, 1, 15, 30, 45) + article.save! + result = show_with_right_format_date article + assert_match /2\/1\/2007/, result + end + + should 'show date with mm/dd' do + Environment.any_instance.stubs(:date_format).returns('numbers') + article = TextileArticle.new(:name => 'post for test', :body => 'post for test', :profile => profile) + article.published_at = Time.zone.local(2007, 2, 1, 15, 30, 45) + article.save! + result = show_with_right_format_date article + assert_match /2\/1/, result + end + + should 'show date with month name' do + Environment.any_instance.stubs(:date_format).returns('month_name') + article = TextileArticle.new(:name => 'post for test', :body => 'post for test', :profile => profile) + article.published_at = Time.zone.local(2007, 2, 1, 15, 30, 45) + article.save! + result = show_with_right_format_date article + assert_match /February 1/, result + end + + should 'show date with month name and year' do + Environment.any_instance.stubs(:date_format).returns('month_name_with_year') + article = TextileArticle.new(:name => 'post for test', :body => 'post for test', :profile => profile) + article.published_at = Time.zone.local(2007, 2, 1, 15, 30, 45) + article.save! + result = show_with_right_format_date article + assert_match /February 1, 2007/, result + end + protected include NoosferoTestHelper include ActionView::Helpers::TextHelper diff --git a/test/unit/dates_helper_test.rb b/test/unit/dates_helper_test.rb index eb56682..55c7da6 100644 --- a/test/unit/dates_helper_test.rb +++ b/test/unit/dates_helper_test.rb @@ -146,4 +146,9 @@ class DatesHelperTest < ActiveSupport::TestCase assert_equal Date.new(Date.today.year, Date.today.month, 1), build_date('', '') end + should 'show how long it has passed since a specific date' do + date = Time.zone.now + assert_equal show_date(date, false, false, true), time_ago_in_words(date) + end + end diff --git a/test/unit/environment_test.rb b/test/unit/environment_test.rb index 460fc74..1fed0f9 100644 --- a/test/unit/environment_test.rb +++ b/test/unit/environment_test.rb @@ -1703,4 +1703,32 @@ class EnvironmentTest < ActiveSupport::TestCase assert !e.has_license? end + should 'validates_inclusion_of date format' do + environment = fast_create(Environment) + + environment.date_format = "invalid_format" + environment.valid? + assert environment.errors[:date_format.to_s].present? + + environment.date_format = "numbers_with_year" + environment.valid? + assert !environment.errors[:date_format.to_s].present? + + environment.date_format = "numbers" + environment.valid? + assert !environment.errors[:date_format.to_s].present? + + environment.date_format = "month_name_with_year" + environment.valid? + assert !environment.errors[:date_format.to_s].present? + + environment.date_format = "month_name" + environment.valid? + assert !environment.errors[:date_format.to_s].present? + + environment.date_format = "past_time" + environment.valid? + assert !environment.errors[:date_format.to_s].present? + end + end -- libgit2 0.21.2