wordnik_ruby_helpers 0.0.1 → 0.0.2

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/Gemfile CHANGED
@@ -1,4 +1,4 @@
1
1
  source "http://rubygems.org"
2
2
 
3
3
  # Specify your gem's dependencies in wordnik_ruby_helpers.gemspec
4
- gemspec
4
+ gemspec
@@ -0,0 +1,93 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ wordnik_ruby_helpers (0.0.1)
5
+ RedCloth (>= 4.2.3)
6
+ i18n
7
+ rails
8
+
9
+ GEM
10
+ remote: http://rubygems.org/
11
+ specs:
12
+ RedCloth (4.2.3)
13
+ abstract (1.0.0)
14
+ actionmailer (3.0.3)
15
+ actionpack (= 3.0.3)
16
+ mail (~> 2.2.9)
17
+ actionpack (3.0.3)
18
+ activemodel (= 3.0.3)
19
+ activesupport (= 3.0.3)
20
+ builder (~> 2.1.2)
21
+ erubis (~> 2.6.6)
22
+ i18n (~> 0.4)
23
+ rack (~> 1.2.1)
24
+ rack-mount (~> 0.6.13)
25
+ rack-test (~> 0.5.6)
26
+ tzinfo (~> 0.3.23)
27
+ activemodel (3.0.3)
28
+ activesupport (= 3.0.3)
29
+ builder (~> 2.1.2)
30
+ i18n (~> 0.4)
31
+ activerecord (3.0.3)
32
+ activemodel (= 3.0.3)
33
+ activesupport (= 3.0.3)
34
+ arel (~> 2.0.2)
35
+ tzinfo (~> 0.3.23)
36
+ activeresource (3.0.3)
37
+ activemodel (= 3.0.3)
38
+ activesupport (= 3.0.3)
39
+ activesupport (3.0.3)
40
+ arel (2.0.6)
41
+ builder (2.1.2)
42
+ diff-lcs (1.1.2)
43
+ erubis (2.6.6)
44
+ abstract (>= 1.0.0)
45
+ i18n (0.5.0)
46
+ mail (2.2.14)
47
+ activesupport (>= 2.3.6)
48
+ i18n (>= 0.4.0)
49
+ mime-types (~> 1.16)
50
+ treetop (~> 1.4.8)
51
+ mime-types (1.16)
52
+ polyglot (0.3.1)
53
+ rack (1.2.1)
54
+ rack-mount (0.6.13)
55
+ rack (>= 1.0.0)
56
+ rack-test (0.5.7)
57
+ rack (>= 1.0)
58
+ rails (3.0.3)
59
+ actionmailer (= 3.0.3)
60
+ actionpack (= 3.0.3)
61
+ activerecord (= 3.0.3)
62
+ activeresource (= 3.0.3)
63
+ activesupport (= 3.0.3)
64
+ bundler (~> 1.0)
65
+ railties (= 3.0.3)
66
+ railties (3.0.3)
67
+ actionpack (= 3.0.3)
68
+ activesupport (= 3.0.3)
69
+ rake (>= 0.8.7)
70
+ thor (~> 0.14.4)
71
+ rake (0.8.7)
72
+ rspec (2.4.0)
73
+ rspec-core (~> 2.4.0)
74
+ rspec-expectations (~> 2.4.0)
75
+ rspec-mocks (~> 2.4.0)
76
+ rspec-core (2.4.0)
77
+ rspec-expectations (2.4.0)
78
+ diff-lcs (~> 1.1.2)
79
+ rspec-mocks (2.4.0)
80
+ thor (0.14.6)
81
+ treetop (1.4.9)
82
+ polyglot (>= 0.3.1)
83
+ tzinfo (0.3.23)
84
+
85
+ PLATFORMS
86
+ ruby
87
+
88
+ DEPENDENCIES
89
+ RedCloth (>= 4.2.3)
90
+ i18n
91
+ rails
92
+ rspec
93
+ wordnik_ruby_helpers!
data/README ADDED
File without changes
data/Rakefile CHANGED
@@ -1,2 +1,2 @@
1
1
  require 'bundler'
