Commit fdf74522 authored by Leandro Nunes's avatar Leandro Nunes
Browse files

Merge branch 'article_datetime' into 'master'

Put start and end dates as datetime for articles

change the start_date and end_date from date to datetime

See merge request !634
parents 180ad529 e7f5b1b3
......@@ -92,10 +92,10 @@ class SearchController < PublicController
def events
if params[:year].blank? && params[:year].blank? && params[:day].blank?
@date = Date.today
@date = DateTime.now
else
year = (params[:year] ? params[:year].to_i : Date.today.year)
month = (params[:month] ? params[:month].to_i : Date.today.month)
year = (params[:year] ? params[:year].to_i : DateTime.now.year)
month = (params[:month] ? params[:month].to_i : DateTime.now.month)
day = (params[:day] ? params[:day].to_i : 1)
@date = build_date(year, month, day)
end
......@@ -106,9 +106,7 @@ class SearchController < PublicController
@events = @category ?
environment.events.by_day(@date).in_category(Category.find(@category_id)).paginate(:per_page => per_page, :page => params[:page]) :
environment.events.by_day(@date).paginate(:per_page => per_page, :page => params[:page])
end
if params[:year] || params[:month]
elsif params[:year] || params[:month]
@events = @category ?
environment.events.by_month(@date).in_category(Category.find(@category_id)).paginate(:per_page => per_page, :page => params[:page]) :
environment.events.by_month(@date).paginate(:per_page => per_page, :page => params[:page])
......
......@@ -51,7 +51,7 @@ module ContentViewerHelper
elsif date_format == 'past_time'
left_time = true
end
content_tag('span', show_date(article.published_at, use_numbers , year, left_time), :class => 'date')
content_tag('span', show_time(article.published_at, use_numbers , year, left_time), :class => 'date')
end
def link_to_comments(article, args = {})
......
......@@ -43,9 +43,14 @@ module DatesHelper
end
# formats a datetime for displaying.
def show_time(time)
if time
_('%{day} %{month} %{year}, %{hour}:%{minutes}') % { :year => time.year, :month => month_name(time.month), :day => time.day, :hour => time.hour, :minutes => time.strftime("%M") }
def show_time(time, use_numbers = false, year = true, left_time = false)
if time && use_numbers
_('%{month}/%{day}/%{year}, %{hour}:%{minutes}') % { :year => (year ? time.year : ''), :month => time.month, :day => time.day, :hour => time.hour, :minutes => time.strftime("%M") }
elsif time && left_time
date_format = time_ago_in_words(time)
elsif time
date_format = year ? _('%{month_name} %{day}, %{year} %{hour}:%{minutes}') : _('%{month_name} %{day} %{hour}:%{minutes}')
date_format % { :day => time.day, :month_name => month_name(time.month), :year => time.year, :hour => time.hour, :minutes => time.strftime("%M") }
else
''
end
......@@ -53,7 +58,7 @@ module DatesHelper
def show_period(date1, date2 = nil, use_numbers = false)
if (date1 == date2) || (date2.nil?)
show_date(date1, use_numbers)
show_time(date1, use_numbers)
else
if date1.year == date2.year
if date1.month == date2.month
......@@ -72,8 +77,8 @@ module DatesHelper
end
else
_('from %{date1} to %{date2}') % {
:date1 => show_date(date1, use_numbers),
:date2 => show_date(date2, use_numbers)
:date1 => show_time(date1, use_numbers),
:date2 => show_time(date2, use_numbers)
}
end
end
......@@ -106,18 +111,18 @@ module DatesHelper
def build_date(year, month, day = 1)
if year.blank? and month.blank? and day.blank?
Date.today
DateTime.now
else
if year.blank?
year = Date.today.year
year = DateTime.now.year
end
if month.blank?
month = Date.today.month
month = DateTime.now.month
end
if day.blank?
day = 1
end
Date.new(year.to_i, month.to_i, day.to_i)
DateTime.new(year.to_i, month.to_i, day.to_i)
end
end
......
......@@ -16,7 +16,7 @@ module EventsHelper
content_tag( 'tr',
content_tag('td',
content_tag('div', show_date(article.start_date) + ( article.end_date.nil? ? '' : (_(" to ") + show_date(article.end_date))),:class => 'event-date' ) +
content_tag('div', show_time(article.start_date) + ( article.end_date.nil? ? '' : (_(" to ") + show_time(article.end_date))),:class => 'event-date' ) +
content_tag('div',link_to(article.name,article.url),:class => 'event-title') +
content_tag('div',(article.address.nil? or article.address == '') ? '' : (_('Place: ') + article.address),:class => 'event-place')
)
......
......@@ -151,7 +151,7 @@ module FormsHelper
datepicker_options[:close_text] ||= _('Done')
datepicker_options[:constrain_input] ||= true
datepicker_options[:current_text] ||= _('Today')
datepicker_options[:date_format] ||= 'mm/dd/yy'
datepicker_options[:date_format] ||= 'yy/mm/dd'
datepicker_options[:day_names] ||= [_('Sunday'), _('Monday'), _('Tuesday'), _('Wednesday'), _('Thursday'), _('Friday'), _('Saturday')]
datepicker_options[:day_names_min] ||= [_('Su'), _('Mo'), _('Tu'), _('We'), _('Th'), _('Fr'), _('Sa')]
datepicker_options[:day_names_short] ||= [_('Sun'), _('Mon'), _('Tue'), _('Wed'), _('Thu'), _('Fri'), _('Sat')]
......@@ -236,7 +236,7 @@ module FormsHelper
weekHeader: #{datepicker_options[:week_header].to_json},
yearRange: #{datepicker_options[:year_range].to_json},
yearSuffix: #{datepicker_options[:year_suffix].to_json}
})
}).datepicker('setDate', new Date('#{value}'))
</script>
".html_safe
result
......
......@@ -23,7 +23,7 @@ class Event < Article
def initialize(*args)
super(*args)
self.start_date ||= Date.today
self.start_date ||= DateTime.now
end
validates_presence_of :title, :start_date
......@@ -35,7 +35,7 @@ class Event < Article
end
scope :by_day, lambda { |date|
{ :conditions => ['start_date = :date AND end_date IS NULL OR (start_date <= :date AND end_date >= :date)', {:date => date}],
{ :conditions => [' start_date >= :start_date AND start_date <= :end_date AND end_date IS NULL OR (start_date <= :end_date AND end_date >= :start_date)', {:start_date => date.beginning_of_day, :end_date => date.end_of_day}],
:order => 'start_date ASC'
}
}
......@@ -80,7 +80,7 @@ class Event < Article
def self.date_range(year, month)
if year.nil? || month.nil?
today = Date.today
today = DateTime.now
year = today.year
month = today.month
else
......@@ -88,7 +88,7 @@ class Event < Article
month = month.to_i
end
first_day = Date.new(year, month, 1)
first_day = DateTime.new(year, month, 1)
last_day = first_day + 1.month - 1.day
first_day..last_day
......@@ -114,7 +114,7 @@ class Event < Article
end
def duration
((self.end_date || self.start_date) - self.start_date).to_i
(((self.end_date || self.start_date) - self.start_date).to_i/60/60/24)
end
alias_method :article_lead, :lead
......
......@@ -8,9 +8,8 @@
<%= render :partial => 'general_fields' %>
<%= render :partial => 'translatable' %>
<%= labelled_form_field(_('Start date'), pick_date(:article, :start_date)) %>
<%= date_range_field('article[start_date]', 'article[end_date]', @article.start_date, @article.end_date, _('%Y-%m-%d %H:%M'), {:time => true}, {:id => 'article_start_date'} ) %>
<%= labelled_form_field(_('End date'), pick_date(:article, :end_date)) %>
<%= labelled_form_field(_('Event website:'), text_field(:article, :link)) %>
......
<span class="publishing-info">
<span class="date">
<%= show_date(@page.published_at) %>
<%= show_time(@page.published_at) %>
</span>
<span class="author">
<%= _(", by %s") % (@page.author ? link_to(@page.author_name, @page.author_url) : @page.author_name) %>
......
class ChangeArticleDateToDatetime < ActiveRecord::Migration
def up
change_table :articles do |t|
t.change :start_date, :datetime
t.change :end_date, :datetime
end
change_table :article_versions do |t|
t.change :start_date, :datetime
t.change :end_date, :datetime
end
end
def down
change_table :articles do |t|
t.change :start_date, :date
t.change :end_date, :date
end
change_table :article_versions do |t|
t.change :start_date, :date
t.change :end_date, :date
end
end
end
......@@ -29,8 +29,8 @@ class CommunityTrackPlugin::Step < Folder
def initialize(*args)
super(*args)
self.start_date ||= Date.today
self.end_date ||= Date.today + 1.day
self.start_date ||= DateTime.now
self.end_date ||= DateTime.now + 1.day
end
def set_hidden_position
......@@ -72,20 +72,20 @@ class CommunityTrackPlugin::Step < Folder
end
def active?
(start_date..end_date).include?(Date.today)
(start_date..end_date).cover?(DateTime.now)
end
def finished?
Date.today > end_date
DateTime.now > end_date
end
def waiting?
Date.today < start_date
DateTime.now < start_date
end
def schedule_activation
return if !changes['start_date'] && !changes['end_date']
if Date.today <= end_date || accept_comments
if DateTime.now <= end_date || accept_comments
schedule_date = !accept_comments ? start_date : end_date + 1.day
CommunityTrackPlugin::ActivationJob.find(id).destroy_all
Delayed::Job.enqueue(CommunityTrackPlugin::ActivationJob.new(self.id), :run_at => schedule_date)
......
......@@ -6,7 +6,7 @@ class ContentViewerControllerTest < ActionController::TestCase
def setup
@profile = Community.create!(:name => 'Sample community', :identifier => 'sample-community')
@track = create_track('track', @profile)
@step = CommunityTrackPlugin::Step.create!(:name => 'step1', :body => 'body', :profile => @profile, :parent => @track, :published => false, :end_date => Date.today, :start_date => Date.today, :tool_type => TinyMceArticle.name)
@step = CommunityTrackPlugin::Step.create!(:name => 'step1', :body => 'body', :profile => @profile, :parent => @track, :published => false, :end_date => DateTime.now.end_of_day, :start_date => DateTime.now.beginning_of_day, :tool_type => TinyMceArticle.name)
user = create_user('testinguser')
login_as(user.login)
......
......@@ -9,7 +9,7 @@ class StepTest < ActiveSupport::TestCase
@track.add_category(@category)
@track.save!
@step = CommunityTrackPlugin::Step.new(:name => 'Step', :body => 'body', :profile => @profile, :parent => @track, :published => false, :end_date => Date.today, :start_date => Date.today)
@step = CommunityTrackPlugin::Step.new(:name => 'Step', :body => 'body', :profile => @profile, :parent => @track, :published => false, :end_date => DateTime.now.end_of_day, :start_date => DateTime.now.beginning_of_day - 1.day)
Delayed::Job.destroy_all
end
......@@ -22,39 +22,39 @@ class StepTest < ActiveSupport::TestCase
end
should 'set accept_comments to false on create' do
today = Date.today
today = DateTime.now
step = CommunityTrackPlugin::Step.create(:name => 'Step', :body => 'body', :profile => @profile, :parent => @track, :start_date => today, :end_date => today, :published => true)
refute step.accept_comments
end
should 'do not allow step creation with a parent that is not a track' do
today = Date.today
today = DateTime.now
blog = fast_create(Blog)
step = CommunityTrackPlugin::Step.new(:name => 'Step', :body => 'body', :profile => @profile, :parent => blog, :start_date => today, :end_date => today, :published => true)
refute step.save
end
should 'do not allow step creation without a parent' do
today = Date.today
today = DateTime.now
step = CommunityTrackPlugin::Step.new(:name => 'Step', :body => 'body', :profile => @profile, :parent => nil, :start_date => today, :end_date => today, :published => true)
refute step.save
end
should 'create step if end date is equal to start date' do
@step.start_date = Date.today
@step.end_date = Date.today
@step.start_date = DateTime.now
@step.end_date = DateTime.now
assert @step.save
end
should 'create step if end date is after start date' do
@step.start_date = Date.today
@step.end_date = Date.today + 1.day
@step.start_date = DateTime.now
@step.end_date = DateTime.now + 1.day
assert @step.save
end
should 'do not create step if end date is before start date' do
@step.start_date = Date.today
@step.end_date = Date.today - 1.day
@step.start_date = DateTime.now
@step.end_date = DateTime.now - 1.day
refute @step.save
end
......@@ -71,20 +71,20 @@ class StepTest < ActiveSupport::TestCase
end
should 'be active if today is between start and end dates' do
@step.start_date = Date.today
@step.end_date = Date.today + 1.day
@step.start_date = DateTime.now
@step.end_date = DateTime.now + 1.day
assert @step.active?
end
should 'be finished if today is after the end date' do
@step.start_date = Date.today - 2.day
@step.end_date = Date.today - 1.day
@step.start_date = DateTime.now - 2.day
@step.end_date = DateTime.now - 1.day
assert @step.finished?
end
should 'be waiting if today is before the end date' do
@step.start_date = Date.today + 1.day
@step.end_date = Date.today + 2.day
@step.start_date = DateTime.now + 1.day
@step.end_date = DateTime.now + 2.day
assert @step.waiting?
end
......@@ -95,17 +95,17 @@ class StepTest < ActiveSupport::TestCase
end
should 'create delayed job' do
@step.start_date = Date.today
@step.end_date = Date.today
@step.start_date = DateTime.now.beginning_of_day
@step.end_date = DateTime.now.end_of_day
@step.accept_comments = false
@step.schedule_activation
assert_equal 1, Delayed::Job.count
assert_equal @step.start_date, Delayed::Job.first.run_at.to_date
assert_equal @step.start_date, Delayed::Job.first.run_at
end
should 'do not duplicate delayed job' do
@step.start_date = Date.today
@step.end_date = Date.today
@step.start_date = DateTime.now
@step.end_date = DateTime.now
@step.schedule_activation
assert_equal 1, Delayed::Job.count
@step.schedule_activation
......@@ -113,30 +113,30 @@ class StepTest < ActiveSupport::TestCase
end
should 'create delayed job when a step is saved' do
@step.start_date = Date.today
@step.end_date = Date.today
@step.start_date = DateTime.now.beginning_of_day
@step.end_date = DateTime.now.end_of_day
@step.save!
assert_equal @step.start_date, Delayed::Job.first.run_at.to_date
assert_equal @step.start_date, Delayed::Job.first.run_at
end
should 'create delayed job even if start date has passed' do
@step.start_date = Date.today - 2.days
@step.end_date = Date.today
@step.start_date = DateTime.now - 2.days
@step.end_date = DateTime.now.end_of_day
@step.accept_comments = false
@step.schedule_activation
assert_equal @step.start_date, Delayed::Job.first.run_at.to_date
assert_equal @step.start_date, Delayed::Job.first.run_at
end
should 'create delayed job if end date has passed' do
@step.start_date = Date.today - 5.days
@step.end_date = Date.today - 2.days
@step.start_date = DateTime.now - 5.days
@step.end_date = DateTime.now - 2.days
@step.schedule_activation
assert_equal @step.end_date + 1.day, Delayed::Job.first.run_at.to_date
assert_equal @step.end_date + 1.day, Delayed::Job.first.run_at
end
should 'do not schedule delayed job if save but do not modify date fields' do
@step.start_date = Date.today
@step.end_date = Date.today
@step.start_date = DateTime.now
@step.end_date = DateTime.now.end_of_day
@step.save!
assert_equal 1, Delayed::Job.count
Delayed::Job.destroy_all
......@@ -149,13 +149,13 @@ class StepTest < ActiveSupport::TestCase
refute @step.position
@step.save!
assert_equal 1, @step.position
step2 = CommunityTrackPlugin::Step.new(:name => 'Step2', :body => 'body', :profile => @profile, :parent => @track, :published => false, :end_date => Date.today, :start_date => Date.today)
step2 = CommunityTrackPlugin::Step.new(:name => 'Step2', :body => 'body', :profile => @profile, :parent => @track, :published => false, :end_date => DateTime.now.end_of_day, :start_date => DateTime.now.beginning_of_day)
step2.save!
assert_equal 2, step2.position
end
should 'accept comments if step is active' do
@step.start_date = Date.today
@step.start_date = DateTime.now
@step.save!
refute @step.accept_comments
@step.toggle_activation
......@@ -164,8 +164,8 @@ class StepTest < ActiveSupport::TestCase
end
should 'do not accept comments if step is not active' do
@step.start_date = Date.today + 2.days
@step.end_date = Date.today + 3.days
@step.start_date = DateTime.now + 2.days
@step.end_date = DateTime.now + 3.days
@step.save!
refute @step.published
@step.toggle_activation
......@@ -174,14 +174,14 @@ class StepTest < ActiveSupport::TestCase
end
should 'do not accept comments if step is not active anymore' do
@step.start_date = Date.today
@step.end_date = DateTime.now.end_of_day
@step.save!
@step.toggle_activation
@step.reload
assert @step.accept_comments
@step.start_date = Date.today - 2.days
@step.end_date = Date.today - 1.day
@step.start_date = DateTime.now - 2.days
@step.end_date = DateTime.now - 1.day
@step.save!
@step.toggle_activation
@step.reload
......@@ -203,7 +203,7 @@ class StepTest < ActiveSupport::TestCase
end
should 'change position to botton if a hidden step becomes visible' do
step1 = CommunityTrackPlugin::Step.new(:name => 'Step1', :body => 'body', :profile => @profile, :parent => @track, :published => false, :end_date => Date.today, :start_date => Date.today)
step1 = CommunityTrackPlugin::Step.new(:name => 'Step1', :body => 'body', :profile => @profile, :parent => @track, :published => false, :end_date => DateTime.now.end_of_day, :start_date => DateTime.now.beginning_of_day)
step1.save!
@step.hidden = true
@step.save!
......@@ -215,7 +215,7 @@ class StepTest < ActiveSupport::TestCase
should 'decrement lower items positions if a step becomes hidden' do
@step.save!
step1 = CommunityTrackPlugin::Step.new(:name => 'Step1', :body => 'body', :profile => @profile, :parent => @track, :published => false, :end_date => Date.today, :start_date => Date.today)
step1 = CommunityTrackPlugin::Step.new(:name => 'Step1', :body => 'body', :profile => @profile, :parent => @track, :published => false, :end_date => DateTime.now.end_of_day, :start_date => DateTime.now.beginning_of_day)
step1.save!
assert_equal 2, step1.position
@step.hidden = true
......@@ -225,7 +225,7 @@ class StepTest < ActiveSupport::TestCase
end
should 'do not publish a hidden step' do
@step.start_date = Date.today
@step.start_date = DateTime.now
@step.hidden = true
@step.save!
refute @step.published
......@@ -266,7 +266,7 @@ class StepTest < ActiveSupport::TestCase
end
should 'enable comments on children when step is activated' do
@step.start_date = Date.today
@step.start_date = DateTime.now
@step.save!
refute @step.accept_comments
article = fast_create(Article, :parent_id => @step.id, :profile_id => @step.profile.id, :accept_comments => false)
......@@ -276,8 +276,7 @@ class StepTest < ActiveSupport::TestCase
end
should 'enable comments on children when step is active' do
@step.start_date = Date.today
@step.start_date = Date.today
@step.start_date = DateTime.now
@step.save!
refute @step.accept_comments
@step.toggle_activation
......
......@@ -30,13 +30,13 @@ class EventPlugin::EventBlock < Block
events = user.nil? ? events.public : events.display_filter(user,nil)
if future_only
events = events.where('start_date >= ?', Date.today)
events = events.where('start_date >= ?', DateTime.now.beginning_of_day)
end
if date_distance_limit > 0
events = events.by_range([
Date.today - date_distance_limit,
Date.today + date_distance_limit
DateTime.now.beginning_of_day - date_distance_limit,
DateTime.now.beginning_of_day + date_distance_limit
])
end
......
require File.dirname(__FILE__) + '/../../../../test/test_helper'
# Re-raise errors caught by the controller.
class HomeController
#append_view_path File.join(File.dirname(__FILE__) + '/../../views')
append_view_path File.join(File.dirname(__FILE__) + '/../../views')
def rescue_action(e)
raise e
end
......@@ -15,7 +16,7 @@ class HomeControllerTest < ActionController::TestCase
@env.enable_plugin('EventPlugin')
@p1 = fast_create(Person, :environment_id => @env.id)
@e1a = fast_create(Event, :name=>'Event p1 A', :profile_id=>@p1.id)
@e1a = Event.create!(:name=>'Event p1 A', :profile =>@p1)
box = Box.create!(:owner => @env)
@block = EventPlugin::EventBlock.create!(:box => box)
......@@ -27,6 +28,7 @@ class HomeControllerTest < ActionController::TestCase
should 'see events microdata sturcture' do
get :index
#raise response.body.inspect
assert_select '.event-plugin_event-block ul.events'
assert_select ev
assert_select ev + 'a[itemprop="url"]'
......@@ -41,15 +43,15 @@ class HomeControllerTest < ActionController::TestCase
should 'see event duration' do
@e1a.slug = 'event1a'
@e1a.start_date = Date.today
@e1a.end_date = Date.today + 1.day
@e1a.start_date = DateTime.now
@e1a.end_date = DateTime.now + 1.day
@e1a.save!
get :index
assert_select ev + 'time.duration[itemprop="endDate"]', /1 day/
@e1a.slug = 'event1a'
@e1a.start_date = Date.today