Skip to content

Commit

Permalink
Added grid helpers
Browse files Browse the repository at this point in the history
  • Loading branch information
Joshua Clayton committed May 1, 2009
1 parent 67c8e73 commit f08491f
Show file tree
Hide file tree
Showing 9 changed files with 156 additions and 23 deletions.
3 changes: 2 additions & 1 deletion Manifest
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
lib/easel_helpers/helpers/date_helper.rb
lib/easel_helpers/helpers/form_helper.rb
lib/easel_helpers/helpers/grid_helper.rb
lib/easel_helpers/helpers/link_helper.rb
lib/easel_helpers/helpers/structure_helper.rb
lib/easel_helpers/helpers/table_helper.rb
lib/easel_helpers/helpers.rb
lib/easel_helpers.rb
Manifest
MIT-LICENSE
rails/init.rb
Rakefile
README.textile
tasks/easel_helpers_tasks.rake
test/easel_helpers_test.rb
test/test_helper.rb
Manifest
2 changes: 1 addition & 1 deletion Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ require 'rake/testtask'
require 'rake/rdoctask'
require 'echoe'

Echoe.new("easel_helpers", "0.1.0") do |p|
Echoe.new("easel_helpers", "0.1.1") do |p|
p.description = "Fusionary Rails View Helpers"
p.url = "http://github.com/fusionary/easel_helpers"
p.author = "Joshua Clayton"
Expand Down
20 changes: 10 additions & 10 deletions easel_helpers.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@

Gem::Specification.new do |s|
s.name = %q{easel_helpers}
s.version = "0.1.0"
s.version = "0.1.1"

