swiss_knife 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (47) hide show
  1. data/Gemfile +9 -0
  2. data/Gemfile.lock +102 -0
  3. data/README.rdoc +183 -0
  4. data/Rakefile +26 -0
  5. data/lib/swiss_knife/action_controller.rb +43 -0
  6. data/lib/swiss_knife/assets.rb +81 -0
  7. data/lib/swiss_knife/dispatcher_js.rb +14 -0
  8. data/lib/swiss_knife/helpers.rb +178 -0
  9. data/lib/swiss_knife/i18n_js.rb +14 -0
  10. data/lib/swiss_knife/jquery.rb +14 -0
  11. data/lib/swiss_knife/jquery_ujs.rb +14 -0
  12. data/lib/swiss_knife/jsmin.rb +205 -0
  13. data/lib/swiss_knife/modernizr.rb +14 -0
  14. data/lib/swiss_knife/railtie.rb +19 -0
  15. data/lib/swiss_knife/rake_tasks.rb +31 -0
  16. data/lib/swiss_knife/rspec/have_tag.rb +115 -0
  17. data/lib/swiss_knife/rspec.rb +3 -0
  18. data/lib/swiss_knife/support/remote_file.rb +11 -0
  19. data/lib/swiss_knife/version.rb +8 -0
  20. data/lib/swiss_knife.rb +12 -0
  21. data/spec/controllers/application_controller_spec.rb +52 -0
  22. data/spec/helpers/helpers_spec.rb +314 -0
  23. data/spec/resources/assets/javascripts/application.js +1 -0
  24. data/spec/resources/assets/javascripts/jquery.js +1 -0
  25. data/spec/resources/assets/javascripts/rails.js +1 -0
  26. data/spec/resources/assets/stylesheets/main.css +1 -0
  27. data/spec/resources/assets/stylesheets/reset.css +1 -0
  28. data/spec/resources/assets/stylesheets/shared.css +1 -0
  29. data/spec/resources/assets.yml +16 -0
  30. data/spec/resources/stylesheets/_shared.less +1 -0
  31. data/spec/resources/stylesheets/main.less +3 -0
  32. data/spec/resources/stylesheets/reset.css +1 -0
  33. data/spec/resources/stylesheets/ui/tab.css +1 -0
  34. data/spec/resources/stylesheets/ui/window.less +1 -0
  35. data/spec/spec_helper.rb +25 -0
  36. data/spec/support/app/controllers/application_controller.rb +2 -0
  37. data/spec/support/config/boot.rb +20 -0
  38. data/spec/support/config/locales/en.yml +7 -0
  39. data/spec/support/log/test.log +8 -0
  40. data/spec/support/rspec/remote_file_shared.rb +21 -0
  41. data/spec/swiss_knife/assets_spec.rb +73 -0
  42. data/spec/swiss_knife/dispatcher_js_spec.rb +8 -0
  43. data/spec/swiss_knife/i18n_js_spec.rb +8 -0
  44. data/spec/swiss_knife/jquery_spec.rb +8 -0
  45. data/spec/swiss_knife/jquery_ujs_spec.rb +8 -0
  46. data/spec/swiss_knife/modernizr_spec.rb +8 -0
  47. metadata +149 -0
