common_view_helpers 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/LICENSE +20 -0
- data/README.rdoc +47 -0
- data/VERSION +1 -0
- data/init.rb +3 -0
- data/lib/common_view_helpers.rb +112 -0
- data/rails/init.rb +3 -0
- data/spec/common_view_helpers_spec.rb +44 -0
- data/spec/spec_helper.rb +9 -0
- metadata +124 -0
data/LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2010 Gemini SBS, LLC
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.rdoc
ADDED
@@ -0,0 +1,47 @@
|
|
1
|
+
= Common View Helpers
|
2
|
+
|
3
|
+
A set of view helpers Gemini uses in all of our Rails applications.
|
4
|
+
|
5
|
+
== Installation
|
6
|
+
|
7
|
+
gem install common_view_helpers
|
8
|
+
|
9
|
+
== Summary
|
10
|
+
|
11
|
+
# Use words if within the last week, otherwise use date
|
12
|
+
# Show year if not this year
|
13
|
+
time_ago_in_words_or_date(date, options={})
|
14
|
+
|
15
|
+
# Output an easily styleable key-value pair
|
16
|
+
info_pair(label, value)
|
17
|
+
|
18
|
+
# Give this helper an array, and get back a string of <li> elements.
|
19
|
+
# The first item gets a class of first and the last, well.. last.
|
20
|
+
# This makes it easier to apply CSS styles to lists, be they ordered or unordered.
|
21
|
+
# http://zeke.tumblr.com/post/98025647/a-nice-little-view-helper-for-generating-list-items
|
22
|
+
convert_to_list_items(items)
|
23
|
+
|
24
|
+
# This works just like link_to, but with one difference..
|
25
|
+
# If the link is to the current page, a class of 'active' is added
|
26
|
+
link(name, options={}, html_options={})
|
27
|
+
|
28
|
+
# Absolute path to an image
|
29
|
+
image_url(source)
|
30
|
+
|
31
|
+
# e.g. http://localhost:3000, or https://productionserver.com
|
32
|
+
base_url
|
33
|
+
|
34
|
+
# Generate a list of column name-value pairs for an AR object
|
35
|
+
list_model_columns(obj)
|
36
|
+
|
37
|
+
# Javascript friendly (escaped) string
|
38
|
+
js_string(string)
|
39
|
+
|
40
|
+
== Copyright & Licensing
|
41
|
+
|
42
|
+
Copyright (c) 2010 {Gemini SBS}[http://geminisbs.com], released under the MIT license
|
43
|
+
|
44
|
+
== Authors
|
45
|
+
|
46
|
+
* {Zeke Sikelianos}[http://github.com/zeke]
|
47
|
+
* {Mauricio Gomes}[http://github.com/mgomes]
|
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
0.1.0
|
data/init.rb
ADDED
@@ -0,0 +1,112 @@
|
|
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
|
+
end
|
112
|
+
end
|
data/rails/init.rb
ADDED
@@ -0,0 +1,44 @@
|
|
1
|
+
require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
|
2
|
+
|
3
|
+
require 'rubygems'
|
4
|
+
require 'RedCloth'
|
5
|
+
require 'active_support' # to get methods like blank? and starts_with?
|
6
|
+
require 'action_view'
|
7
|
+
require File.dirname(__FILE__) + '/../lib/common_view_helpers'
|
8
|
+
|
9
|
+
include ActionView::Helpers
|
10
|
+
include CommonViewHelpers::ViewHelpers
|
11
|
+
# include ActionView::Helpers::TextHelper
|
12
|
+
# include ActionView::Helpers::TagHelper
|
13
|
+
# include ActionView::Helpers::DateHelper
|
14
|
+
# include ActionView::Helpers::CacheHelper
|
15
|
+
# include ActionView::Helpers::CaptureHelper # For the generate_table method
|
16
|
+
|
17
|
+
describe CommonViewHelpers do
|
18
|
+
context "time_ago_in_words_or_date" do
|
19
|
+
it "gets relative times right" do
|
20
|
+
time_ago_in_words_or_date(Time.now - 10.minutes).should == "10 minutes ago"
|
21
|
+
time_ago_in_words_or_date(Time.now - 1.day).should == "1 day ago"
|
22
|
+
time_ago_in_words_or_date(Time.now - 2.days).should == "2 days ago"
|
23
|
+
end
|
24
|
+
|
25
|
+
it "should use short format for non-relative dates in the last year" do
|
26
|
+
time_ago_in_words_or_date(Time.now - 8.days).should =~ /\w{3} \d+/i
|
27
|
+
time_ago_in_words_or_date(Time.now - 6.months).should =~ /\w{3} \d+/i
|
28
|
+
end
|
29
|
+
|
30
|
+
it "should display year format for non-relative dates beyond one year ago" do
|
31
|
+
time_ago_in_words_or_date(Time.now - 13.months).should =~ /\w{3} \d+, \d{4}/i
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
context "generate_table" do
|
36
|
+
it "should generate a simple table" do
|
37
|
+
collection = [%w(lucy 12)]
|
38
|
+
headers = %w(name age)
|
39
|
+
# generate_table(collection, headers).should == "<table><thead><tr><th>name</th><th>age</th></tr></thead><tbody><tr><td>lucy</td><td>12</td></tr></tbody></table>"
|
40
|
+
end
|
41
|
+
|
42
|
+
end
|
43
|
+
|
44
|
+
end
|
data/spec/spec_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,124 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: common_view_helpers
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
hash: 27
|
5
|
+
prerelease: false
|
6
|
+
segments:
|
7
|
+
- 0
|
8
|
+
- 1
|
9
|
+
- 0
|
10
|
+
version: 0.1.0
|
11
|
+
platform: ruby
|
12
|
+
authors:
|
13
|
+
- Zeke Sikelianos
|
14
|
+
- Mauricio Gomes
|
15
|
+
autorequire:
|
16
|
+
bindir: bin
|
17
|
+
cert_chain: []
|
18
|
+
|
19
|
+
date: 2010-07-14 00:00:00 -04:00
|
20
|
+
default_executable:
|
21
|
+
dependencies:
|
22
|
+
- !ruby/object:Gem::Dependency
|
23
|
+
name: activesupport
|
24
|
+
prerelease: false
|
25
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
26
|
+
none: false
|
27
|
+
requirements:
|
28
|
+
- - ">="
|
29
|
+
- !ruby/object:Gem::Version
|
30
|
+
hash: 15
|
31
|
+
segments:
|
32
|
+
- 2
|
33
|
+
- 0
|
34
|
+
- 0
|
35
|
+
version: 2.0.0
|
36
|
+
type: :runtime
|
37
|
+
version_requirements: *id001
|
38
|
+
- !ruby/object:Gem::Dependency
|
39
|
+
name: rspec
|
40
|
+
prerelease: false
|
41
|
+
requirement: &id002 !ruby/object:Gem::Requirement
|
42
|
+
none: false
|
43
|
+
requirements:
|
44
|
+
- - ">="
|
45
|
+
- !ruby/object:Gem::Version
|
46
|
+
hash: 13
|
47
|
+
segments:
|
48
|
+
- 1
|
49
|
+
- 2
|
50
|
+
- 9
|
51
|
+
version: 1.2.9
|
52
|
+
type: :development
|
53
|
+
version_requirements: *id002
|
54
|
+
- !ruby/object:Gem::Dependency
|
55
|
+
name: RedCloth
|
56
|
+
prerelease: false
|
57
|
+
requirement: &id003 !ruby/object:Gem::Requirement
|
58
|
+
none: false
|
59
|
+
requirements:
|
60
|
+
- - ">="
|
61
|
+
- !ruby/object:Gem::Version
|
62
|
+
hash: 59
|
63
|
+
segments:
|
64
|
+
- 4
|
65
|
+
- 1
|
66
|
+
- 0
|
67
|
+
version: 4.1.0
|
68
|
+
type: :development
|
69
|
+
version_requirements: *id003
|
70
|
+
description: Common view helpers used by Gemini in most of our applications
|
71
|
+
email: admin@geminisbs.com
|
72
|
+
executables: []
|
73
|
+
|
74
|
+
extensions: []
|
75
|
+
|
76
|
+
extra_rdoc_files:
|
77
|
+
- LICENSE
|
78
|
+
- README.rdoc
|
79
|
+
files:
|
80
|
+
- LICENSE
|
81
|
+
- README.rdoc
|
82
|
+
- VERSION
|
83
|
+
- init.rb
|
84
|
+
- lib/common_view_helpers.rb
|
85
|
+
- rails/init.rb
|
86
|
+
- spec/common_view_helpers_spec.rb
|
87
|
+
- spec/spec_helper.rb
|
88
|
+
has_rdoc: true
|
89
|
+
homepage: http://github.com/geminisbs/common_view_helpers
|
90
|
+
licenses: []
|
91
|
+
|
92
|
+
post_install_message:
|
93
|
+
rdoc_options:
|
94
|
+
- --charset=UTF-8
|
95
|
+
require_paths:
|
96
|
+
- lib
|
97
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
98
|
+
none: false
|
99
|
+
requirements:
|
100
|
+
- - ">="
|
101
|
+
- !ruby/object:Gem::Version
|
102
|
+
hash: 3
|
103
|
+
segments:
|
104
|
+
- 0
|
105
|
+
version: "0"
|
106
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
107
|
+
none: false
|
108
|
+
requirements:
|
109
|
+
- - ">="
|
110
|
+
- !ruby/object:Gem::Version
|
111
|
+
hash: 3
|
112
|
+
segments:
|
113
|
+
- 0
|
114
|
+
version: "0"
|
115
|
+
requirements: []
|
116
|
+
|
117
|
+
rubyforge_project:
|
118
|
+
rubygems_version: 1.3.7
|
119
|
+
signing_key:
|
120
|
+
specification_version: 3
|
121
|
+
summary: Common view helpers for Rails apps
|
122
|
+
test_files:
|
123
|
+
- spec/common_view_helpers_spec.rb
|
124
|
+
- spec/spec_helper.rb
|