swiss_knife 0.1.4 → 0.1.5

Sign up to get free protection for your applications and to get access to all the features.
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --format=documentation
data/Gemfile CHANGED
@@ -1,10 +1,2 @@
1
1
  source :rubygems
2
-
3
- gem "rails", "3.0.3"
4
- gem "rspec-rails", "2.1.0"
5
- gem "nokogiri"
6
- gem "fakeweb"
7
-
8
- platforms :mri_19 do
9
- gem "ruby-debug19", :require => "ruby-debug"
10
- end
2
+ gemspec
data/Gemfile.lock CHANGED
@@ -1,95 +1,90 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ swiss_knife (0.1.5)
5
+ rails (~> 3.0.0)
6
+
1
7
  GEM
2
8
  remote: http://rubygems.org/
3
9
  specs:
4
10
  abstract (1.0.0)
5
- actionmailer (3.0.3)
6
- actionpack (= 3.0.3)
7
- mail (~> 2.2.9)
8
- actionpack (3.0.3)
9
- activemodel (= 3.0.3)
10
- activesupport (= 3.0.3)
11
+ actionmailer (3.0.5)
12
+ actionpack (= 3.0.5)
13
+ mail (~> 2.2.15)
14
+ actionpack (3.0.5)
15
+ activemodel (= 3.0.5)
16
+ activesupport (= 3.0.5)
11
17
  builder (~> 2.1.2)
12
18
  erubis (~> 2.6.6)
13
19
  i18n (~> 0.4)
14
20
  rack (~> 1.2.1)
15
21
  rack-mount (~> 0.6.13)
16
- rack-test (~> 0.5.6)
22
+ rack-test (~> 0.5.7)
17
23
  tzinfo (~> 0.3.23)
18
- activemodel (3.0.3)
19
- activesupport (= 3.0.3)
24
+ activemodel (3.0.5)
25
+ activesupport (= 3.0.5)
20
26
  builder (~> 2.1.2)
21
27
  i18n (~> 0.4)
22
- activerecord (3.0.3)
23
- activemodel (= 3.0.3)
24
- activesupport (= 3.0.3)
28
+ activerecord (3.0.5)
29
+ activemodel (= 3.0.5)
30
+ activesupport (= 3.0.5)
25
31
  arel (~> 2.0.2)
26
32
  tzinfo (~> 0.3.23)
27
- activeresource (3.0.3)
28
- activemodel (= 3.0.3)
29
- activesupport (= 3.0.3)
30
- activesupport (3.0.3)
31
- archive-tar-minitar (0.5.2)
32
- arel (2.0.4)
33
+ activeresource (3.0.5)
34
+ activemodel (= 3.0.5)
35
+ activesupport (= 3.0.5)
36
+ activesupport (3.0.5)
37
+ arel (2.0.9)
33
38
  builder (2.1.2)
34
- columnize (0.3.2)
35
39
  diff-lcs (1.1.2)
36
40
  erubis (2.6.6)
37
41
  abstract (>= 1.0.0)
38
42
  fakeweb (1.3.0)
39
- i18n (0.4.2)
40
- linecache19 (0.5.11)
41
- ruby_core_source (>= 0.1.4)
42
- mail (2.2.10)
43
+ i18n (0.5.0)
44
+ mail (2.2.15)
43
45
  activesupport (>= 2.3.6)
44
- i18n (~> 0.4.1)
46
+ i18n (>= 0.4.0)
45
47
  mime-types (~> 1.16)
46
48
  treetop (~> 1.4.8)
47
49
  mime-types (1.16)
48
- nokogiri (1.4.3.1)
50
+ nokogiri (1.4.4)
49
51
  polyglot (0.3.1)
50
52
  rack (1.2.1)
51
53
  rack-mount (0.6.13)
52
54
  rack (>= 1.0.0)
53
- rack-test (0.5.6)
55
+ rack-test (0.5.7)
54
56
  rack (>= 1.0)
55
- rails (3.0.3)
56
- actionmailer (= 3.0.3)
57
- actionpack (= 3.0.3)
58
- activerecord (= 3.0.3)
59
- activeresource (= 3.0.3)
60
- activesupport (= 3.0.3)
57
+ rails (3.0.5)
58
+ actionmailer (= 3.0.5)
59
+ actionpack (= 3.0.5)
60
+ activerecord (= 3.0.5)
61
+ activeresource (= 3.0.5)
62
+ activesupport (= 3.0.5)
61
63
  bundler (~> 1.0)
62
- railties (= 3.0.3)
63
- railties (3.0.3)
64
- actionpack (= 3.0.3)
65
- activesupport (= 3.0.3)
64
+ railties (= 3.0.5)
65
+ railties (3.0.5)
66
+ actionpack (= 3.0.5)
67
+ activesupport (= 3.0.5)
66
68
  rake (>= 0.8.7)
67
69
  thor (~> 0.14.4)
