diff --git a/app/controllers/public/search_controller.rb b/app/controllers/public/search_controller.rb index 20ab6b9..15a8dfa 100644 --- a/app/controllers/public/search_controller.rb +++ b/app/controllers/public/search_controller.rb @@ -16,7 +16,7 @@ class SearchController < ApplicationController @search_in = SEARCH_IN @searching = {} @search_in.each do |key, name| - @searching[key] = params[:find_in].nil? || params[:find_in].empty? || params[:find_in].include?(key.to_s) + @searching[key] = (params[:asset].blank? && (params[:find_in].nil? || params[:find_in].empty? || params[:find_in].include?(key.to_s))) || (params[:asset] == key.to_s) end end @@ -91,6 +91,33 @@ class SearchController < ApplicationController end end + def events + @events = @results[:events] + @calendar = Event.date_range(params[:year], params[:month]).map do |date| + [ + # the day itself + date, + # list of events of that day + @events.select do |event| + event.date_range.include?(date) + end, + # is this date in the current month? + true + ] + end + + # pad with days before + while @calendar.first.first.wday != 0 + @calendar.unshift([@calendar.first.first - 1.day, [], false]) + end + + # pad with days after (until Saturday) + while @calendar.last.first.wday != 6 + @calendar << [@calendar.last.first + 1.day, [], false] + end + + end + ####################################################### # view the summary of one category @@ -104,7 +131,7 @@ class SearchController < ApplicationController [ :comments, _('Recent comments'), @finder.recent('comments') ], [ :most_commented_articles, _('Most commented articles'), @finder.most_commented_articles ], [ :enterprises, _('Recently created enterprises'), @finder.recent('enterprises') ], - [ :events, _('Recently added events'), @finder.recent('events') ] + [ :events, _('Recently added events'), @finder.current_events(params[:year], params[:month]) ] ].each do |key, name, list| @results[key] = list @names[key] = name @@ -112,12 +139,12 @@ class SearchController < ApplicationController end attr_reader :category - def assets - @results = { @asset => @finder.recent(@asset, LIST_LIMIT) } + #def assets + #@results = { @asset => @finder.recent(@asset, LIST_LIMIT) } - @asset_name = gettext(SEARCH_IN.find { |entry| entry.first == @asset }[1]) - @names = { @asset => @asset_name } - end + #@asset_name = gettext(SEARCH_IN.find { |entry| entry.first == @asset }[1]) + #@names = { @asset => @asset_name } + #end def directory @results = { @asset => @finder.find_by_initial(@asset, params[:initial]) } diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb index 8f2765e..2f35aa4 100644 --- a/app/helpers/application_helper.rb +++ b/app/helpers/application_helper.rb @@ -13,6 +13,8 @@ module ApplicationHelper include AssetsHelper include BlockHelper + + include DatesHelper # Displays context help. You can pass the content of the help message as the # first parameter or using template code inside a block passed to this @@ -457,32 +459,6 @@ module ApplicationHelper end - # formats a date for displaying. - def show_date(date) - if date - date.strftime(_('%d %B %Y')) - else - '' - end - end - - # formats a datetime for displaying. - def show_time(time) - if time - time.strftime(_('%d %B %Y, %H:%m')) - else - '' - end - end - - def show_period(date1, date2 = nil) - if (date1 == date2) || (date2.nil?) - show_date(date1) - else - _('from %s to %s') % [show_date(date1), show_date(date2)] - end - end - def gravatar_url_for(email, options = {}) # Ta dando erro de roteamento url_for( { :gravatar_id => Digest::MD5.hexdigest(email), diff --git a/app/helpers/dates_helper.rb b/app/helpers/dates_helper.rb new file mode 100644 index 0000000..5824b05 --- /dev/null +++ b/app/helpers/dates_helper.rb @@ -0,0 +1,89 @@ +module DatesHelper + + include GetText + + # formats a date for displaying. + def show_date(date) + if date + date.strftime(_('%d %B %Y')) + else + '' + end + end + + # formats a datetime for displaying. + def show_time(time) + if time + time.strftime(_('%d %B %Y, %H:%m')) + else + '' + end + end + + def show_period(date1, date2 = nil) + if (date1 == date2) || (date2.nil?) + show_date(date1) + else + _('from %s to %s') % [show_date(date1), show_date(date2)] + end + end + + def show_day_of_week(date) + # FIXME Date#strftime should translate this for us !!!! + _([ + N_('Sunday'), + N_('Monday'), + N_('Tuesday'), + N_('Wednesday'), + N_('Thursday'), + N_('Friday'), + N_('Saturday'), + ][date.wday]) + end + + def show_month(year, month) + # FIXME Date#strftime should translate this for us !!! + monthname = _([ + N_('January'), + N_('February'), + N_('March'), + N_('April'), + N_('May'), + N_('June'), + N_('July'), + N_('August'), + N_('September'), + N_('October'), + N_('November'), + N_('December') + ][month.to_i - 1]) + + _('%{month} %{year}') % { :year => year, :month => monthname } + end + + def link_to_previous_month(year, month) + year = year.to_i + month = month.to_i + if month == 1 + year -= 1 + month = 12 + else + month -= 1 + end + + link_to '← ' + show_month(year, month), :year => year, :month => month + end + + def link_to_next_month(year, month) + year = year.to_i + month = month.to_i + if month == 12 + year += 1 + month = 1 + else + month += 1 + end + + link_to show_month(year, month) + ' →', :year => year, :month => month + end +end diff --git a/app/models/category.rb b/app/models/category.rb index 3e88d67..1c251e3 100644 --- a/app/models/category.rb +++ b/app/models/category.rb @@ -25,6 +25,8 @@ class Category < ActiveRecord::Base has_many :articles, :through => :article_categorizations has_many :comments, :through => :articles + has_many :events, :through => :article_categorizations, :class_name => 'Event', :source => :article + has_many :profile_categorizations has_many :enterprises, :through => :profile_categorizations, :source => :profile, :class_name => 'Enterprise' has_many :people, :through => :profile_categorizations, :source => :profile, :class_name => 'Person' diff --git a/app/models/category_finder.rb b/app/models/category_finder.rb index 7f9bebb..c77f838 100644 --- a/app/models/category_finder.rb +++ b/app/models/category_finder.rb @@ -31,6 +31,12 @@ class CategoryFinder Article.find(:all, options_for_find(Article, :limit => limit, :order => 'comments_count DESC')) end + def current_events(year, month) + range = Event.date_range(year, month) + + Event.find(:all, :include => :categories, :conditions => { 'categories.id' => category_ids, :start_date => range }) + end + protected def find_in_categorized(klass, query, options={}) diff --git a/app/models/event.rb b/app/models/event.rb index eb9ab3b..bc73ea3 100644 --- a/app/models/event.rb +++ b/app/models/event.rb @@ -26,4 +26,61 @@ class Event < Article 'event' end + def self.by_month(year = nil, month = nil) + self.find(:all, :conditions => { :start_date => date_range(year, month) }) + end + + def self.date_range(year, month) + if year.nil? || month.nil? + today = Date.today + year = today.year + month = today.month + else + year = year.to_i + month = month.to_i + end + + first_day = Date.new(year, month, 1) + last_day = Date.new(year, month, 1) + 1.month - 1.day + + first_day..last_day + end + + def date_range + start_date..(end_date||start_date) + end + + # FIXME this shouldn't be needed + include ActionView::Helpers::TagHelper + include ActionView::Helpers::UrlHelper + include ActionController::UrlWriter + include DatesHelper + + def to_html + + result = '' + html = Builder::XmlMarkup.new(:target => result) + + html.div { + html.ul { + html.li { + html.strong _('URL:') + html.a(self.link || "", 'href' => self.link || "") + } + html.li { + html.strong _('Address:') + html.text! self.address || "" + } + html.li { + html.strong _('When:') + html.text! show_period(start_date, end_date) + } + } + + html.div self.description + } + + result + end + end diff --git a/app/views/search/assets.rhtml b/app/views/search/assets.rhtml deleted file mode 100644 index a086577..0000000 --- a/app/views/search/assets.rhtml +++ /dev/null @@ -1,10 +0,0 @@ -

<%= @category ? (_('%{asset_name} in %{category}') % { :asset_name => @asset_name, :category => @category.name}) : @asset_name %>

- -
- <%= link_to_unless_current(_('Recent'), :action => 'assets') %> -   - <%= (?a..?z).map { |initial| link_to_unless_current(('' << initial).upcase, :action => 'directory', :initial => ('' << initial)) }.join('   ') %> -
-
- -<%= render :partial => 'display_results' %> diff --git a/app/views/search/events.rhtml b/app/views/search/events.rhtml new file mode 100644 index 0000000..ffc5d15 --- /dev/null +++ b/app/views/search/events.rhtml @@ -0,0 +1,35 @@ +

<%= show_month(params[:year], params[:month]) %>

+ +
+ <%= link_to_previous_month(params[:year], params[:month]) %> +   +   +   + <%= link_to_next_month(params[:year], params[:month]) %> +
+ + + + <% @calendar.first(7).each do |day,event| %> + + <% end %> + + <% @calendar.in_groups_of(7).each do |week| %> + + <% week.each do |date, events, this_month| %> + + <% end %> + + <% end %> +
<%= show_day_of_week(day) %>
+
+ <%= date.day %> +
+ + <% events.each do |event| %> +
+ <%= link_to event.name, event.url, :style => 'display: block;' %> +
+ <% end %> + +
diff --git a/config/routes.rb b/config/routes.rb index c8e77ea..255f0b0 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -31,7 +31,7 @@ ActionController::Routing::Routes.draw do |map| map.tag 'tag/:tag', :controller => 'search', :action => 'tag' # categories index map.category 'cat/*category_path', :controller => 'search', :action => 'category_index' - map.assets 'assets/:asset/*category_path', :controller => 'search', :action => 'assets' + map.assets 'assets/:asset/*category_path', :controller => 'search', :action => 'index' map.directory 'directory/:asset/:initial/*category_path', :controller => 'search', :action => 'directory' # search map.connect 'search/:action/*category_path', :controller => 'search' diff --git a/test/functional/search_controller_test.rb b/test/functional/search_controller_test.rb index a4f036e..8269ce6 100644 --- a/test/functional/search_controller_test.rb +++ b/test/functional/search_controller_test.rb @@ -901,14 +901,14 @@ class SearchControllerTest < Test::Unit::TestCase end %w[ people enterprises articles events communities products comments ].each do |asset| - should "render asset-specific template when searching for #{asset}" do get :index, :find_in => [ asset ] assert_template asset end - end + should 'test somehow the display of events as calendar' + ################################################################## ################################################################## diff --git a/test/unit/application_helper_test.rb b/test/unit/application_helper_test.rb index 0b79336..28e25ab 100644 --- a/test/unit/application_helper_test.rb +++ b/test/unit/application_helper_test.rb @@ -100,28 +100,6 @@ class ApplicationHelperTest < Test::Unit::TestCase assert_not_nil theme_javascript end - should 'generate period with two dates' do - date1 = mock - expects(:show_date).with(date1).returns('XXX') - date2 = mock - expects(:show_date).with(date2).returns('YYY') - expects(:_).with('from %s to %s').returns('from %s to %s') - assert_equal 'from XXX to YYY', show_period(date1, date2) - end - - should 'generate period with two equal dates' do - date1 = mock - expects(:show_date).with(date1).returns('XXX') - assert_equal 'XXX', show_period(date1, date1) - end - - should 'generate period with one date only' do - date1 = mock - expects(:show_date).with(date1).returns('XXX') - assert_equal 'XXX', show_period(date1) - end - - protected def content_tag(tag, content, options) diff --git a/test/unit/category_finder_test.rb b/test/unit/category_finder_test.rb index def706b..64e9317 100644 --- a/test/unit/category_finder_test.rb +++ b/test/unit/category_finder_test.rb @@ -272,4 +272,18 @@ class CategoryFinderTest < ActiveSupport::TestCase assert_not_includes people, p2 end + should 'find current events' do + finder = CategoryFinder.new(@category) + person = create_user('testuser').person + + e1 = Event.create!(:name => 'e1', :profile => person, :start_date => Date.new(2008,1,1), :categories => [@category]) + + # not in category + e2 = Event.create!(:name => 'e2', :profile => person, :start_date => Date.new(2008,1,15)) + + events = finder.current_events(2008, 1) + assert_includes events, e1 + assert_not_includes events, e2 + end + end diff --git a/test/unit/dates_helper_test.rb b/test/unit/dates_helper_test.rb new file mode 100644 index 0000000..5947cc9 --- /dev/null +++ b/test/unit/dates_helper_test.rb @@ -0,0 +1,61 @@ +require File.dirname(__FILE__) + '/../test_helper' + +class DatesHelperTest < Test::Unit::TestCase + + include DatesHelper + + should 'generate period with two dates' do + date1 = mock + expects(:show_date).with(date1).returns('XXX') + date2 = mock + expects(:show_date).with(date2).returns('YYY') + expects(:_).with('from %s to %s').returns('from %s to %s') + assert_equal 'from XXX to YYY', show_period(date1, date2) + end + + should 'generate period with two equal dates' do + date1 = mock + expects(:show_date).with(date1).returns('XXX') + assert_equal 'XXX', show_period(date1, date1) + end + + should 'generate period with one date only' do + date1 = mock + expects(:show_date).with(date1).returns('XXX') + assert_equal 'XXX', show_period(date1) + end + + should 'show day of week' do + expects(:_).with("Sunday").returns("Domingo") + date = mock + date.expects(:wday).returns(0) + assert_equal "Domingo", show_day_of_week(date) + end + + should 'show month' do + expects(:_).with('January').returns('January') + expects(:_).with('%{month} %{year}').returns('%{month} %{year}') + assert_equal 'January 2008', show_month(2008, 1) + end + + should 'provide link to previous month' do + expects(:link_to).with('← January 2008', { :year => 2008, :month => 1}) + link_to_previous_month('2008', '2') + end + + should 'support last year in link to previous month' do + expects(:link_to).with('← December 2007', { :year => 2007, :month => 12}) + link_to_previous_month('2008', '1') + end + + should 'provide link to next month' do + expects(:link_to).with('March 2008 →', { :year => 2008, :month => 3}) + link_to_next_month('2008', '2') + end + + should 'support next year in link to next month' do + expects(:link_to).with('January 2009 →', { :year => 2009, :month => 1}) + link_to_next_month('2008', '12') + end + +end diff --git a/test/unit/event_test.rb b/test/unit/event_test.rb index 30332a3..64c9de0 100644 --- a/test/unit/event_test.rb +++ b/test/unit/event_test.rb @@ -76,4 +76,74 @@ class EventTest < ActiveSupport::TestCase assert !e.errors.invalid?(:start_date) end + should 'find by year and month' do + profile = create_user('testuser').person + e1 = Event.create!(:name => 'e1', :start_date => Date.new(2008,1,1), :profile => profile) + e2 = Event.create!(:name => 'e2', :start_date => Date.new(2008,2,1), :profile => profile) + e3 = Event.create!(:name => 'e3', :start_date => Date.new(2008,3,1), :profile => profile) + + found = Event.by_month(2008, 2) + assert_includes found, e2 + assert_not_includes found, e1 + assert_not_includes found, e3 + end + + should 'find when in first day of month' do + profile = create_user('testuser').person + e1 = Event.create!(:name => 'e1', :start_date => Date.new(2008,1,1), :profile => profile) + assert_includes Event.by_month(2008, 1), e1 + end + + should 'find when in last day of month' do + profile = create_user('testuser').person + e1 = Event.create!(:name => 'e1', :start_date => Date.new(2008,1,31), :profile => profile) + assert_includes Event.by_month(2008, 1), e1 + end + + should 'use current month by default' do + profile = create_user('testuser').person + e1 = Event.create!(:name => 'e1', :start_date => Date.new(2008,1,31), :profile => profile) + Date.expects(:today).returns(Date.new(2008, 1, 15)) + assert_includes Event.by_month, e1 + end + + should 'provide period for searching in month' do + assert_equal Date.new(2008, 1, 1)..Date.new(2008,1,31), Event.date_range(2008, 1) + assert_equal Date.new(2008, 2, 1)..Date.new(2008,2,29), Event.date_range(2008, 2) + assert_equal Date.new(2007, 2, 1)..Date.new(2007,2,28), Event.date_range(2007, 2) + end + + should 'support string arguments to Event#date_range' do + assert_equal Date.new(2008,1,1)..Date.new(2008,1,31), Event.date_range('2008', '1') + end + + should 'provide range of dates for event with both dates filled' do + e = Event.new(:start_date => Date.new(2008, 1, 1), :end_date => Date.new(2008, 1, 5)) + + assert_equal (Date.new(2008,1,1)..Date.new(2008,1,5)), e.date_range + end + + should 'provide range of dates for event with only start date' do + e = Event.new(:start_date => Date.new(2008, 1, 1)) + + assert_equal (Date.new(2008,1,1)..Date.new(2008,1,1)), e.date_range + end + + should 'provide nice display format' do + e = Event.new(:start_date => Date.new(2008,1,1), :end_date => Date.new(2008,1,1), :link => 'http://www.myevent.org', :description => 'my somewhat short description') + + assert_tag_in_string e.to_html, :content => Regexp.new("1 January 2008") + assert_tag_in_string e.to_html, :content => 'my somewhat short description' + assert_tag_in_string e.to_html, :tag => 'a', :attributes => { :href => 'http://www.myevent.org' }, :content => 'http://www.myevent.org' + + end + + protected + + def assert_tag_in_string(text, options) + doc = HTML::Document.new(text, false, false) + tag = doc.find(options) + assert tag, "expected tag #{options.inspect}, but not found in #{text.inspect}" + end + end -- libgit2 0.21.2