radiant-featured_pages-extension 2.0.0 → 2.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -35,13 +35,9 @@ module FeaturedPagesTags
35
35
  find_options.merge!(:limit => limit) if limit
36
36
 
37
37
  date = tag.attr["date"] || nil
38
- @i18n_date_formats ||= (I18n.config.backend.send(:translations)[I18n.locale][:date][:formats] rescue {})
38
+
39
39
  format = tag.attr['format']
40
- if format
41
- format = @i18n_date_formats.keys.include?(format.to_sym) ? format.to_sym : format
42
- else
43
- format = "%m/%d/%Y"
44
- end
40
+ format = i18n_date_formats[format] || "%m/%d/%Y"
45
41
 
46
42
  offset_pair = change_pairs(tag.attr['offset'])
47
43
  window_pair = change_pairs(tag.attr['window'])
@@ -107,6 +103,92 @@ module FeaturedPagesTags
107
103
  tag.expand unless tag.locals.page == tag.locals.featured_pages.first
108
104
  end
109
105
 
106
+ desc %{
107
+ Displays it's contents if the current page is featured in the given window of time.
108
+ Accepts these parameters:
109
+
110
+ * date - no default. Use a formatted date or 'today', 'future', or 'past'
111
+ * format - used only with the date parameters to specify the format of the date you are using
112
+ * window - '+3 days' no default, allows you to add(+n) or subtract(-n) days, weeks, months, years
113
+ * offset - '-1 month' no default, allows you to offset the actual date from the date given
114
+
115
+ Selecting a date of 'future' or 'past' will expand the window to any time in the future or past
116
+ beyond the offset.
117
+
118
+ *Usage:*
119
+ <pre><code><r:if_featured>...</r:if_featured></code></pre>
120
+ }
121
+ tag 'if_featured' do |tag|
122
+ date = tag.attr["date"] || nil
123
+
124
+ if date
125
+ offset_pair = change_pairs(tag.attr['offset'])
126
+ window_pair = change_pairs(tag.attr['window'])
127
+
128
+ format = tag.attr['format']
129
+ format = i18n_date_formats[format] || "%m/%d/%Y"
130
+
131
+ focus_date = begin
132
+ I18n.l(date, :format => format).in_time_zone
133
+ rescue
134
+ Rails.env.test? ? Time.zone.now - 1.second : Time.zone.now
135
+ end
136
+
137
+ date_with_offset = focus_date.in_time_zone + offset_pair.first.send(offset_pair.last)
138
+ date_with_window = date_with_offset + window_pair.first.send(window_pair.last)
139
+ range_dates = [date_with_offset, date_with_window].sort
140
+
141
+ in_future_window = (date == 'future' && tag.locals.page.featured_date > date_with_offset)
142
+ in_past_window = (date == 'past' && tag.locals.page.featured_date < date_with_offset)
143
+ in_range_window = (tag.locals.page.featured_date >= range_dates.first.beginning_of_day && tag.locals.page.featured_date <= range_dates.last.end_of_day)
144
+
145
+ if in_future_window || in_past_window || in_range_window
146
+ tag.expand
147
+ end
148
+ else
149
+ tag.expand if tag.locals.page.featured_date.present?
150
+ end
151
+ end
152
+
153
+ desc %{
154
+ Displays it's contents if the current page is not featured in the given window of time.
155
+ Accepts the same parameters as if_featured.
156
+
157
+ *Usage:*
158
+ <pre><code><r:unless_featured>...</r:unless_featured></code></pre>
159
+ }
160
+ tag 'unless_featured' do |tag|
161
+ date = tag.attr["date"] || nil
162
+
163
+ if date
164
+ offset_pair = change_pairs(tag.attr['offset'])
165
+ window_pair = change_pairs(tag.attr['window'])
166
+
167
+ format = tag.attr['format']
168
+ format = i18n_date_formats[format] || "%m/%d/%Y"
169
+
170
+ focus_date = begin
171
+ I18n.l(date, :format => format).in_time_zone
172
+ rescue
173
+ Rails.env.test? ? Time.zone.now - 1.second : Time.zone.now
174
+ end
175
+
176
+ date_with_offset = (focus_date.in_time_zone + offset_pair.first.send(offset_pair.last)).beginning_of_day
177
+ date_with_window = (date_with_offset + window_pair.first.send(window_pair.last)).end_of_day
178
+ range_dates = [date_with_offset, date_with_window].sort
179
+
180
+ in_future_window = (date == 'future' && tag.locals.page.featured_date > date_with_offset)
181
+ in_past_window = (date == 'past' && tag.locals.page.featured_date < date_with_offset)
182
+ in_range_window = (tag.locals.page.featured_date >= range_dates.first.beginning_of_day && tag.locals.page.featured_date <= range_dates.last.end_of_day)
183
+
184
+ unless (in_future_window || in_past_window || in_range_window)
185
+ tag.expand
186
+ end
187
+ else
188
+ tag.expand unless tag.locals.page.featured_date.present?
189
+ end
190
+ end
191
+
110
192
  private
111
193
 
112
194
  def change_pairs(text='')
@@ -116,4 +198,13 @@ module FeaturedPagesTags
116
198
  unit = change_parts.last =~ /^(second|minute|hour|day|week|month|year)s?$/ ? change_parts.last : 'days'
117
199
  [num, unit]
118
200
  end
201
+
202
+ def date_change_amount(text_or_pair)
203
+ pair = change_pairs(text_or_pair)
204
+ pair.first.send(pair.second)
205
+ end
206
+
207
+ def i18n_date_formats
208
+ @i18n_date_formats ||= I18n.t('date.formats')
209
+ end
119
210
  end
