Commit b86297084d964b7916c304ffd30909645a02cfee
Committed by
Rodrigo Souto
1 parent
769eb2c5
Exists in
master
and in
22 other branches
Add date format option for articles from the environment
Signed-off-by: Arthur Del Esposte <arthurmde@gmail.com> Signed-off-by: David Carlos <ddavidcarlos1392@gmail.com> Signed-off-by: Fabio Teixeira <fabio1079@gmail.com> Signed-off-by: Gabriela Navarro <navarro1703@gmail.com>
Showing
10 changed files
with
150 additions
and
8 deletions
Show diff stats
app/helpers/content_viewer_helper.rb
... | ... | @@ -2,6 +2,7 @@ module ContentViewerHelper |
2 | 2 | |
3 | 3 | include BlogHelper |
4 | 4 | include ForumHelper |
5 | + include DatesHelper | |
5 | 6 | |
6 | 7 | def display_number_of_comments(n) |
7 | 8 | base_str = "<span class='comment-count hide'>#{n}</span>" |
... | ... | @@ -24,8 +25,9 @@ module ContentViewerHelper |
24 | 25 | unless args[:no_comments] || !article.accept_comments |
25 | 26 | comments = (" - %s") % link_to_comments(article) |
26 | 27 | end |
28 | + date_format = show_with_right_format_date article | |
27 | 29 | title << content_tag('span', |
28 | - content_tag('span', show_date(article.published_at), :class => 'date') + | |
30 | + date_format + | |
29 | 31 | content_tag('span', _(", by %s") % (article.author ? link_to(article.author_name, article.author_url) : article.author_name), :class => 'author') + |
30 | 32 | content_tag('span', comments, :class => 'comments'), |
31 | 33 | :class => 'created-at' |
... | ... | @@ -34,6 +36,24 @@ module ContentViewerHelper |
34 | 36 | title |
35 | 37 | end |
36 | 38 | |
39 | + def show_with_right_format_date article | |
40 | + date_format = article.environment.date_format | |
41 | + use_numbers = false | |
42 | + year = true | |
43 | + left_time = false | |
44 | + if date_format == 'numbers_with_year' | |
45 | + use_numbers = true | |
46 | + elsif date_format == 'numbers' | |
47 | + use_numbers = true | |
48 | + year = false | |
49 | + elsif date_format == 'month_name' | |
50 | + year = false | |
51 | + elsif date_format == 'past_time' | |
52 | + left_time = true | |
53 | + end | |
54 | + content_tag('span', show_date(article.published_at, use_numbers , year, left_time), :class => 'date') | |
55 | + end | |
56 | + | |
37 | 57 | def link_to_comments(article, args = {}) |
38 | 58 | return '' unless article.accept_comments? |
39 | 59 | reference_to_article number_of_comments(article), article, 'comments_list' | ... | ... |
app/helpers/dates_helper.rb
... | ... | @@ -2,6 +2,7 @@ require 'noosfero/i18n' |
2 | 2 | |
3 | 3 | module DatesHelper |
4 | 4 | |
5 | + include ActionView::Helpers::DateHelper | |
5 | 6 | def months |
6 | 7 | I18n.t('date.month_names') |
7 | 8 | end |
... | ... | @@ -15,10 +16,12 @@ module DatesHelper |
15 | 16 | end |
16 | 17 | |
17 | 18 | # formats a date for displaying. |
18 | - def show_date(date, use_numbers = false, year=true) | |
19 | + def show_date(date, use_numbers = false, year = true, left_time = false) | |
19 | 20 | if date && use_numbers |
20 | 21 | date_format = year ? _('%{month}/%{day}/%{year}') : _('%{month}/%{day}') |
21 | 22 | date_format % { :day => date.day, :month => date.month, :year => date.year } |
23 | + elsif date && left_time | |
24 | + date_format = time_ago_in_words(date) | |
22 | 25 | elsif date |
23 | 26 | date_format = year ? _('%{month_name} %{day}, %{year}') : _('%{month_name} %{day}') |
24 | 27 | date_format % { :day => date.day, :month_name => month_name(date.month), :year => date.year } | ... | ... |
app/models/environment.rb
... | ... | @@ -3,7 +3,17 @@ |
3 | 3 | # domains. |
4 | 4 | class Environment < ActiveRecord::Base |
5 | 5 | |
6 | - 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 | |
6 | + attr_accessible :name, :is_default, :signup_welcome_text_subject, | |
7 | + :signup_welcome_text_body, :terms_of_use, | |
8 | + :message_for_disabled_enterprise, :news_amount_by_folder, | |
9 | + :default_language, :languages, :description, | |
10 | + :organization_approval_method, :enabled_plugins, | |
11 | + :enabled_features, :redirection_after_login, | |
12 | + :redirection_after_signup, :contact_email, :theme, | |
13 | + :reports_lower_bound, :noreply_email, | |
14 | + :signup_welcome_screen_body, :members_whitelist_enabled, | |
15 | + :members_whitelist, :highlighted_news_amount, | |
16 | + :portal_news_amount, :date_format | |
7 | 17 | |
8 | 18 | has_many :users |
9 | 19 | |
... | ... | @@ -14,6 +24,12 @@ class Environment < ActiveRecord::Base |
14 | 24 | |
15 | 25 | IDENTIFY_SCRIPTS = /(php[0-9s]?|[sp]htm[l]?|pl|py|cgi|rb)/ |
16 | 26 | |
27 | + validates_inclusion_of :date_format, | |
28 | + :in => [ 'numbers_with_year', 'numbers', | |
29 | + 'month_name_with_year', 'month_name', | |
30 | + 'past_time'], | |
31 | + :if => :date_format | |
32 | + | |
17 | 33 | def self.verify_filename(filename) |
18 | 34 | filename += '.txt' if File.extname(filename) =~ IDENTIFY_SCRIPTS |
19 | 35 | filename | ... | ... |
app/views/admin_panel/_site_info.html.erb
... | ... | @@ -3,6 +3,21 @@ |
3 | 3 | <%= labelled_form_field(_('No reply email'), text_field(:environment, :noreply_email)) %> |
4 | 4 | <% themes_options = Theme.system_themes.map {|theme| [theme.name, theme.id] }.sort %> |
5 | 5 | <%= labelled_form_field(_('Theme'), select(:environment, :theme, options_for_select(themes_options, environment.theme))) %> |
6 | + | |
7 | +<%= labelled_form_field( | |
8 | + _("Article's date format"), | |
9 | + select(:environment, :date_format, | |
10 | + options_for_select([ | |
11 | + [ _('mm/dd/yyyy'), 'numbers_with_year'], | |
12 | + [ _('mm/dd'), 'numbers'], | |
13 | + [ _('Month dd, yyyy'), 'month_name_with_year'], | |
14 | + [ _('Month dd'), 'month_name'], | |
15 | + [ _('X minutes/hours/days/months/years ago'), 'past_time'] | |
16 | + ], environment.date_format | |
17 | + ) | |
18 | + ) | |
19 | +) %> | |
20 | + | |
6 | 21 | <%= required f.text_field(:reports_lower_bound, :size => 3) %> |
7 | 22 | <%= labelled_form_field(_('Default language'), select(:environment, :default_language, environment.locales.invert, { :selected => environment.default_locale, :include_blank => true })) %> |
8 | 23 | <%= label_tag :languages, _('Available languages') %> | ... | ... |
db/migrate/20150529180110_add_date_format_to_environment.rb
0 → 100644
test/functional/admin_panel_controller_test.rb
... | ... | @@ -130,6 +130,20 @@ class AdminPanelControllerTest < ActionController::TestCase |
130 | 130 | assert_equal "This <strong>is</strong> my new environment", Environment.default.message_for_disabled_enterprise |
131 | 131 | end |
132 | 132 | |
133 | + should 'save site article date format option' do | |
134 | + post :site_info, :environment => { :date_format => "numbers_with_year" } | |
135 | + assert_redirected_to :action => 'index' | |
136 | + | |
137 | + assert_equal "numbers_with_year", Environment.default.date_format | |
138 | + end | |
139 | + | |
140 | + should 'dont save site article date format option when a invalid option is passed' do | |
141 | + post :site_info, :environment => { :date_format => "invalid_format" } | |
142 | + | |
143 | + assert_match "Date format is not included in the list", @response.body | |
144 | + assert_equal "month_name_with_year", Environment.default.date_format | |
145 | + end | |
146 | + | |
133 | 147 | should 'set portal community' do |
134 | 148 | e = Environment.default |
135 | 149 | @controller.stubs(:environment).returns(e) | ... | ... |
test/test_helper.rb
test/unit/content_viewer_helper_test.rb
... | ... | @@ -18,7 +18,7 @@ class ContentViewerHelperTest < ActionView::TestCase |
18 | 18 | result = article_title(post) |
19 | 19 | assert_tag_in_string result, :tag => 'span', :content => show_date(post.published_at) |
20 | 20 | end |
21 | - | |
21 | + | |
22 | 22 | should 'display published-at for forum posts' do |
23 | 23 | forum = fast_create(Forum, :name => 'Forum test', :profile_id => profile.id) |
24 | 24 | post = TextileArticle.create!(:name => 'post test', :profile => profile, :parent => forum) |
... | ... | @@ -112,6 +112,42 @@ class ContentViewerHelperTest < ActionView::TestCase |
112 | 112 | assert_match 'bt-bookmark.gif', addthis_image_tag |
113 | 113 | end |
114 | 114 | |
115 | + should 'show date with mm/dd/yyyy' do | |
116 | + Environment.any_instance.stubs(:date_format).returns('numbers_with_year') | |
117 | + article = TextileArticle.new(:name => 'post for test', :body => 'post for test', :profile => profile) | |
118 | + article.published_at = Time.zone.local(2007, 2, 1, 15, 30, 45) | |
119 | + article.save! | |
120 | + result = show_with_right_format_date article | |
121 | + assert_match /2\/1\/2007/, result | |
122 | + end | |
123 | + | |
124 | + should 'show date with mm/dd' do | |
125 | + Environment.any_instance.stubs(:date_format).returns('numbers') | |
126 | + article = TextileArticle.new(:name => 'post for test', :body => 'post for test', :profile => profile) | |
127 | + article.published_at = Time.zone.local(2007, 2, 1, 15, 30, 45) | |
128 | + article.save! | |
129 | + result = show_with_right_format_date article | |
130 | + assert_match /2\/1/, result | |
131 | + end | |
132 | + | |
133 | + should 'show date with month name' do | |
134 | + Environment.any_instance.stubs(:date_format).returns('month_name') | |
135 | + article = TextileArticle.new(:name => 'post for test', :body => 'post for test', :profile => profile) | |
136 | + article.published_at = Time.zone.local(2007, 2, 1, 15, 30, 45) | |
137 | + article.save! | |
138 | + result = show_with_right_format_date article | |
139 | + assert_match /February 1/, result | |
140 | + end | |
141 | + | |
142 | + should 'show date with month name and year' do | |
143 | + Environment.any_instance.stubs(:date_format).returns('month_name_with_year') | |
144 | + article = TextileArticle.new(:name => 'post for test', :body => 'post for test', :profile => profile) | |
145 | + article.published_at = Time.zone.local(2007, 2, 1, 15, 30, 45) | |
146 | + article.save! | |
147 | + result = show_with_right_format_date article | |
148 | + assert_match /February 1, 2007/, result | |
149 | + end | |
150 | + | |
115 | 151 | protected |
116 | 152 | include NoosferoTestHelper |
117 | 153 | include ActionView::Helpers::TextHelper | ... | ... |
test/unit/dates_helper_test.rb
... | ... | @@ -146,4 +146,9 @@ class DatesHelperTest < ActiveSupport::TestCase |
146 | 146 | assert_equal Date.new(Date.today.year, Date.today.month, 1), build_date('', '') |
147 | 147 | end |
148 | 148 | |
149 | + should 'show how long it has passed since a specific date' do | |
150 | + date = Time.zone.now | |
151 | + assert_equal show_date(date, false, false, true), time_ago_in_words(date) | |
152 | + end | |
153 | + | |
149 | 154 | end | ... | ... |
test/unit/environment_test.rb
... | ... | @@ -1703,4 +1703,32 @@ class EnvironmentTest < ActiveSupport::TestCase |
1703 | 1703 | assert !e.has_license? |
1704 | 1704 | end |
1705 | 1705 | |
1706 | + should 'validates_inclusion_of date format' do | |
1707 | + environment = fast_create(Environment) | |
1708 | + | |
1709 | + environment.date_format = "invalid_format" | |
1710 | + environment.valid? | |
1711 | + assert environment.errors[:date_format.to_s].present? | |
1712 | + | |
1713 | + environment.date_format = "numbers_with_year" | |
1714 | + environment.valid? | |
1715 | + assert !environment.errors[:date_format.to_s].present? | |
1716 | + | |
1717 | + environment.date_format = "numbers" | |
1718 | + environment.valid? | |
1719 | + assert !environment.errors[:date_format.to_s].present? | |
1720 | + | |
1721 | + environment.date_format = "month_name_with_year" | |
1722 | + environment.valid? | |
1723 | + assert !environment.errors[:date_format.to_s].present? | |
1724 | + | |
1725 | + environment.date_format = "month_name" | |
1726 | + environment.valid? | |
1727 | + assert !environment.errors[:date_format.to_s].present? | |
1728 | + | |
1729 | + environment.date_format = "past_time" | |
1730 | + environment.valid? | |
1731 | + assert !environment.errors[:date_format.to_s].present? | |
1732 | + end | |
1733 | + | |
1706 | 1734 | end | ... | ... |