2
- Bundler::GemHelper.install_tasks
2
+ Bundler::GemHelper.install_tasks
data/init.rb ADDED
@@ -0,0 +1,3 @@
1
+ require File.dirname(__FILE__) + '/lib/wordnik_ruby_helpers'
2
+
3
+ ActionView::Base.send( :include, WordnikRubyHelpers::ViewHelpers )
@@ -1,22 +1,102 @@
1
+ require 'i18n'
2
+ path=File.dirname(__FILE__)
3
+ I18n.load_path += Dir[ File.join(path, 'locales', '*.{rb,yml}') ]
4
+
1
5
  module WordnikRubyHelpers
6
+ module ViewHelpers
7
+
8
+ # Use words if within the last week, otherwise use date (show year if not this year)
9
+ def time_ago_in_words_or_date(date,options={})
10
+ return unless date
11
+ if (Time.now-date)/60/60/24 < 7
12
+ time_ago_in_words(date) + " ago"
13
+ elsif date.year == Time.now.year
14
+ options[:short_format] ? date.strftime(options[:short_format]) : date.strftime("%b %e").gsub(" ", " ")
15
+ else
16
+ options[:long_format] ? date.strftime(options[:long_format]) : date.strftime("%b %e, %Y").gsub(" ", " ")
17
+ end
18
+ end
19
+
20
+ # Output an easily styleable key-value pair
21
+ def info_pair(label, value)
22
+ value = content_tag(:span, "None", :class => "blank") if value.blank?
23
+ label = content_tag(:span, "#{label}:", :class => "label")
24
+ content_tag(:span, [label, value].join(" "), :class => "info_pair")
25
+ end
26
+
27
+ # Give this helper an array, and get back a string of <li> elements.
28
+ # The first item gets a class of first and the last, well.. last.
29
+ # This makes it easier to apply CSS styles to lists, be they ordered or unordered.
30
+ # http://zeke.tumblr.com/post/98025647/a-nice-little-view-helper-for-generating-list-items
31
+ def convert_to_list_items(items, *args)
32
+ default_options = {:stripe => true}
33
+ options = default_options.merge(args.extract_options!)
34
+ out = []
35
+ items.each_with_index do |item, index|
36
+ css = []
37
+ css << "first" if items.first == item
38
+ css << "last" if items.last == item
39
+ css << "even" if options[:stripe] && index%2 == 1
40
+ css << "odd" if options[:stripe] && index%2 == 0 # (First item is odd (1))
41
+ out << content_tag(:li, item, :class => css.join(" "))
42
+ end
43
+ raw out.join("\n")
44
+ end
45
+
46
+ # Build an HTML table
47
+ # For collection, pass an array of arrays
48
+ # For headers, pass an array of label strings for the top of the table
49
+ # All other options will be passed along to the table content_tag
50
+ def generate_table(collection, headers=nil, options={})
51
+ return if collection.blank?
52
+ thead = content_tag(:thead) do
53
+ content_tag(:tr) do
54
+ headers.map {|header| content_tag(:th, header)}
55
+ end
56
+ end unless headers.nil?
57
+ tbody = content_tag(:tbody) do
58
+ collection.map do |values|
59
+ content_tag(:tr) do
60
+ values.map {|value| content_tag(:td, value)}
61
+ end
62
+ end
63
+ end
64
+ content_tag(:table, [thead, tbody].compact.join("\n"), options)
65
+ end
2
66
 
