Commit 70396fe6af3f0d00c78dcfd76135a56107e46713

Authored by Rodrigo Souto
2 parents 6085f246 e1aad422

Merge branch 'event_block_improvements' into 'master'

Event block improvements

See #724

See merge request !190
src/noosfero-spb/software_communities/lib/software_events_block.rb
1 class SoftwareEventsBlock < Block 1 class SoftwareEventsBlock < Block
2 2
  3 + settings_items :amount_of_events, :type => :integer, :default => 3
  4 + attr_accessible :amount_of_events
  5 +
  6 + validates :amount_of_events,
  7 + :presence => true, :numericality => {
  8 + greater_than_or_equal_to: 1
  9 + }
  10 +
3 def self.description 11 def self.description
4 _('Software community events') 12 _('Software community events')
5 end 13 end
@@ -29,7 +37,7 @@ class SoftwareEventsBlock &lt; Block @@ -29,7 +37,7 @@ class SoftwareEventsBlock &lt; Block
29 37
30 def get_events 38 def get_events
31 yesterday = DateTime.yesterday.end_of_day 39 yesterday = DateTime.yesterday.end_of_day
32 - self.owner.events.where("end_date > ? OR end_date IS NULL", yesterday).order(:start_date) 40 + self.owner.events.where("start_date > ?", yesterday).order(:start_date, :id).limit(self.amount_of_events)
33 end 41 end
34 42
35 def get_events_except event_slug="" 43 def get_events_except event_slug=""
src/noosfero-spb/software_communities/test/functional/software_communities_plugin_profile_controller_test.rb
@@ -57,6 +57,37 @@ class SoftwareCommunitiesPluginProfileControllerTest &lt; ActionController::TestCas @@ -57,6 +57,37 @@ class SoftwareCommunitiesPluginProfileControllerTest &lt; ActionController::TestCas
57 assert_equal "Could not find the download file", session[:notice] 57 assert_equal "Could not find the download file", session[:notice]
58 end 58 end
59 59
  60 + should "display limited community events" do
  61 + @e1 = Event.new :name=>"Event 1", :body=>"Event 1 body",
  62 + :start_date=>DateTime.now,
  63 + :end_date=>(DateTime.now + 1.month)
  64 +
  65 + @e2 = Event.new :name=>"Event 2", :body=>"Event 2 body",
  66 + :start_date=>(DateTime.now + 10.days),
  67 + :end_date=>(DateTime.now + 11.days)
  68 +
  69 + @e3 = Event.new :name=>"Event 3", :body=>"Event 3 body",
  70 + :start_date=>(DateTime.now + 20.days),
  71 + :end_date=>(DateTime.now + 30.days)
  72 +
  73 + @software.community.events << @e1
  74 + @software.community.events << @e2
  75 + @software.community.events << @e3
  76 + @software.community.save!
  77 +
  78 + events_block = SoftwareEventsBlock.new
  79 + events_block.amount_of_events = 2
  80 + events_block.display = "always"
  81 + box = MainBlock.last.box
  82 + events_block.box = box
  83 + events_block.save!
  84 +
  85 + get :index, :profile=>@software.community.identifier
  86 + assert_tag :tag => 'div', :attributes => { :class => 'software-community-events-block' }, :descendant => { :tag => 'a', :content => 'Event 1' }
  87 + assert_tag :tag => 'div', :attributes => { :class => 'software-community-events-block' }, :descendant => { :tag => 'a', :content => 'Event 2' }
  88 + assert_no_tag :tag => 'div', :attributes => { :class => 'software-community-events-block' }, :descendant => { :tag => 'a', :content => 'Event 3' }
  89 + end
  90 +