s.required_rubygems_version = Gem::Requirement.new(">= 1.2") if s.respond_to? :required_rubygems_version=
s.authors = ["Joshua Clayton"]
s.date = %q{2009-04-28}
s.date = %q{2009-05-01}
s.description = %q{Fusionary Rails View Helpers}
s.email = %q{joshua.clayton@gmail.com}
s.extra_rdoc_files = ["lib/easel_helpers/helpers/date_helper.rb", "lib/easel_helpers/helpers/form_helper.rb", "lib/easel_helpers/helpers/link_helper.rb", "lib/easel_helpers/helpers/structure_helper.rb", "lib/easel_helpers/helpers/table_helper.rb", "lib/easel_helpers/helpers.rb", "lib/easel_helpers.rb", "README.textile", "tasks/easel_helpers_tasks.rake"]
s.files = ["lib/easel_helpers/helpers/date_helper.rb", "lib/easel_helpers/helpers/form_helper.rb", "lib/easel_helpers/helpers/link_helper.rb", "lib/easel_helpers/helpers/structure_helper.rb", "lib/easel_helpers/helpers/table_helper.rb", "lib/easel_helpers/helpers.rb", "lib/easel_helpers.rb", "MIT-LICENSE", "rails/init.rb", "Rakefile", "README.textile", "tasks/easel_helpers_tasks.rake", "test/easel_helpers_test.rb", "test/test_helper.rb", "Manifest", "easel_helpers.gemspec"]
s.extra_rdoc_files = ["lib/easel_helpers/helpers/date_helper.rb", "lib/easel_helpers/helpers/form_helper.rb", "lib/easel_helpers/helpers/grid_helper.rb", "lib/easel_helpers/helpers/link_helper.rb", "lib/easel_helpers/helpers/structure_helper.rb", "lib/easel_helpers/helpers/table_helper.rb", "lib/easel_helpers/helpers.rb", "lib/easel_helpers.rb", "README.textile", "tasks/easel_helpers_tasks.rake"]
s.files = ["lib/easel_helpers/helpers/date_helper.rb", "lib/easel_helpers/helpers/form_helper.rb", "lib/easel_helpers/helpers/grid_helper.rb", "lib/easel_helpers/helpers/link_helper.rb", "lib/easel_helpers/helpers/structure_helper.rb", "lib/easel_helpers/helpers/table_helper.rb", "lib/easel_helpers/helpers.rb", "lib/easel_helpers.rb", "Manifest", "MIT-LICENSE", "rails/init.rb", "Rakefile", "README.textile", "tasks/easel_helpers_tasks.rake", "test/easel_helpers_test.rb", "test/test_helper.rb", "easel_helpers.gemspec"]
s.has_rdoc = true
s.homepage = %q{http://github.com/fusionary/easel_helpers}
s.rdoc_options = ["--line-numbers", "--inline-source", "--title", "Easel_helpers", "--main", "README.textile"]
Expand All @@ -25,14 +25,14 @@ Gem::Specification.new do |s|
s.specification_version = 3

if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
s.add_development_dependency(%q<actionview>, [">= 0", ">= 2.1.0"])
s.add_development_dependency(%q<activesupport>, [">= 0", ">= 2.1.0"])
s.add_development_dependency(%q<actionview>, [">= 0", "= 2.1.0"])
s.add_development_dependency(%q<activesupport>, [">= 0", "= 2.1.0"])
else
s.add_dependency(%q<actionview>, [">= 0", ">= 2.1.0"])
s.add_dependency(%q<activesupport>, [">= 0", ">= 2.1.0"])
s.add_dependency(%q<actionview>, [">= 0", "= 2.1.0"])
s.add_dependency(%q<activesupport>, [">= 0", "= 2.1.0"])
end
else
s.add_dependency(%q<actionview>, [">= 0", ">= 2.1.0"])
s.add_dependency(%q<activesupport>, [">= 0", ">= 2.1.0"])
s.add_dependency(%q<actionview>, [">= 0", "= 2.1.0"])
s.add_dependency(%q<activesupport>, [">= 0", "= 2.1.0"])
end
end
37 changes: 33 additions & 4 deletions lib/easel_helpers/helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
require "easel_helpers/helpers/link_helper"
require "easel_helpers/helpers/structure_helper"
require "easel_helpers/helpers/table_helper"
require "easel_helpers/helpers/grid_helper"

module EaselHelpers
module Helpers
Expand All @@ -11,13 +12,41 @@ module Helpers
include LinkHelper
include StructureHelper
include TableHelper
include GridHelper

protected

def clean_css_classes(string_or_array)
css_classes = [string_or_array].flatten.join(" ")
css_classes.split(/ /).map(&:strip).uniq.join(" ").strip
def clean_css_classes(string_or_array, replace = {})
returning [] do |css_classes|
css_classes << [string_or_array].flatten.join(" ")
css_classes = css_classes.split(/ /).map(&:strip).uniq.join(" ").strip

replace.keys.each do |k|
if css_classes.include? k
css_classes.delete(k)
css_classes << replace[k]
end
end
end
end

private

BLOCK_CALLED_FROM_ERB = 'defined? __in_erb_template'

if RUBY_VERSION < '1.9.0'
# Check whether we're called from an erb template.
# We'd return a string in any other case, but erb <%= ... %>
# can't take an <% end %> later on, so we have to use <% ... %>
# and implicitly concat.
def block_is_within_action_view?(block)
block && eval(BLOCK_CALLED_FROM_ERB, block)
end
else
def block_is_within_action_view?(block)
block && eval(BLOCK_CALLED_FROM_ERB, block.binding)
end
end

end

end
4 changes: 3 additions & 1 deletion lib/easel_helpers/helpers/date_helper.rb
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
module EaselHelpers
module Helpers
module DateHelper

def datetime(dt = nil, default_text = "", format_string = :long)
dt ? dt.to_time.to_s(format_string) : default_text
end

def date(dt = nil, default_text = "", format_string = :long)
dt ? dt.to_date.to_s(format_string) : default_text
end

end
end
end
102 changes: 102 additions & 0 deletions lib/easel_helpers/helpers/grid_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
module EaselHelpers
module Helpers
module GridHelper
MULTIPLES = {
:one_twentyfourth => (1/24.to_f),
:one_twelfth => (1/12.to_f),
:one_eigth => (1/8.to_f),
:one_sixth => (1/6.to_f),
:five_twentyfourths => (5/24.to_f),
:one_fourth => (1/4.to_f),
:seven_twentyfourths => (7/24.to_f),
:one_third => (1/3.to_f),
:three_eigths => (3/8.to_f),
:five_twelfths => (5/12.to_f),
:eleven_twentyfourths => (11/24.to_f),
:one_half => (1/2.to_f),
:half => (1/2.to_f),
:thirteen_twentyfourths => (13/24.to_f),
:seven_twelfths => (7/12.to_f),
:five_eigths => (5/8.to_f),
:two_thirds => (2/3.to_f),
:seventeen_twentyfourths => (17/24.to_f),
:three_fourths => (3/4.to_f),
:nineteen_twentyfourths => (19/24.to_f),
:five_sixths => (5/6.to_f),
:seven_eigths => (7/8.to_f),
:eleven_twelfths => (11/12.to_f),
:twentythree_twentyfourths => (23/24.to_f),
:full => (1.to_f)
}.freeze
MULTIPLE_FRACTIONS = MULTIPLES.keys.map {|k| k.to_s }.freeze

def column(size=nil, *args, &block)
@_easel_column_count ||= application_width
col(size, *args, &block)
end

def method_missing_with_easel_widths(call, *args)
# filter out any initial helper calls
found = false
MULTIPLE_FRACTIONS.each do |w|
found = true and break if call.to_s.include?(w)
end
method_missing_without_easel_widths(call, *args) and return unless found

# one of the widths is somewhere in the helper call; let's find it
call.to_s =~ /^((append|prepend|col)_)?(.+)$/
class_name = $2 || "col"
class_width = $3

if MULTIPLES.keys.include?(class_width.to_sym)
width = @_easel_column_count || application_width
"#{class_name}-#{(width*MULTIPLES[class_width.to_sym]).to_i}"
else
method_missing_without_easel_widths(call, *args)
end
end

alias_method_chain :method_missing, :easel_widths

private

def application_width; 24; end

def increase_depth(size)
@_easel_current_width = @_easel_column_count.to_s
@_easel_column_count = size.to_s =~ /^\d+$/ ? size.to_s.to_i : (@_easel_column_count*MULTIPLES[size]).to_i
end

def decrease_depth(size)
@_easel_column_count = size.is_a?(Integer) ? @_easel_current_width.to_i : (@_easel_column_count/MULTIPLES[size]).to_i
@_easel_current_width = nil
end

def col(size=nil, *args, &block)
size = :full if size.blank?

increase_depth(size)
output_tag = generate_output_tag(size, args, &block)

if block_given? && block_is_within_action_view?(block)
concat(output_tag)
decrease_depth(size)
else
decrease_depth(size)
output_tag
end
end

def generate_output_tag(size, *args, &block)
options = args.extract_options!

css_classes = [] << options.delete(:class) << args
css_classes << "col-#{@_easel_column_count}" unless options.delete(:suppress_col)
css_classes << "col-last" if size.to_sym == :full && @_easel_column_count != application_width

content_tag(:div, capture(&block), {:class => clean_css_classes(css_classes, {"last" => "col-last"})}.merge(options))
end

end
end
end
5 changes: 2 additions & 3 deletions lib/easel_helpers/helpers/link_helper.rb
Original file line number Diff line number Diff line change
@@ -1,21 +1,20 @@
module EaselHelpers
module Helpers
module LinkHelper

def link_button(*args, &block)
doc = Hpricot(link_to(*args, &block))
doc.at("a").inner_html = "<span>#{doc.at("a").inner_html}</span>"
(doc/:a).add_class("btn")
doc.to_html
end

def link_to_email(email_address, *args)
options = args.extract_options!
link = args.first.is_a?(String) ? h(args.first) : email_address
return link if email_address.blank?
link_to link, "mailto:#{email_address}", options
end

end
end
end
4 changes: 2 additions & 2 deletions lib/easel_helpers/helpers/structure_helper.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
module EaselHelpers
module Helpers
module StructureHelper

def blockquote(*args, &block)
options = args.extract_options!
author = options.delete(:author)
Expand All @@ -18,7 +18,7 @@ def blockquote(*args, &block)

concat(html)
end

end
end
end
2 changes: 1 addition & 1 deletion lib/easel_helpers/helpers/table_helper.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
module EaselHelpers
module Helpers
module TableHelper

def zebra_row(options = {}, &block)
cycle_list = options.delete(:cycle_list) || [nil, "alt"]
css_classes = [cycle(*cycle_list)] << options.delete(:class)
Expand Down

0 comments on commit f08491f

Please sign in to comment.