3
- # Give this helper an array, and get back a string of <li> elements.
4
- # The first item gets a class of first and the last, well.. last.
5
- # This makes it easier to apply CSS styles to lists, be they ordered or unordered.
6
- # http://zeke.tumblr.com/post/98025647/a-nice-little-view-helper-for-generating-list-items
7
- def convert_to_list_items(items, *args)
8
- default_options = {:stripe => true}
9
- options = default_options.merge(args.extract_options!)
10
- out = []
11
- items.each_with_index do |item, index|
12
- css = []
13
- css << "first" if items.first == item
14
- css << "last" if items.last == item
15
- css << "even" if options[:stripe] && index%2 == 1
16
- css << "odd" if options[:stripe] && index%2 == 0 # (First item is odd (1))
17
- out << content_tag(:li, item, :class => css.join(" "))
18
- end
19
- raw out.join("\n")
20
- end
67
+ # Pass in an ActiveRecord object, get back edit and delete links inside a TD tag
68
+ def options_td(record_or_name_or_array, hide_destroy = false)
69
+ items = []
70
+ items << link_to('Edit', edit_polymorphic_path(record_or_name_or_array))
71
+ items << link_to('Delete', polymorphic_path(record_or_name_or_array), :confirm => 'Are you sure?', :method => :delete, :class => "destructive") unless hide_destroy
72
+ list = content_tag(:ul, convert_to_list_items(items))
73
+ content_tag(:td, list, :class => "options")
74
+ end
75
+
76
+ # This works just like link_to, but with one difference..
77
+ # If the link is to the current page, a class of 'active' is added
78
+ def link(name, options={}, html_options={})
79
+ link_to_unless_current(name, options, html_options) do
80
+ html_options[:class] = (html_options[:class] || "").split(" ").push("active").join(" ")
81
+ link_to(name, options, html_options)
82
+ end
83
+ end
84
+
85
+ # Absolute path to a local image
86
+ def image_url(source)
87
+ base_url + image_path(source)
88
+ end
89
+
90
+ # e.g. http://localhost:3000, or https://productionserver.com
91
+ def base_url
92
+ "#{request.protocol}#{request.host_with_port}"
93
+ end
94
+
95
+ # Generate a list of column name-value pairs for an AR object
96
+ def list_model_columns(obj)
97
+ items = obj.class.columns.map{ |col| info_pair(col.name, obj[col.name]) }
98
+ content_tag(:ul, convert_to_list_items(items), :class => "model_columns")
99
+ end
21
100
 
101
+ end
22
102
  end
@@ -1,3 +1,3 @@
1
1
  module WordnikRubyHelpers
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.2"
3
3
  end
@@ -0,0 +1,4 @@
1
+ --colour
2
+ --format progress
3
+ --loadby mtime
4
+ --reverse
@@ -0,0 +1,24 @@
1
+ require 'rubygems'
2
+ require 'bundler/setup'
3
+
4
+
5
+ # http://stackoverflow.com/questions/3053119/use-rails-3s-activesupport-core-extensions-outside-rails
6
+
7
+ require 'wordnik_ruby_helpers' # and any other gems you need
8
+ require 'active_support/all' # to get methods like blank? and starts_with?
9
+ require 'action_view'
10
+ # require 'active_support/core_ext/string'
11
+
12
+ include ActionView::Helpers
13
+ include ActiveSupport
14
+ include WordnikRubyHelpers::ViewHelpers
15
+
16
+ # include ActionView::Helpers::TextHelper
17
+ # include ActionView::Helpers::TagHelper
18
+ include ActionView::Helpers::DateHelper
19
+ # include ActionView::Helpers::CacheHelper
20
+ # include ActionView::Helpers::CaptureHelper # For the generate_table method
21
+
22
+ RSpec.configure do |config|
23
+ # some (optional) config here
24
+ end
@@ -0,0 +1,30 @@
1
+ require 'spec_helper'
2
+
3
+ describe WordnikRubyHelpers do
4
+ context "time_ago_in_words_or_date" do
5
+ it "should get relative times right" do
6
+ time_ago_in_words_or_date(Time.now - 10.minutes).should == "10 minutes ago"
7
+ time_ago_in_words_or_date(Time.now - 1.day).should == "1 day ago"
8
+ time_ago_in_words_or_date(Time.now - 2.days).should == "2 days ago"
9
+ end
10
+
11
+ it "should use short format for non-relative dates in the last year" do
12
+ time_ago_in_words_or_date(Time.now - 8.days).should =~ /\w{3} \d+/i
13
+ time_ago_in_words_or_date(Time.now - 6.months).should =~ /\w{3} \d+/i
14
+ end
15
+
16
+ it "should display year format for non-relative dates beyond one year ago" do
17
+ time_ago_in_words_or_date(Time.now - 13.months).should =~ /\w{3} \d+, \d{4}/i
18
+ end
19
+ end
20
+
21
+ context "generate_table" do
22
+ it "should generate a simple table" do
23
+ collection = [%w(lucy 12)]
24
+ headers = %w(name age)
25
+ # 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>"
26
+ end
27
+
28
+ end
29
+
30
+ end
@@ -14,6 +14,12 @@ Gem::Specification.new do |s|
14
14
 
