simple_show 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
data/README.rdoc CHANGED
@@ -34,6 +34,9 @@ In our show action we can then do this:
34
34
  <%= s.show :phone, :label => 'Telephone' %>
35
35
  <%= s.show(:email) {|o| o.email.downcase} %>
36
36
  <%= s.show :born_on, :value => '**censored**' %>
37
+ <%= s.show :updated_at, :format => '%B %d' %>
38
+ <%= s.show :created_at, :format => :short # Assumes you've defined a DATE_FORMAT for :short %>
39
+ <%= s.show :cash_in_wallet, :format => '$%.2f' %>
37
40
  <% end %>
38
41
 
39
42
  Which would generate (using the defaults) output like this:
@@ -55,6 +58,18 @@ Which would generate (using the defaults) output like this:
55
58
  <label>Born on:</label>
56
59
  <span class="value">**censored**</span>
57
60
  </div>
61
+ <div class="show">
62
+ <label>Updated at:</label>
63
+ <span class="value">August 2</span>
64
+ </div>
65
+ <div class="show">
66
+ <label>Created at:</label>
67
+ <span class="value">Aug 1 2010</span>
68
+ </div>
69
+ <div class="show">
70
+ <label>Cash in wallet:</label>
71
+ <span class="value">$123.45</span>
72
+ </div>
58
73
  <br clear="all">
59
74
  </div>
60
75
 
@@ -67,7 +82,7 @@ if you don't want that BR tag then set SimpleShow.clear_on_close to false.
67
82
  == TODO:
68
83
 
69
84
  - Add support for associations.
70
- - Add options for formatting dates, numerics, decimals, and booleans.
85
+ - Add options for formatting and booleans.
71
86
 
72
87
  == License:
73
88
 
@@ -28,8 +28,16 @@ module SimpleShow
28
28
  else
29
29
  value = options[:value] || @record.send(attr)
30
30
  end
31
+ if options[:format].present?
32
+ if [:datetime, :timestamp, :time, :date].include?(@record.class.columns_hash[attr.to_s].type)
33
+ value = value.send(options[:format].is_a?(Symbol) ? :to_s : :strftime, options[:format])
34
+ else
35
+ value = options[:format] % value
36
+ end
37
+ end
38
+
31
39
  @binding.content_tag(SimpleShow.value_tag, :class => SimpleShow.value_class) do
32
- [SimpleShow.value_prefix, value, SimpleShow.value_suffix].compact.join
40
+ [SimpleShow.value_prefix, value, SimpleShow.value_suffix].compact.join.html_safe
33
41
  end
34
42
  end
35
43
  end
@@ -1,3 +1,3 @@
1
1
  module SimpleShow
2
- VERSION = "0.0.2"
2
+ VERSION = "0.0.3"
3
3
  end
data/test/test_helper.rb CHANGED
@@ -34,6 +34,10 @@ end
34
34
 
35
35
  ################################################################################
36
36
 
37
+ ::Time::DATE_FORMATS[:mmddyy] = ::Date::DATE_FORMATS[:mmddyy] = '%m/%d/%y'
38
+
39
+ ################################################################################
40
+
37
41
  class Golfer < ActiveRecord::Base
38
42
  end
39
43
 
@@ -2,19 +2,45 @@ require 'test_helper'
2
2
 
3
3
  class SimpleShowValueTest < SimpleShowTestCase
4
4
 
5
- test 'values and value overrides' do
6
- doc = Nokogiri::HTML.fragment(
5
+ def setup
6
+ super
7
+ @doc = Nokogiri::HTML.fragment(
7
8
  simple_show_for @philip do |s|
8
9
  o = ActiveSupport::SafeBuffer.new
9
10
  o += s.show :name
10
11
  o += s.show(:email) {|o| o.email.upcase }
11
12
  o += s.show :phone, :value => '867-5309'
13
+ o += s.show :born_on, :format => '%B %d'
14
+ o += s.show :born_on, :format => :mmddyy
15
+ o += s.show(:html) {|o| '<b>html</b>' }
16
+ o += s.show :handicap, :format => '%.3f'
17
+ o += s.show :name, :format => '%20s'
12
18
  o
13
19
  end
14
20
  )
15
- assert_equal 'Philip Hallstrom', doc.css('span.value')[0].text
16
- assert_equal 'PHILIP@PJKH.COM', doc.css('span.value')[1].text
17
- assert_equal '867-5309', doc.css('span.value')[2].text
21
+ end
22
+
23
+ test 'values and value overrides' do
24
+ assert_equal 'Philip Hallstrom', @doc.css('span.value')[0].text
25
+ assert_equal 'PHILIP@PJKH.COM', @doc.css('span.value')[1].text
26
+ assert_equal '867-5309', @doc.css('span.value')[2].text
27
+ end
28
+
29
+ test 'date/time with formatting' do
30
+ assert_equal 'May 24', @doc.css('span.value')[3].text
31
+ assert_equal '05/24/74', @doc.css('span.value')[4].text
32
+ end
33
+
34
+ test 'values containing html' do
35
+ assert_equal '<b>html</b>', @doc.css('span.value')[5].inner_html
36
+ end
37
+
38
+ test 'numbers with formatting' do
39
+ assert_equal '6.500', @doc.css('span.value')[6].text
40
+ end
41
+
42
+ test 'strings with formatting' do
43
+ assert_equal ' Philip Hallstrom', @doc.css('span.value')[7].text
18
44
  end
19
45
 
20
46
  end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: simple_show
3
3
  version: !ruby/object:Gem::Version
4
- hash: 27
4
+ hash: 25
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
8
  - 0
9
- - 2
10
- version: 0.0.2
9
+ - 3
10
+ version: 0.0.3
11
11
  platform: ruby
12
12
  authors:
13
13
  - Philip Hallstrom