rails-block-labels 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +12 -0
- data/.rspec +1 -0
- data/README.md +243 -0
- data/Rakefile +23 -0
- data/lib/rails-block-labels.rb +1 -0
- data/lib/rails_block_labels.rb +34 -0
- data/rails-block-labels.gemspec +20 -0
- data/spec/rails_block_labels_spec.rb +7 -0
- data/spec/spec_helper.rb +6 -0
- metadata +73 -0
data/.gitignore
ADDED
data/.rspec
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
--color
|
data/README.md
ADDED
@@ -0,0 +1,243 @@
|
|
1
|
+
# Shaven - Templating without mustaches!
|
2
|
+
|
3
|
+
Hey guys, look at present fasion... mustaches are not fashionable anymore =P.
|
4
|
+
Take a look how nice looking are shaven templates.
|
5
|
+
|
6
|
+
## Motivation
|
7
|
+
|
8
|
+
I'm not a designer, usualy all templates in my work are prepared by external
|
9
|
+
design studios or freelancers... But of course they are always pure xhtml.
|
10
|
+
So still we have to deal with them, convert to haml, fill in with mustaches or
|
11
|
+
erb sh**t! Now, my patience is over. Shaven will readmit some MVPC's fresh
|
12
|
+
air to your web apps and allow you to get rid of stupid logic from your views.
|
13
|
+
|
14
|
+
## Installation
|
15
|
+
|
16
|
+
Installation with rubygems should go without quirks. Shaven depends on Nokogiri - if
|
17
|
+
you don't have it installed yet then i recommend you to check out its documentation
|
18
|
+
to avoid problems.
|
19
|
+
|
20
|
+
$ gem install shaven
|
21
|
+
|
22
|
+
## How it works?
|
23
|
+
|
24
|
+
Shaven views are splited into two layers (similar to defunk's mustache) - Template
|
25
|
+
and Presenter. Templates are pure html files, Presenters are ruby classes
|
26
|
+
which provides data for templates. Depending on the data type provided by
|
27
|
+
presenter's methods you can freely and easily manipulate all contents within
|
28
|
+
your templates. Ok, lets finish talking and take a look at examples...
|
29
|
+
|
30
|
+
### Simple usage
|
31
|
+
|
32
|
+
class SimplePresenter < Shaven::Presenter
|
33
|
+
def title
|
34
|
+
"Hello world!"
|
35
|
+
end
|
36
|
+
|
37
|
+
def description
|
38
|
+
"Yeah, hello beautiful code..."
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
html = <<-HTML
|
43
|
+
<!DOCTYPE html>
|
44
|
+
<html>
|
45
|
+
<head>
|
46
|
+
<title data-fill="title">Example title!</title>
|
47
|
+
</head>
|
48
|
+
<body>
|
49
|
+
<h1 data-fill="title">Example title</h1>
|
50
|
+
<p data-fill="description">Example description...</p>
|
51
|
+
</body>
|
52
|
+
</html>
|
53
|
+
HTML
|
54
|
+
|
55
|
+
SimplePresenter.feed(html).to_html
|
56
|
+
|
57
|
+
This code produces following html:
|
58
|
+
|
59
|
+
<!DOCTYPE html>
|
60
|
+
<html>
|
61
|
+
<head>
|
62
|
+
<title>Hello World!</title>
|
63
|
+
</head>
|
64
|
+
<body>
|
65
|
+
<h1>Hello World!</h1>
|
66
|
+
<p>Yeah, hello beautiful code...</p>
|
67
|
+
</body>
|
68
|
+
</html>
|
69
|
+
|
70
|
+
### DOM manipulation
|
71
|
+
|
72
|
+
class ManipulationPresenter < Shaven::Presenter
|
73
|
+
# If you add parameter to the presenter method, original node will
|
74
|
+
# passed to it. Given element is an Nokogiri::XML::Node with some
|
75
|
+
# extra helpers for content manipulation.
|
76
|
+
def login_link(node)
|
77
|
+
node.update!(:href => logout_path, :method => "delete")
|
78
|
+
end
|
79
|
+
|
80
|
+
# You can use extra html helpers to create new dom elements...
|
81
|
+
def home_page_link
|
82
|
+
a(:href => root_path, :class => "home-page-link") { "Go home!" }
|
83
|
+
end
|
84
|
+
|
85
|
+
# ... or to replace current.
|
86
|
+
def title(node)
|
87
|
+
node.replace! { tag(:h1, :id => "header") { "This is Sparta! "} }
|
88
|
+
end
|
89
|
+
end
|
90
|
+
|
91
|
+
html = <<-HTML
|
92
|
+
<!DOCTYPE html>
|
93
|
+
<html>
|
94
|
+
<body>
|
95
|
+
<div rb="title">Example title</div>
|
96
|
+
<a href="#" data-fill="logout_link">Logout!</a>
|
97
|
+
<div data-fill="home_page_link">Home page link will go here...</div>
|
98
|
+
</body>
|
99
|
+
</html>
|
100
|
+
HTML
|
101
|
+
|
102
|
+
ManipulationPresenter.feed(html).to_html
|
103
|
+
|
104
|
+
Result:
|
105
|
+
|
106
|
+
<!DOCTYPE html>
|
107
|
+
<html>
|
108
|
+
<body>
|
109
|
+
<h1 id="header">This is Sparta!</h1>
|
110
|
+
<a href="/logout" data-method="delete">Logout!</a>
|
111
|
+
<div><a href="/" class="home-page-link">Go Home!</a></div>
|
112
|
+
</body>
|
113
|
+
</html>
|
114
|
+
|
115
|
+
### Hash scopes and lists
|
116
|
+
|
117
|
+
Now, the true power of Shaven. Suport for lists and scopes.
|
118
|
+
|
119
|
+
class ComplexPresenter < Shaven::Presenter
|
120
|
+
# As scopes are treaded all hashes and objects responding to `#to_shaven`
|
121
|
+
# method (which returns hash with attributes).
|
122
|
+
def user
|
123
|
+
{ :name => "John Doe",
|
124
|
+
:email => "john@doe.com",
|
125
|
+
}
|
126
|
+
end
|
127
|
+
|
128
|
+
def users_list
|
129
|
+
[ { :name => tag(:strong) { "Emmet Brown" }, :email => "emmet@brown.com"},
|
130
|
+
{ :name => proc { |node| node.update!(:class => "marty") { "Marty Macfly" }, :email => "marty@macfly.com" },
|
131
|
+
{ :name => "Biff Tannen", :email => "biff@tannen.com" }
|
132
|
+
]
|
133
|
+
end
|
134
|
+
end
|
135
|
+
|
136
|
+
html = <<-HTML
|
137
|
+
<!DOCTYPE html>
|
138
|
+
<html>
|
139
|
+
<body>
|
140
|
+
<h1>Single user here!</h1>
|
141
|
+
<div data-fill="user">
|
142
|
+
<h2 data-fill="name">Sample name</h2>
|
143
|
+
<p data-fill="email">sapmle@email.com</p>
|
144
|
+
</div>
|
145
|
+
<h1>More users</h1>
|
146
|
+
<ul id="users">
|
147
|
+
<li data-fill="users_list">
|
148
|
+
<span data-fill="name">Sample name</span>
|
149
|
+
<span data-fill="email">sample@email.com</span>
|
150
|
+
<li>
|
151
|
+
</ul>
|
152
|
+
</body>
|
153
|
+
</html>
|
154
|
+
HTML
|
155
|
+
|
156
|
+
And the awesome result is:
|
157
|
+
|
158
|
+
<!DOCTYPE html>
|
159
|
+
<html>
|
160
|
+
<body>
|
161
|
+
<h1>Single user here!</h1>
|
162
|
+
<div data-fill="user">
|
163
|
+
<h2>Adam Smith</h2>
|
164
|
+
<p>adam@smith.com</p>
|
165
|
+
</div>
|
166
|
+
<h1>More users</h1>
|
167
|
+
<ul id="users">
|
168
|
+
<li>
|
169
|
+
<span><strong>Emmet Brown</strong></span>
|
170
|
+
<span>brown@brown.com</span>
|
171
|
+
<li>
|
172
|
+
<li class="marty">
|
173
|
+
<span>Marty Macfly</span>
|
174
|
+
<span>marty@macfly.com</span>
|
175
|
+
<li>
|
176
|
+
<li>
|
177
|
+
<span>Biff Tannen</span>
|
178
|
+
<span>biff@tannen.com</span>
|
179
|
+
<li>
|
180
|
+
</ul>
|
181
|
+
</body>
|
182
|
+
</html>
|
183
|
+
|
184
|
+
### Conditionals
|
185
|
+
|
186
|
+
class ConditionalsPresenter < Shaven::Presenter
|
187
|
+
def true?
|
188
|
+
true
|
189
|
+
end
|
190
|
+
|
191
|
+
def false?
|
192
|
+
false
|
193
|
+
end
|
194
|
+
end
|
195
|
+
|
196
|
+
html = <<-HTML
|
197
|
+
<!DOCTYPE html>
|
198
|
+
<html>
|
199
|
+
<body>
|
200
|
+
<div data-if="true?">Hello...</div>
|
201
|
+
<div data-unless="false?">World!</div>
|
202
|
+
</body>
|
203
|
+
</html>
|
204
|
+
HTML
|
205
|
+
|
206
|
+
### Dummy elements
|
207
|
+
|
208
|
+
html = <<-HTML
|
209
|
+
<!DOCTYPE html>
|
210
|
+
<html>
|
211
|
+
<body>
|
212
|
+
<h1>Hello dummies!</h1>
|
213
|
+
<div data-dummy="yes">This is dummy text!</div>
|
214
|
+
</body>
|
215
|
+
</html>
|
216
|
+
HTML
|
217
|
+
|
218
|
+
produces:
|
219
|
+
|
220
|
+
<!DOCTYPE html>
|
221
|
+
<html>
|
222
|
+
<body>
|
223
|
+
<h1>Hello dummies!</h1>
|
224
|
+
</body>
|
225
|
+
</html>
|
226
|
+
|
227
|
+
## Benchmarks
|
228
|
+
|
229
|
+
Ruby 1.9.2
|
230
|
+
|
231
|
+
user system total real
|
232
|
+
Shaven 3.480000 0.050000 3.530000 ( 3.539728)
|
233
|
+
ERB 1.360000 0.010000 1.370000 ( 1.374822)
|
234
|
+
Mustache 6.830000 0.080000 6.910000 ( 6.904736)
|
235
|
+
HAML 10.800000 0.080000 10.880000 ( 10.886872)
|
236
|
+
|
237
|
+
Ruby 1.8.7
|
238
|
+
|
239
|
+
user system total real
|
240
|
+
Shaven 8.510000 0.120000 8.630000 ( 8.658850)
|
241
|
+
ERB 1.510000 0.010000 1.520000 ( 1.516075)
|
242
|
+
Mustache 5.230000 0.070000 5.300000 ( 5.314266)
|
243
|
+
HAML 13.100000 0.230000 13.330000 ( 13.337038)
|
data/Rakefile
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
# -*- ruby -*-
|
2
|
+
|
3
|
+
begin
|
4
|
+
require 'rspec/core/rake_task'
|
5
|
+
RSpec::Core::RakeTask.new(:spec) do |t|
|
6
|
+
t.rspec_opts = ENV['RSPEC_OPTS']
|
7
|
+
end
|
8
|
+
rescue LoadError
|
9
|
+
task :spec do
|
10
|
+
abort 'Run `gem install rspec` to install RSpec'
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
task :test => :spec
|
15
|
+
task :default => :test
|
16
|
+
|
17
|
+
require 'rake/rdoctask'
|
18
|
+
Rake::RDocTask.new do |rdoc|
|
19
|
+
rdoc.rdoc_dir = 'rdoc'
|
20
|
+
rdoc.title = "Rails Block Labels"
|
21
|
+
rdoc.rdoc_files.include('README*')
|
22
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
23
|
+
end
|
@@ -0,0 +1 @@
|
|
1
|
+
require 'rails_block_labels'
|
@@ -0,0 +1,34 @@
|
|
1
|
+
require 'rails'
|
2
|
+
|
3
|
+
class ActionView::Helpers::InstanceTag
|
4
|
+
def to_label_tag(text = nil, options = {}, &block)
|
5
|
+
options = options.stringify_keys
|
6
|
+
tag_value = options.delete("value")
|
7
|
+
name_and_id = options.dup
|
8
|
+
|
9
|
+
if name_and_id["for"]
|
10
|
+
name_and_id["id"] = name_and_id["for"]
|
11
|
+
else
|
12
|
+
name_and_id.delete("id")
|
13
|
+
end
|
14
|
+
|
15
|
+
add_default_name_and_id_for_value(tag_value, name_and_id)
|
16
|
+
options.delete("index")
|
17
|
+
options["for"] ||= name_and_id["id"]
|
18
|
+
|
19
|
+
content = if text.blank?
|
20
|
+
method_and_value = tag_value.present? ? "#{method_name}.#{tag_value}" : method_name
|
21
|
+
I18n.t("helpers.label.#{object_name}.#{method_and_value}", :default => "").presence
|
22
|
+
else
|
23
|
+
text.to_s
|
24
|
+
end
|
25
|
+
|
26
|
+
content ||= if object && object.class.respond_to?(:human_attribute_name)
|
27
|
+
object.class.human_attribute_name(method_name)
|
28
|
+
end
|
29
|
+
|
30
|
+
content ||= method_name.humanize
|
31
|
+
content += capture(&block)
|
32
|
+
label_tag(name_and_id["id"], content.html_safe, options)
|
33
|
+
end
|
34
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
# -*- ruby -*-
|
2
|
+
$LOAD_PATH.unshift(File.expand_path('../lib', __FILE__))
|
3
|
+
require 'shaven/version'
|
4
|
+
|
5
|
+
Gem::Specification.new do |s|
|
6
|
+
s.name = "rails-block-labels"
|
7
|
+
s.rubyforge_project = "RailsBlockLabels"
|
8
|
+
s.version = "0.0.1"
|
9
|
+
s.authors = ["Chris Kowalik"]
|
10
|
+
s.email = ["chris@nu7hat.ch"]
|
11
|
+
s.homepage = "http://github.com/nu7hatch/rails-block-labels"
|
12
|
+
s.summary = "Hack for using i18n powered block labels in rails3 app!"
|
13
|
+
s.description = "Allows to use block labels as wrappers for inputs, without loosing i18n stuff."
|
14
|
+
|
15
|
+
s.files = `git ls-files`.split("\n")
|
16
|
+
s.test_files = `git ls-files -- {spec}/*`.split("\n")
|
17
|
+
s.require_paths = ['lib']
|
18
|
+
|
19
|
+
s.add_dependency 'rails', '~> 3.0'
|
20
|
+
end
|
data/spec/spec_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,73 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: rails-block-labels
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
prerelease:
|
5
|
+
version: 0.0.1
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Chris Kowalik
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
|
13
|
+
date: 2011-09-30 00:00:00 Z
|
14
|
+
dependencies:
|
15
|
+
- !ruby/object:Gem::Dependency
|
16
|
+
name: rails
|
17
|
+
prerelease: false
|
18
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
19
|
+
none: false
|
20
|
+
requirements:
|
21
|
+
- - ~>
|
22
|
+
- !ruby/object:Gem::Version
|
23
|
+
version: "3.0"
|
24
|
+
type: :runtime
|
25
|
+
version_requirements: *id001
|
26
|
+
description: Allows to use block labels as wrappers for inputs, without loosing i18n stuff.
|
27
|
+
email:
|
28
|
+
- chris@nu7hat.ch
|
29
|
+
executables: []
|
30
|
+
|
31
|
+
extensions: []
|
32
|
+
|
33
|
+
extra_rdoc_files: []
|
34
|
+
|
35
|
+
files:
|
36
|
+
- .gitignore
|
37
|
+
- .rspec
|
38
|
+
- README.md
|
39
|
+
- Rakefile
|
40
|
+
- lib/rails-block-labels.rb
|
41
|
+
- lib/rails_block_labels.rb
|
42
|
+
- rails-block-labels.gemspec
|
43
|
+
- spec/rails_block_labels_spec.rb
|
44
|
+
- spec/spec_helper.rb
|
45
|
+
homepage: http://github.com/nu7hatch/rails-block-labels
|
46
|
+
licenses: []
|
47
|
+
|
48
|
+
post_install_message:
|
49
|
+
rdoc_options: []
|
50
|
+
|
51
|
+
require_paths:
|
52
|
+
- lib
|
53
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
54
|
+
none: false
|
55
|
+
requirements:
|
56
|
+
- - ">="
|
57
|
+
- !ruby/object:Gem::Version
|
58
|
+
version: "0"
|
59
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
60
|
+
none: false
|
61
|
+
requirements:
|
62
|
+
- - ">="
|
63
|
+
- !ruby/object:Gem::Version
|
64
|
+
version: "0"
|
65
|
+
requirements: []
|
66
|
+
|
67
|
+
rubyforge_project: RailsBlockLabels
|
68
|
+
rubygems_version: 1.8.10
|
69
|
+
signing_key:
|
70
|
+
specification_version: 3
|
71
|
+
summary: Hack for using i18n powered block labels in rails3 app!
|
72
|
+
test_files: []
|
73
|
+
|