Commit 114a2689bf17b950a695406d763488669b9a31e8

Authored by Francisco Marcelo de Araújo Lima Júnior
1 parent 01bea002

display content plugin : add publish date attribute

plugins/display_content/lib/display_content_block.rb
1 1 class DisplayContentBlock < Block
2 2  
  3 + MONTHS = [
  4 + N_('January'),
  5 + N_('February'),
  6 + N_('March'),
  7 + N_('April'),
  8 + N_('May'),
  9 + N_('June'),
  10 + N_('July'),
  11 + N_('August'),
  12 + N_('September'),
  13 + N_('October'),
  14 + N_('November'),
  15 + N_('December')
  16 + ]
  17 +
3 18 settings_items :nodes, :type => Array, :default => []
4 19 settings_items :parent_nodes, :type => Array, :default => []
5 20 settings_items :sections,
6 21 :type => Array,
7   - :default => [{:name => _('Title'), :checked => true},
  22 + :default => [{:name => _('Publish date'), :checked => true},
  23 + {:name => _('Title'), :checked => true},
8 24 {:name => _('Abstract'), :checked => true},
9 25 {:name => _('Body'), :checked => false},
10 26 {:name => _('Image'), :checked => false}]
... ... @@ -54,10 +70,12 @@ class DisplayContentBlock &lt; Block
54 70  
55 71 sections.select { |section|
56 72 case section[:name]
  73 + when 'Publish date'
  74 + content_sections += (display_section?(section) ? (content_tag('div', show_date(item.published_at, false), :class => 'published-at') ) : '')
57 75 when 'Title'
58 76 content_sections += (display_section?(section) ? (content_tag('div', link_to(h(item.title), item.url), :class => 'title') ) : '')
59 77 when 'Abstract'
60   - content_sections += (display_section?(section) ? (content_tag('div', item.abstract ,:class => 'lead')) : '' )
  78 + content_sections += (display_section?(section) ? (content_tag('div', item.abstract , :class => 'lead')) : '' )
61 79 if display_section?(section)
62 80 read_more_section = content_tag('div', link_to(_('Read more'), item.url), :class => 'read_more')
63 81 end
... ... @@ -115,4 +133,20 @@ class DisplayContentBlock &lt; Block
115 133 { :profile => [:article], :environment => [:article] }
116 134 end
117 135  
  136 + def show_date(date, use_numbers = false, year=true)
  137 + if date && use_numbers
  138 + date_format = year ? _('%{month}/%{day}/%{year}') : _('%{month}/%{day}')
  139 + date_format % { :day => date.day, :month => date.month, :year => date.year }
  140 + elsif date
  141 + date_format = year ? _('%{month_name} %{day}, %{year}') : _('%{month_name} %{day}')
  142 + date_format % { :day => date.day, :month_name => month_name(date.month), :year => date.year }
  143 + else
  144 + ''
  145 + end
  146 + end
  147 +
  148 + def month_name(n)
  149 + _(MONTHS[n-1])
  150 + end
  151 +
118 152 end
... ...
plugins/display_content/public/style.css
... ... @@ -15,6 +15,7 @@
15 15 margin: 5px;
16 16 }
17 17  
  18 +.block.display-content-block .published-at,
18 19 .block.display-content-block .title,
19 20 .block.display-content-block .lead,
20 21 .block.display-content-block .body,
... ... @@ -23,3 +24,4 @@
23 24 margin: 2px 0px 2px 0px;
24 25 }
25 26  
  27 +
... ...
plugins/display_content/test/unit/display_content_block_test.rb
... ... @@ -674,4 +674,27 @@ class DisplayContentBlockTest &lt; ActiveSupport::TestCase
674 674 assert block.display_section?(section)
675 675 end
676 676  
  677 + should 'display_attribute be true for publish date by default' do
  678 + profile = create_user('testuser').person
  679 +
  680 + block = DisplayContentBlock.new
  681 +
  682 + assert block.display_section?({:name => 'Publish date', :checked => true})
  683 + end
  684 +
  685 + should 'show publishd date if defined by user' do
  686 + profile = create_user('testuser').person
  687 + a = fast_create(TextArticle, :name => 'test article 1', :profile_id => profile.id, :body => 'some body')
  688 +
  689 + block = DisplayContentBlock.new
  690 + block.nodes = [a.id]
  691 + block.sections = [{:name => 'Publish date', :checked => true}]
  692 + box = mock()
  693 + block.stubs(:box).returns(box)
  694 + box.stubs(:owner).returns(profile)
  695 +
  696 + assert_match /#{a.published_at}/, block.content
  697 + end
  698 +
  699 +
677 700 end
... ...