common_view_helpers 0.3.0 → 0.3.1

Sign up to get free protection for your applications and to get access to all the features.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.3.0
1
+ 0.3.1
data/init.rb CHANGED
@@ -1,3 +1 @@
1
- require File.dirname(__FILE__) + '/lib/common_view_helpers'
2
-
3
- ActionView::Base.send(:include, CommonViewHelpers::ViewHelpers)
1
+ require File.expand_path('lib/common_view_helpers', File.dirname(__FILE__))
@@ -0,0 +1,127 @@
1
+ module CommonViewHelpers
2
+ module ViewHelpers
3
+
4
+ # Use words if within the last week, otherwise use date (show year if not this year)
5
+ def time_ago_in_words_or_date(date,options={})
6
+ return unless date
7
+ if (Time.now-date)/60/60/24 < 7
8
+ time_ago_in_words(date) + " ago"
9
+ elsif date.year == Time.now.year
10
+ options[:short_format] ? date.strftime(options[:short_format]) : date.strftime("%b %e").gsub(" ", " ")
11
+ else
12
+ options[:long_format] ? date.strftime(options[:long_format]) : date.strftime("%b %e, %Y").gsub(" ", " ")
13
+ end
14
+ end
15
+
16
+ # Output an easily styleable key-value pair
17
+ def info_pair(label, value)
18
+ value = content_tag(:span, "None", :class => "blank") if value.blank?
19
+ label = content_tag(:span, "#{label}:", :class => "label")
20
+ content_tag(:span, [label, value].join(" "), :class => "info_pair")
21
+ end
22
+
23
+ # Give this helper an array, and get back a string of <li> elements.
24
+ # The first item gets a class of first and the last, well.. last.
25
+ # This makes it easier to apply CSS styles to lists, be they ordered or unordered.
26
+ # http://zeke.tumblr.com/post/98025647/a-nice-little-view-helper-for-generating-list-items
27
+ def convert_to_list_items(items, *args)
28
+ default_options = {:stripe => true}
29
+ options = default_options.merge(args.extract_options!)
30
+ out = []
31
+ items.each_with_index do |item, index|
32
+ css = []
33
+ css << "first" if items.first == item
34
+ css << "last" if items.last == item
35
+ css << "even" if options[:stripe] && index%2 == 1
36
+ css << "odd" if options[:stripe] && index%2 == 0 # (First item is odd (1))
37
+ out << content_tag(:li, item, :class => css.join(" "))
38
+ end
39
+ out.join("\n")
40
+ end
41
+
42
+ # Build an HTML table
43
+ # For collection, pass an array of arrays
44
+ # For headers, pass an array of label strings for the top of the table
45
+ # All other options will be passed along to the table content_tag
46
+ def generate_table(collection, headers=nil, options={})
47
+ return if collection.blank?
48
+ thead = content_tag(:thead) do
49
+ content_tag(:tr) do
50
+ headers.map {|header| content_tag(:th, header)}
51
+ end
52
+ end unless headers.nil?
53
+ tbody = content_tag(:tbody) do
54
+ collection.map do |values|
55
+ content_tag(:tr) do
56
+ values.map {|value| content_tag(:td, value)}
57
+ end
58
+ end
59
+ end
60
+ content_tag(:table, [thead, tbody].compact.join("\n"), options)
61
+ end
62
+
63
+ # Pass in an ActiveRecord object, get back edit and delete links inside a TD tag
64
+ def options_td(record_or_name_or_array, hide_destroy = false)
65
+ items = []
66
+ items << link_to('Edit', edit_polymorphic_path(record_or_name_or_array))
67
+ items << link_to('Delete', polymorphic_path(record_or_name_or_array), :confirm => 'Are you sure?', :method => :delete, :class => "destructive") unless hide_destroy
68
+ list = content_tag(:ul, convert_to_list_items(items))
69
+ content_tag(:td, list, :class => "options")
70
+ end
71
+
72
+ # This works just like link_to, but with one difference..
73
+ # If the link is to the current page, a class of 'active' is added
74
+ def link(name, options={}, html_options={})
75
+ link_to_unless_current(name, options, html_options) do
76
+ html_options[:class] = (html_options[:class] || "").split(" ").push("active").join(" ")
77
+ link_to(name, options, html_options)
78
+ end
79
+ end
80
+
81
+ # Absolute path to an image
82
+ def image_url(source)
83
+ base_url + image_path(source)
84
+ end
85
+
86
+ # e.g. http://localhost:3000, or https://productionserver.com
87
+ def base_url
88
+ "#{request.protocol}#{request.host_with_port}"
89
+ end
90
+
91
+ # Generate a list of column name-value pairs for an AR object
92
+ def list_model_columns(obj)
93
+ items = obj.class.columns.map{ |col| info_pair(col.name, obj[col.name]) }
94
+ content_tag(:ul, convert_to_list_items(items), :class => "model_columns")
95
+ end
96
+
97
+ # Outputs a JS friendly (escaped) string
98
+ def js_string(string)
99
+ js_escape_map = {
100
+ '\\' => '\\\\',
101
+ '</' => '<\/',
102
+ "\r\n" => '\n',
103
+ "\n" => '\n',
104
+ "\r" => '\n',
105
+ '"' => '\\"',
106
+ "'" => "\\'" }
107
+
108
+ string.gsub(/(\\|<\/|\r\n|[\n\r"'])/) { js_escape_map[$1] }
109
+ end
110
+
111
+ # Generates a URL and automatically adds http:// if not already there
112
+ # Useful alongside link() or when linking to user submitted addresses
113
+ def urlify(addr)
114
+ (addr.blank? || addr.starts_with?('http')) ? addr : "http://#{addr}"
115
+ end
116
+
117
+ # Breaks up a BigNum, FixNum or Float with commas
118
+ # e.g., 532566 => 532,566 and 79593255.66 => 79,593,255.66
119
+ def commify(num)
120
+ num.to_s =~ /([^\.]*)(\..*)?/
121
+ int, dec = $1.reverse, $2 ? $2 : ""
122
+ while int.gsub!(/(,|\.|^)(\d{3})(\d)/, '\1\2,\3'); end
123
+ int.reverse + dec
124
+ end
125
+
126
+ end
127
+ end
@@ -1,127 +1,3 @@
1
- module CommonViewHelpers
2
- module ViewHelpers
1
+ require 'common_view_helpers/common_view_helpers'
3
2
 
4
- # Use words if within the last week, otherwise use date (show year if not this year)
5
- def time_ago_in_words_or_date(date,options={})
6
- return unless date
7
- if (Time.now-date)/60/60/24 < 7
8
- time_ago_in_words(date) + " ago"
9
- elsif date.year == Time.now.year
10
- options[:short_format] ? date.strftime(options[:short_format]) : date.strftime("%b %e").gsub(" ", " ")
11
- else
12
- options[:long_format] ? date.strftime(options[:long_format]) : date.strftime("%b %e, %Y").gsub(" ", " ")
13
- end
14
- end
15
-
16
- # Output an easily styleable key-value pair
17
- def info_pair(label, value)
18
- value = content_tag(:span, "None", :class => "blank") if value.blank?
19
- label = content_tag(:span, "#{label}:", :class => "label")
20
- content_tag(:span, [label, value].join(" "), :class => "info_pair")
21
- end
22
-
23
- # Give this helper an array, and get back a string of <li> elements.
24
- # The first item gets a class of first and the last, well.. last.
25
- # This makes it easier to apply CSS styles to lists, be they ordered or unordered.
26
- # http://zeke.tumblr.com/post/98025647/a-nice-little-view-helper-for-generating-list-items
27
- def convert_to_list_items(items, *args)
28
- default_options = {:stripe => true}
29
- options = default_options.merge(args.extract_options!)
30
- out = []
31
- items.each_with_index do |item, index|
32
- css = []
33
- css << "first" if items.first == item
34
- css << "last" if items.last == item
35
- css << "even" if options[:stripe] && index%2 == 1
36
- css << "odd" if options[:stripe] && index%2 == 0 # (First item is odd (1))
37
- out << content_tag(:li, item, :class => css.join(" "))
38
- end
39
- out.join("\n")
40
- end
41
-
42
- # Build an HTML table
43
- # For collection, pass an array of arrays
44
- # For headers, pass an array of label strings for the top of the table
45
- # All other options will be passed along to the table content_tag
46
- def generate_table(collection, headers=nil, options={})
47
- return if collection.blank?
48
- thead = content_tag(:thead) do
49
- content_tag(:tr) do
50
- headers.map {|header| content_tag(:th, header)}
51
- end
52
- end unless headers.nil?
53
- tbody = content_tag(:tbody) do
54
- collection.map do |values|
55
- content_tag(:tr) do
56
- values.map {|value| content_tag(:td, value)}
57
- end
58
- end
59
- end
60
- content_tag(:table, [thead, tbody].compact.join("\n"), options)
61
- end
62
-
63
- # Pass in an ActiveRecord object, get back edit and delete links inside a TD tag
64
- def options_td(record_or_name_or_array, hide_destroy = false)
65
- items = []
66
- items << link_to('Edit', edit_polymorphic_path(record_or_name_or_array))
67
- items << link_to('Delete', polymorphic_path(record_or_name_or_array), :confirm => 'Are you sure?', :method => :delete, :class => "destructive") unless hide_destroy
68
- list = content_tag(:ul, convert_to_list_items(items))
69
- content_tag(:td, list, :class => "options")
70
- end
71
-
72
- # This works just like link_to, but with one difference..
73
- # If the link is to the current page, a class of 'active' is added
74
- def link(name, options={}, html_options={})
75
- link_to_unless_current(name, options, html_options) do
76
- html_options[:class] = (html_options[:class] || "").split(" ").push("active").join(" ")
77
- link_to(name, options, html_options)
78
- end
79
- end
80
-
81
- # Absolute path to an image
82
- def image_url(source)
83
- base_url + image_path(source)
84
- end
85
-
86
- # e.g. http://localhost:3000, or https://productionserver.com
87
- def base_url
88
- "#{request.protocol}#{request.host_with_port}"
89
- end
90
-
91
- # Generate a list of column name-value pairs for an AR object
92
- def list_model_columns(obj)
93
- items = obj.class.columns.map{ |col| info_pair(col.name, obj[col.name]) }
94
- content_tag(:ul, convert_to_list_items(items), :class => "model_columns")
95
- end
96
-
97
- # Outputs a JS friendly (escaped) string
98
- def js_string(string)
99
- js_escape_map = {
100
- '\\' => '\\\\',
101
- '</' => '<\/',
102
- "\r\n" => '\n',
103
- "\n" => '\n',
104
- "\r" => '\n',
105
- '"' => '\\"',
106
- "'" => "\\'" }
107
-
108
- string.gsub(/(\\|<\/|\r\n|[\n\r"'])/) { js_escape_map[$1] }
109
- end
110
-
111
- # Generates a URL and automatically adds http:// if not already there
112
- # Useful alongside link() or when linking to user submitted addresses
113
- def urlify(addr)
114
- (addr.blank? || addr.starts_with?('http')) ? addr : "http://#{addr}"
115
- end
116
-
117
- # Breaks up a BigNum, FixNum or Float with commas
118
- # e.g., 532566 => 532,566 and 79593255.66 => 79,593,255.66
119
- def commify(num)
120
- num.to_s =~ /([^\.]*)(\..*)?/
121
- int, dec = $1.reverse, $2 ? $2 : ""
122
- while int.gsub!(/(,|\.|^)(\d{3})(\d)/, '\1\2,\3'); end
123
- int.reverse + dec
124
- end
125
-
126
- end
127
- end
3
+ ActionView::Base.send(:include, CommonViewHelpers::ViewHelpers)
data/rails/init.rb CHANGED
@@ -1,3 +1 @@
1
- require File.dirname(__FILE__) + '/../lib/common_view_helpers'
2
-
3
- ActionView::Base.send(:include, CommonViewHelpers::ViewHelpers)
1
+ require File.expand_path('../lib/common_view_helpers', File.dirname(__FILE__))
metadata CHANGED
@@ -1,13 +1,12 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: common_view_helpers
3
3
  version: !ruby/object:Gem::Version
4
- hash: 19
5
4
  prerelease: false
6
5
  segments:
7
6
  - 0
8
7
  - 3
9
- - 0
10
- version: 0.3.0
8
+ - 1
9
+ version: 0.3.1
11
10
  platform: ruby
12
11
  authors:
13
12
  - Zeke Sikelianos
@@ -16,7 +15,7 @@ autorequire:
16
15
  bindir: bin
17
16
  cert_chain: []
18
17
 
19
- date: 2010-07-30 00:00:00 -04:00
18
+ date: 2010-09-11 00:00:00 -04:00
20
19
  default_executable:
21
20
  dependencies:
22
21
  - !ruby/object:Gem::Dependency
@@ -27,7 +26,6 @@ dependencies:
27
26
  requirements:
28
27
  - - ">="
29
28
  - !ruby/object:Gem::Version
30
- hash: 15
31
29
  segments:
32
30
  - 2
33
31
  - 0
@@ -43,7 +41,6 @@ dependencies:
43
41
  requirements:
44
42
  - - ">="
45
43
  - !ruby/object:Gem::Version
46
- hash: 13
47
44
  segments:
48
45
  - 1
49
46
  - 2
@@ -59,7 +56,6 @@ dependencies:
59
56
  requirements:
60
57
  - - ">="
61
58
  - !ruby/object:Gem::Version
62
- hash: 59
63
59
  segments:
64
60
  - 4
65
61
  - 1
@@ -82,6 +78,7 @@ files:
82
78
  - VERSION
83
79
  - init.rb
84
80
  - lib/common_view_helpers.rb
81
+ - lib/common_view_helpers/common_view_helpers.rb
85
82
  - rails/init.rb
86
83
  - spec/common_view_helpers_spec.rb
87
84
  - spec/spec_helper.rb
@@ -99,7 +96,6 @@ required_ruby_version: !ruby/object:Gem::Requirement
99
96
  requirements:
100
97
  - - ">="
101
98
  - !ruby/object:Gem::Version
102
- hash: 3
103
99
  segments:
104
100
  - 0
105
101
  version: "0"
@@ -108,7 +104,6 @@ required_rubygems_version: !ruby/object:Gem::Requirement
108
104
  requirements:
109
105
  - - ">="
110
106
  - !ruby/object:Gem::Version
111
- hash: 3
112
107
  segments:
113
108
  - 0
114
109
  version: "0"