sinatra_more 0.0.7 → 0.0.8
Sign up to get free protection for your applications and to get access to all the features.
- data/README.rdoc +13 -1
- data/TODO +12 -4
- data/VERSION +1 -1
- data/lib/sinatra_more/markup_plugin/asset_tag_helpers.rb +11 -4
- data/lib/sinatra_more/markup_plugin/form_builder/abstract_form_builder.rb +16 -0
- data/lib/sinatra_more/markup_plugin/form_builder/standard_form_builder.rb +6 -0
- data/lib/sinatra_more/markup_plugin/form_helpers.rb +22 -22
- data/lib/sinatra_more/markup_plugin/format_helpers.rb +8 -3
- data/lib/sinatra_more/markup_plugin/output_helpers.rb +38 -0
- data/lib/sinatra_more/markup_plugin/tag_helpers.rb +5 -2
- data/lib/sinatra_more/markup_plugin.rb +1 -0
- data/lib/sinatra_more/warden_plugin/warden_helpers.rb +4 -4
- data/lib/sinatra_more/warden_plugin.rb +10 -2
- data/sinatra_more.gemspec +2 -1
- metadata +2 -1
data/README.rdoc
CHANGED
@@ -2,7 +2,10 @@
|
|
2
2
|
|
3
3
|
== Introduction
|
4
4
|
|
5
|
-
|
5
|
+
(Not quite ready to be used yet!)
|
6
|
+
|
7
|
+
This will be a plugin which expand sinatra's capabilities in many ways.
|
8
|
+
Note that certain template specific helpers are known to work with haml, erb, and erubis
|
6
9
|
|
7
10
|
Let me expand briefly on what I want to accomplish with this gem. I love sinatra but if I want to use it
|
8
11
|
for any non-trivial application I very quickly miss a lot of the extra tools provided by rails.
|
@@ -68,6 +71,15 @@ provided to make interacting with warden dead simple.
|
|
68
71
|
|
69
72
|
...list methods here...
|
70
73
|
|
74
|
+
== Acknowledgements
|
75
|
+
|
76
|
+
Thanks to keldredd for the sinatra-helpers code that helped me to create erb capture and concat methods!
|
77
|
+
|
78
|
+
== Contributers
|
79
|
+
|
80
|
+
* Nathan Esquenazi - Project creator and code maintainer
|
81
|
+
* Arthur Chiu - Forming the idea and various code contributions
|
82
|
+
|
71
83
|
== Note on Patches/Pull Requests
|
72
84
|
|
73
85
|
* Fork the project.
|
data/TODO
CHANGED
@@ -1,5 +1,13 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
1
|
+
= UNFINISHED
|
2
|
+
|
3
|
+
* image_tag should start in images_path (or /images)
|
4
|
+
* I have got to add tests, basically create dummy sinatra applications and use Webrat
|
4
5
|
* Become total warden solution (basically just require warden gem installed, do everything else)
|
5
|
-
*
|
6
|
+
* Make warden password strategy support a callback which explains what to do with username, password
|
7
|
+
* WardenPlugin.authenticate_callback { |username, password| User.authenticate(username, password) }
|
8
|
+
* Remove dependency on activesupport! and enumerate dependencies in rakefile
|
9
|
+
|
10
|
+
= COMPLETED
|
11
|
+
|
12
|
+
* Pull from sinatra-helpers and make erb templates work (and credit keldredd)
|
13
|
+
- http://github.com/kelredd/sinatra-helpers/tree/master/lib/sinatra_helpers/erb/
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.0.
|
1
|
+
0.0.8
|
@@ -1,6 +1,7 @@
|
|
1
1
|
module SinatraMore
|
2
2
|
module AssetTagHelpers
|
3
|
-
|
3
|
+
|
4
|
+
# flash_tag(:notice, :class => 'flash', :id => 'flash-notice')
|
4
5
|
def flash_tag(kind, options={})
|
5
6
|
flash_text = flash[kind]
|
6
7
|
return '' if flash_text.blank?
|
@@ -8,13 +9,14 @@ module SinatraMore
|
|
8
9
|
content_tag(:div, flash_text, options)
|
9
10
|
end
|
10
11
|
|
11
|
-
#
|
12
|
+
# link_to 'click me', '/dashboard', :class => 'linky'
|
13
|
+
# parameters: name, url='javascript:void(0)', options={}, &block
|
12
14
|
def link_to(*args, &block)
|
13
15
|
if block_given?
|
14
16
|
url, options = (args[0] || 'javascript:void(0);'), (args[1] || {})
|
15
17
|
options.reverse_merge!(:href => url)
|
16
|
-
link_content =
|
17
|
-
|
18
|
+
link_content = capture_html(&block)
|
19
|
+
concat_content(content_tag(:a, link_content, options))
|
18
20
|
else
|
19
21
|
name, url, options = args.first, (args[1] || 'javascript:void(0);'), (args[2] || {})
|
20
22
|
options.reverse_merge!(:href => url)
|
@@ -22,27 +24,32 @@ module SinatraMore
|
|
22
24
|
end
|
23
25
|
end
|
24
26
|
|
27
|
+
# image_tag('icons/avatar.png')
|
25
28
|
def image_tag(url, options={})
|
26
29
|
options.reverse_merge!(:src => url)
|
27
30
|
tag(:img, options)
|
28
31
|
end
|
29
32
|
|
33
|
+
# stylesheet_link_tag 'style', 'application', 'layout'
|
30
34
|
def stylesheet_link_tag(*sources)
|
31
35
|
options = sources.extract_options!.symbolize_keys
|
32
36
|
sources.collect { |sheet| stylesheet_tag(sheet, options) }.join("\n")
|
33
37
|
end
|
34
38
|
|
39
|
+
# stylesheet_tag('style', :media => 'screen')
|
35
40
|
def stylesheet_tag(source, options={})
|
36
41
|
rel_path = "/stylesheets/#{source}.css?#{Time.now.to_i}"
|
37
42
|
options = options.dup.reverse_merge!(:href => rel_path, :media => 'screen', :rel => 'stylesheet', :type => 'text/css')
|
38
43
|
tag(:link, options)
|
39
44
|
end
|
40
45
|
|
46
|
+
# javascript_include_tag 'application', 'special'
|
41
47
|
def javascript_include_tag(*sources)
|
42
48
|
options = sources.extract_options!.symbolize_keys
|
43
49
|
sources.collect { |script| javascript_tag(script, options) }.join("\n")
|
44
50
|
end
|
45
51
|
|
52
|
+
# javascript_tag 'application', :src => '/javascripts/base/application.js'
|
46
53
|
def javascript_tag(source, options={})
|
47
54
|
rel_path = "/javascripts/#{source}.js?#{Time.now.to_i}"
|
48
55
|
options = options.dup.reverse_merge!(:content => "", :src => rel_path, :type => 'text/javascript')
|
@@ -8,59 +8,75 @@ class AbstractFormBuilder
|
|
8
8
|
@object = object
|
9
9
|
end
|
10
10
|
|
11
|
+
# f.error_messages
|
11
12
|
def error_messages(options={})
|
12
13
|
@template.error_messages_for(@object, options)
|
13
14
|
end
|
14
15
|
|
16
|
+
# f.label :username, :caption => "Nickname"
|
15
17
|
def label(field, options={})
|
16
18
|
options.reverse_merge!(:caption => field.to_s.titleize)
|
17
19
|
@template.label_tag(field_id(field), options)
|
18
20
|
end
|
19
21
|
|
22
|
+
# f.text_field :username, :value => "(blank)", :id => 'username'
|
20
23
|
def text_field(field, options={})
|
21
24
|
options.reverse_merge!(:value => field_value(field), :id => field_id(field))
|
22
25
|
@template.text_field_tag field_name(field), options
|
23
26
|
end
|
24
27
|
|
28
|
+
# f.text_area :summary, :value => "(enter summary)", :id => 'summary'
|
25
29
|
def text_area(field, options={})
|
26
30
|
options.reverse_merge!(:value => field_value(field), :id => field_id(field))
|
27
31
|
@template.text_area_tag field_name(field), options
|
28
32
|
end
|
29
33
|
|
34
|
+
# f.password_field :password, :id => 'password'
|
30
35
|
def password_field(field, options={})
|
31
36
|
options.reverse_merge!(:value => field_value(field), :id => field_id(field))
|
32
37
|
@template.password_field_tag field_name(field), options
|
33
38
|
end
|
34
39
|
|
40
|
+
# f.file_field(:photo, :class => 'avatar')
|
35
41
|
def file_field(field, options={})
|
36
42
|
options.reverse_merge!(:id => field_id(field))
|
37
43
|
@template.file_field_tag field_name(field), options
|
38
44
|
end
|
39
45
|
|
46
|
+
# f.submit "Update", :class => 'large'
|
40
47
|
def submit(caption, options={})
|
41
48
|
@template.submit_tag caption, options
|
42
49
|
end
|
43
50
|
|
44
51
|
protected
|
45
52
|
|
53
|
+
# Returns the known field types for a formbuilder
|
46
54
|
def self.field_types
|
47
55
|
[:text_field, :text_area, :password_field, :file_field]
|
48
56
|
end
|
49
57
|
|
50
58
|
private
|
51
59
|
|
60
|
+
# Returns the object's models name
|
61
|
+
# => user_assignment
|
52
62
|
def object_name
|
53
63
|
object.class.to_s.underscore
|
54
64
|
end
|
55
65
|
|
66
|
+
# Returns the value for the object's field
|
67
|
+
# field_value(:username) => "Joey"
|
56
68
|
def field_value(field)
|
57
69
|
@object && @object.respond_to?(field) ? @object.send(field) : ""
|
58
70
|
end
|
59
71
|
|
72
|
+
# Returns the name for the given field
|
73
|
+
# field_name(:username) => "user[username]"
|
60
74
|
def field_name(field)
|
61
75
|
"#{object_name}[#{field}]"
|
62
76
|
end
|
63
77
|
|
78
|
+
# Returns the id for the given field
|
79
|
+
# field_id(:username) => "user_username"
|
64
80
|
def field_id(field)
|
65
81
|
"#{object_name}_#{field}"
|
66
82
|
end
|
@@ -1,4 +1,9 @@
|
|
1
1
|
class StandardFormBuilder < AbstractFormBuilder
|
2
|
+
|
3
|
+
# text_field_block(:username, { :class => 'long' }, { :class => 'wide-label' })
|
4
|
+
# text_area_block(:username, { :class => 'long' }, { :class => 'wide-label' })
|
5
|
+
# password_field_block(:username, { :class => 'long' }, { :class => 'wide-label' })
|
6
|
+
# file_field_block(:username, { :class => 'long' }, { :class => 'wide-label' })
|
2
7
|
self.field_types.each do |field_type|
|
3
8
|
class_eval <<-EOF
|
4
9
|
def #{field_type}_block(field, options={}, label_options={})
|
@@ -11,6 +16,7 @@ class StandardFormBuilder < AbstractFormBuilder
|
|
11
16
|
EOF
|
12
17
|
end
|
13
18
|
|
19
|
+
# submit_block("Update")
|
14
20
|
def submit_block(caption)
|
15
21
|
@template.content_block_tag(:p) do
|
16
22
|
@template.submit_tag(caption)
|
@@ -1,28 +1,28 @@
|
|
1
1
|
module SinatraMore
|
2
2
|
module FormHelpers
|
3
|
-
# form_for @user, '/register', :id => 'register'
|
3
|
+
# form_for @user, '/register', :id => 'register' do |f| ... end
|
4
4
|
def form_for(object, url, settings={}, &block)
|
5
|
-
|
5
|
+
configured_builder = settings[:builder] || self.options.default_builder.constantize
|
6
6
|
settings.reverse_merge!(:method => 'post', :action => url)
|
7
7
|
settings[:enctype] = "multipart/form-data" if settings.delete(:multipart)
|
8
|
-
|
9
|
-
|
10
|
-
haml_concat content_tag('form', form_html, settings)
|
8
|
+
form_html = capture_html(configured_builder.new(self, object), &block)
|
9
|
+
concat_content content_tag('form', form_html, settings)
|
11
10
|
end
|
12
11
|
|
13
12
|
# form_tag '/register' do ... end
|
14
13
|
def form_tag(url, options={}, &block)
|
15
14
|
options.reverse_merge!(:method => 'post', :action => url)
|
16
|
-
|
17
|
-
haml_concat content_tag('form', capture_haml(&block), options)
|
15
|
+
concat_content content_tag('form', capture_html(&block), options)
|
18
16
|
end
|
19
17
|
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
18
|
+
# field_set_tag("Office", :class => 'office-set')
|
19
|
+
# parameters: legend_text=nil, options={}
|
20
|
+
def field_set_tag(*args, &block)
|
21
|
+
options = args.extract_options!
|
22
|
+
legend_text = args[0].is_a?(String) ? args.first : nil
|
23
|
+
legend_html = legend_text.blank? ? '' : content_tag(:legend, legend_text)
|
24
|
+
field_set_content = legend_html + capture_html(&block)
|
25
|
+
concat_content content_tag('fieldset', field_set_content, options)
|
26
26
|
end
|
27
27
|
|
28
28
|
# error_messages_for @user
|
@@ -38,44 +38,44 @@ module SinatraMore
|
|
38
38
|
end
|
39
39
|
end
|
40
40
|
|
41
|
-
# label_tag :username
|
41
|
+
# label_tag :username, :class => 'long-label'
|
42
|
+
# label_tag :username, :class => 'long-label' do ... end
|
42
43
|
def label_tag(name, options={}, &block)
|
43
44
|
options.reverse_merge!(:caption => name.to_s.titleize, :for => name)
|
44
45
|
caption_text = options.delete(:caption) + ": "
|
45
|
-
# TODO make this work with erb!!
|
46
46
|
if block_given? # label with inner content
|
47
|
-
label_content = caption_text +
|
48
|
-
|
47
|
+
label_content = caption_text + capture_html(&block)
|
48
|
+
concat_content(content_tag(:label, label_content, options))
|
49
49
|
else # regular label
|
50
50
|
content_tag(:label, caption_text, options)
|
51
51
|
end
|
52
52
|
end
|
53
53
|
|
54
|
-
# text_field_tag :username
|
54
|
+
# text_field_tag :username, :class => 'long'
|
55
55
|
def text_field_tag(name, options={})
|
56
56
|
options.reverse_merge!(:name => name)
|
57
57
|
input_tag(:text, options)
|
58
58
|
end
|
59
59
|
|
60
|
-
#
|
60
|
+
# text_area_tag :username, :class => 'long'
|
61
61
|
def text_area_tag(name, options={})
|
62
62
|
options.reverse_merge!(:name => name)
|
63
63
|
content_tag(:textarea, '', options)
|
64
64
|
end
|
65
65
|
|
66
|
-
# password_field_tag :password
|
66
|
+
# password_field_tag :password, :class => 'long'
|
67
67
|
def password_field_tag(name, options={})
|
68
68
|
options.reverse_merge!(:name => name)
|
69
69
|
input_tag(:password, options)
|
70
70
|
end
|
71
71
|
|
72
|
-
# field_field_tag
|
72
|
+
# field_field_tag :photo, :class => 'long'
|
73
73
|
def file_field_tag(name, options={})
|
74
74
|
options.reverse_merge!(:name => name)
|
75
75
|
input_tag(:file, options)
|
76
76
|
end
|
77
77
|
|
78
|
-
# submit_tag "Create"
|
78
|
+
# submit_tag "Create", :class => 'success'
|
79
79
|
def submit_tag(caption, options={})
|
80
80
|
options.reverse_merge!(:value => caption)
|
81
81
|
input_tag(:submit, options)
|
@@ -1,5 +1,7 @@
|
|
1
1
|
module SinatraMore
|
2
2
|
module FormatHelpers
|
3
|
+
|
4
|
+
# relative_time_ago(Time.now)
|
3
5
|
def relative_time_ago(date)
|
4
6
|
date = date.to_date
|
5
7
|
date = Date.parse(date, true) unless /Date.*/ =~ date.class.to_s
|
@@ -16,15 +18,18 @@ module SinatraMore
|
|
16
18
|
return date.strftime('%A, %B %e, %Y')
|
17
19
|
end
|
18
20
|
|
19
|
-
|
20
|
-
|
21
|
+
# escape_javascript("<h1>Hey</h1>")
|
22
|
+
# Used in xxxx.js.erb files to escape html so that it can be passed to javascript from sinatra
|
23
|
+
def escape_javascript(html_content)
|
24
|
+
return '' unless html_content
|
21
25
|
javascript_mapping = { '\\' => '\\\\', '</' => '<\/', "\r\n" => '\n', "\n" => '\n' }
|
22
26
|
javascript_mapping.merge("\r" => '\n', '"' => '\\"', "'" => "\\'")
|
23
|
-
escaped_string =
|
27
|
+
escaped_string = html_content.gsub(/(\\|<\/|\r\n|[\n\r"'])/) { javascript_mapping[$1] }
|
24
28
|
"\"#{escaped_string}\""
|
25
29
|
end
|
26
30
|
|
27
31
|
alias js_escape escape_javascript
|
32
|
+
alias escape_for_javascript escape_javascript
|
28
33
|
|
29
34
|
end
|
30
35
|
end
|
@@ -0,0 +1,38 @@
|
|
1
|
+
module SinatraMore
|
2
|
+
module OutputHelpers
|
3
|
+
# Captures the html from a block of template code for erb or haml
|
4
|
+
# capture_html(&block) => "...html..."
|
5
|
+
def capture_html(*args, &block)
|
6
|
+
if is_haml?
|
7
|
+
block_is_haml?(block) ? capture_haml(*args, &block) : block.call
|
8
|
+
else
|
9
|
+
capture_erb(*args, &block)
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
# Outputs the given text to the templates buffer directly
|
14
|
+
# concat_content("This will be output to the template buffer in erb or haml")
|
15
|
+
def concat_content(text="")
|
16
|
+
if is_haml?
|
17
|
+
haml_concat(text)
|
18
|
+
else
|
19
|
+
@_out_buf << text
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
# Used to capture the html from a block of erb code
|
24
|
+
# capture_erb(&block) => '...html...'
|
25
|
+
def capture_erb(*args, &block)
|
26
|
+
erb_with_output_buffer { block.call(*args) }
|
27
|
+
end
|
28
|
+
|
29
|
+
# Used to direct the buffer for the erb capture
|
30
|
+
def erb_with_output_buffer(buf = '') #:nodoc:
|
31
|
+
@_out_buf, old_buffer = buf, @_out_buf
|
32
|
+
yield
|
33
|
+
@_out_buf
|
34
|
+
ensure
|
35
|
+
@_out_buf = old_buffer
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
@@ -6,16 +6,19 @@ module SinatraMore
|
|
6
6
|
tag(:input, options)
|
7
7
|
end
|
8
8
|
|
9
|
+
# content_block_tag(:p, :class => 'dark') do ... end
|
9
10
|
def content_block_tag(name, options={}, &block)
|
10
|
-
|
11
|
-
options.merge!(:content => block.call)
|
11
|
+
options.merge!(:content => capture_html(&block))
|
12
12
|
tag(name, options)
|
13
13
|
end
|
14
14
|
|
15
|
+
# content_tag(:p, "hello", :class => 'light')
|
15
16
|
def content_tag(name, content, options={})
|
16
17
|
tag(name, options.merge(:content => content))
|
17
18
|
end
|
18
19
|
|
20
|
+
# tag(:br, :style => 'clear:both')
|
21
|
+
# tag(:p, :content => "hello", :class => 'large')
|
19
22
|
def tag(name, options={})
|
20
23
|
content = options.delete(:content)
|
21
24
|
html_attrs = options.collect { |a, v| v.blank? ? nil : "#{a}=\"#{v}\"" }.compact.join(" ")
|
@@ -25,8 +25,8 @@ module SinatraMore
|
|
25
25
|
def authenticated?(&block)
|
26
26
|
if block_given?
|
27
27
|
return '' unless logged_in?
|
28
|
-
authenticated_content =
|
29
|
-
|
28
|
+
authenticated_content = capture_html(&block)
|
29
|
+
concat_content(authenticated_content)
|
30
30
|
else
|
31
31
|
return logged_in?
|
32
32
|
end
|
@@ -37,8 +37,8 @@ module SinatraMore
|
|
37
37
|
def unregistered?(&block)
|
38
38
|
if block_given?
|
39
39
|
return '' if logged_in?
|
40
|
-
unregistered_content =
|
41
|
-
|
40
|
+
unregistered_content = capture_html(&block)
|
41
|
+
concat_content(unregistered_content)
|
42
42
|
else
|
43
43
|
return !logged_in?
|
44
44
|
end
|
@@ -15,13 +15,21 @@ module SinatraMore
|
|
15
15
|
|
16
16
|
Warden::Strategies.add(:password) do
|
17
17
|
def valid?
|
18
|
-
|
18
|
+
username || password
|
19
19
|
end
|
20
20
|
|
21
21
|
def authenticate!
|
22
|
-
u = User.authenticate(
|
22
|
+
u = User.authenticate(username, password)
|
23
23
|
u.nil? ? fail!("Could not log in") : success!(u)
|
24
24
|
end
|
25
|
+
|
26
|
+
def username
|
27
|
+
params['username'] || params['nickname'] || params['login'] || params['email']
|
28
|
+
end
|
29
|
+
|
30
|
+
def password
|
31
|
+
params['password'] || params['pass']
|
32
|
+
end
|
25
33
|
end
|
26
34
|
end
|
27
35
|
end
|
data/sinatra_more.gemspec
CHANGED
@@ -5,7 +5,7 @@
|
|
5
5
|
|
6
6
|
Gem::Specification.new do |s|
|
7
7
|
s.name = %q{sinatra_more}
|
8
|
-
s.version = "0.0.
|
8
|
+
s.version = "0.0.8"
|
9
9
|
|
10
10
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
11
|
s.authors = ["Nathan Esquenazi"]
|
@@ -31,6 +31,7 @@ Gem::Specification.new do |s|
|
|
31
31
|
"lib/sinatra_more/markup_plugin/form_builder/standard_form_builder.rb",
|
32
32
|
"lib/sinatra_more/markup_plugin/form_helpers.rb",
|
33
33
|
"lib/sinatra_more/markup_plugin/format_helpers.rb",
|
34
|
+
"lib/sinatra_more/markup_plugin/output_helpers.rb",
|
34
35
|
"lib/sinatra_more/markup_plugin/tag_helpers.rb",
|
35
36
|
"lib/sinatra_more/render_plugin.rb",
|
36
37
|
"lib/sinatra_more/render_plugin/render_helpers.rb",
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: sinatra_more
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.8
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Nathan Esquenazi
|
@@ -46,6 +46,7 @@ files:
|
|
46
46
|
- lib/sinatra_more/markup_plugin/form_builder/standard_form_builder.rb
|
47
47
|
- lib/sinatra_more/markup_plugin/form_helpers.rb
|
48
48
|
- lib/sinatra_more/markup_plugin/format_helpers.rb
|
49
|
+
- lib/sinatra_more/markup_plugin/output_helpers.rb
|
49
50
|
- lib/sinatra_more/markup_plugin/tag_helpers.rb
|
50
51
|
- lib/sinatra_more/render_plugin.rb
|
51
52
|
- lib/sinatra_more/render_plugin/render_helpers.rb
|