kelredd-useful 0.1.9 → 0.1.10
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/useful/sinatra_helpers/environment_tests.rb +7 -5
- data/lib/useful/sinatra_helpers/erb.rb +3 -3
- data/lib/useful/sinatra_helpers/erb/error_pages.rb +5 -3
- data/lib/useful/sinatra_helpers/erb/partials.rb +3 -3
- data/lib/useful/sinatra_helpers/tags.rb +2 -92
- data/lib/useful/sinatra_helpers/tags/forms.rb +110 -0
- data/lib/useful/sinatra_helpers/tags/globals.rb +45 -0
- data/lib/useful/sinatra_helpers/tags/helpers.rb +33 -0
- data/lib/useful/sinatra_helpers/tags/links.rb +67 -0
- data/lib/useful/version.rb +1 -1
- metadata +7 -2
@@ -1,17 +1,19 @@
|
|
1
1
|
require 'sinatra/base'
|
2
2
|
|
3
|
-
module
|
3
|
+
module Useful
|
4
4
|
module SinatraHelpers
|
5
5
|
module EnvironmentTests
|
6
|
+
|
6
7
|
def production?
|
7
8
|
Sinatra::Application.environment.to_s == 'production'
|
8
9
|
end
|
9
10
|
def development?
|
10
11
|
Sinatra::Application.environment.to_s == 'development'
|
11
12
|
end
|
13
|
+
|
12
14
|
end
|
13
|
-
end
|
14
|
-
|
15
|
-
Sinatra::Application.helpers Sinatra::SinatraHelpers::EnvironmentTests
|
16
|
-
Sinatra::Application.register Sinatra::SinatraHelpers::EnvironmentTests
|
15
|
+
end
|
17
16
|
end
|
17
|
+
|
18
|
+
Sinatra::Application.helpers Useful::SinatraHelpers::EnvironmentTests
|
19
|
+
Sinatra::Application.register Useful::SinatraHelpers::EnvironmentTests
|
@@ -1,3 +1,3 @@
|
|
1
|
-
|
2
|
-
require
|
3
|
-
|
1
|
+
Dir[File.join(File.dirname(__FILE__), "erb" ,"*.rb")].each do |file|
|
2
|
+
require file
|
3
|
+
end
|
@@ -1,9 +1,10 @@
|
|
1
1
|
require 'sinatra/base'
|
2
2
|
|
3
|
-
module
|
3
|
+
module Useful
|
4
4
|
module SinatraHelpers
|
5
5
|
module Erb
|
6
6
|
module ErrorPages
|
7
|
+
|
7
8
|
def self.registered(app)
|
8
9
|
app.configure :production do
|
9
10
|
app.not_found do
|
@@ -15,9 +16,10 @@ module Sinatra
|
|
15
16
|
end
|
16
17
|
end
|
17
18
|
end
|
19
|
+
|
18
20
|
end
|
19
21
|
end
|
20
22
|
end
|
21
|
-
|
22
|
-
Sinatra::Application.register Sinatra::SinatraHelpers::Erb::ErrorPages
|
23
23
|
end
|
24
|
+
|
25
|
+
Sinatra::Application.register Useful::SinatraHelpers::Erb::ErrorPages
|
@@ -1,6 +1,6 @@
|
|
1
1
|
require 'sinatra/base'
|
2
2
|
|
3
|
-
module
|
3
|
+
module Useful
|
4
4
|
module SinatraHelpers
|
5
5
|
module Erb
|
6
6
|
module Partials
|
@@ -36,6 +36,6 @@ module Sinatra
|
|
36
36
|
end
|
37
37
|
end
|
38
38
|
end
|
39
|
-
|
40
|
-
Sinatra::Application.helpers Sinatra::SinatraHelpers::Erb::Partials
|
41
39
|
end
|
40
|
+
|
41
|
+
Sinatra::Application.helpers Useful::SinatraHelpers::Erb::Partials
|
@@ -1,93 +1,3 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
module Sinatra
|
4
|
-
module SinatraHelpers
|
5
|
-
module Tags
|
6
|
-
|
7
|
-
# helper to emulate action view's 'link_to'
|
8
|
-
# => inspired from vanntastic-sinatra-gen gem, http://github.com/vanntastic/sinatra-gen/tree/master
|
9
|
-
# EX : link_to "google", "http://google.com"
|
10
|
-
# => <a href="http://google.com">google</a>
|
11
|
-
def link_to(content,href,options={})
|
12
|
-
options.update :href => href
|
13
|
-
tag(:a, options) { content }
|
14
|
-
end
|
15
|
-
|
16
|
-
def mail_link_to(email)
|
17
|
-
link_to email, "mailto: #{email}"
|
18
|
-
end
|
19
|
-
|
20
|
-
# helper to emulate 'image_tag'
|
21
|
-
# => inspired from vanntastic-sinatra-gen gem, http://github.com/vanntastic/sinatra-gen/tree/master
|
22
|
-
# helper for image_tags
|
23
|
-
# EX : image_tag 'logo.jpg'
|
24
|
-
# => <img src="images/logo.jpg" />
|
25
|
-
def image_tag(src,options={})
|
26
|
-
options[:src] = ['/'].include?(src[0..0]) ? src : "/images/#{src}"
|
27
|
-
tag(:img, options)
|
28
|
-
end
|
29
|
-
|
30
|
-
def clear_tag(options={})
|
31
|
-
options[:tag] ||= :div
|
32
|
-
options[:style] ||= ''
|
33
|
-
options[:style] = "clear: both;#{options[:style]}"
|
34
|
-
tag(options.delete(:tag), options) { '' }
|
35
|
-
end
|
36
|
-
|
37
|
-
include Rack::Utils
|
38
|
-
alias_method :h, :escape_html
|
39
|
-
|
40
|
-
# helper to emulate 'stylesheet_link_tag'
|
41
|
-
# => inspired from vanntastic-sinatra-gen gem, http://github.com/vanntastic/sinatra-gen/tree/master
|
42
|
-
# EX : stylesheet_link_tag 'default'
|
43
|
-
# => <link rel="stylesheet" href="/stylesheets/default.css" type="text/css" media="all" title="no title" charset="utf-8">
|
44
|
-
def stylesheet_link_tag(srcs,options={})
|
45
|
-
options[:media] ||= "screen"
|
46
|
-
options[:type] ||= "text/css"
|
47
|
-
options[:rel] ||= "stylesheet"
|
48
|
-
srcs.to_a.collect do |src|
|
49
|
-
options[:href] = "/stylesheets/#{src}.css#{"?#{Time.now.to_i}" if Sinatra::Application.environment.to_s == 'development'}"
|
50
|
-
tag(:link, options)
|
51
|
-
end.join("\n")
|
52
|
-
end
|
53
|
-
|
54
|
-
# helper to emulate 'javascript_include_tag'
|
55
|
-
# => inspired from vanntastic-sinatra-gen gem, http://github.com/vanntastic/sinatra-gen/tree/master
|
56
|
-
# EX : javascript_include_tag 'app'
|
57
|
-
# => <script src="/js/app.js" type="text/javascript" />
|
58
|
-
# EX : javascript_include_tag ['app', 'jquery']
|
59
|
-
# => <script src="/js/app.js" type="text/javascript" />
|
60
|
-
# => <script src="/js/jquery.js" type="text/javascript" />
|
61
|
-
def javascript_include_tag(srcs,options={})
|
62
|
-
options[:type] ||= "text/javascript"
|
63
|
-
srcs.to_a.collect do |src|
|
64
|
-
options[:src] = "/javascripts/#{src}.js#{"?#{Time.now.to_i}" if Sinatra::Application.environment.to_s == 'development'}"
|
65
|
-
tag(:script, options) { '' }
|
66
|
-
end.join("\n")
|
67
|
-
end
|
68
|
-
|
69
|
-
# helper to emulate 'javascript_tag'
|
70
|
-
def javascript_tag(options={})
|
71
|
-
options[:type] ||= "text/javascript"
|
72
|
-
tag(:script, options) { yield }
|
73
|
-
end
|
74
|
-
|
75
|
-
# emulator for 'tag'
|
76
|
-
# => inspired from vanntastic-sinatra-gen gem, http://github.com/vanntastic/sinatra-gen/tree/master
|
77
|
-
# EX : tag :h1, "shizam", :title => "shizam"
|
78
|
-
# => <h1 title="shizam">shizam</h1>
|
79
|
-
def tag(name,options={})
|
80
|
-
"<#{name.to_s} #{hash_to_html_attrs(options)} #{block_given? ? ">#{yield}</#{name}" : "/"}>"
|
81
|
-
end
|
82
|
-
|
83
|
-
private
|
84
|
-
|
85
|
-
def hash_to_html_attrs(a_hash)
|
86
|
-
a_hash.collect{|key, val| "#{key}=\"#{val}\""}.join(' ')
|
87
|
-
end
|
88
|
-
|
89
|
-
end
|
90
|
-
end
|
91
|
-
|
92
|
-
Sinatra::Application.helpers Sinatra::SinatraHelpers::Tags
|
1
|
+
Dir[File.join(File.dirname(__FILE__), "tags" ,"*.rb")].each do |file|
|
2
|
+
require file
|
93
3
|
end
|
@@ -0,0 +1,110 @@
|
|
1
|
+
require 'sinatra/base'
|
2
|
+
require File.join(File.dirname(__FILE__), 'helpers.rb')
|
3
|
+
require File.join(File.dirname(__FILE__), 'globals.rb')
|
4
|
+
|
5
|
+
module Useful
|
6
|
+
module SinatraHelpers
|
7
|
+
module Tags
|
8
|
+
module Forms
|
9
|
+
|
10
|
+
include Useful::SinatraHelpers::Tags::Helpers
|
11
|
+
|
12
|
+
def form_tag(url, options={}, &block)
|
13
|
+
options.update :action => url
|
14
|
+
tag(:form, options, &block)
|
15
|
+
end
|
16
|
+
|
17
|
+
def field_set_tag(legend=nil, options=nil, &block)
|
18
|
+
content = "#{tag(:legend) { lengend.to_s } unless legend.nil?}#{sinatra_tag_helper_capture(&block)}"
|
19
|
+
tag(:fieldset, options) { content }
|
20
|
+
end
|
21
|
+
|
22
|
+
def label_tag(name, value=nil, options={})
|
23
|
+
value ||= name.to_s.capitalize
|
24
|
+
options.update :for => name.to_s
|
25
|
+
tag(:label, options) { value }
|
26
|
+
end
|
27
|
+
|
28
|
+
def hidden_field_tag(name, value=nil, options={})
|
29
|
+
input_tag('hidden', name, value, options)
|
30
|
+
end
|
31
|
+
|
32
|
+
def password_field_tag(name="password", value=nil, options={})
|
33
|
+
options[:disabled] = sinatra_tag_helper_disabled_option if options[:disabled]
|
34
|
+
input_tag('password', name, value, options)
|
35
|
+
end
|
36
|
+
|
37
|
+
def file_field_tag(name, options={})
|
38
|
+
options[:disabled] = sinatra_tag_helper_disabled_option if options[:disabled]
|
39
|
+
input_tag('file', name, nil, options)
|
40
|
+
end
|
41
|
+
|
42
|
+
def check_box_tag(name, label=nil, value='1', checked=false, options={})
|
43
|
+
options[:tag] ||= :div
|
44
|
+
if options.has_key?(:class)
|
45
|
+
options[:class] += ' checkbox'
|
46
|
+
else
|
47
|
+
options[:class] = 'checkbox'
|
48
|
+
end
|
49
|
+
options[:disabled] = sinatra_tag_helper_disabled_option if options[:disabled]
|
50
|
+
options[:checked] = sinatra_tag_helper_checked_option if checked
|
51
|
+
input_str = input_tag('checkbox', name, value, options)
|
52
|
+
if label.nil?
|
53
|
+
input_str
|
54
|
+
else
|
55
|
+
tag(options.delete(:tag), :class => 'checkbox') { input_str + label_tag(options[:id], label) }
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
59
|
+
def radio_button_tag(name, value, label, checked=false, options={})
|
60
|
+
options[:tag] ||= :span
|
61
|
+
if options.has_key?(:class)
|
62
|
+
options[:class] += ' radiobutton'
|
63
|
+
else
|
64
|
+
options[:class] = 'radiobutton'
|
65
|
+
end
|
66
|
+
options[:disabled] = sinatra_tag_helper_disabled_option if options[:disabled]
|
67
|
+
options[:checked] = sinatra_tag_helper_checked_option if checked
|
68
|
+
input_str = input_tag('radio', name, value, options)
|
69
|
+
if label.nil?
|
70
|
+
input_str
|
71
|
+
else
|
72
|
+
label_tag(name, input_str + tag(options.delete(:tag)) { label }, :class => options.delete(:class))
|
73
|
+
end
|
74
|
+
end
|
75
|
+
|
76
|
+
def select_tag(name, option_tags=nil, options={})
|
77
|
+
options[:disabled] = sinatra_tag_helper_disabled_option if options[:disabled]
|
78
|
+
html_name = (options[:multiple] == true && !name.to_s[(name.to_s.length-2)..-1] == "[]") ? "#{name}[]" : name
|
79
|
+
options[:multiple] = sinatra_tag_helper_multiple_option if options[:multiple] == true
|
80
|
+
input_tag('select', name, nil, options) { option_tags || '' }
|
81
|
+
end
|
82
|
+
|
83
|
+
def text_area_tag(name, content=nil, options={})
|
84
|
+
options[:disabled] = sinatra_tag_helper_disabled_option if options[:disabled]
|
85
|
+
options[:tag] = 'textarea'
|
86
|
+
input_tag(nil, name, nil, options) { content || '' }
|
87
|
+
end
|
88
|
+
|
89
|
+
def text_field_tag(name, value=nil, options={})
|
90
|
+
options[:disabled] = sinatra_tag_helper_disabled_option if options[:disabled]
|
91
|
+
input_tag('text', name, value, options)
|
92
|
+
end
|
93
|
+
|
94
|
+
def submit_tag(value="Save", options={})
|
95
|
+
options[:disabled] = sinatra_tag_helper_disabled_option if options[:disabled]
|
96
|
+
input_tag('submit', 'commit', value, options)
|
97
|
+
end
|
98
|
+
|
99
|
+
def image_submit_tag(source, options={})
|
100
|
+
options[:disabled] = sinatra_tag_helper_disabled_option if options[:disabled]
|
101
|
+
options[:src] = source
|
102
|
+
input_tag('image', nil, nil, options)
|
103
|
+
end
|
104
|
+
|
105
|
+
end
|
106
|
+
end
|
107
|
+
end
|
108
|
+
end
|
109
|
+
|
110
|
+
Sinatra::Application.helpers Useful::SinatraHelpers::Tags::Forms
|
@@ -0,0 +1,45 @@
|
|
1
|
+
require 'sinatra/base'
|
2
|
+
require File.join(File.dirname(__FILE__), 'helpers.rb')
|
3
|
+
|
4
|
+
module Useful
|
5
|
+
module SinatraHelpers
|
6
|
+
module Tags
|
7
|
+
module Globals
|
8
|
+
|
9
|
+
include Useful::SinatraHelpers::Tags::Helpers
|
10
|
+
|
11
|
+
def input_tag(type, name, value, options={}, &block)
|
12
|
+
options[:tag] ||= :input
|
13
|
+
options[:type] = type unless type.nil?
|
14
|
+
unless name.nil?
|
15
|
+
options[:name] = name
|
16
|
+
options[:id] ||= sinatra_tag_helper_safe_id(name)
|
17
|
+
end
|
18
|
+
options[:value] = value unless value.nil?
|
19
|
+
tag(options.delete(:tag), options, &block)
|
20
|
+
end
|
21
|
+
|
22
|
+
def clear_tag(options={})
|
23
|
+
options[:tag] ||= :div
|
24
|
+
options[:style] ||= ''
|
25
|
+
options[:style] = "clear: both;#{options[:style]}"
|
26
|
+
tag(options.delete(:tag), options) { '' }
|
27
|
+
end
|
28
|
+
|
29
|
+
include Rack::Utils
|
30
|
+
alias_method :h, :escape_html
|
31
|
+
|
32
|
+
# emulator for 'tag'
|
33
|
+
# EX : tag :h1, "shizam", :title => "shizam"
|
34
|
+
# => <h1 title="shizam">shizam</h1>
|
35
|
+
def tag(name,options={})
|
36
|
+
"<#{name.to_s} #{sinatra_tag_helper_hash_to_html_attrs(options)} #{block_given? ? ">#{yield}</#{name}" : "/"}>"
|
37
|
+
end
|
38
|
+
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
Sinatra::Application.helpers Useful::SinatraHelpers::Tags::Globals
|
45
|
+
|
@@ -0,0 +1,33 @@
|
|
1
|
+
module Useful
|
2
|
+
module SinatraHelpers
|
3
|
+
module Tags
|
4
|
+
module Helpers
|
5
|
+
|
6
|
+
def sinatra_tag_helper_safe_id(id)
|
7
|
+
id.gsub(/\W/,'')
|
8
|
+
end
|
9
|
+
|
10
|
+
def sinatra_tag_helper_capture(*args, &block)
|
11
|
+
block.call(*args)
|
12
|
+
end
|
13
|
+
|
14
|
+
def sinatra_tag_helper_disabled_option
|
15
|
+
'disabled'
|
16
|
+
end
|
17
|
+
|
18
|
+
def sinatra_tag_helper_checked_option
|
19
|
+
'checked'
|
20
|
+
end
|
21
|
+
|
22
|
+
def sinatra_tag_helper_multiple_option
|
23
|
+
'multiple'
|
24
|
+
end
|
25
|
+
|
26
|
+
def sinatra_tag_helper_hash_to_html_attrs(a_hash)
|
27
|
+
a_hash.collect{|key, val| "#{key}=\"#{val}\""}.join(' ')
|
28
|
+
end
|
29
|
+
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
@@ -0,0 +1,67 @@
|
|
1
|
+
require 'sinatra/base'
|
2
|
+
require File.join(File.dirname(__FILE__), 'globals.rb')
|
3
|
+
|
4
|
+
module Sinatra
|
5
|
+
module SinatraHelpers
|
6
|
+
module Tags
|
7
|
+
module Links
|
8
|
+
|
9
|
+
# helper to emulate action view's 'link_to'
|
10
|
+
# EX : link_to "google", "http://google.com"
|
11
|
+
# => <a href="http://google.com">google</a>
|
12
|
+
def link_to(content,href,options={})
|
13
|
+
options.update :href => href
|
14
|
+
tag(:a, options) { content }
|
15
|
+
end
|
16
|
+
|
17
|
+
def mail_link_to(email)
|
18
|
+
link_to email, "mailto: #{email}"
|
19
|
+
end
|
20
|
+
|
21
|
+
# helper to emulate 'image_tag'
|
22
|
+
# EX : image_tag 'logo.jpg'
|
23
|
+
# => <img src="images/logo.jpg" />
|
24
|
+
def image_tag(src,options={})
|
25
|
+
options[:src] = ['/'].include?(src[0..0]) ? src : "/images/#{src}"
|
26
|
+
tag(:img, options)
|
27
|
+
end
|
28
|
+
|
29
|
+
# helper to emulate 'stylesheet_link_tag'
|
30
|
+
# EX : stylesheet_link_tag 'default'
|
31
|
+
# => <link rel="stylesheet" href="/stylesheets/default.css" type="text/css" media="all" title="no title" charset="utf-8">
|
32
|
+
def stylesheet_link_tag(srcs,options={})
|
33
|
+
options[:media] ||= "screen"
|
34
|
+
options[:type] ||= "text/css"
|
35
|
+
options[:rel] ||= "stylesheet"
|
36
|
+
srcs.to_a.collect do |src|
|
37
|
+
options[:href] = "/stylesheets/#{src}.css#{"?#{Time.now.to_i}" if Sinatra::Application.environment.to_s == 'development'}"
|
38
|
+
tag(:link, options)
|
39
|
+
end.join("\n")
|
40
|
+
end
|
41
|
+
|
42
|
+
# helper to emulate 'javascript_include_tag'
|
43
|
+
# EX : javascript_include_tag 'app'
|
44
|
+
# => <script src="/js/app.js" type="text/javascript" />
|
45
|
+
# EX : javascript_include_tag ['app', 'jquery']
|
46
|
+
# => <script src="/js/app.js" type="text/javascript" />
|
47
|
+
# => <script src="/js/jquery.js" type="text/javascript" />
|
48
|
+
def javascript_include_tag(srcs,options={})
|
49
|
+
options[:type] ||= "text/javascript"
|
50
|
+
srcs.to_a.collect do |src|
|
51
|
+
options[:src] = "/javascripts/#{src}.js#{"?#{Time.now.to_i}" if Sinatra::Application.environment.to_s == 'development'}"
|
52
|
+
tag(:script, options) { '' }
|
53
|
+
end.join("\n")
|
54
|
+
end
|
55
|
+
|
56
|
+
# helper to emulate 'javascript_tag'
|
57
|
+
def javascript_tag(options={})
|
58
|
+
options[:type] ||= "text/javascript"
|
59
|
+
tag(:script, options) { yield }
|
60
|
+
end
|
61
|
+
|
62
|
+
end
|
63
|
+
end
|
64
|
+
end
|
65
|
+
|
66
|
+
Sinatra::Application.helpers Sinatra::SinatraHelpers::Tags::Links
|
67
|
+
end
|
data/lib/useful/version.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: kelredd-useful
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.10
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Kelredd
|
@@ -9,7 +9,7 @@ autorequire:
|
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
11
|
|
12
|
-
date: 2009-06-
|
12
|
+
date: 2009-06-24 00:00:00 -07:00
|
13
13
|
default_executable:
|
14
14
|
dependencies: []
|
15
15
|
|
@@ -63,6 +63,11 @@ files:
|
|
63
63
|
- lib/useful/sinatra_helpers/erb/error_pages.rb
|
64
64
|
- lib/useful/sinatra_helpers/erb/partials.rb
|
65
65
|
- lib/useful/sinatra_helpers/erb.rb
|
66
|
+
- lib/useful/sinatra_helpers/tags
|
67
|
+
- lib/useful/sinatra_helpers/tags/forms.rb
|
68
|
+
- lib/useful/sinatra_helpers/tags/globals.rb
|
69
|
+
- lib/useful/sinatra_helpers/tags/helpers.rb
|
70
|
+
- lib/useful/sinatra_helpers/tags/links.rb
|
66
71
|
- lib/useful/sinatra_helpers/tags.rb
|
67
72
|
- lib/useful/sinatra_helpers.rb
|
68
73
|
- lib/useful/version.rb
|