60 should "notice when given invalid download params" do 91 should "notice when given invalid download params" do
61 download_block = DownloadBlock.last 92 download_block = DownloadBlock.last
62 get :download_file, :profile=>@software.community.identifier, 93 get :download_file, :profile=>@software.community.identifier,
src/noosfero-spb/software_communities/test/unit/software_events_block_test.rb
@@ -27,29 +27,40 @@ class SoftwareEventsBlockTest &lt; ActiveSupport::TestCase @@ -27,29 +27,40 @@ class SoftwareEventsBlockTest &lt; ActiveSupport::TestCase
27 :start_date=>(DateTime.now), :end_date=>(DateTime.now) 27 :start_date=>(DateTime.now), :end_date=>(DateTime.now)
28 28
29 @e5 = Event.new :name=>"Event 5", :body=>"Event 5 body", 29 @e5 = Event.new :name=>"Event 5", :body=>"Event 5 body",
30 - :start_date=>(DateTime.now - 5.days) 30 + :start_date=>(DateTime.now + 5.days)
  31 +
  32 + @e6 = Event.new :name=>"Event 6", :body=>"Event 5 body",
  33 + :start_date=>(DateTime.now)
31 34
32 @community.events << @e1 35 @community.events << @e1
33 @community.events << @e2 36 @community.events << @e2
34 @community.events << @e3 37 @community.events << @e3
35 @community.events << @e4 38 @community.events << @e4
36 @community.events << @e5 39 @community.events << @e5
  40 + @community.events << @e6
37 @community.save! 41 @community.save!
38 end 42 end
39 43
40 - should "give community events that have not yet finished ordered by start date" do 44 + should "get events with start date equals or bigger than current day ordered by start date" do
41 events = @software_events_block.get_events 45 events = @software_events_block.get_events
42 46
  47 + assert_equal false, events.include?(@e2)
43 assert_equal false, events.include?(@e3) 48 assert_equal false, events.include?(@e3)
  49 +
  50 + assert_equal true, events.include?(@e1)
44 assert_equal true, events.include?(@e4) 51 assert_equal true, events.include?(@e4)
45 - assert_equal @e2, events.first  
46 - assert_equal @e1, events.last 52 + assert_equal true, events.include?(@e5)
  53 + assert_equal true, events.include?(@e6)
  54 +
  55 + assert_equal @e1, events.first
  56 + assert_equal @e5, events.last
47 end 57 end
48 58
49 should "include community events that have no end date" do 59 should "include community events that have no end date" do
50 events = @software_events_block.get_events 60 events = @software_events_block.get_events
51 61
52 assert_equal true, events.include?(@e5) 62 assert_equal true, events.include?(@e5)
  63 + assert_equal true, events.include?(@e6)
53 end 64 end
54 65
55 should "give community events except by a event with a given slug" do 66 should "give community events except by a event with a given slug" do
@@ -69,8 +80,11 @@ class SoftwareEventsBlockTest &lt; ActiveSupport::TestCase @@ -69,8 +80,11 @@ class SoftwareEventsBlockTest &lt; ActiveSupport::TestCase
69 should "tell if there are events to be displayed when given a event page slug" do 80 should "tell if there are events to be displayed when given a event page slug" do
70 update_all_events (DateTime.now - 2.days), (DateTime.now - 1.day) 81 update_all_events (DateTime.now - 2.days), (DateTime.now - 1.day)
71 82
  83 + assert_equal false, @software_events_block.has_events_to_display?
  84 +
72 last_event = @community.events.last 85 last_event = @community.events.last
73 - last_event.end_date = DateTime.now + 10.days 86 + last_event.start_date = DateTime.now
  87 + last_event.end_date = DateTime.now + 1.day
74 last_event.save! 88 last_event.save!
75 89
76 assert_equal true, @software_events_block.has_events_to_display? 90 assert_equal true, @software_events_block.has_events_to_display?
src/noosfero-spb/software_communities/views/box_organizer/_software_events_block.html.erb 0 → 100644
@@ -0,0 +1,8 @@ @@ -0,0 +1,8 @@
  1 +<div id='edit-software-events-block' class='formfieldline'>
  2 + <%= label_tag 'block_amount_of_events', _('Amount of events to display:') %>
  3 + <span class='hint' title=" <%= _('This field must contain values bigger than 0') %>"> (?) </span>
  4 + <div class='formfield'>
  5 + <%= number_field_tag 'block[amount_of_events]', @block.amount_of_events, min: 1, max: 100 %>
  6 + </div>
  7 +</div>
  8 +