helpers 0.0.1 → 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGELOG +3 -0
- data/examples/config.ru +45 -0
- data/helpers.gemspec +1 -1
- data/lib/helpers.rb +0 -36
- data/lib/helpers/assets.rb +69 -0
- data/lib/helpers/forms.rb +41 -0
- data/lib/helpers/general.rb +52 -0
- metadata +7 -4
data/CHANGELOG
CHANGED
data/examples/config.ru
ADDED
@@ -0,0 +1,45 @@
|
|
1
|
+
#!/usr/bin/env rackup -p 4000 -s thin
|
2
|
+
# encoding: utf-8
|
3
|
+
|
4
|
+
$:.unshift(::File.expand_path("../../lib", __FILE__))
|
5
|
+
|
6
|
+
require "helpers"
|
7
|
+
|
8
|
+
class GoogleAnalytics
|
9
|
+
def initialize(app)
|
10
|
+
@app = app
|
11
|
+
end
|
12
|
+
|
13
|
+
# And here the fun begins, in our middlewares.
|
14
|
+
# No more body.gsub, no more 100x parsing output
|
15
|
+
# as a nokogiri document, just use it like DOM
|
16
|
+
# in your browser and return it back to rack
|
17
|
+
def call(env)
|
18
|
+
@app.call(env).tap do |response|
|
19
|
+
body = response[2].content.find_tag { |tag| tag.name.eql?(:body) }
|
20
|
+
body.tag(:script, src: "htpp://google.com/ga.js")
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
use GoogleAnalytics
|
26
|
+
use ContentLength
|
27
|
+
use ContentType
|
28
|
+
|
29
|
+
map("/") do
|
30
|
+
run lambda { |env|
|
31
|
+
html = Helpers::Tag.new(:html) do |html|
|
32
|
+
html.tag(:head) do |head|
|
33
|
+
head.tag(:title, "This is the dog's bollocks!")
|
34
|
+
end
|
35
|
+
|
36
|
+
html.tag(:body) do |body|
|
37
|
+
body.tag(:h1, "Hello World!")
|
38
|
+
body.tag(:p, class: :perex) do |p|
|
39
|
+
p.content << "Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum."
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
43
|
+
[200, Hash.new, html]
|
44
|
+
}
|
45
|
+
end
|
data/helpers.gemspec
CHANGED
@@ -6,7 +6,7 @@ require "base64"
|
|
6
6
|
|
7
7
|
Gem::Specification.new do |s|
|
8
8
|
s.name = "helpers"
|
9
|
-
s.version = "0.0.
|
9
|
+
s.version = "0.0.2"
|
10
10
|
s.authors = ["Jakub Stastny aka Botanicus"]
|
11
11
|
s.homepage = "http://github.com/botanicus/helpers"
|
12
12
|
s.summary = "Framework-agnostic template helpers"
|
data/lib/helpers.rb
CHANGED
@@ -109,40 +109,4 @@ module Helpers
|
|
109
109
|
self.to_s
|
110
110
|
end
|
111
111
|
end
|
112
|
-
|
113
|
-
# Form.new("/posts/create") do |form|
|
114
|
-
# form.text_field "post[title]"
|
115
|
-
# form.text_area "post[body]"
|
116
|
-
# form.submit "Create"
|
117
|
-
# end
|
118
|
-
class Form < Tag
|
119
|
-
def initialize
|
120
|
-
super(:form, Array.new, action)
|
121
|
-
end
|
122
|
-
end
|
123
|
-
|
124
|
-
# Form.new(@post, "/posts/create") do |form|
|
125
|
-
# form.text_field :title
|
126
|
-
# form.text_area :body
|
127
|
-
# form.submit "Create"
|
128
|
-
# end
|
129
|
-
class FormFor < Form
|
130
|
-
def initialize(object, action)
|
131
|
-
end
|
132
|
-
end
|
133
|
-
|
134
|
-
# ButtonToDelete.new(post_path(@post), confirm: "Are you sure?")
|
135
|
-
class ButtonTo < Tag
|
136
|
-
def initialize(title, action, attrs = Hash.new, &block)
|
137
|
-
button = Tag.new(:button, title, attrs.merge(type: "submit"))
|
138
|
-
super(:form, button, action: action, &block)
|
139
|
-
end
|
140
|
-
end
|
141
|
-
|
142
|
-
class ButtonToDelete < ButtonTo
|
143
|
-
def initialize(title, action = nil, attrs = Hash.new)
|
144
|
-
action, title = title, "Destroy" if title && action.nil?
|
145
|
-
super(title, action + "?_method=DELETE", attrs.merge(name: name, value: value))
|
146
|
-
end
|
147
|
-
end
|
148
112
|
end
|
@@ -0,0 +1,69 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
# TODO: javascript "application" => media/javascripts/application.js
|
4
|
+
# ... but what if I need full path? It should be tested if file exist, of course
|
5
|
+
# javascript Path.new("design/whatever.js")
|
6
|
+
|
7
|
+
begin
|
8
|
+
require "media-path"
|
9
|
+
rescue LoadError
|
10
|
+
raise LoadError, "You have to install media-path gem!"
|
11
|
+
end
|
12
|
+
|
13
|
+
module Helpers
|
14
|
+
module Assets
|
15
|
+
# @since 0.0.2
|
16
|
+
def self.root
|
17
|
+
@@root
|
18
|
+
rescue NameError
|
19
|
+
if Dir.exist?("public")
|
20
|
+
MediaPath.new("public")
|
21
|
+
else
|
22
|
+
raise "You have to set Helpers::Assets.root!"
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
# @since 0.0.2
|
27
|
+
def self.root=(root)
|
28
|
+
case root
|
29
|
+
when String
|
30
|
+
@@root = MediaPath.new(root)
|
31
|
+
when Pathname
|
32
|
+
@@root = MediaPath.new(root.to_s)
|
33
|
+
when MediaPath
|
34
|
+
@@root = root
|
35
|
+
else
|
36
|
+
raise ArgumentError, "Helpers::Assets.root has to be a MediaPath!"
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
# @since 0.0.2
|
41
|
+
def javascript(basename)
|
42
|
+
path = Assets.root.join("javascripts/#{basename}.js")
|
43
|
+
Tag.new(:script, src: path.url, type: "text/javascript")
|
44
|
+
end
|
45
|
+
|
46
|
+
# @since 0.0.2
|
47
|
+
def stylesheet(basename, attrs = Hash.new)
|
48
|
+
path = Assets.root.join("stylesheets/#{basename}")
|
49
|
+
default = {href: path.url, media: 'screen', rel: 'stylesheet', type: 'text/css'}
|
50
|
+
SelfCloseTag.new(:link, default.merge(attrs))
|
51
|
+
end
|
52
|
+
|
53
|
+
def image(basename, attrs = Hash.new)
|
54
|
+
path = Assets.root.join("images/#{basename}")
|
55
|
+
default = {src: path.url, alt: path.basename}
|
56
|
+
SelfCloseTag.new(:img, default.merge(attrs))
|
57
|
+
end
|
58
|
+
|
59
|
+
# @since 0.0.2
|
60
|
+
def javascripts(*names)
|
61
|
+
names.map { |name| self.javascript(name) }.join("\n")
|
62
|
+
end
|
63
|
+
|
64
|
+
# @since 0.0.2
|
65
|
+
def stylesheets(*names)
|
66
|
+
names.map { |name| self.stylesheet(name) }.join("\n")
|
67
|
+
end
|
68
|
+
end
|
69
|
+
end
|
@@ -0,0 +1,41 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
module Helpers
|
4
|
+
class Forms
|
5
|
+
# Form.new("/posts/create") do |form|
|
6
|
+
# form.text_field "post[title]"
|
7
|
+
# form.text_area "post[body]"
|
8
|
+
# form.submit "Create"
|
9
|
+
# end
|
10
|
+
class Form < Tag
|
11
|
+
def initialize
|
12
|
+
super(:form, Array.new, action)
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
# Form.new(@post, "/posts/create") do |form|
|
17
|
+
# form.text_field :title
|
18
|
+
# form.text_area :body
|
19
|
+
# form.submit "Create"
|
20
|
+
# end
|
21
|
+
class FormFor < Form
|
22
|
+
def initialize(object, action)
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
# ButtonToDelete.new(post_path(@post), confirm: "Are you sure?")
|
27
|
+
class ButtonTo < Tag
|
28
|
+
def initialize(title, action, attrs = Hash.new, &block)
|
29
|
+
button = Tag.new(:button, title, attrs.merge(type: "submit"))
|
30
|
+
super(:form, button, action: action, &block)
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
class ButtonToDelete < ButtonTo
|
35
|
+
def initialize(title, action = nil, attrs = Hash.new)
|
36
|
+
action, title = title, "Destroy" if title && action.nil?
|
37
|
+
super(title, action + "?_method=DELETE", attrs.merge(name: name, value: value))
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
@@ -0,0 +1,52 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
require "uri"
|
4
|
+
|
5
|
+
module Rango
|
6
|
+
module Helpers
|
7
|
+
# @since 0.0.1
|
8
|
+
def copyright(from)
|
9
|
+
now = Time.now.year
|
10
|
+
now.eql?(from) ? now : "#{from} - #{now}"
|
11
|
+
end
|
12
|
+
|
13
|
+
# @since 0.0.2
|
14
|
+
def link_to(name, url, options = Hash.new)
|
15
|
+
default = {href: URI.escape(url), title: name.to_s.gsub(/'/, ''')}
|
16
|
+
Tag.new(:a, name, default.merge(options))
|
17
|
+
end
|
18
|
+
|
19
|
+
# @since 0.0.2
|
20
|
+
def link_item(name, url)
|
21
|
+
Tag.new(:li, link_to(name, url))
|
22
|
+
end
|
23
|
+
|
24
|
+
# @since 0.0.2
|
25
|
+
# mail_to "joe@example.com"
|
26
|
+
# => "<a href='mailto:joe@example.com'>joe@example.com</a>"
|
27
|
+
# mail_to "joe@example.com", "Title"
|
28
|
+
# => "<a href='mailto:joe@example.com'>Title</a>"
|
29
|
+
def mail_to(mail, text = mail)
|
30
|
+
mail.gsub!("@" "@")
|
31
|
+
Tag.new(:a, text, href: "mailto:#{mail}")
|
32
|
+
end
|
33
|
+
|
34
|
+
# @since 0.0.2
|
35
|
+
def error_messages_for(model_instance)
|
36
|
+
Tag.new(:ul) do
|
37
|
+
messages = model_instance.errors.full_messages
|
38
|
+
messages.map { |message| tag :li, message }
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
def truncate(text, *args)
|
43
|
+
options = args.extract_options!
|
44
|
+
unless args.empty?
|
45
|
+
options[:size] = args[0] || 75
|
46
|
+
options[:omission] = args[1] || "..."
|
47
|
+
end
|
48
|
+
options = {size: 75, omission: "..."}.merge(options)
|
49
|
+
text.scan(/(\S+)(\s+)/)[0..options[:size]].flatten.join << options[:omission] if text
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: helpers
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Jakub Stastny aka Botanicus
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain:
|
11
|
-
date: 2010-
|
11
|
+
date: 2010-03-14 00:00:00 +00:00
|
12
12
|
default_executable:
|
13
13
|
dependencies: []
|
14
14
|
|
@@ -25,11 +25,15 @@ files:
|
|
25
25
|
- CHANGELOG
|
26
26
|
- LICENSE
|
27
27
|
- README.textile
|
28
|
+
- examples/config.ru
|
28
29
|
- helpers.gemspec
|
29
30
|
- helpers.pre.gemspec
|
30
31
|
- lib/helpers.rb
|
31
32
|
- lib/helpers/adapters/rails.rb
|
32
33
|
- lib/helpers/adapters/rango.rb
|
34
|
+
- lib/helpers/assets.rb
|
35
|
+
- lib/helpers/forms.rb
|
36
|
+
- lib/helpers/general.rb
|
33
37
|
- spec/helpers_spec.rb
|
34
38
|
- spec/spec.opts
|
35
39
|
- spec/spec_helper.rb
|
@@ -37,8 +41,7 @@ has_rdoc: true
|
|
37
41
|
homepage: http://github.com/botanicus/helpers
|
38
42
|
licenses: []
|
39
43
|
|
40
|
-
post_install_message: "[\e[32mVersion 0.0.
|
41
|
-
[\e[32mVersion 0.0.1\e[0m] Form, FormFor, ButtonTo, ButtonToDelete classes\n"
|
44
|
+
post_install_message: "[\e[32mVersion 0.0.2\e[0m] Imported helpers from Rango\n"
|
42
45
|
rdoc_options: []
|
43
46
|
|
44
47
|
require_paths:
|