diff --git a/app/controllers/my_profile/cms_controller.rb b/app/controllers/my_profile/cms_controller.rb
index d3c073c..007ae4b 100644
--- a/app/controllers/my_profile/cms_controller.rb
+++ b/app/controllers/my_profile/cms_controller.rb
@@ -188,6 +188,18 @@ class CmsController < MyProfileController
render :action => 'edit'
end
+ def general_configuration
+ @profile_data = profile
+ if request.post?
+ if @profile_data.update_attributes(params[:profile_data])
+ session[:notice] = _("Article date format updated successfully")
+ else
+ session[:notice] = _("Could not update article's date format")
+ end
+ redirect_to :action => 'index'
+ end
+ end
+
post_only :set_home_page
def set_home_page
return render_access_denied unless user.can_change_homepage?
diff --git a/app/helpers/blog_helper.rb b/app/helpers/blog_helper.rb
index aa08107..a185502 100644
--- a/app/helpers/blog_helper.rb
+++ b/app/helpers/blog_helper.rb
@@ -2,7 +2,6 @@ module BlogHelper
include ArticleHelper
-
def custom_options_for_article(article,tokenized_children)
@article = article
hidden_field_tag('article[published]', 1) +
diff --git a/app/helpers/content_viewer_helper.rb b/app/helpers/content_viewer_helper.rb
index 4a64d29..0a33d60 100644
--- a/app/helpers/content_viewer_helper.rb
+++ b/app/helpers/content_viewer_helper.rb
@@ -24,8 +24,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 +35,24 @@ module ContentViewerHelper
title
end
+ def show_with_right_format_date article
+ date_format = article.profile.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..a00c74c 100644
--- a/app/helpers/dates_helper.rb
+++ b/app/helpers/dates_helper.rb
@@ -15,10 +15,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/profile.rb b/app/models/profile.rb
index 7c5a0e9..3e53d7e 100644
--- a/app/models/profile.rb
+++ b/app/models/profile.rb
@@ -3,7 +3,17 @@
# which by default is the one returned by Environment:default.
class Profile < ActiveRecord::Base
- attr_accessible :name, :identifier, :public_profile, :nickname, :custom_footer, :custom_header, :address, :zip_code, :contact_phone, :image_builder, :description, :closed, :template_id, :environment, :lat, :lng, :is_template, :fields_privacy, :preferred_domain_id, :category_ids, :country, :city, :state, :national_region_code, :email, :contact_email, :redirect_l10n, :notification_time, :redirection_after_login, :email_suggestions, :allow_members_to_invite, :invite_friends_only, :secret
+
+ attr_accessible :name, :identifier, :public_profile, :nickname,
+ :custom_footer, :custom_header, :address, :zip_code,
+ :contact_phone, :image_builder, :description, :closed,
+ :template_id, :environment, :lat, :lng, :is_template,
+ :fields_privacy, :preferred_domain_id, :category_ids,
+ :country, :city, :state, :national_region_code, :email,
+ :contact_email, :redirect_l10n,
+ :notification_time, :redirection_after_login,
+ :email_suggestions, :allow_members_to_invite,
+ :invite_friends_only, :secret, :date_format
# use for internationalizable human type names in search facets
# reimplement on subclasses
@@ -181,6 +191,12 @@ class Profile < ActiveRecord::Base
validates_length_of :description, :maximum => 550, :allow_nil => true
+ validates_inclusion_of :date_format,
+ :in => [ 'numbers_with_year', 'numbers',
+ 'month_name_with_year', 'month_name',
+ 'past_time'],
+ :if => :date_format
+
# Valid identifiers must match this format.
IDENTIFIER_FORMAT = /^#{Noosfero.identifier_format}$/
diff --git a/app/views/cms/general_configuration.html.erb b/app/views/cms/general_configuration.html.erb
new file mode 100644
index 0000000..2a07905
--- /dev/null
+++ b/app/views/cms/general_configuration.html.erb
@@ -0,0 +1,25 @@
+
+
+
<%= _('General Configuration:') %>
+<%= labelled_form_for :profile_data do |f|%>
+ <%= _("Article's date format") %>
+
+ <%= _("This option will define how article's date will be showed") %>
+
+
+ <%= select_tag('profile_data[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']
+ ], @profile_data.date_format)) %>
+
+
+
+
+ <%= submit_button('save', _('Save')) %>
+ <%= modal_close_button(_('Cancel')) %>
+<% end %>
+
+
diff --git a/app/views/cms/view.html.erb b/app/views/cms/view.html.erb
index b9a2b0c..ec0af1b 100644
--- a/app/views/cms/view.html.erb
+++ b/app/views/cms/view.html.erb
@@ -18,6 +18,7 @@
<% parent_id = ((@article && @article.allow_children?) ? @article : nil) %>
<%= modal_button('new', _('New content'), :action => 'new', :parent_id => parent_id, :cms => true) %>
+ <%= modal_button('edit', _('General Configuration'), :action => 'general_configuration', :parent_id => parent_id, :cms => true) %>
<%= button(:back, _('Back to control panel'), :controller => 'profile_editor', :action => "index") %>
<% end %>
diff --git a/db/migrate/20150529180110_add_date_format_to_profile.rb b/db/migrate/20150529180110_add_date_format_to_profile.rb
new file mode 100644
index 0000000..36d4dee
--- /dev/null
+++ b/db/migrate/20150529180110_add_date_format_to_profile.rb
@@ -0,0 +1,9 @@
+class AddDateFormatToProfile < ActiveRecord::Migration
+ def up
+ add_column :profiles, :date_format, :string, :default => 'month_name_with_year'
+ end
+
+ def down
+ remove_column :profiles, :date_format
+ end
+end
diff --git a/public/stylesheets/application.css b/public/stylesheets/application.css
index c441388..764d080 100644
--- a/public/stylesheets/application.css
+++ b/public/stylesheets/application.css
@@ -3462,6 +3462,11 @@ table.cms-articles .icon:hover {
width: 455px;
}
+.general-configuration {
+ padding: 5px 20px;
+ width: 455px;
+}
+
.article-types {
padding-left: 5px;
margin-top: 20px;
diff --git a/test/functional/cms_controller_test.rb b/test/functional/cms_controller_test.rb
index 11d1ed7..91be37c 100644
--- a/test/functional/cms_controller_test.rb
+++ b/test/functional/cms_controller_test.rb
@@ -1869,6 +1869,24 @@ class CmsControllerTest < ActionController::TestCase
assert_equal '[{"label":"linux","value":"linux"}]', @response.body
end
+ should 'save general configuration when is passed a valid value' do
+ post :general_configuration, profile: profile.identifier, :profile_data => { date_format: "numbers_with_year" }
+ profile.reload
+
+ assert_equal profile.date_format, "numbers_with_year"
+ end
+
+ should 'not save general configuration when is not passed a valid value' do
+ profile.date_format = "month_name_with_year"
+ profile.save!
+
+ post :general_configuration, profile: profile.identifier, :profile_data => { date_format: "invalid_format" }
+ profile.reload
+
+ assert_equal profile.date_format, "month_name_with_year"
+ end
+
+
protected
# FIXME this is to avoid adding an extra dependency for a proper JSON parser.
diff --git a/test/unit/content_viewer_helper_test.rb b/test/unit/content_viewer_helper_test.rb
index 6dbb90b..9ba2fa5 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
+ 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!
+ profile.date_format = "numbers_with_year"
+ result = show_with_right_format_date article
+ assert_match /2\/1\/2007/, result
+ end
+
+ should 'show date with mm/dd' do
+ 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!
+ profile.date_format = "numbers"
+ result = show_with_right_format_date article
+ assert_match /2\/1/, result
+ end
+
+ should 'show date with month name' do
+ 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!
+ profile.date_format = "month_name"
+ result = show_with_right_format_date article
+ assert_match /February 1/, result
+ end
+
+ should 'show date with month name and year' do
+ 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!
+ profile.date_format = "month_name_with_year"
+ 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/profile_test.rb b/test/unit/profile_test.rb
index 7bf13da..9c04760 100644
--- a/test/unit/profile_test.rb
+++ b/test/unit/profile_test.rb
@@ -2176,4 +2176,33 @@ class ProfileTest < ActiveSupport::TestCase
assert_includes Profile.enabled, p2
assert_not_includes Profile.enabled, p3
end
+
+ should 'validates_inclusion_of date format' do
+ profile = fast_create(Profile)
+
+ profile.date_format = "invalid_format"
+ profile.valid?
+ assert profile.errors[:date_format.to_s].present?
+
+ profile.date_format = "numbers_with_year"
+ profile.valid?
+ assert !profile.errors[:date_format.to_s].present?
+
+ profile.date_format = "numbers"
+ profile.valid?
+ assert !profile.errors[:date_format.to_s].present?
+
+ profile.date_format = "month_name_with_year"
+ profile.valid?
+ assert !profile.errors[:date_format.to_s].present?
+
+ profile.date_format = "month_name"
+ profile.valid?
+ assert !profile.errors[:date_format.to_s].present?
+
+ profile.date_format = "past_time"
+ profile.valid?
+ assert !profile.errors[:date_format.to_s].present?
+
+ end
end
--
libgit2 0.21.2