68
70
  rake (0.8.7)
69
- rspec (2.1.0)
70
- rspec-core (~> 2.1.0)
71
- rspec-expectations (~> 2.1.0)
72
- rspec-mocks (~> 2.1.0)
73
- rspec-core (2.1.0)
74
- rspec-expectations (2.1.0)
71
+ rspec (2.5.0)
72
+ rspec-core (~> 2.5.0)
73
+ rspec-expectations (~> 2.5.0)
74
+ rspec-mocks (~> 2.5.0)
75
+ rspec-core (2.5.1)
76
+ rspec-expectations (2.5.0)
75
77
  diff-lcs (~> 1.1.2)
76
- rspec-mocks (2.1.0)
77
- rspec-rails (2.1.0)
78
- rspec (~> 2.1.0)
79
- ruby-debug-base19 (0.11.24)
80
- columnize (>= 0.3.1)
81
- linecache19 (>= 0.5.11)
82
- ruby_core_source (>= 0.1.4)
83
- ruby-debug19 (0.11.6)
84
- columnize (>= 0.3.1)
85
- linecache19 (>= 0.5.11)
86
- ruby-debug-base19 (>= 0.11.19)
87
- ruby_core_source (0.1.4)
88
- archive-tar-minitar (>= 0.5.2)
78
+ rspec-mocks (2.5.0)
79
+ rspec-rails (2.5.0)
80
+ actionpack (~> 3.0)
81
+ activesupport (~> 3.0)
82
+ railties (~> 3.0)
83
+ rspec (~> 2.5.0)
89
84
  thor (0.14.6)
90
- treetop (1.4.8)
85
+ treetop (1.4.9)
91
86
  polyglot (>= 0.3.1)
92
- tzinfo (0.3.23)
87
+ tzinfo (0.3.24)
93
88
 
94
89
  PLATFORMS
95
90
  ruby
@@ -97,6 +92,5 @@ PLATFORMS
97
92
  DEPENDENCIES
98
93
  fakeweb
99
94
  nokogiri
100
- rails (= 3.0.3)
101
- rspec-rails (= 2.1.0)
102
- ruby-debug19
95
+ rspec-rails (~> 2.5.0)
96
+ swiss_knife!
@@ -49,17 +49,24 @@ module SwissKnife
49
49
  image_tag url, { :class => "gravatar", :alt => options[:alt], :title => options[:title] }
50
50
  end
51
51
 
52
+ # Display flash messages.
53
+ #
54
+ # flash_messages
55
+ # #=> '<p class="message alert">This is just an alert!</p>
56
+ #
52
57
  def flash_messages
53
- html = ""
54
-
55
- flash.each do |name, message|
56
- html << content_tag(:p, message, :class => "message #{name}")
57
- flash.discard(name)
58
+ safe_buffer do |html|
59
+ flash.each do |name, message|
60
+ html << content_tag(:p, message, :class => "message #{name}")
61
+ flash.discard(name)
62
+ end
58
63
  end
59
-
60
- html.html_safe
61
64
  end
62
65
 
66
+ # Create a meta tag for http://github.com/fnando/dispatcher-js.
67
+ #
68
+ # <%= dispatcher %>
69
+ #
63
70
  def dispatcher_tag
64
71
  controller_name = controller.class.name.underscore
