Commit ac4a7edca517d5c2b50c4e0779749fbc3905de2c

Authored by Gabriela Navarro
Committed by Fabio Teixeira
1 parent fa785f1a

Add date format option for profile's articles

Signed-off-by: Arthur Del Esposte <arthurmde@gmail.com>
Signed-off-by: David Carlos <ddavidcarlos1392@gmail.com>
Signed-off-by: Gabriela Navarro <navarro1703@gmail.com>
app/controllers/my_profile/cms_controller.rb
... ... @@ -188,6 +188,18 @@ class CmsController &lt; MyProfileController
188 188 render :action => 'edit'
189 189 end
190 190  
  191 + def general_configuration
  192 + @profile_data = profile
  193 + if request.post?
  194 + if @profile_data.update_attributes(params[:profile_data])
  195 + session[:notice] = _("Article date format updated successfully")
  196 + else
  197 + session[:notice] = _("Could not update article's date format")
  198 + end
  199 + redirect_to :action => 'index'
  200 + end
  201 + end
  202 +
191 203 post_only :set_home_page
192 204 def set_home_page
193 205 return render_access_denied unless user.can_change_homepage?
... ...
app/helpers/blog_helper.rb
... ... @@ -2,7 +2,6 @@ module BlogHelper
2 2  
3 3 include ArticleHelper
4 4  
5   -
6 5 def custom_options_for_article(article,tokenized_children)
7 6 @article = article
8 7 hidden_field_tag('article[published]', 1) +
... ...
app/helpers/content_viewer_helper.rb
... ... @@ -24,8 +24,9 @@ module ContentViewerHelper
24 24 unless args[:no_comments] || !article.accept_comments
25 25 comments = (" - %s") % link_to_comments(article)
26 26 end
  27 + date_format = show_with_right_format_date article
27 28 title << content_tag('span',
28   - content_tag('span', show_date(article.published_at), :class => 'date') +
  29 + date_format +
29 30 content_tag('span', _(", by %s") % (article.author ? link_to(article.author_name, article.author_url) : article.author_name), :class => 'author') +
30 31 content_tag('span', comments, :class => 'comments'),
31 32 :class => 'created-at'
... ... @@ -34,6 +35,24 @@ module ContentViewerHelper
34 35 title
35 36 end
36 37  
  38 + def show_with_right_format_date article
  39 + date_format = article.profile.date_format
  40 + use_numbers = false
  41 + year = true
  42 + left_time = false
  43 + if date_format == 'numbers_with_year'
  44 + use_numbers = true
  45 + elsif date_format == 'numbers'
  46 + use_numbers = true
  47 + year = false
  48 + elsif date_format == 'month_name'
  49 + year = false
  50 + elsif date_format == 'past_time'
  51 + left_time = true
  52 + end
  53 + content_tag('span', show_date(article.published_at, use_numbers , year, left_time), :class => 'date')
  54 + end
  55 +
37 56 def link_to_comments(article, args = {})
38 57 return '' unless article.accept_comments?
39 58 reference_to_article number_of_comments(article), article, 'comments_list'
... ...
app/helpers/dates_helper.rb
... ... @@ -15,10 +15,12 @@ module DatesHelper
15 15 end
16 16  
17 17 # formats a date for displaying.
18   - def show_date(date, use_numbers = false, year=true)
  18 + def show_date(date, use_numbers = false, year = true, left_time = false)
19 19 if date && use_numbers
20 20 date_format = year ? _('%{month}/%{day}/%{year}') : _('%{month}/%{day}')
21 21 date_format % { :day => date.day, :month => date.month, :year => date.year }
  22 + elsif date && left_time
  23 + date_format = time_ago_in_words(date)
22 24 elsif date
23 25 date_format = year ? _('%{month_name} %{day}, %{year}') : _('%{month_name} %{day}')
24 26 date_format % { :day => date.day, :month_name => month_name(date.month), :year => date.year }
... ...
app/models/profile.rb
... ... @@ -3,7 +3,17 @@
3 3 # which by default is the one returned by Environment:default.
4 4 class Profile < ActiveRecord::Base
5 5  
6   - 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
  6 +
  7 + attr_accessible :name, :identifier, :public_profile, :nickname,
  8 + :custom_footer, :custom_header, :address, :zip_code,
  9 + :contact_phone, :image_builder, :description, :closed,
  10 + :template_id, :environment, :lat, :lng, :is_template,
  11 + :fields_privacy, :preferred_domain_id, :category_ids,
  12 + :country, :city, :state, :national_region_code, :email,
  13 + :contact_email, :redirect_l10n,
  14 + :notification_time, :redirection_after_login,
  15 + :email_suggestions, :allow_members_to_invite,
  16 + :invite_friends_only, :secret, :date_format
7 17  
8 18 # use for internationalizable human type names in search facets
9 19 # reimplement on subclasses
... ... @@ -181,6 +191,12 @@ class Profile &lt; ActiveRecord::Base
181 191  
182 192 validates_length_of :description, :maximum => 550, :allow_nil => true
183 193  
  194 + validates_inclusion_of :date_format,
  195 + :in => [ 'numbers_with_year', 'numbers',
  196 + 'month_name_with_year', 'month_name',
  197 + 'past_time'],
  198 + :if => :date_format
  199 +
184 200 # Valid identifiers must match this format.
185 201 IDENTIFIER_FORMAT = /^#{Noosfero.identifier_format}$/
186 202  
... ...
app/views/cms/general_configuration.html.erb 0 → 100644
... ... @@ -0,0 +1,25 @@
  1 +<div class="general-configuration">
  2 +
  3 +<h2> <%= _('General Configuration:') %> </h2>
  4 +<%= labelled_form_for :profile_data do |f|%>
  5 + <h2><%= _("Article's date format") %> </h2>
  6 +
  7 + <%= _("This option will define how article's date will be showed") %>
  8 + <br>
  9 + <br>
  10 + <%= select_tag('profile_data[date_format]', 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 + ], @profile_data.date_format)) %>
  17 +
  18 + <br>
  19 + <br>
  20 +
  21 + <%= submit_button('save', _('Save')) %>
  22 + <%= modal_close_button(_('Cancel')) %>
  23 +<% end %>
  24 +
  25 +</div>
