forked from zeke/common_view_helpers
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 2775245
Showing
10 changed files
with
142 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
Copyright (c) 2009 [name of plugin creator] | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining | ||
a copy of this software and associated documentation files (the | ||
"Software"), to deal in the Software without restriction, including | ||
without limitation the rights to use, copy, modify, merge, publish, | ||
distribute, sublicense, and/or sell copies of the Software, and to | ||
permit persons to whom the Software is furnished to do so, subject to | ||
the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be | ||
included in all copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, | ||
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF | ||
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND | ||
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE | ||
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION | ||
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION | ||
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
= Common View Helpers | ||
|
||
A set of view helpers I find myself using in most Rails applications. | ||
|
||
== Installation | ||
|
||
cd my_rails_app | ||
script/plugin install git://github.com/zeke/common_view_helpers.git | ||
|
||
== Summary | ||
|
||
* *time_ago_in_words_or_date(date,options={})* uses words if within the last week, otherwise uses date. Shows year if not this year | ||
* *convert_to_list_items(items)* takes an array, and gives back a string of <li> elements. {See the full post}[http://zeke.tumblr.com/post/98025647/a-nice-little-view-helper-for-generating-list-items]. | ||
* *link(name, options={}, html_options={})* works just like link_to, but with one difference: If the link is to the current page, a class of 'active' is added. {See the full post}[http://zeke.tumblr.com/post/129164334/a-new-twist-on-link-to-unless-current]. | ||
* *info_pair(label, value)* outputs an easily styleable key-value pair | ||
* *base_url* gives you the current host's full URL, including the port. e.g. http://localhost:3000, or https://productionserver.com | ||
* *list_model_columns(obj)* generates a list of column name-value pairs for an AR object | ||
|
||
Copyright (c) 2009 {Zeke Sikelianos}[http://zeke.sikelianos.com], released under the MIT license |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
require 'rake' | ||
require 'rake/testtask' | ||
require 'rake/rdoctask' | ||
|
||
desc 'Default: run unit tests.' | ||
task :default => :test | ||
|
||
desc 'Test the common_view_helpers plugin.' | ||
Rake::TestTask.new(:test) do |t| | ||
t.libs << 'lib' | ||
t.libs << 'test' | ||
t.pattern = 'test/**/*_test.rb' | ||
t.verbose = true | ||
end | ||
|
||
desc 'Generate documentation for the common_view_helpers plugin.' | ||
Rake::RDocTask.new(:rdoc) do |rdoc| | ||
rdoc.rdoc_dir = 'rdoc' | ||
rdoc.title = 'CommonViewHelpers' | ||
rdoc.options << '--line-numbers' << '--inline-source' | ||
rdoc.rdoc_files.include('README') | ||
rdoc.rdoc_files.include('lib/**/*.rb') | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
require File.dirname(__FILE__) + '/lib/common_view_helpers' | ||
|
||
ActionView::Base.send( :include, CommonViewHelpers::ViewHelpers ) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
# Install hook code here |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
module CommonViewHelpers | ||
module ViewHelpers | ||
|
||
# Use words if within the last week, otherwise use date (show year if not this year) | ||
def time_ago_in_words_or_date(date,options={}) | ||
return nil unless date | ||
if (Time.now-date)/60/60/24 < 7 | ||
time_ago_in_words(date) + " ago" | ||
else | ||
options[:medium] ? date.strftime("%m/%d/%y") : date.to_s(:medium) | ||
end | ||
end | ||
|
||
# Output an easily styleable key-value pair | ||
def info_pair(label, value) | ||
value = content_tag(:span, "None", :class => "blank") if value.blank? | ||
label = content_tag(:span, "#{label}:", :class => "label") | ||
content_tag(:span, [label, value].join(" "), :class => "info_pair") | ||
end | ||
|
||
# Give this helper an array, and get back a string of <li> elements. | ||
# The first item gets a class of first and the last, well.. last. | ||
# This makes it easier to apply CSS styles to lists, be they ordered or unordered. | ||
# http://zeke.tumblr.com/post/98025647/a-nice-little-view-helper-for-generating-list-items | ||
def convert_to_list_items(items) | ||
items.remove_blanks.inject([]) do |all, item| | ||
css = [] | ||
css << "first" if items.first == item | ||
css << "last" if items.last == item | ||
all << content_tag(:li, item, :class => css.join(" ")) | ||
end.join("\n") | ||
end | ||
|
||
# This works just like link_to, but with one difference.. | ||
# If the link is to the current page, a class of 'active' is added | ||
def link(name, options={}, html_options={}) | ||
link_to_unless_current(name, options, html_options) do | ||
html_options[:class] = (html_options[:class] || "").split(" ").push("active").join(" ") | ||
link_to(name, options, html_options) | ||
end | ||
end | ||
|
||
# Absolute path to an image | ||
def image_url(source) | ||
base_url + image_path(source) | ||
end | ||
|
||
# e.g. http://localhost:3000, or https://productionserver.com | ||
def base_url | ||
"#{request.protocol}#{request.host_with_port}" | ||
end | ||
|
||
# Generate a list of column name-value pairs for an AR object | ||
def list_model_columns(obj) | ||
items = obj.class.columns.map{ |col| info_pair(col.name, obj[col.name]) } | ||
content_tag(:ul, convert_to_list_items(items), :class => "model_columns") | ||
end | ||
|
||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
# desc "Explaining what the task does" | ||
# task :common_view_helpers do | ||
# # Task goes here | ||
# end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
require 'test_helper' | ||
|
||
class CommonViewHelpersTest < ActiveSupport::TestCase | ||
# Replace this with your real tests. | ||
test "the truth" do | ||
assert true | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
require 'rubygems' | ||
require 'active_support' | ||
require 'active_support/test_case' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
# Uninstall hook code here |