65
72
  controller_name.gsub!(/\//, "_")
@@ -68,12 +75,29 @@ module SwissKnife
68
75
  %[<meta name="page" content="#{controller_name}##{controller.action_name}" />].html_safe
69
76
  end
70
77
 
78
+ # Wrap the content in a <BODY> tag.
79
+ #
80
+ # <%= body do %>
81
+ # <h1>Hi there!</h1>
82
+ # <% end %>
83
+ #
84
+ # This will set some attributes like <tt>id</tt> and <tt>class</tt>,
85
+ # with controller and action name.
86
+ #
87
+ # You can set additional CSS classes by using the <tt>:append_class</tt> option.
88
+ #
89
+ # body(:append_class => "theme-red") { ...some content }
90
+ #
71
91
  def body(options = {}, &block)
72
92
  action_name = ACTION_ALIASES[controller.action_name] || controller.action_name
73
93
 
74
94
  options = {
75
95
  :id => "#{controller.controller_name}-page",
76
- :class => "#{controller.controller_name}-#{action_name} #{I18n.locale}"
96
+ :class => [
97
+ "#{controller.controller_name}-#{action_name}",
98
+ "#{controller.controller_name}-#{controller.action_name}",
99
+ I18n.locale
100
+ ].join(" ")
77
101
  }.merge(options)
78
102
 
79
103
  options[:class] << (" " + options.delete(:append_class).to_s) if options[:append_class]
@@ -113,6 +137,12 @@ module SwissKnife
113
137
  content_tag(tag, capture(&block), options)
114
138
  end
115
139
 
140
+ # Create a encrypted mailto link.
141
+ # <b>THIS API IS NOT RAILS COMPLIANT.</b>
142
+ #
143
+ # mail_to "john@doe.com"
144
+ # mail_to "john@doe.com", "Contact me"
145
+ #
116
146
  def mail_to(email, label = nil)
117
147
  encrypt = proc do |text|
118
148
  text.to_enum(:each_byte).collect {|c| sprintf("&#%d;", c) }.join
@@ -126,44 +156,42 @@ module SwissKnife
126
156
 
127
157
  def javascript_includes(*args)
128
158
  options = args.extract_options!
129
- html = ""
130
-
131
- args.each do |name|
132
- bundle = SwissKnife::Assets.config["javascripts"][name.to_s] rescue nil
133
159
 
134
- if SwissKnife::Assets.merge? && bundle
135
- html << javascript_include_tag("#{name}_packaged".html_safe, options)
136
- elsif bundle
137
- bundle.each do |file|
138
- html << javascript_include_tag(file.to_s.html_safe, options)
160
+ safe_buffer do |html|
161
+ args.each do |name|
162
+ bundle = SwissKnife::Assets.config["javascripts"][name.to_s] rescue nil
163
+
164
+ if SwissKnife::Assets.merge? && bundle
165
+ html << javascript_include_tag("#{name}_packaged".html_safe, options)
166
+ elsif bundle
167
+ bundle.each do |file|
168
+ html << javascript_include_tag(file.to_s.html_safe, options)
169
+ end
170
+ else
171
+ html << javascript_include_tag(name.to_s.html_safe, options)
139
172
  end
140
- else
141
- html << javascript_include_tag(name.to_s.html_safe, options)
142
173
  end
143
174
  end
144
-
145
- html.html_safe
146
175
  end
147
176
 
148
177
  def stylesheet_includes(*args)
149
178
  options = args.extract_options!
150
- html = ""
151
179
 
152
- args.each do |name|
153
- bundle = SwissKnife::Assets.config["stylesheets"][name.to_s] rescue nil
154
-
155
- if SwissKnife::Assets.merge? && bundle
156
- html << stylesheet_link_tag("#{name}_packaged", options)
157
- elsif bundle
158
- bundle.each do |file|
159
- html << stylesheet_link_tag("#{file}", options)
180
+ safe_buffer do |html|
181
+ args.each do |name|
182
+ bundle = SwissKnife::Assets.config["stylesheets"][name.to_s] rescue nil
183
+
184
+ if SwissKnife::Assets.merge? && bundle
185
+ html << stylesheet_link_tag("#{name}_packaged", options)
186
+ elsif bundle
187
+ bundle.each do |file|
188
+ html << stylesheet_link_tag("#{file}", options)
189
+ end
190
+ else
191
+ html << stylesheet_link_tag("#{name}", options)
160
192
  end
161
- else
162
- html << stylesheet_link_tag("#{name}", options)
163
193
  end
164
194
  end
165
-
166
- html.html_safe
167
195
  end
168
196
 
169
197
  def fieldset(legend, options = {}, &block)
@@ -187,11 +215,11 @@ module SwissKnife
187
215
  def submit_or_cancel(url, options = {})
188
216
  options.reverse_merge!(:button => :"swiss_knife.submit", :cancel => :"swiss_knife.cancel")
189
217
 
190
- String.new.tap do |html|
218
+ safe_buffer do |html|
191
219
  html << submit_tag(t(options[:button], :default => options[:button]), :class => "button")
192
220
  html << " "
193
221
  html << link_to(t(options[:cancel], :default => options[:cancel]), url, :class => "cancel")
194
- end.html_safe
222
+ end
195
223
  end
196
224
 
197
225
  # Load jQuery from Google's CDN and if the world is collapsing and Google is down,
@@ -205,16 +233,45 @@ module SwissKnife
205
233
  def jquery_script_tag(version = "1.4.4")
206
234
  local_path = compute_public_path("jquery-#{version}.min.js", "javascripts")
207
235
  remote_path = "#{request.protocol}ajax.googleapis.com/ajax/libs/jquery/#{version}/jquery.min.js"
208
- html = <<-HTML
209
- <script type="text/javascript" src="#{remote_path}"></script>
210
- <script type="text/javascript">
211
- if (typeof jQuery === "undefined") {
212
- document.write(unescape("%3Cscript src='#{local_path}' type='text/javascript'%3E%3C/script%3E"));
213
- };
214
- </script>
215
- HTML
216
-
217
- html.html_safe
236
+
237
+ safe_buffer do
238
+ %[
239
+ <script type="text/javascript" src="#{remote_path}"></script>
240
+ <script type="text/javascript">
241
+ if (typeof jQuery === "undefined") {
242
+ document.write(unescape("%3Cscript src='#{local_path}' type='text/javascript'%3E%3C/script%3E"));
243
+ };
244
+ </script>
245
+ ]
246
+ end
247
+ end
248
+
249
+ # Return a string marked as safe html.
250
+ #
251
+ # Change the buffer object when block expects one argument.
252
+ #
253
+ # safe_buffer {|html| html << "<p>Some content</p>"}
254
+ #
255
+ # Use block's return when block expects no argument.
256
+ #
257
+ # safe_buffer { "<p>Some content</p>" }
258
+ #
259
+ # Get a safe buffer object.
260
+ #
261
+ # buffer = safe_buffer
262
+ #
263
+ def safe_buffer(&block)
264
+ buffer = ActiveSupport::SafeBuffer.new
265
+
266
+ if block_given?
267
+ if block.arity == 1
268
+ yield buffer
269
+ else
270
+ buffer = yield(buffer)
271
+ end
272
+ end
273
+
274
+ buffer.html_safe
218
275
  end
219
276
  end
220
277
  end
@@ -2,7 +2,7 @@ module SwissKnife
2
2
  module Version
3
3
  MAJOR = 0
4
4
  MINOR = 1
5
- PATCH = 4
5
+ PATCH = 5
6
6
  STRING = "#{MAJOR}.#{MINOR}.#{PATCH}"
7
7
  end
8
8
  end
@@ -17,6 +17,8 @@ describe SwissKnife::Helpers do
17
17
 
18
18
  subject { helper.flash_messages }
19
19
 
20
+ it { should be_html_safe }
21
+
20
22
  it "should render multiple flash messages" do
21
23
  subject.should have_tag("p.message", :count => 3)
22
24
  end
@@ -36,41 +38,50 @@ describe SwissKnife::Helpers do
36
38
 
37
39
  describe "block wrappers" do
38
40
  context "body" do
39
- it "should use defaults" do
40
- html = helper.body { "Body" }
41
+ subject { helper.body { "Body" } }
41
42
 
42
- html.should have_tag("body", :count => 1)
43
- html.should have_tag("body#sample-page")
44
- html.should have_tag("body.sample-index")
45
- html.should have_tag("body.en")
43
+ it { should be_html_safe }
44
+
45
+ it "should use defaults" do
46
+ subject.should have_tag("body", :count => 1)
47
+ subject.should have_tag("body#sample-page")
48
+ subject.should have_tag("body.sample-index")
49
+ subject.should have_tag("body.en")
46
50
  end
47
51
 
48
- it "should use alias for action" do
52
+ it "should use alias for create action" do
49
53
  @controller.stub(:action_name => "create")
50
- helper.body { "Body" }.should have_tag("body.sample-new")
54
+ html = helper.body { "Body" }
55
+
56
+ html.should have_tag("body.sample-new")
57
+ html.should have_tag("body.sample-create")
58
+ end
51
59
 
60
+ it "should use alias for update action" do
52
61
  @controller.stub(:action_name => "update")
53
62
  helper.body { "Body" }.should have_tag("body.sample-edit")
63
+ end
54
64
 
65
+ it "should use alias for delete action" do
55
66
  @controller.stub(:action_name => "destroy")
56
67
  helper.body { "Body" }.should have_tag("body.sample-destroy")
57
68
  end
58
69
 
59
70
  it "should use custom settings" do
60
- html = helper.body(:id => "page", :class => "dark", :onload => "init();") { "Body" }
71
+ subject = helper.body(:id => "page", :class => "dark", :onload => "init();") { "Body" }
61
72
 
62
- html.should have_tag("body#page")
63
- html.should have_tag("body.dark")
64
- html.should have_tag("body[onload='init();']")
73
+ subject.should have_tag("body#page")
74
+ subject.should have_tag("body.dark")
75
+ subject.should have_tag("body[onload='init();']")
65
76
  end
66
77
 
67
78
  it "should append classes" do
68
- html = helper.body(:append_class => "more classes") { "Body" }
79
+ subject = helper.body(:append_class => "more classes") { "Body" }
69
80
 
70
- html.should have_tag("body.more")
71
- html.should have_tag("body.classes")
72
- html.should have_tag("body.en")
73
- html.should have_tag("body.sample-index")
81
+ subject.should have_tag("body.more")
82
+ subject.should have_tag("body.classes")
83
+ subject.should have_tag("body.en")
84
+ subject.should have_tag("body.sample-index")
74
85
  end
75
86
 
76
87
  it "should not have append_class attribute" do
@@ -107,18 +118,22 @@ describe SwissKnife::Helpers do
107
118
  end
108
119
 
109
120
  it "should use other options like css class" do
110
- html = helper.wrapper(:div, :id => "container", :class => "rounded") { "Some content" }
111
- html.should have_tag("div#container.rounded", "Some content")
121
+ subject = helper.wrapper(:div, :id => "container", :class => "rounded") { "Some content" }
122
+ subject.should have_tag("div#container.rounded", "Some content")
112
123
  end
113
124
  end
114
125
 
115
126
  describe "#dispatcher_tag" do
127
+ it "should be html safe" do
128
+ helper.dispatcher_tag.should be_html_safe
129
+ end
130
+
116
131
  it "should contain meta tag" do
117
132
  @controller.class.stub!(:name).and_return("SampleController")
118
133
 
119
- html = helper.dispatcher_tag
120
- html.should have_tag("meta")
121
- html.should have_tag("meta[name=page][content='sample#index']")
134
+ subject = helper.dispatcher_tag
135
+ subject.should have_tag("meta")
136
+ subject.should have_tag("meta[name=page][content='sample#index']")
122
137
  end
123
138
 
124
139
  it "should return normalized controller name for namespaced controller" do
@@ -130,6 +145,8 @@ describe SwissKnife::Helpers do
130
145
  describe "#mail_to" do
131
146
  subject { helper.mail_to("john@doe.com") }
132
147
 
148
+ it { should be_html_safe }
149
+
133
150
  it "should be encrypted" do
134
151
  subject.should_not match(/john@doe\.com/)
135
152
  end
@@ -152,99 +169,101 @@ describe SwissKnife::Helpers do
152
169
 
153
170
  describe "javascript includes" do
154
171
  it "should use defaults" do
155
- html = helper.javascript_includes("application")
172
+ subject = helper.javascript_includes("application")
156
173
 
157
- html.should have_tag("script[type='text/javascript']", :count => 1)
158
- html.should match(%r{/javascripts/application.js(\?\d+)?})
174
+ subject.should have_tag("script[type='text/javascript']", :count => 1)
175
+ subject.should match(%r{/javascripts/application.js(\?\d+)?})
159
176
  end
160
177
 
161
178
  it "should return several includes for bundle when not in production" do
162
179
  SwissKnife::Assets.stub(:merge? => false)
163
- html = helper.javascript_includes(:base)
180
+ subject = helper.javascript_includes(:base)
164
181
 
165
- html.should have_tag("script[type='text/javascript']", :count => 3)
166
- html.should match(%r{/javascripts/application.js(\?\d+)?})
167
- html.should match(%r{/javascripts/jquery.js(\?\d+)?})
168
- html.should match(%r{/javascripts/rails.js(\?\d+)?})
182
+ subject.should have_tag("script[type='text/javascript']", :count => 3)
183
+ subject.should match(%r{/javascripts/application.js(\?\d+)?})
184
+ subject.should match(%r{/javascripts/jquery.js(\?\d+)?})
185
+ subject.should match(%r{/javascripts/rails.js(\?\d+)?})
169
186
  end
170
187
 
171
188
  it "should return bundle only when in production" do
172
189
  SwissKnife::Assets.stub(:merge? => true)
173
- html = helper.javascript_includes(:base)
190
+ subject = helper.javascript_includes(:base)
174
191
 
175
- html.should have_tag("script[type='text/javascript']", :count => 1)
176
- html.should match(%r{/javascripts/base_packaged.js(\?\d+)?})
192
+ subject.should have_tag("script[type='text/javascript']", :count => 1)
193
+ subject.should match(%r{/javascripts/base_packaged.js(\?\d+)?})
177
194
  end
178
195
  end
179
196
 
180
197
  describe "stylesheet includes" do
181
198
  it "should use defaults" do
182
- html = helper.stylesheet_includes("application")
199
+ subject = helper.stylesheet_includes("application")
183
200
 
184
- html.should have_tag("link[rel='stylesheet']", :count => 1)
185
- html.should match(%r{/stylesheets/application.css(\?\d+)?})
201
+ subject.should have_tag("link[rel='stylesheet']", :count => 1)
202
+ subject.should match(%r{/stylesheets/application.css(\?\d+)?})
186
203
  end
187
204
 
188
205
  it "should return several includes for bundle when not in production" do
189
206
  SwissKnife::Assets.stub(:merge? => false)
190
- html = helper.stylesheet_includes(:base)
207
+ subject = helper.stylesheet_includes(:base)
191
208
 
192
- html.should have_tag("link[rel='stylesheet']", :count => 2)
193
- html.should match(%r{/stylesheets/reset.css(\?\d+)?})
194
- html.should match(%r{/stylesheets/main.css(\?\d+)?})
209
+ subject.should have_tag("link[rel='stylesheet']", :count => 2)
210
+ subject.should match(%r{/stylesheets/reset.css(\?\d+)?})
211
+ subject.should match(%r{/stylesheets/main.css(\?\d+)?})
195
212
  end
196
213
 
197
214
  it "should return only bundle when in production" do
198
215
  SwissKnife::Assets.stub(:merge? => true)
199
- html = helper.stylesheet_includes(:base)
216
+ subject = helper.stylesheet_includes(:base)
200
217
 
201
- html.should have_tag("link[rel='stylesheet']", :count => 1)
202
- html.should match(%r{/stylesheets/base_packaged.css(\?\d+)?})
218
+ subject.should have_tag("link[rel='stylesheet']", :count => 1)
219
+ subject.should match(%r{/stylesheets/base_packaged.css(\?\d+)?})
203
220
  end
204
221
  end
205
222
  end
206
223
 
207
224
  describe "#fieldset" do
208
- before do
209
- @html = helper.fieldset("Nice legend", :class => "sample") { "<p>Fieldset</p>" }
210
- end
225
+ subject { helper.fieldset("Nice legend", :class => "sample") { "<p>Fieldset</p>" } }
226
+
227
+ it { should be_html_safe }
211
228
 
212
229
  it "should use provided options" do
213
- @html.should have_tag("fieldset.sample", :count => 1)
230
+ subject.should have_tag("fieldset.sample", :count => 1)
214
231
  end
215
232
 
216
233
  it "should use legend as text" do
217
- @html.should have_tag("fieldset > legend", "Nice legend")
234
+ subject.should have_tag("fieldset > legend", "Nice legend")
218
235
  end
219
236
 
220
237
  it "should use translated legend" do
221
238
  I18n.locale = :pt
222
239
  I18n.backend.should_receive(:translations).and_return(:pt => {:sample => "Legenda"})
223
240
 
224
- @html = helper.fieldset("sample") {}
225
- @html.should have_tag("fieldset > legend", "Legenda")
241
+ subject = helper.fieldset("sample") {}
242
+ subject.should have_tag("fieldset > legend", "Legenda")
226
243
  end
227
244
 
228
245
  it "should return content" do
229
- @html.should have_tag("fieldset > p", "Fieldset")
246
+ subject.should have_tag("fieldset > p", "Fieldset")
230
247
  end
231
248
  end
232
249
 
233
250
  describe "#gravatar_tag" do
234
251
  before do
235
252
  ActionController::Base.asset_host = "http://example.com"
236
- @email = "john@doe.com"
237
253
  end
238
254
 
255
+ let(:email) { "john@doe.com" }
256
+ subject { helper.gravatar_tag(email) }
257
+
239
258
  it "should return an image" do
240
- helper.gravatar_tag(@email).should have_tag("img.gravatar", :count => 1)
259
+ subject.should have_tag("img.gravatar", :count => 1)
241
260
  end
242
261
 
243
262
  it "should use default options" do
244
- uri = uri_for(helper.gravatar_tag(@email))
263
+ uri = uri_for(subject)
245
264
  uri.scheme.should == "http"
246
265
  uri.host.should == "www.gravatar.com"
247
- uri.path.should == "/avatar/#{Digest::MD5.hexdigest(@email)}.jpg"
266
+ uri.path.should == "/avatar/#{Digest::MD5.hexdigest(email)}.jpg"
248
267
  uri.params.should == {"s" => "32", "r" => "g", "d" => "http://example.com/images/gravatar.jpg"}
249
268
  end
250
269
 
@@ -254,49 +273,49 @@ describe SwissKnife::Helpers do
254
273
  end
255
274
 
256
275
  it "should use custom default image" do
257
- uri = uri_for(helper.gravatar_tag(@email, :default => "custom.jpg"))
276
+ uri = uri_for(helper.gravatar_tag(email, :default => "custom.jpg"))
258
277
  uri.params["d"].should == "http://example.com/images/custom.jpg"
259
278
  end
260
279
 
261
280
  it "should use custom default image" do
262
- uri = uri_for(helper.gravatar_tag(@email, :default => "custom.jpg"))
281
+ uri = uri_for(helper.gravatar_tag(email, :default => "custom.jpg"))
263
282
  uri.params["d"].should == "http://example.com/images/custom.jpg"
264
283
  end
265
284
 
266
285
  it "should use predefined default image" do
267
- uri = uri_for(helper.gravatar_tag(@email, :default => :mm))
286
+ uri = uri_for(helper.gravatar_tag(email, :default => :mm))
268
287
  uri.params["d"].should == "mm"
269
288
  end
270
289
 
271
290
  it "should use custom size" do
272
- uri = uri_for(helper.gravatar_tag(@email, :size => 80))
291
+ uri = uri_for(helper.gravatar_tag(email, :size => 80))
273
292
  uri.params["s"].should == "80"
274
293
  end
275
294
 
276
295
  it "should use custom rating" do
277
- uri = uri_for(helper.gravatar_tag(@email, :rating => "pg"))
296
+ uri = uri_for(helper.gravatar_tag(email, :rating => "pg"))
278
297
  uri.params["r"].should == "pg"
279
298
  end
280
299
 
281
300
  it "should use secure host when in a secure request" do
282
301
  helper.request.should_receive(:ssl?).and_return(true)
283
- uri = uri_for(helper.gravatar_tag(@email))
302
+ uri = uri_for(helper.gravatar_tag(email))
284
303
  uri.host.should == "secure.gravatar.com"
285
304
  uri.scheme.should == "https"
286
305
  end
287
306
 
288
307
  it "should use secure host when using :ssl option" do
289
- uri = uri_for(helper.gravatar_tag(@email, :ssl => true))
308
+ uri = uri_for(helper.gravatar_tag(email, :ssl => true))
290
309
  uri.host.should == "secure.gravatar.com"
291
310
  uri.scheme.should == "https"
292
311
  end
293
312
 
294
313
  it "should use alt" do
295
- helper.gravatar_tag(@email, :alt => "alt text").should match(/\balt="alt text"/)
314
+ helper.gravatar_tag(email, :alt => "alt text").should match(/\balt="alt text"/)
296
315
  end
297
316
 
298
317
  it "should use title" do
299
- helper.gravatar_tag(@email, :title => "title text").should match(/\btitle="title text"/)
318
+ helper.gravatar_tag(email, :title => "title text").should match(/\btitle="title text"/)
300
319
  end
301
320
 
302
321
  private
@@ -315,54 +334,56 @@ describe SwissKnife::Helpers do
315
334
 
316
335
  describe "#submit_or_cancel" do
317
336
  it "should use defaults" do
318
- html = helper.submit_or_cancel("/some/path")
319
- html.should have_tag("a.cancel[href='/some/path']", :text => "Cancel")
320
- html.should have_tag("input.button[type=submit][value=Submit]")
337
+ subject = helper.submit_or_cancel("/some/path")
338
+ subject.should have_tag("a.cancel[href='/some/path']", :text => "Cancel")
339
+ subject.should have_tag("input.button[type=submit][value=Submit]")
321
340
  end
322
341
 
323
342
  it "should use custom link text from I18n file" do
324
343
  I18n.backend.stub :translations => {:en => {:go_back => "Go back"}}
325
- html = helper.submit_or_cancel("/some/path", :cancel => :go_back)
326
- html.should have_tag("a.cancel", :text => "Go back")
344
+ subject = helper.submit_or_cancel("/some/path", :cancel => :go_back)
345
+ subject.should have_tag("a.cancel", :text => "Go back")
327
346
  end
328
347
 
329
348
  it "should use custom link text" do
330
- html = helper.submit_or_cancel("/some/path", :cancel => "Go back")
331
- html.should have_tag("a.cancel", :text => "Go back")
349
+ subject = helper.submit_or_cancel("/some/path", :cancel => "Go back")
350
+ subject.should have_tag("a.cancel", :text => "Go back")
332
351
  end
333
352
 
334
353
  it "should use custom button text from I18n file" do
335
354
  I18n.backend.stub :translations => {:en => {:send => "Send"}}
336
- html = helper.submit_or_cancel("/some/path", :button => :send)
337
- html.should have_tag("input[type=submit][value=Send]")
355
+ subject = helper.submit_or_cancel("/some/path", :button => :send)
356
+ subject.should have_tag("input[type=submit][value=Send]")
338
357
  end
339
358
 
340
359
  it "should use custom button text" do
341
- html = helper.submit_or_cancel("/some/path", :button => "Send")
342
- html.should have_tag("input[type=submit][value=Send]")
360
+ subject = helper.submit_or_cancel("/some/path", :button => "Send")
361
+ subject.should have_tag("input[type=submit][value=Send]")
343
362
  end
344
363
  end
345
364
 
346
365
  describe "#jquery_script_tag" do
366
+ subject { helper.jquery_script_tag }
367
+
368
+ it { should be_html_safe }
369
+
347
370
  it "should default to version 1.4.4" do
348
- html = helper.jquery_script_tag("1.4.4")
349
- html.should have_tag("script[src='http://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js']")
371
+ subject.should have_tag("script[src='http://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js']")
350
372
  end
351
373
 
352
374
  it "should use specified version" do
353
- html = helper.jquery_script_tag("1.2.3")
354
- html.should have_tag("script[src='http://ajax.googleapis.com/ajax/libs/jquery/1.2.3/jquery.min.js']")
375
+ subject = helper.jquery_script_tag("1.2.3")
376
+ subject.should have_tag("script[src='http://ajax.googleapis.com/ajax/libs/jquery/1.2.3/jquery.min.js']")
355
377
  end
356
378
 
357
379
  it "should use https protocol" do
358
380
  helper.request.stub :protocol => "https://"
359
- html = helper.jquery_script_tag
360
- html.should have_tag("script[src='https://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js']")
381
+ subject = helper.jquery_script_tag
382
+ subject.should have_tag("script[src='https://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js']")
361
383
  end
362
384
 
363
385
  it "should set fallback path" do
364
- html = helper.jquery_script_tag
365
- html.should match(%r[src='http://example.com/javascripts/jquery-1.4.4.min.js'])
386
+ subject.should match(%r[src='http://example.com/javascripts/jquery-1.4.4.min.js'])
366
387
  end
367
388
  end
368
389
  end
data/spec/spec_helper.rb CHANGED
@@ -5,6 +5,8 @@ require "swiss_knife"
5
5
  require File.dirname(__FILE__) + "/support/config/boot"
6
6
  require "rspec/rails"
7
7
  require "swiss_knife/rspec"
8
+ require "fakeweb"
9
+ require "nokogiri"
8
10
 
9
11
  FakeWeb.allow_net_connect = false
10
12
 
data/swiss_knife.gemspec CHANGED
@@ -18,6 +18,8 @@ Gem::Specification.new do |s|
18
18
  s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
19
19
  s.require_paths = ["lib"]
20
20
 
21
- s.add_dependency "rails", ">= 3.0.0"
22
- s.add_development_dependency "rspec", ">= 2.0.0"
21
+ s.add_dependency "rails", "~> 3.0.0"
22
+ s.add_development_dependency "rspec-rails", "~> 2.5.0"
23
+ s.add_development_dependency "nokogiri"
24
+ s.add_development_dependency "fakeweb"
23
25
  end
metadata CHANGED
@@ -1,12 +1,8 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: swiss_knife
3
3
  version: !ruby/object:Gem::Version
4
- prerelease: false
5
- segments:
6
- - 0
7
- - 1
8
- - 4
9
- version: 0.1.4
4
+ prerelease:
5
+ version: 0.1.5
10
6
  platform: ruby
11
7
  authors:
12
8
  - Nando Vieira
@@ -14,7 +10,7 @@ autorequire:
14
10
  bindir: bin
15
11
  cert_chain: []
16
12
 
17
- date: 2010-11-27 00:00:00 -02:00
13
+ date: 2011-03-14 00:00:00 -03:00
18
14
  default_executable:
19
15
  dependencies:
20
16
  - !ruby/object:Gem::Dependency
@@ -23,30 +19,44 @@ dependencies:
23
19
  requirement: &id001 !ruby/object:Gem::Requirement
24
20
  none: false
25
21
  requirements:
26
- - - ">="
22
+ - - ~>
27
23
  - !ruby/object:Gem::Version
28
- segments:
29
- - 3
30
- - 0
31
- - 0
32
24
  version: 3.0.0
33
25
  type: :runtime
34
26
  version_requirements: *id001
35
27
  - !ruby/object:Gem::Dependency
36
- name: rspec
28
+ name: rspec-rails
37
29
  prerelease: false
38
30
  requirement: &id002 !ruby/object:Gem::Requirement
39
31
  none: false
40
32
  requirements:
41
- - - ">="
33
+ - - ~>
42
34
  - !ruby/object:Gem::Version
43
- segments:
44
- - 2
45
- - 0
46
- - 0
47
- version: 2.0.0
35
+ version: 2.5.0
48
36
  type: :development
49
37
  version_requirements: *id002
38
+ - !ruby/object:Gem::Dependency
39
+ name: nokogiri
40
+ prerelease: false
41
+ requirement: &id003 !ruby/object:Gem::Requirement
42
+ none: false
43
+ requirements:
44
+ - - ">="
45
+ - !ruby/object:Gem::Version
46
+ version: "0"
47
+ type: :development
48
+ version_requirements: *id003
49
+ - !ruby/object:Gem::Dependency
50
+ name: fakeweb
51
+ prerelease: false
52
+ requirement: &id004 !ruby/object:Gem::Requirement
53
+ none: false
54
+ requirements:
55
+ - - ">="
56
+ - !ruby/object:Gem::Version
57
+ version: "0"
58
+ type: :development
59
+ version_requirements: *id004
50
60
  description: Several helpers for Rails 3
51
61
  email:
52
62
  - fnando.vieira@gmail.com
@@ -58,6 +68,7 @@ extra_rdoc_files: []
58
68
 
59
69
  files:
60
70
  - .gitignore
71
+ - .rspec
61
72
  - Gemfile
62
73
  - Gemfile.lock
63
74
  - README.rdoc
@@ -126,21 +137,17 @@ required_ruby_version: !ruby/object:Gem::Requirement
126
137
  requirements:
127
138
  - - ">="
128
139
  - !ruby/object:Gem::Version
129
- segments:
130
- - 0
131
140
  version: "0"
132
141
  required_rubygems_version: !ruby/object:Gem::Requirement
133
142
  none: false
134
143
  requirements:
135
144
  - - ">="
136
145
  - !ruby/object:Gem::Version
137
- segments:
138
- - 0
139
146
  version: "0"
140
147
  requirements: []
141
148
 
142
149
  rubyforge_project:
143
- rubygems_version: 1.3.7
150
+ rubygems_version: 1.6.0
144
151
  signing_key:
145
152
  specification_version: 3
146
153
  summary: Several helpers for Rails 3