easel_helpers 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2009 Josh Clayton
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.
@@ -0,0 +1,31 @@
1
+ easel_helpers.gemspec
2
+ lib/easel_helpers/helpers/date_helper.rb
3
+ lib/easel_helpers/helpers/form_helper.rb
4
+ lib/easel_helpers/helpers/grid_helper.rb
5
+ lib/easel_helpers/helpers/jquery_helper.rb
6
+ lib/easel_helpers/helpers/link_helper.rb
7
+ lib/easel_helpers/helpers/message_helper.rb
8
+ lib/easel_helpers/helpers/navigation_helper.rb
9
+ lib/easel_helpers/helpers/rjs_helper.rb
10
+ lib/easel_helpers/helpers/structure_helper.rb
11
+ lib/easel_helpers/helpers/table_helper.rb
12
+ lib/easel_helpers/helpers.rb
13
+ lib/easel_helpers/rails_partial_caching.rb
14
+ lib/easel_helpers.rb
15
+ Manifest
16
+ MIT-LICENSE
17
+ rails/init.rb
18
+ Rakefile
19
+ README.textile
20
+ tasks/easel_helpers_tasks.rake
21
+ test/date_helper_test.rb
22
+ test/form_helper_test.rb
23
+ test/grid_helper_test.rb
24
+ test/jquery_helper_test.rb
25
+ test/link_helper_test.rb
26
+ test/message_helper_test.rb
27
+ test/navigation_helper_test.rb
28
+ test/rjs_helper_test.rb
29
+ test/structure_helper_test.rb
30
+ test/table_helper_test.rb
31
+ test/test_helper.rb
@@ -0,0 +1,3 @@
1
+ h1. Easel Helpers
2
+
3
+ Copyright (c) 2009 Josh Clayton, released under the MIT license
@@ -0,0 +1,18 @@
1
+ require 'rake'
2
+ require 'rake/testtask'
3
+ require 'rcov/rcovtask'
4
+ require 'echoe'
5
+
6
+ Echoe.new("easel_helpers", "0.3.0") do |p|
7
+ p.description = "Fusionary Rails View Helpers"
8
+ p.url = "http://github.com/fusionary/easel_helpers"
9
+ p.author = "Joshua Clayton"
10
+ p.email = "joshua.clayton@gmail.com"
11
+ p.ignore_pattern = ["tmp/*"]
12
+ p.development_dependencies = ["actionview >= 2.1.0", "activesupport >= 2.1.0", "hpricot >= 0.8.1"]
13
+ end
14
+
15
+ Rcov::RcovTask.new("rcov:current") do |t|
16
+ t.libs << "test"
17
+ t.test_files = FileList['test/*_test.rb']
18
+ end
@@ -0,0 +1,41 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ Gem::Specification.new do |s|
4
+ s.name = %q{easel_helpers}
5
+ s.version = "0.3.0"
6
+
7
+ s.required_rubygems_version = Gem::Requirement.new(">= 1.2") if s.respond_to? :required_rubygems_version=
8
+ s.authors = ["Joshua Clayton"]
9
+ s.date = %q{2009-08-21}
10
+ s.description = %q{Fusionary Rails View Helpers}
11
+ s.email = %q{joshua.clayton@gmail.com}
12
+ 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/jquery_helper.rb", "lib/easel_helpers/helpers/link_helper.rb", "lib/easel_helpers/helpers/message_helper.rb", "lib/easel_helpers/helpers/navigation_helper.rb", "lib/easel_helpers/helpers/rjs_helper.rb", "lib/easel_helpers/helpers/structure_helper.rb", "lib/easel_helpers/helpers/table_helper.rb", "lib/easel_helpers/helpers.rb", "lib/easel_helpers/rails_partial_caching.rb", "lib/easel_helpers.rb", "README.textile", "tasks/easel_helpers_tasks.rake"]
13
+ s.files = ["easel_helpers.gemspec", "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/jquery_helper.rb", "lib/easel_helpers/helpers/link_helper.rb", "lib/easel_helpers/helpers/message_helper.rb", "lib/easel_helpers/helpers/navigation_helper.rb", "lib/easel_helpers/helpers/rjs_helper.rb", "lib/easel_helpers/helpers/structure_helper.rb", "lib/easel_helpers/helpers/table_helper.rb", "lib/easel_helpers/helpers.rb", "lib/easel_helpers/rails_partial_caching.rb", "lib/easel_helpers.rb", "Manifest", "MIT-LICENSE", "rails/init.rb", "Rakefile", "README.textile", "tasks/easel_helpers_tasks.rake", "test/date_helper_test.rb", "test/form_helper_test.rb", "test/grid_helper_test.rb", "test/jquery_helper_test.rb", "test/link_helper_test.rb", "test/message_helper_test.rb", "test/navigation_helper_test.rb", "test/rjs_helper_test.rb", "test/structure_helper_test.rb", "test/table_helper_test.rb", "test/test_helper.rb"]
14
+ s.has_rdoc = true
15
+ s.homepage = %q{http://github.com/fusionary/easel_helpers}
16
+ s.rdoc_options = ["--line-numbers", "--inline-source", "--title", "Easel_helpers", "--main", "README.textile"]
17
+ s.require_paths = ["lib"]
18
+ s.rubyforge_project = %q{easel_helpers}
19
+ s.rubygems_version = %q{1.3.2}
20
+ s.summary = %q{Fusionary Rails View Helpers}
21
+ s.test_files = ["test/date_helper_test.rb", "test/form_helper_test.rb", "test/grid_helper_test.rb", "test/jquery_helper_test.rb", "test/link_helper_test.rb", "test/message_helper_test.rb", "test/navigation_helper_test.rb", "test/rjs_helper_test.rb", "test/structure_helper_test.rb", "test/table_helper_test.rb", "test/test_helper.rb"]
22
+
23
+ if s.respond_to? :specification_version then
24
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
25
+ s.specification_version = 3
26
+
27
+ if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
28
+ s.add_development_dependency(%q<actionview>, [">= 0", "= 2.1.0"])
29
+ s.add_development_dependency(%q<activesupport>, [">= 0", "= 2.1.0"])
30
+ s.add_development_dependency(%q<hpricot>, [">= 0", "= 0.8.1"])
31
+ else
32
+ s.add_dependency(%q<actionview>, [">= 0", "= 2.1.0"])
33
+ s.add_dependency(%q<activesupport>, [">= 0", "= 2.1.0"])
34
+ s.add_dependency(%q<hpricot>, [">= 0", "= 0.8.1"])
35
+ end
36
+ else
37
+ s.add_dependency(%q<actionview>, [">= 0", "= 2.1.0"])
38
+ s.add_dependency(%q<activesupport>, [">= 0", "= 2.1.0"])
39
+ s.add_dependency(%q<hpricot>, [">= 0", "= 0.8.1"])
40
+ end
41
+ end
@@ -0,0 +1,3 @@
1
+ require "hpricot"
2
+ require "easel_helpers/helpers"
3
+ require "easel_helpers/rails_partial_caching"
@@ -0,0 +1,81 @@
1
+ require "easel_helpers/helpers/date_helper"
2
+ require "easel_helpers/helpers/form_helper"
3
+ require "easel_helpers/helpers/link_helper"
4
+ require "easel_helpers/helpers/structure_helper"
5
+ require "easel_helpers/helpers/table_helper"
6
+ require "easel_helpers/helpers/grid_helper"
7
+ require "easel_helpers/helpers/message_helper"
8
+ require "easel_helpers/helpers/rjs_helper"
9
+ require "easel_helpers/helpers/jquery_helper"
10
+ require "easel_helpers/helpers/navigation_helper"
11
+
12
+ module EaselHelpers
13
+ module Helpers
14
+ include DateHelper
15
+ include FormHelper
16
+ include LinkHelper
17
+ include StructureHelper
18
+ include TableHelper
19
+ include GridHelper
20
+ include MessageHelper
21
+ include RjsHelper
22
+ include JqueryHelper
23
+ include NavigationHelper
24
+
25
+ protected
26
+
27
+ def other_than_grid?(classes)
28
+ (standardize_css_classes(classes).map {|s| s.to_s } -
29
+ EaselHelpers::Helpers::GridHelper::MULTIPLE_FRACTIONS).any?
30
+ end
31
+
32
+ def clean_css_classes(string_or_array, replace = {})
33
+ css_classes = [] + standardize_css_classes(string_or_array)
34
+
35
+ if replace.any?
36
+ replace.keys.each do |k|
37
+ if css_classes.include? k
38
+ css_classes.delete(k)
39
+ css_classes << replace[k]
40
+ end
41
+ end
42
+ end
43
+
44
+ fractions = css_classes & EaselHelpers::Helpers::GridHelper::MULTIPLE_FRACTIONS
45
+ if css_classes.any? && fractions.any?
46
+ fractions.each do |fraction|
47
+ css_classes.delete(fraction)
48
+ css_classes << self.send(fraction)
49
+ if fraction == "full" && @_easel_column_count != application_width
50
+ css_classes << last_column
51
+ end
52
+ end
53
+ end
54
+
55
+ css_classes.map {|s| s.strip }.reject {|s| s.blank? }.uniq.join(" ").strip
56
+ end
57
+
58
+ private
59
+
60
+ def standardize_css_classes(string_or_array)
61
+ [string_or_array].flatten.join(" ").split(/ /)
62
+ end
63
+
64
+ BLOCK_CALLED_FROM_ERB = 'defined? __in_erb_template'
65
+
66
+ if RUBY_VERSION < '1.9.0'
67
+ # Check whether we're called from an erb template.
68
+ # We'd return a string in any other case, but erb <%= ... %>
69
+ # can't take an <% end %> later on, so we have to use <% ... %>
70
+ # and implicitly concat.
71
+ def block_is_within_action_view?(block)
72
+ block && eval(BLOCK_CALLED_FROM_ERB, block)
73
+ end
74
+ else
75
+ def block_is_within_action_view?(block)
76
+ block && eval(BLOCK_CALLED_FROM_ERB, block.binding)
77
+ end
78
+ end
79
+
80
+ end
81
+ end
@@ -0,0 +1,15 @@
1
+ module EaselHelpers
2
+ module Helpers
3
+ module DateHelper
4
+
5
+ def datetime(dt = nil, default_text = "", format_string = :long)
6
+ dt ? dt.to_time.to_s(format_string) : default_text
7
+ end
8
+
9
+ def date(dt = nil, default_text = "", format_string = :long)
10
+ dt ? dt.to_date.to_s(format_string) : default_text
11
+ end
12
+
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,68 @@
1
+ module EaselHelpers
2
+ module Helpers
3
+ module FormHelper
4
+
5
+ def submit_button(value, *args)
6
+ options = args.extract_options!
7
+ css_classes = ["btn"] << options.delete(:class) << args
8
+ css_classes = clean_css_classes(css_classes, {"last" => last_column})
9
+
10
+ content_tag :button,
11
+ "<span>#{value}</span>",
12
+ { :value => value,
13
+ :type => "submit",
14
+ :class => css_classes
15
+ }.merge(options)
16
+ end
17
+
18
+ def set(*args, &block)
19
+ options = args.extract_options!
20
+ css_classes = [] << options.delete(:class) << args
21
+
22
+ if !other_than_grid?(args.map(&:to_s) - ["error", "last", last_column.to_s])
23
+ css_classes << "text"
24
+ end
25
+
26
+ if standardize_css_classes(css_classes).include?("textarea")
27
+ css_classes << "text"
28
+ end
29
+
30
+ css_classes = clean_css_classes(css_classes, {"last" => last_column})
31
+
32
+ html = clean_column(css_classes) do
33
+ content_tag :div,
34
+ capture(&block),
35
+ options.merge(:class => css_classes)
36
+ end
37
+
38
+ concat(html)
39
+ end
40
+
41
+ def fieldset(*args, &block)
42
+ options = args.extract_options!
43
+ css_classes = [] << options.delete(:class) << args
44
+ title = args.shift if args.first.is_a?(String)
45
+ legend = if title.blank?
46
+ ""
47
+ else
48
+ legend_opts = options.delete(:legend) || {}
49
+ legend_classes = clean_css_classes([legend_opts.delete(:class)] << "legend")
50
+ content_tag :h3,
51
+ title,
52
+ {:class => legend_classes}.merge(legend_opts)
53
+ end
54
+
55
+ css_classes = clean_css_classes(css_classes, {"last" => last_column})
56
+
57
+ html = clean_column(css_classes) do
58
+ content_tag :fieldset,
59
+ legend + capture(&block),
60
+ options.merge(:class => css_classes)
61
+ end
62
+
63
+ concat(html)
64
+ end
65
+
66
+ end
67
+ end
68
+ end
@@ -0,0 +1,178 @@
1
+ module EaselHelpers
2
+ module Helpers
3
+ module GridHelper
4
+ MULTIPLES = {
5
+ :one_twentyfourth => (1/24.to_f),
6
+ :one_twelfth => (1/12.to_f),
7
+ :one_eigth => (1/8.to_f),
8
+ :one_sixth => (1/6.to_f),
9
+ :five_twentyfourths => (5/24.to_f),
10
+ :one_fourth => (1/4.to_f),
11
+ :seven_twentyfourths => (7/24.to_f),
12
+ :one_third => (1/3.to_f),
13
+ :three_eigths => (3/8.to_f),
14
+ :five_twelfths => (5/12.to_f),
15
+ :eleven_twentyfourths => (11/24.to_f),
16
+ :one_half => (1/2.to_f),
17
+ :half => (1/2.to_f),
18
+ :thirteen_twentyfourths => (13/24.to_f),
19
+ :seven_twelfths => (7/12.to_f),
20
+ :five_eigths => (5/8.to_f),
21
+ :two_thirds => (2/3.to_f),
22
+ :seventeen_twentyfourths => (17/24.to_f),
23
+ :three_fourths => (3/4.to_f),
24
+ :nineteen_twentyfourths => (19/24.to_f),
25
+ :five_sixths => (5/6.to_f),
26
+ :seven_eigths => (7/8.to_f),
27
+ :eleven_twelfths => (11/12.to_f),
28
+ :twentythree_twentyfourths => (23/24.to_f),
29
+ :full => (1.to_f)
30
+ }.freeze
31
+ MULTIPLE_FRACTIONS = MULTIPLES.keys.map {|key| key.to_s }.freeze
32
+
33
+ def self.easel_grid!
34
+ @@last_column = "col-last"
35
+ @@column_prefix = "col"
36
+ end
37
+
38
+ def self.blueprint_grid!
39
+ @@last_column = "last"
40
+ @@column_prefix = "span"
41
+ end
42
+
43
+ easel_grid!
44
+
45
+ def last_column
46
+ @@last_column
47
+ end
48
+
49
+ def column(*args, &block)
50
+ @_easel_column_count ||= application_width
51
+ col(*args, &block)
52
+ end
53
+
54
+ def container(size=nil, *args, &block)
55
+ opts = args.extract_options!
56
+ opts.merge!(:suppress_col => true) if size.nil?
57
+ args = args.insert(0, :container)
58
+ column(size, *([args, opts].flatten), &block)
59
+ end
60
+
61
+ def clean_column(classes, &block)
62
+ size = classes.scan(/#{column_prefix}-(\d+)/).flatten.last
63
+
64
+ if size.nil?
65
+ html = capture(&block)
66
+ if block_given? && block_is_within_action_view?(block)
67
+ concat(html)
68
+ else
69
+ html
70
+ end
71
+ else
72
+ size = size.to_i
73
+ increase_depth(size)
74
+ html = capture(&block)
75
+
76
+ if block_given? && block_is_within_action_view?(block)
77
+ concat(html)
78
+ decrease_depth(size)
79
+ else
80
+ decrease_depth(size)
81
+ html
82
+ end
83
+ end
84
+ end
85
+
86
+ def method_missing_with_easel_widths(call, *args)
87
+ # filter out any initial helper calls
88
+ found = false
89
+ MULTIPLE_FRACTIONS.each do |fraction|
90
+ found = true and break if call.to_s.include?(fraction)
91
+ end
92
+
93
+ method_missing_without_easel_widths(call, *args) and return unless found
94
+
95
+ # one of the widths is somewhere in the helper call; let's find it
96
+ call.to_s =~ /^((append|prepend|#{column_prefix})_)?(.+)$/
97
+ class_name = $2 || column_prefix
98
+ class_width = $3
99
+
100
+ if MULTIPLES.keys.include?(class_width.to_sym)
101
+ width = @_easel_column_count || application_width
102
+ "#{class_name}-#{(width*MULTIPLES[class_width.to_sym]).to_i}"
103
+ else
104
+ method_missing_without_easel_widths(call, *args)
105
+ end
106
+ end
107
+
108
+ alias_method_chain :method_missing, :easel_widths
109
+
110
+ private
111
+
112
+ def application_width; 24; end
113
+
114
+ def increase_depth(size)
115
+ @_easel_current_width ||= [application_width.to_s]
116
+
117
+ if @_easel_column_count.present?
118
+ @_easel_current_width.push(@_easel_column_count.to_s)
119
+ end
120
+
121
+ @_easel_column_count = if size.to_s =~ /^\d+$/
122
+ size.to_s.to_i
123
+ else
124
+ (@_easel_column_count*MULTIPLES[size]).to_i
125
+ end
126
+ end
127
+
128
+ def decrease_depth(size)
129
+ @_easel_column_count = if size.is_a?(Integer)
130
+ @_easel_current_width.last.to_i
131
+ else
132
+ (@_easel_column_count/MULTIPLES[size]).to_i
133
+ end
134
+
135
+ @_easel_current_width.pop
136
+ end
137
+
138
+ def col(*args, &block)
139
+ fractional_width = MULTIPLE_FRACTIONS.include?(args.first.to_s)
140
+ explicit_column_width = args.first.is_a?(Integer)
141
+ size = (fractional_width || explicit_column_width) ? args.shift : :full
142
+
143
+ increase_depth(size)
144
+ output_tag = generate_output_tag(size, *args, &block)
145
+
146
+ if block_given? && block_is_within_action_view?(block)
147
+ concat(output_tag)
148
+ decrease_depth(size)
149
+ else
150
+ decrease_depth(size)
151
+ output_tag
152
+ end
153
+ end
154
+
155
+ def generate_output_tag(size, *args, &block)
156
+ options = args.extract_options!
157
+
158
+ css_classes = [] << options.delete(:class) << args
159
+ unless options.delete(:suppress_col)
160
+ css_classes << "#{column_prefix}-#{@_easel_column_count}"
161
+ end
162
+
163
+ if (size.to_sym rescue nil) == :full && @_easel_column_count != application_width
164
+ css_classes << last_column
165
+ end
166
+
167
+ css_classes = clean_css_classes(css_classes, {"last" => last_column})
168
+ content_tag(options.delete(:tag) || :div,
169
+ capture(&block),
170
+ {:class => css_classes}.merge(options))
171
+ end
172
+
173
+ def column_prefix
174
+ @@column_prefix
175
+ end
176
+ end
177
+ end
178
+ end