... ...
app/views/cms/view.html.erb
... ... @@ -18,6 +18,7 @@
18 18 <% parent_id = ((@article && @article.allow_children?) ? @article : nil) %>
19 19  
20 20 <%= modal_button('new', _('New content'), :action => 'new', :parent_id => parent_id, :cms => true) %>
  21 + <%= modal_button('edit', _('General Configuration'), :action => 'general_configuration', :parent_id => parent_id, :cms => true) %>
21 22 <%= button(:back, _('Back to control panel'), :controller => 'profile_editor', :action => "index") %>
22 23 <% end %>
23 24  
... ...
db/migrate/20150529180110_add_date_format_to_profile.rb 0 → 100644
... ... @@ -0,0 +1,9 @@
  1 +class AddDateFormatToProfile < ActiveRecord::Migration
  2 + def up
  3 + add_column :profiles, :date_format, :string, :default => 'month_name_with_year'
  4 + end
  5 +
  6 + def down
  7 + remove_column :profiles, :date_format
  8 + end
  9 +end
... ...
public/stylesheets/application.css
... ... @@ -3462,6 +3462,11 @@ table.cms-articles .icon:hover {
3462 3462 width: 455px;
3463 3463 }
3464 3464  
  3465 +.general-configuration {
  3466 + padding: 5px 20px;
  3467 + width: 455px;
  3468 +}
  3469 +
3465 3470 .article-types {
3466 3471 padding-left: 5px;
3467 3472 margin-top: 20px;
... ...
test/functional/cms_controller_test.rb
... ... @@ -1869,6 +1869,24 @@ class CmsControllerTest &lt; ActionController::TestCase
1869 1869 assert_equal '[{"label":"linux","value":"linux"}]', @response.body
1870 1870 end
1871 1871  
  1872 + should 'save general configuration when is passed a valid value' do
  1873 + post :general_configuration, profile: profile.identifier, :profile_data => { date_format: "numbers_with_year" }
  1874 + profile.reload
  1875 +
  1876 + assert_equal profile.date_format, "numbers_with_year"
  1877 + end
  1878 +
  1879 + should 'not save general configuration when is not passed a valid value' do
  1880 + profile.date_format = "month_name_with_year"
  1881 + profile.save!
  1882 +
  1883 + post :general_configuration, profile: profile.identifier, :profile_data => { date_format: "invalid_format" }
  1884 + profile.reload
  1885 +
  1886 + assert_equal profile.date_format, "month_name_with_year"
  1887 + end
  1888 +
  1889 +
1872 1890 protected
1873 1891  
1874 1892 # FIXME this is to avoid adding an extra dependency for a proper JSON parser.
... ...
test/unit/content_viewer_helper_test.rb
... ... @@ -18,7 +18,7 @@ class ContentViewerHelperTest &lt; 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 &lt; 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 + article = TextileArticle.new(:name => 'post for test', :body => 'post for test', :profile => profile)
  117 + article.published_at = Time.zone.local(2007, 2, 1, 15, 30, 45)
  118 + article.save!
  119 + profile.date_format = "numbers_with_year"
  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 + article = TextileArticle.new(:name => 'post for test', :body => 'post for test', :profile => profile)
  126 + article.published_at = Time.zone.local(2007, 2, 1, 15, 30, 45)
  127 + article.save!
  128 + profile.date_format = "numbers"
  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 + article = TextileArticle.new(:name => 'post for test', :body => 'post for test', :profile => profile)
  135 + article.published_at = Time.zone.local(2007, 2, 1, 15, 30, 45)
  136 + article.save!
  137 + profile.date_format = "month_name"
  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 + article = TextileArticle.new(:name => 'post for test', :body => 'post for test', :profile => profile)
  144 + article.published_at = Time.zone.local(2007, 2, 1, 15, 30, 45)
  145 + article.save!
  146 + profile.date_format = "month_name_with_year"
  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 &lt; 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/profile_test.rb
... ... @@ -2176,4 +2176,33 @@ class ProfileTest &lt; ActiveSupport::TestCase
2176 2176 assert_includes Profile.enabled, p2
2177 2177 assert_not_includes Profile.enabled, p3
2178 2178 end
  2179 +
  2180 + should 'validates_inclusion_of date format' do
  2181 + profile = fast_create(Profile)
  2182 +
  2183 + profile.date_format = "invalid_format"
  2184 + profile.valid?
  2185 + assert profile.errors[:date_format.to_s].present?
  2186 +
  2187 + profile.date_format = "numbers_with_year"
  2188 + profile.valid?
  2189 + assert !profile.errors[:date_format.to_s].present?
  2190 +
  2191 + profile.date_format = "numbers"
  2192 + profile.valid?
  2193 + assert !profile.errors[:date_format.to_s].present?
  2194 +
  2195 + profile.date_format = "month_name_with_year"
  2196 + profile.valid?
  2197 + assert !profile.errors[:date_format.to_s].present?
  2198 +
  2199 + profile.date_format = "month_name"
  2200 + profile.valid?
  2201 + assert !profile.errors[:date_format.to_s].present?
  2202 +
  2203 + profile.date_format = "past_time"
  2204 + profile.valid?
  2205 + assert !profile.errors[:date_format.to_s].present?
  2206 +
  2207 + end
2179 2208 end
... ...