Skip to content
Browse files

Add timestamp helper

* Use <time> as default tag
* title: false skips title
* Don't mutate options hash
  • Loading branch information...
1 parent 4a48cca commit dc9f696d2356232e007c39a3409f2bd4292b31aa @calebthompson calebthompson committed Feb 19, 2013
Showing with 70 additions and 0 deletions.
  1. +22 −0 Gemfile.lock
  2. +1 −0 flutie.gemspec
  3. +4 −0 lib/flutie/railtie.rb
  4. +9 −0 lib/flutie/timestamp_helper.rb
  5. +34 −0 spec/helpers/timestamp_helper_spec.rb
View
22 Gemfile.lock
@@ -26,13 +26,25 @@ GEM
bundler
rake
builder (3.0.4)
+ capybara (2.0.2)
+ mime-types (>= 1.16)
+ nokogiri (>= 1.3.3)
+ rack (>= 1.0.0)
+ rack-test (>= 0.5.4)
+ selenium-webdriver (~> 2.0)
+ xpath (~> 1.0.0)
+ childprocess (0.3.8)
+ ffi (~> 1.0, >= 1.0.11)
diff-lcs (1.2.1)
erubis (2.7.0)
+ ffi (1.4.0)
hike (1.2.1)
i18n (0.6.4)
journey (1.0.4)
json (1.7.7)
+ mime-types (1.21)
multi_json (1.6.1)
+ nokogiri (1.5.6)
rack (1.4.5)
rack-cache (1.2)
rack (>= 0.4)
@@ -61,6 +73,12 @@ GEM
rspec-core (~> 2.13.0)
rspec-expectations (~> 2.13.0)
rspec-mocks (~> 2.13.0)
+ rubyzip (0.9.9)
+ selenium-webdriver (2.29.0)
+ childprocess (>= 0.2.5)
+ multi_json (~> 1.0)
+ rubyzip
+ websocket (~> 1.0.4)
sprockets (2.2.2)
hike (~> 1.2)
multi_json (~> 1.0)
@@ -69,12 +87,16 @@ GEM
sqlite3 (1.3.7)
thor (0.17.0)
tilt (1.3.4)
+ websocket (1.0.7)
+ xpath (1.0.0)
+ nokogiri (~> 1.3)
PLATFORMS
ruby
DEPENDENCIES
appraisal
+ capybara
flutie!
rspec-rails
sqlite3
View
1 flutie.gemspec
@@ -19,4 +19,5 @@ Gem::Specification.new do |s|
s.add_development_dependency('appraisal')
s.add_development_dependency('sqlite3')
s.add_development_dependency('rspec-rails')
+ s.add_development_dependency('capybara')
end
View
4 lib/flutie/railtie.rb
@@ -1,5 +1,6 @@
require 'flutie/body_class_helper'
require 'flutie/page_title_helper'
+require 'flutie/timestamp_helper'
module Flutie
@@ -10,6 +11,9 @@ class Railtie < Rails::Railtie
initializer 'body_class.helper' do |app|
ActionView::Base.send :include, BodyClassHelper
end
+ initializer 'timestamp.helper' do |app|
+ ActionView::Base.send :include, TimestampHelper
+ end
end
end
View
9 lib/flutie/timestamp_helper.rb
@@ -0,0 +1,9 @@
+module TimestampHelper
+ def timestamp(time, options = {})
+ options = options.dup
+ tag = options.delete(:tag) { :time }
+ options[:title] = options[:title] == false ? nil : l(time)
+ options[:datetime] = time.to_s
+ content_tag(tag, "#{time_ago_in_words(time).capitalize} ago", options)
+ end
+end
View
34 spec/helpers/timestamp_helper_spec.rb
@@ -0,0 +1,34 @@
+require 'spec_helper'
+
+describe TimestampHelper, '#timestamp' do
+ it 'creates a time tag with a datetime and the relative date' do
+ time = 10.minutes.ago
+
+ tag = Capybara.string(timestamp(time)).find('time')
+
+ tag.should have_content(time_ago_in_words(time))
+ tag[:title].should eq(l(time))
+ tag[:datetime].should eq(time.to_s)
+ end
+
+ context 'with optional arguments' do
+ it 'uses tag rather than a <time>' do
+ tag = Capybara.string timestamp(1.minute.ago, :tag => :span)
+
+ tag.should have_css('span')
+ end
+
+ it 'skips the title attribute when :title => false' do
+ tag = Capybara.string timestamp(1.minute.ago, :title => false)
+
+ tag.find('time')[:title].should be_blank
+ end
+
+ it 'does not mutate the options hash' do
+ options = { :tag => :foo }
+ timestamp(1.minute.ago, options)
+
+ options[:tag].should == :foo
+ end
+ end
+end

0 comments on commit dc9f696

Please sign in to comment.
Something went wrong with that request. Please try again.