15
15
  s.rubyforge_project = "wordnik_ruby_helpers"
16
16
 
17
+ s.add_dependency 'i18n'
18
+ s.add_dependency 'rails'
19
+ s.add_dependency 'RedCloth', '>= 4.2.3'
20
+
21
+ s.add_development_dependency 'rspec'
22
+
17
23
  s.files = `git ls-files`.split("\n")
18
24
  s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
19
25
  s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 0
7
7
  - 0
8
- - 1
9
- version: 0.0.1
8
+ - 2
9
+ version: 0.0.2
10
10
  platform: ruby
11
11
  authors:
12
12
  - Zeke Sikelianos
@@ -17,8 +17,61 @@ cert_chain: []
17
17
 
18
18
  date: 2011-01-12 00:00:00 -08:00
19
19
  default_executable:
20
- dependencies: []
21
-
20
+ dependencies:
21
+ - !ruby/object:Gem::Dependency
22
+ name: i18n
23
+ prerelease: false
24
+ requirement: &id001 !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ">="
28
+ - !ruby/object:Gem::Version
29
+ segments:
30
+ - 0
31
+ version: "0"
32
+ type: :runtime
33
+ version_requirements: *id001
34
+ - !ruby/object:Gem::Dependency
35
+ name: rails
36
+ prerelease: false
37
+ requirement: &id002 !ruby/object:Gem::Requirement
38
+ none: false
39
+ requirements:
40
+ - - ">="
41
+ - !ruby/object:Gem::Version
42
+ segments:
43
+ - 0
44
+ version: "0"
45
+ type: :runtime
46
+ version_requirements: *id002
47
+ - !ruby/object:Gem::Dependency
48
+ name: RedCloth
49
+ prerelease: false
50
+ requirement: &id003 !ruby/object:Gem::Requirement
51
+ none: false
52
+ requirements:
53
+ - - ">="
54
+ - !ruby/object:Gem::Version
55
+ segments:
56
+ - 4
57
+ - 2
58
+ - 3
59
+ version: 4.2.3
60
+ type: :runtime
61
+ version_requirements: *id003
62
+ - !ruby/object:Gem::Dependency
63
+ name: rspec
64
+ prerelease: false
65
+ requirement: &id004 !ruby/object:Gem::Requirement
66
+ none: false
67
+ requirements:
68
+ - - ">="
69
+ - !ruby/object:Gem::Version
70
+ segments:
71
+ - 0
72
+ version: "0"
73
+ type: :development
74
+ version_requirements: *id004
22
75
  description: A handful of monkey patches and view helpers that we use in our ruby/rails projects
23
76
  email:
24
77
  - zeke@wordnik.com
@@ -32,9 +85,15 @@ extra_rdoc_files: []
32
85
  files:
33
86
  - .gitignore
34
87
  - Gemfile
88
+ - Gemfile.lock
89
+ - README
35
90
  - Rakefile
91
+ - init.rb
36
92
  - lib/wordnik_ruby_helpers.rb
37
93
  - lib/wordnik_ruby_helpers/version.rb
94
+ - spec/spec.opts
95
+ - spec/spec_helper.rb
96
+ - spec/wordnik_ruby_helpers_spec.rb
38
97
  - wordnik_ruby_helpers.gemspec
39
98
  has_rdoc: true
40
99
  homepage: http://wordnik.com
@@ -68,5 +127,7 @@ rubygems_version: 1.3.7
68
127
  signing_key:
69
128
  specification_version: 3
70
129
  summary: A handful of monkey patches and view helpers that we use in our ruby/rails projects
71
- test_files: []
72
-
130
+ test_files:
131
+ - spec/spec.opts
132
+ - spec/spec_helper.rb
133
+ - spec/wordnik_ruby_helpers_spec.rb