Permalink
Browse files

Add #pretty_format and use it in index and show screens.

  • Loading branch information...
1 parent 13c5f88 commit ba051159a621d5d23096db04a85034918255f817 @pcreux pcreux committed Mar 17, 2011
@@ -17,6 +17,7 @@ Feature: Index as Table
ActiveAdmin.register Post
"""
Then I should see "Hello World"
+ Then I should see nicely formatted datetimes
And I should see a link to "View"
And I should see a link to "Edit"
And I should see a link to "Delete"
@@ -12,6 +12,7 @@ Feature: Show Resource
"""
Then I should see the attribute "Title" with "Hello World"
And I should see the attribute "Body" with "Empty"
+ And I should see the attribute "Created At" with a nicely formatted datetime
And I should see the attribute "Author" with "jane_doe"
Scenario: Attributes should link when linked resource is registered
@@ -0,0 +1,10 @@
+Then "I should see nicely formatted datetimes" do
+ page.body.should =~ /\w+ \d{1,2}, \d{4} \d{2}:\d{2}/
+end
+
+Then /^I should see the attribute "([^"]*)" with a nicely formatted datetime$/ do |title|
+ Then "I should see \"#{title}\" within \"table.resource_attributes th\""
+ # TODO scope this check
+ page.body.should =~ /\w+ \d{1,2}, \d{4} \d{2}:\d{2}/
+end
+
@@ -94,8 +94,7 @@ def resource
def table_cell(column, item)
row_content = call_method_or_proc_on(item, column.data) || ""
- l(row_content) if [Date, DateTime, Time].include?(row_content)
- content_tag :td, row_content
+ content_tag :td, pretty_format(row_content)
end
end
@@ -14,6 +14,23 @@ def display_name(resource)
resource.send(display_name_method_for(resource))
end
+ # Return a pretty string for any object
+ # Date Time are formatted via #localize with :format => :long
+ # ActiveRecord objects are formatted via #auto_link
+ # We attempt to #display_name of any other objects
+ def pretty_format(object)
+ case object
+ when String
+ object
+ when Date, Time
+ localize(object, :format => :long)
+ when ActiveRecord::Base
+ auto_link(object)
+ else
+ display_name(object)
+ end
+ end
+
end
end
end
@@ -62,7 +62,7 @@ def default_content_for(resource, attr)
if attr.to_s =~ /^([\w]+)_id$/ && resource.respond_to?($1.to_sym)
default_content_for(resource, $1)
else
- value = auto_link(resource.send(attr.to_sym))
+ value = pretty_format(resource.send(attr.to_sym))
value == "" ? default_empty : value
end
end
@@ -0,0 +1,35 @@
+require 'spec_helper'
+
+describe "#pretty_format" do
+ include ActiveAdmin::ViewHelpers::DisplayHelper
+
+ context "when a String is passed in" do
+ it "should return the String passed in" do
+ pretty_format("hello").should == "hello"
+ end
+ end
+
+ context "when a Date or a Time is passed in" do
+ it "should return a localized Date or Time with long format" do
+ t = Time.now
+ self.should_receive(:localize).with(t, {:format => :long}) { "Just Now!" }
+ pretty_format(t).should == "Just Now!"
+ end
+ end
+
+ context "when an ActiveRecord object is passed in" do
+ it "should delegate to auto_link" do
+ post = Post.new
+ self.should_receive(:auto_link).with(post) { "model name" }
+ pretty_format(post).should == "model name"
+ end
+ end
+
+ context "when something else is passed in" do
+ it "should delegate to display_name" do
+ something = Class.new.new
+ self.should_receive(:display_name).with(something) { "I'm not famous" }
+ pretty_format(something).should == "I'm not famous"
+ end
+ end
+end

0 comments on commit ba05115

Please sign in to comment.