@@ -1,3 +1,3 @@
1
1
  module RadiantFeaturedPagesExtension
2
- VERSION = '2.0.0'
2
+ VERSION = '2.0.1'
3
3
  end
@@ -84,4 +84,92 @@ describe Page do
84
84
  page.should render('<r:featured_pages:each><r:unless_first><r:title /> </r:unless_first></r:featured_pages:each>').as('plus_2_months plus_2_weeks plus_1_day page minus_1_week ')
85
85
  end
86
86
  end
87
+
88
+ describe '<r:if_featured>' do
89
+ subject{ page }
90
+ it "should expand contents for a page with a featured date" do
91
+ page.should render('<r:if_featured>YES!</r:if_featured>').as('YES!')
92
+ end
93
+ it 'should not render for a non-featured page' do
94
+ page.featured_date = nil
95
+ page.should render('<r:if_featured>YES!</r:if_featured>').as('')
96
+ end
97
+ context 'with saved featured pages' do
98
+ before do
99
+ save_pages
100
+ end
101
+ it {
102
+ should render(
103
+ '<r:featured_pages:each date="today" window="1 month"><r:if_featured date="today" window="1 week"><r:title /> </r:if_featured></r:featured_pages:each>'
104
+ ).as(
105
+ 'plus_1_day page '
106
+ )
107
+ }
108
+ it {
109
+ should render(
110
+ '<r:featured_pages:each><r:if_featured date="today" window="1 week" offset="1 week"><r:title /> </r:if_featured></r:featured_pages:each>'
111
+ ).as(
112
+ 'plus_2_weeks '
113
+ )
114
+ }
115
+ it {
116
+ should render(
117
+ '<r:featured_pages:each><r:if_featured date="future" offset="3 weeks"><r:title /> </r:if_featured></r:featured_pages:each>'
118
+ ).as(
119
+ 'plus_2_years plus_2_months '
120
+ )
121
+ }
122
+ it {
123
+ should render(
124
+ '<r:featured_pages:each><r:if_featured date="past" offset="2 weeks"><r:title /> </r:if_featured></r:featured_pages:each>'
125
+ ).as(
126
+ 'plus_2_weeks plus_1_day page minus_1_week '
127
+ )
128
+ }
129
+ end
130
+ end
131
+
132
+ describe '<r:unless_featured>' do
133
+ subject{ page }
134
+ it "should not expand contents for a page with a featured date" do
135
+ page.should render('<r:unless_featured>YES!</r:unless_featured>').as('')
136
+ end
137
+ it 'should not render for a non-featured page' do
138
+ page.featured_date = nil
139
+ page.should render('<r:unless_featured>YES!</r:unless_featured>').as('YES!')
140
+ end
141
+ context 'with saved featured pages' do
142
+ before do
143
+ save_pages
144
+ end
145
+ it {
146
+ should render(
147
+ '<r:featured_pages:each date="today" window="1 month"><r:unless_featured date="today" window="1 week"><r:title /> </r:unless_featured></r:featured_pages:each>'
148
+ ).as(
149
+ 'plus_2_weeks '
150
+ )
151
+ }
152
+ it {
153
+ should render(
154
+ '<r:featured_pages:each><r:unless_featured date="today" window="1 week" offset="1 week"><r:title /> </r:unless_featured></r:featured_pages:each>'
155
+ ).as(
156
+ 'plus_2_years plus_2_months plus_1_day page minus_1_week '
157
+ )
158
+ }
159
+ it {
160
+ should render(
161
+ '<r:featured_pages:each><r:unless_featured date="future" offset="3 weeks"><r:title /> </r:unless_featured></r:featured_pages:each>'
162
+ ).as(
163
+ 'plus_2_weeks plus_1_day page minus_1_week '
164
+ )
165
+ }
166
+ it {
167
+ should render(
168
+ '<r:featured_pages:each><r:unless_featured date="past" offset="2 weeks"><r:title /> </r:unless_featured></r:featured_pages:each>'
169
+ ).as(
170
+ 'plus_2_years plus_2_months '
171
+ )
172
+ }
173
+ end
174
+ end
87
175
  end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: radiant-featured_pages-extension
3
3
  version: !ruby/object:Gem::Version
4
- hash: 15
5
- prerelease: false
4
+ hash: 13
5
+ prerelease:
6
6
  segments:
7
7
  - 2
8
8
  - 0
9
- - 0
10
- version: 2.0.0
9
+ - 1
10
+ version: 2.0.1
11
11
  platform: ruby
12
12
  authors:
13
13
  - Jim Gay
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2010-11-08 00:00:00 -05:00
18
+ date: 2011-03-12 00:00:00 -05:00
19
19
  default_executable:
20
20
  dependencies: []
21
21
 
@@ -60,7 +60,7 @@ has_rdoc: true
60
60
  homepage: http://github.com/saturnflyer/radiant-featured_pages-extension
61
61
  licenses: []
62
62
 
63
- post_install_message: "\n Add this to your radiant project with:\n config.gem 'radiant-featured_pages-extension', :version => '2.0.0'\n "
63
+ post_install_message: "\n Add this to your radiant project with:\n config.gem 'radiant-featured_pages-extension', :version => '2.0.1'\n "
64
64
  rdoc_options: []
65
65
 
66
66
  require_paths:
@@ -86,7 +86,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
86
86
  requirements: []
87
87
 
88
88
  rubyforge_project:
89
- rubygems_version: 1.3.7
89
+ rubygems_version: 1.5.2
90
90
  signing_key:
91
91
  specification_version: 3
92
92
  summary: Featured Pages Extension for Radiant CMS