@@ -0,0 +1,14 @@
1
+ module SwissKnife
2
+ module DispatcherJs
3
+ extend self
4
+ extend Support::RemoteFile
5
+
6
+ def file
7
+ "javascripts/dispatcher.js"
8
+ end
9
+
10
+ def url
11
+ "http://github.com/fnando/dispatcher-js/raw/master/dispatcher.js"
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,178 @@
1
+ module SwissKnife
2
+ module Helpers
3
+ ACTION_ALIASES = {
4
+ "create" => "new",
5
+ "update" => "edit",
6
+ "remove" => "destroy"
7
+ }
8
+
9
+ DEFAULT_GRAVATARS = [:mm, :identicon, :monsterid, :wavatar, :retro, 404]
10
+ GRAVATAR_URLS = {
11
+ :http => "http://www.gravatar.com/avatar/",
12
+ :https => "https://secure.gravatar.com/avatar/"
13
+ }
14
+
15
+ # Display gravatar images.
16
+ #
17
+ # <%= gravatar "cb5d9e9095cd41b636764a85e57ade4b" %>
18
+ # <%= gravatar user.email, :alt => user.name, :size => 80 %>
19
+ # <%= gravatar user.email, :title => user.name %>
20
+ # <%= gravatar user.email, :rating => :g %>
21
+ # <%= gravatar user.email, :default => :mm %>
22
+ # <%= gravatar user.email, :default => "gravatar.jpg" %>
23
+ # <%= gravatar user.email, :ssl => true %>
24
+ #
25
+ #
26
+ def gravatar_tag(email, options = {})
27
+ email = Digest::MD5.hexdigest(email) unless email =~ /^[a-z0-9]{32}$/i
28
+ options.reverse_merge!(
29
+ :rating => :g,
30
+ :default => "gravatar.jpg",
31
+ :ssl => request.ssl?,
32
+ :size => 32
33
+ )
34
+
35
+ params = {
36
+ :r => options[:rating],
37
+ :d => DEFAULT_GRAVATARS.include?(options[:default]) ?
38
+ options[:default] : compute_public_path(options[:default], "images", nil, true),
39
+ :s => options[:size]
40
+ }
41
+
42
+ url = String.new.tap do |s|
43
+ s << (options[:ssl] ? GRAVATAR_URLS[:https] : GRAVATAR_URLS[:http])
44
+ s << email.to_s << ".jpg"
45
+ s << "?"
46
+ s << params.collect {|k, v| v.to_s.to_query(k) }.join("&")
47
+ end
48
+
49
+ image_tag url, { :class => "gravatar", :alt => options[:alt], :title => options[:title] }
50
+ end
51
+
52
+ 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
+ end
59
+
60
+ html
61
+ end
62
+
63
+ def dispatcher_tag
64
+ controller_name = controller.class.name.underscore
65
+ controller_name.gsub!(/\//, "_")
66
+ controller_name.gsub!(/_controller$/, "")
67
+
68
+ %[<meta name="page" content="#{controller_name}##{controller.action_name}" />].html_safe
69
+ end
70
+
71
+ def body(options = {}, &block)
72
+ action_name = ACTION_ALIASES[controller.action_name] || controller.action_name
73
+
74
+ options = {
75
+ :id => "#{controller.controller_name}-page",
76
+ :class => "#{controller.controller_name}-#{action_name} #{I18n.locale}"
77
+ }.merge(options)
78
+
79
+ options[:class] << (" " + options.delete(:append_class).to_s) if options[:append_class]
80
+
81
+ content_tag(:body, capture(&block), options).html_safe
82
+ end
83
+
84
+ def page(options = {}, &block)
85
+ wrapper(:div, options.merge(:id => "page"), &block)
86
+ end
87
+
88
+ def main(options = {}, &block)
89
+ wrapper(:div, options.merge(:id => "main"), &block)
90
+ end
91
+
92
+ def sidebar(options = {}, &block)
93
+ wrapper(:sidebar, options, &block)
94
+ end
95
+
96
+ def header(options = {}, &block)
97
+ wrapper(:header, options, &block)
98
+ end
99
+
100
+ def footer(options = {}, &block)
101
+ wrapper(:footer, options, &block)
102
+ end
103
+
104
+ def article(options = {}, &block)
105
+ wrapper(:article, options, &block)
106
+ end
107
+
108
+ def section(options = {}, &block)
109
+ wrapper(:section, options, &block)
110
+ end
111
+
112
+ def wrapper(tag, options = {}, &block)
113
+ content_tag(tag, capture(&block), options)
114
+ end
115
+
116
+ def mail_to(email, label = nil)
117
+ encrypt = proc do |text|
118
+ text.to_enum(:each_byte).collect {|c| sprintf("&#%d;", c) }.join
119
+ end
120
+
121
+ url = encrypt.call("mailto:#{email}").html_safe
122
+ label ||= encrypt.call(email).html_safe
123
+
124
+ link_to(label, url)
125
+ end
126
+
127
+ def javascript_includes(*args)
128
+ options = args.extract_options!
129
+ html = ""
130
+
131
+ args.each do |name|
132
+ bundle = SwissKnife::Assets.config["javascripts"][name.to_s] rescue nil
133
+
134
+ if SwissKnife::Assets.merge? && bundle
135
+ html << javascript_include_tag("#{name}_packaged", options)
136
+ elsif bundle
137
+ bundle.each do |file|
138
+ html << javascript_include_tag("#{file}", options)
139
+ end
140
+ else
141
+ html << javascript_include_tag("#{name}", options)
142
+ end
143
+ end
144
+ html
145
+ end
146
+
147
+ def stylesheet_includes(*args)
148
+ options = args.extract_options!
149
+ html = ""
150
+
151
+ args.each do |name|
152
+ bundle = SwissKnife::Assets.config["stylesheets"][name.to_s] rescue nil
153
+
154
+ if SwissKnife::Assets.merge? && bundle
155
+ html << stylesheet_link_tag("#{name}_packaged", options)
156
+ elsif bundle
157
+ bundle.each do |file|
158
+ html << stylesheet_link_tag("#{file}", options)
159
+ end
160
+ else
161
+ html << stylesheet_link_tag("#{name}", options)
162
+ end
163
+ end
164
+
165
+ html
166
+ end
167
+
168
+ def fieldset(legend, options = {}, &block)
169
+ legend = t(legend, :default => legend.to_s)
170
+
171
+ content_tag(:fieldset, options) do
172
+ body = content_tag(:legend, legend)
173
+ body << yield.to_s.html_safe
174
+ body
175
+ end
176
+ end
177
+ end
178
+ end
@@ -0,0 +1,14 @@
1
+ module SwissKnife
2
+ module I18nJs
3
+ extend self
4
+ extend Support::RemoteFile
5
+
6
+ def file
7
+ "javascripts/i18n.js"
8
+ end
9
+
10
+ def url
11
+ "http://github.com/fnando/i18n-js/raw/master/source/i18n.js"
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,14 @@
1
+ module SwissKnife
2
+ module Jquery
3
+ extend self
4
+ extend Support::RemoteFile
5
+
6
+ def file
7
+ "javascripts/jquery.js"
8
+ end
9
+
10
+ def url
11
+ "http://code.jquery.com/jquery-latest.min.js"
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,14 @@
1
+ module SwissKnife
2
+ module JqueryUjs
3
+ extend self
4
+ extend Support::RemoteFile
5
+
6
+ def file
7
+ "javascripts/rails.js"
8
+ end
9
+
10
+ def url
11
+ "http://github.com/rails/jquery-ujs/raw/master/src/rails.js"
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,205 @@
1
+ #!/usr/bin/ruby
2
+ # jsmin.rb 2007-07-20
3
+ # Author: Uladzislau Latynski
4
+ # This work is a translation from C to Ruby of jsmin.c published by
5
+ # Douglas Crockford. Permission is hereby granted to use the Ruby
6
+ # version under the same conditions as the jsmin.c on which it is
7
+ # based.
8
+ #
9
+ # /* jsmin.c
10
+ # 2003-04-21
11
+ #
12
+ # Copyright (c) 2002 Douglas Crockford (www.crockford.com)
13
+ #
14
+ # Permission is hereby granted, free of charge, to any person obtaining a copy of
15
+ # this software and associated documentation files (the "Software"), to deal in
16
+ # the Software without restriction, including without limitation the rights to
17
+ # use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
18
+ # of the Software, and to permit persons to whom the Software is furnished to do
19
+ # so, subject to the following conditions:
20
+ #
21
+ # The above copyright notice and this permission notice shall be included in all
22
+ # copies or substantial portions of the Software.
23
+ #
24
+ # The Software shall be used for Good, not Evil.
25
+ #
26
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
27
+ # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
28
+ # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
29
+ # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
30
+ # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
31
+ # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
32
+ # SOFTWARE.
33
+
34
+ EOF = -1
35
+ $theA = ""
36
+ $theB = ""
37
+
38
+ # isAlphanum -- return true if the character is a letter, digit, underscore,
39
+ # dollar sign, or non-ASCII character
40
+ def isAlphanum(c)
41
+ return false if !c || c == EOF
42
+ return ((c >= 'a' && c <= 'z') || (c >= '0' && c <= '9') ||
43
+ (c >= 'A' && c <= 'Z') || c == '_' || c == '$' ||
44
+ c == '\\' || (c[0].class == String ? c[0].ord : c[0]) > 126)
45
+ end
46
+
47
+ # get -- return the next character from stdin. Watch out for lookahead. If
48
+ # the character is a control character, translate it to a space or linefeed.
49
+ def get()
50
+ c = $stdin.getc
51
+ return EOF if(!c)
52
+ c = c.chr
53
+ return c if (c >= " " || c == "\n" || c.unpack("c") == EOF)
54
+ return "\n" if (c == "\r")
55
+ return " "
56
+ end
57
+
58
+ # Get the next character without getting it.
59
+ def peek()
60
+ lookaheadChar = $stdin.getc
61
+ $stdin.ungetc(lookaheadChar)
62
+ return lookaheadChar.chr
63
+ end
64
+
65
+ # mynext -- get the next character, excluding comments.
66
+ # peek() is used to see if a '/' is followed by a '/' or '*'.
67
+ def mynext()
68
+ c = get
69
+ if (c == "/")
70
+ if(peek == "/")
71
+ while(true)
72
+ c = get
73
+ if (c <= "\n")
74
+ return c
75
+ end
76
+ end
77
+ end
78
+ if(peek == "*")
79
+ get
80
+ while(true)
81
+ case get
82
+ when "*"
83
+ if (peek == "/")
84
+ get
85
+ return " "
86
+ end
87
+ when EOF
88
+ raise "Unterminated comment"
89
+ end
90
+ end
91
+ end
92
+ end
93
+ return c
94
+ end
95
+
96
+
97
+ # action -- do something! What you do is determined by the argument: 1
98
+ # Output A. Copy B to A. Get the next B. 2 Copy B to A. Get the next B.
99
+ # (Delete A). 3 Get the next B. (Delete B). action treats a string as a
100
+ # single character. Wow! action recognizes a regular expression if it is
101
+ # preceded by ( or , or =.
102
+ def action(a)
103
+ if(a==1)
104
+ $stdout.write $theA
105
+ end
106
+ if(a==1 || a==2)
107
+ $theA = $theB
108
+ if ($theA == "\'" || $theA == "\"")
109
+ while (true)
110
+ $stdout.write $theA
111
+ $theA = get
112
+ break if ($theA == $theB)
113
+ raise "Unterminated string literal" if ($theA <= "\n")
114
+ if ($theA == "\\")
115
+ $stdout.write $theA
116
+ $theA = get
117
+ end
118
+ end
119
+ end
120
+ end
121
+ if(a==1 || a==2 || a==3)
122
+ $theB = mynext
123
+ if ($theB == "/" && ($theA == "(" || $theA == "," || $theA == "=" ||
124
+ $theA == ":" || $theA == "[" || $theA == "!" ||
125
+ $theA == "&" || $theA == "|" || $theA == "?" ||
126
+ $theA == "{" || $theA == "}" || $theA == ";" ||
127
+ $theA == "\n"))
128
+ $stdout.write $theA
129
+ $stdout.write $theB
130
+ while (true)
131
+ $theA = get
132
+ if ($theA == "/")
133
+ break
134
+ elsif ($theA == "\\")
135
+ $stdout.write $theA
136
+ $theA = get
137
+ elsif ($theA <= "\n")
138
+ raise "Unterminated RegExp Literal"
139
+ end
140
+ $stdout.write $theA
141
+ end
142
+ $theB = mynext
143
+ end
144
+ end
145
+ end
146
+
147
+ # jsmin -- Copy the input to the output, deleting the characters which are
148
+ # insignificant to JavaScript. Comments will be removed. Tabs will be
149
+ # replaced with spaces. Carriage returns will be replaced with linefeeds.
150
+ # Most spaces and linefeeds will be removed.
151
+ def jsmin
152
+ $theA = "\n"
153
+ action(3)
154
+ while ($theA != EOF)
155
+ case $theA
156
+ when " "
157
+ if (isAlphanum($theB))
158
+ action(1)
159
+ else
160
+ action(2)
161
+ end
162
+ when "\n"
163
+ case ($theB)
164
+ when "{","[","(","+","-"
165
+ action(1)
166
+ when " "
167
+ action(3)
168
+ else
169
+ if (isAlphanum($theB))
170
+ action(1)
171
+ else
172
+ action(2)
173
+ end
174
+ end
175
+ else
176
+ case ($theB)
177
+ when " "
178
+ if (isAlphanum($theA))
179
+ action(1)
180
+ else
181
+ action(3)
182
+ end
183
+ when "\n"
184
+ case ($theA)
185
+ when "}","]",")","+","-","\"","\\", "'", '"'
186
+ action(1)
187
+ else
188
+ if (isAlphanum($theA))
189
+ action(1)
190
+ else
191
+ action(3)
192
+ end
193
+ end
194
+ else
195
+ action(1)
196
+ end
197
+ end
198
+ end
199
+ end
200
+
201
+ ARGV.each do |anArg|
202
+ $stdout.write "// #{anArg}\n"
203
+ end
204
+
205
+ jsmin
@@ -0,0 +1,14 @@
1
+ module SwissKnife
2
+ module Modernizr
3
+ extend self
4
+ extend Support::RemoteFile
5
+
6
+ def file
7
+ "javascripts/modernizr.js"
8
+ end
9
+
10
+ def url
11
+ "http://github.com/Modernizr/Modernizr/raw/master/modernizr.js"
12
+ end
13
+ end
14
+ end