swiss_knife 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/Gemfile +9 -0
- data/Gemfile.lock +102 -0
- data/README.rdoc +183 -0
- data/Rakefile +26 -0
- data/lib/swiss_knife/action_controller.rb +43 -0
- data/lib/swiss_knife/assets.rb +81 -0
- data/lib/swiss_knife/dispatcher_js.rb +14 -0
- data/lib/swiss_knife/helpers.rb +178 -0
- data/lib/swiss_knife/i18n_js.rb +14 -0
- data/lib/swiss_knife/jquery.rb +14 -0
- data/lib/swiss_knife/jquery_ujs.rb +14 -0
- data/lib/swiss_knife/jsmin.rb +205 -0
- data/lib/swiss_knife/modernizr.rb +14 -0
- data/lib/swiss_knife/railtie.rb +19 -0
- data/lib/swiss_knife/rake_tasks.rb +31 -0
- data/lib/swiss_knife/rspec/have_tag.rb +115 -0
- data/lib/swiss_knife/rspec.rb +3 -0
- data/lib/swiss_knife/support/remote_file.rb +11 -0
- data/lib/swiss_knife/version.rb +8 -0
- data/lib/swiss_knife.rb +12 -0
- data/spec/controllers/application_controller_spec.rb +52 -0
- data/spec/helpers/helpers_spec.rb +314 -0
- data/spec/resources/assets/javascripts/application.js +1 -0
- data/spec/resources/assets/javascripts/jquery.js +1 -0
- data/spec/resources/assets/javascripts/rails.js +1 -0
- data/spec/resources/assets/stylesheets/main.css +1 -0
- data/spec/resources/assets/stylesheets/reset.css +1 -0
- data/spec/resources/assets/stylesheets/shared.css +1 -0
- data/spec/resources/assets.yml +16 -0
- data/spec/resources/stylesheets/_shared.less +1 -0
- data/spec/resources/stylesheets/main.less +3 -0
- data/spec/resources/stylesheets/reset.css +1 -0
- data/spec/resources/stylesheets/ui/tab.css +1 -0
- data/spec/resources/stylesheets/ui/window.less +1 -0
- data/spec/spec_helper.rb +25 -0
- data/spec/support/app/controllers/application_controller.rb +2 -0
- data/spec/support/config/boot.rb +20 -0
- data/spec/support/config/locales/en.yml +7 -0
- data/spec/support/log/test.log +8 -0
- data/spec/support/rspec/remote_file_shared.rb +21 -0
- data/spec/swiss_knife/assets_spec.rb +73 -0
- data/spec/swiss_knife/dispatcher_js_spec.rb +8 -0
- data/spec/swiss_knife/i18n_js_spec.rb +8 -0
- data/spec/swiss_knife/jquery_spec.rb +8 -0
- data/spec/swiss_knife/jquery_ujs_spec.rb +8 -0
- data/spec/swiss_knife/modernizr_spec.rb +8 -0
- metadata +149 -0
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,102 @@
|
|
1
|
+
GEM
|
2
|
+
remote: http://rubygems.org/
|
3
|
+
specs:
|
4
|
+
abstract (1.0.0)
|
5
|
+
actionmailer (3.0.0)
|
6
|
+
actionpack (= 3.0.0)
|
7
|
+
mail (~> 2.2.5)
|
8
|
+
actionpack (3.0.0)
|
9
|
+
activemodel (= 3.0.0)
|
10
|
+
activesupport (= 3.0.0)
|
11
|
+
builder (~> 2.1.2)
|
12
|
+
erubis (~> 2.6.6)
|
13
|
+
i18n (~> 0.4.1)
|
14
|
+
rack (~> 1.2.1)
|
15
|
+
rack-mount (~> 0.6.12)
|
16
|
+
rack-test (~> 0.5.4)
|
17
|
+
tzinfo (~> 0.3.23)
|
18
|
+
activemodel (3.0.0)
|
19
|
+
activesupport (= 3.0.0)
|
20
|
+
builder (~> 2.1.2)
|
21
|
+
i18n (~> 0.4.1)
|
22
|
+
activerecord (3.0.0)
|
23
|
+
activemodel (= 3.0.0)
|
24
|
+
activesupport (= 3.0.0)
|
25
|
+
arel (~> 1.0.0)
|
26
|
+
tzinfo (~> 0.3.23)
|
27
|
+
activeresource (3.0.0)
|
28
|
+
activemodel (= 3.0.0)
|
29
|
+
activesupport (= 3.0.0)
|
30
|
+
activesupport (3.0.0)
|
31
|
+
archive-tar-minitar (0.5.2)
|
32
|
+
arel (1.0.1)
|
33
|
+
activesupport (~> 3.0.0)
|
34
|
+
builder (2.1.2)
|
35
|
+
columnize (0.3.1)
|
36
|
+
diff-lcs (1.1.2)
|
37
|
+
erubis (2.6.6)
|
38
|
+
abstract (>= 1.0.0)
|
39
|
+
i18n (0.4.1)
|
40
|
+
linecache19 (0.5.11)
|
41
|
+
ruby_core_source (>= 0.1.4)
|
42
|
+
mail (2.2.6.1)
|
43
|
+
activesupport (>= 2.3.6)
|
44
|
+
mime-types
|
45
|
+
treetop (>= 1.4.5)
|
46
|
+
mime-types (1.16)
|
47
|
+
nokogiri (1.4.3.1)
|
48
|
+
polyglot (0.3.1)
|
49
|
+
rack (1.2.1)
|
50
|
+
rack-mount (0.6.13)
|
51
|
+
rack (>= 1.0.0)
|
52
|
+
rack-test (0.5.6)
|
53
|
+
rack (>= 1.0)
|
54
|
+
rails (3.0.0)
|
55
|
+
actionmailer (= 3.0.0)
|
56
|
+
actionpack (= 3.0.0)
|
57
|
+
activerecord (= 3.0.0)
|
58
|
+
activeresource (= 3.0.0)
|
59
|
+
activesupport (= 3.0.0)
|
60
|
+
bundler (~> 1.0.0)
|
61
|
+
railties (= 3.0.0)
|
62
|
+
railties (3.0.0)
|
63
|
+
actionpack (= 3.0.0)
|
64
|
+
activesupport (= 3.0.0)
|
65
|
+
rake (>= 0.8.4)
|
66
|
+
thor (~> 0.14.0)
|
67
|
+
rake (0.8.7)
|
68
|
+
rspec (2.0.0)
|
69
|
+
rspec-core (= 2.0.0)
|
70
|
+
rspec-expectations (= 2.0.0)
|
71
|
+
rspec-mocks (= 2.0.0)
|
72
|
+
rspec-core (2.0.0)
|
73
|
+
rspec-expectations (2.0.0)
|
74
|
+
diff-lcs (>= 1.1.2)
|
75
|
+
rspec-mocks (2.0.0)
|
76
|
+
rspec-core (= 2.0.0)
|
77
|
+
rspec-expectations (= 2.0.0)
|
78
|
+
rspec-rails (2.0.0)
|
79
|
+
rspec (= 2.0.0)
|
80
|
+
ruby-debug-base19 (0.11.24)
|
81
|
+
columnize (>= 0.3.1)
|
82
|
+
linecache19 (>= 0.5.11)
|
83
|
+
ruby_core_source (>= 0.1.4)
|
84
|
+
ruby-debug19 (0.11.6)
|
85
|
+
columnize (>= 0.3.1)
|
86
|
+
linecache19 (>= 0.5.11)
|
87
|
+
ruby-debug-base19 (>= 0.11.19)
|
88
|
+
ruby_core_source (0.1.4)
|
89
|
+
archive-tar-minitar (>= 0.5.2)
|
90
|
+
thor (0.14.3)
|
91
|
+
treetop (1.4.8)
|
92
|
+
polyglot (>= 0.3.1)
|
93
|
+
tzinfo (0.3.23)
|
94
|
+
|
95
|
+
PLATFORMS
|
96
|
+
ruby
|
97
|
+
|
98
|
+
DEPENDENCIES
|
99
|
+
nokogiri
|
100
|
+
rails (= 3.0.0)
|
101
|
+
rspec-rails (= 2.0.0)
|
102
|
+
ruby-debug19
|
data/README.rdoc
ADDED
@@ -0,0 +1,183 @@
|
|
1
|
+
= Swiss Knife
|
2
|
+
|
3
|
+
Lots of Rails 3 helpers that I use on my own projects.
|
4
|
+
|
5
|
+
== Usage
|
6
|
+
|
7
|
+
=== dispatcher_tag
|
8
|
+
|
9
|
+
If you're using http://github.com/fnando/dispatcher-js, this helper will create the meta tag.
|
10
|
+
|
11
|
+
<%= dispatcher_tag %>
|
12
|
+
|
13
|
+
=== body(&block)
|
14
|
+
|
15
|
+
Create the <tt>body</tt> tag setting some attributes:
|
16
|
+
|
17
|
+
<%= body do %>
|
18
|
+
<% end %>
|
19
|
+
|
20
|
+
Will be converted to
|
21
|
+
|
22
|
+
<body id="sample-page" class="sample-index en">
|
23
|
+
</body>
|
24
|
+
|
25
|
+
Set any attribute by providing a hash:
|
26
|
+
|
27
|
+
<%= body :id => "foo", :class => "bar", :onload => "init();" do %>
|
28
|
+
<% end %>
|
29
|
+
|
30
|
+
If you just want to append more classes, use the options <tt>:append_class</tt>:
|
31
|
+
|
32
|
+
<%= body :append_class => do %>
|
33
|
+
<% end %>
|
34
|
+
|
35
|
+
=== flash_messages
|
36
|
+
|
37
|
+
Display all flash messages in <tt><p class="message #{type}"></p></tt> tags.
|
38
|
+
So if you set messages like
|
39
|
+
|
40
|
+
flash[:notice] = "Notice"
|
41
|
+
|
42
|
+
you can add this to your view
|
43
|
+
|
44
|
+
<%= flash_messages %>
|
45
|
+
|
46
|
+
and the helper will output
|
47
|
+
|
48
|
+
<p class="message notice">Notice</p>
|
49
|
+
|
50
|
+
=== Block wrappers
|
51
|
+
|
52
|
+
Just hiding some HTML.
|
53
|
+
|
54
|
+
<%= main do %>
|
55
|
+
<!-- Wrap the content into a div#main tag -->
|
56
|
+
<% end %>
|
57
|
+
|
58
|
+
<%= sidebar do %>
|
59
|
+
<!-- Wrap the content into a sidebar tag -->
|
60
|
+
<% end %>
|
61
|
+
|
62
|
+
There's also <tt>page</tt>, <tt>section</tt>, <tt>article</tt>, <tt>header</tt>, and <tt>footer</tt> wrappers. You can set other attributes, like CSS classes:
|
63
|
+
|
64
|
+
<%= main :class => "rounded" do %>
|
65
|
+
<% end %>
|
66
|
+
|
67
|
+
=== Assets (JavaScripts & Stylesheets)
|
68
|
+
|
69
|
+
There are two helpers that merge files like Assets Packager.
|
70
|
+
|
71
|
+
<%= javascript_includes :base %>
|
72
|
+
<%= stylesheet_includes :base %>
|
73
|
+
|
74
|
+
You need to create a <tt>config/assets.yml</tt> file like this:
|
75
|
+
|
76
|
+
javascripts:
|
77
|
+
base:
|
78
|
+
- jquery
|
79
|
+
- rails
|
80
|
+
- application
|
81
|
+
stylesheets:
|
82
|
+
base:
|
83
|
+
- reset
|
84
|
+
|
85
|
+
To merge files, you have to run a rake task:
|
86
|
+
|
87
|
+
$ rake assets:merge
|
88
|
+
|
89
|
+
=== fieldset
|
90
|
+
|
91
|
+
A fieldset helper with I18n support.
|
92
|
+
|
93
|
+
<%= fieldset "labels.user.create" do %>
|
94
|
+
<!-- Some html -->
|
95
|
+
<% end %>
|
96
|
+
|
97
|
+
=== page_title
|
98
|
+
|
99
|
+
Set page title right from I18n files.
|
100
|
+
|
101
|
+
en:
|
102
|
+
titles:
|
103
|
+
users:
|
104
|
+
new: "Sign up"
|
105
|
+
show: "%{name}'s Profile"
|
106
|
+
|
107
|
+
You can set interpolation options:
|
108
|
+
|
109
|
+
page_title :name => @user.name
|
110
|
+
|
111
|
+
To display page title:
|
112
|
+
|
113
|
+
<%= page_title %>
|
114
|
+
|
115
|
+
The are some action aliases:
|
116
|
+
|
117
|
+
create => new
|
118
|
+
update => edit
|
119
|
+
remove => destroy
|
120
|
+
|
121
|
+
=== RSpec
|
122
|
+
|
123
|
+
Swiss Knife includes some RSpec matchers. To use them, add the following like to your <tt>spec_helper.rb</tt> file:
|
124
|
+
|
125
|
+
require "swiss_knife/rspec"
|
126
|
+
|
127
|
+
==== have_tag and have_node
|
128
|
+
|
129
|
+
The <tt>have_tag</tt> matcher uses Nokogiri, so you can use any CSS selector accepted by Nokogiri to filter elements.
|
130
|
+
|
131
|
+
html.should have_tag("p", "Hello world!")
|
132
|
+
html.should have_tag("p", :text => "Hello world!")
|
133
|
+
html.should have_tag("p", :count => 1)
|
134
|
+
html.should have_tag("p", :maximum => 3)
|
135
|
+
html.should have_tag("p", :minimum => 3)
|
136
|
+
html.should have_tag("form") do |form|
|
137
|
+
form.should have_tag("input.submit", :count => 1)
|
138
|
+
end
|
139
|
+
|
140
|
+
If you're inspecting some XML snippet, you can use the <tt>have_node</tt> matcher.
|
141
|
+
|
142
|
+
=== gravatar_tag
|
143
|
+
|
144
|
+
gravatar_tag user.email
|
145
|
+
gravatar_tag "098f6bcd4621d373cade4e832627b4f6"
|
146
|
+
gravatar_tag user.email, :size => 80
|
147
|
+
gravatar_tag user.email, :rating => :x
|
148
|
+
|
149
|
+
# Predefined default images provided by Gravatar. Can be
|
150
|
+
# :mm, :identicon, :monsterid, :wavatar, :retro, 404
|
151
|
+
gravatar_tag user.email, :default => :mm
|
152
|
+
|
153
|
+
gravatar_tag user.email, :default => "gravatar.png"
|
154
|
+
gravatar_tag user.email, :ssl => true
|
155
|
+
gravatar_tag user.email, :alt => user.name
|
156
|
+
gravatar_tag user.email, :title => user.name
|
157
|
+
|
158
|
+
== Maintainer
|
159
|
+
|
160
|
+
* Nando Vieira (<http://simplesideias.com.br>)
|
161
|
+
|
162
|
+
== License
|
163
|
+
|
164
|
+
(The MIT License)
|
165
|
+
|
166
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
167
|
+
a copy of this software and associated documentation files (the
|
168
|
+
'Software'), to deal in the Software without restriction, including
|
169
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
170
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
171
|
+
permit persons to whom the Software is furnished to do so, subject to
|
172
|
+
the following conditions:
|
173
|
+
|
174
|
+
The above copyright notice and this permission notice shall be
|
175
|
+
included in all copies or substantial portions of the Software.
|
176
|
+
|
177
|
+
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
|
178
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
179
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
180
|
+
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
|
181
|
+
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
|
182
|
+
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
|
183
|
+
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/Rakefile
ADDED
@@ -0,0 +1,26 @@
|
|
1
|
+
require "rspec/core/rake_task"
|
2
|
+
require "./lib/swiss_knife/version"
|
3
|
+
|
4
|
+
RSpec::Core::RakeTask.new
|
5
|
+
|
6
|
+
begin
|
7
|
+
require "jeweler"
|
8
|
+
|
9
|
+
JEWEL = Jeweler::Tasks.new do |gem|
|
10
|
+
gem.name = "swiss_knife"
|
11
|
+
gem.version = SwissKnife::Version::STRING
|
12
|
+
gem.summary = "Several helpers for Rails 3"
|
13
|
+
gem.description = "Several helpers for Rails 3"
|
14
|
+
gem.authors = ["Nando Vieira"]
|
15
|
+
gem.email = "fnando.vieira@gmail.com"
|
16
|
+
gem.homepage = "http://github.com/fnando/swiss_knife"
|
17
|
+
gem.has_rdoc = true
|
18
|
+
gem.add_dependency "rails", ">= 3.0.0"
|
19
|
+
gem.add_development_dependency "rspec", ">= 2.0.0"
|
20
|
+
gem.files = FileList["{Gemfile,Gemfile.lock,Rakefile,README.*,swiss_knife.gemspec}", "{spec,lib}/**/*"]
|
21
|
+
end
|
22
|
+
|
23
|
+
Jeweler::GemcutterTasks.new
|
24
|
+
rescue LoadError => e
|
25
|
+
puts "You don't Jeweler installed, so you won't be able to build gems."
|
26
|
+
end
|
@@ -0,0 +1,43 @@
|
|
1
|
+
module SwissKnife
|
2
|
+
module ActionController
|
3
|
+
# Page title.
|
4
|
+
#
|
5
|
+
# page_title
|
6
|
+
# page_title "Some title"
|
7
|
+
# page_title :name => "Some product"
|
8
|
+
#
|
9
|
+
# Locale:
|
10
|
+
#
|
11
|
+
# en:
|
12
|
+
# titles:
|
13
|
+
# products:
|
14
|
+
# show: "Product: %{name}"
|
15
|
+
# index: "All products"
|
16
|
+
#
|
17
|
+
def page_title(*args)
|
18
|
+
unless args.empty?
|
19
|
+
options = args.extract_options!
|
20
|
+
page_title_set args.first, options
|
21
|
+
end
|
22
|
+
|
23
|
+
page_title_get
|
24
|
+
end
|
25
|
+
|
26
|
+
private
|
27
|
+
def page_title_set(title, options)
|
28
|
+
@_page_title_options = {:title => title, :options => options}
|
29
|
+
end
|
30
|
+
|
31
|
+
def page_title_get
|
32
|
+
controller = self unless respond_to?(:controller)
|
33
|
+
controller_name = controller.controller_name
|
34
|
+
action_name = SwissKnife::Helpers::ACTION_ALIASES[controller.action_name] || controller.action_name
|
35
|
+
|
36
|
+
@_page_title_options ||= {}
|
37
|
+
options = @_page_title_options[:options] || {}
|
38
|
+
title = @_page_title_options[:title]
|
39
|
+
title ||= t("titles.#{controller_name}.#{action_name}", options)
|
40
|
+
title
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
@@ -0,0 +1,81 @@
|
|
1
|
+
module SwissKnife
|
2
|
+
class Assets
|
3
|
+
class << self
|
4
|
+
attr_accessor :configuration
|
5
|
+
end
|
6
|
+
|
7
|
+
EXTENSIONS = { :javascripts => "js", :stylesheets => "css" }
|
8
|
+
|
9
|
+
def self.config_file
|
10
|
+
Rails.root.join("config/assets.yml")
|
11
|
+
end
|
12
|
+
|
13
|
+
def self.config
|
14
|
+
@config ||= YAML.load_file(config_file)
|
15
|
+
end
|
16
|
+
|
17
|
+
def self.public_dir
|
18
|
+
Rails.root.join("public")
|
19
|
+
end
|
20
|
+
|
21
|
+
def self.config?
|
22
|
+
File.file?(config_file) && config
|
23
|
+
end
|
24
|
+
|
25
|
+
def self.merge?
|
26
|
+
config? && Rails.env.production?
|
27
|
+
end
|
28
|
+
|
29
|
+
# Taken from http://github.com/sbecker/asset_packager/
|
30
|
+
def self.compress_js(source)
|
31
|
+
jsmin_path = File.dirname(__FILE__)
|
32
|
+
tmp_path = Rails.root.join("tmp/js")
|
33
|
+
|
34
|
+
# write out to a temp file
|
35
|
+
File.open("#{tmp_path}_uncompressed.js", "w") {|f| f.write(source) }
|
36
|
+
|
37
|
+
# compress file with JSMin library
|
38
|
+
`ruby #{jsmin_path}/jsmin.rb <#{tmp_path}_uncompressed.js >#{tmp_path}_compressed.js \n`
|
39
|
+
|
40
|
+
# read it back in and trim it
|
41
|
+
result = ""
|
42
|
+
File.open("#{tmp_path}_compressed.js", "r") { |f| result += f.read.strip }
|
43
|
+
|
44
|
+
# delete temp files if they exist
|
45
|
+
File.delete("#{tmp_path}_uncompressed.js") if File.exists?("#{tmp_path}_uncompressed.js")
|
46
|
+
File.delete("#{tmp_path}_compressed.js") if File.exists?("#{tmp_path}_compressed.js")
|
47
|
+
|
48
|
+
result
|
49
|
+
end
|
50
|
+
|
51
|
+
# Taken from http://github.com/sbecker/asset_packager/
|
52
|
+
def self.compress_css(source)
|
53
|
+
source.gsub!(/\s+/, " ") # collapse space
|
54
|
+
source.gsub!(/\/\*(.*?)\*\//, "") # remove comments - caution, might want to remove this if using css hacks
|
55
|
+
source.gsub!(/\} /, "}\n") # add line breaks
|
56
|
+
source.gsub!(/\n$/, "") # remove last break
|
57
|
+
source.gsub!(/ \{ /, " {") # trim inside brackets
|
58
|
+
source.gsub!(/; \}/, "}") # trim inside brackets
|
59
|
+
source
|
60
|
+
end
|
61
|
+
|
62
|
+
def self.export(type)
|
63
|
+
assets_dir = public_dir.join(type.to_s)
|
64
|
+
ext = EXTENSIONS[type]
|
65
|
+
group = config[type.to_s]
|
66
|
+
|
67
|
+
return unless group
|
68
|
+
|
69
|
+
group.each do |name, files|
|
70
|
+
File.open(assets_dir.join("#{name}_packaged.#{ext}"), "w+") do |f|
|
71
|
+
files.each do |file|
|
72
|
+
file << ".#{ext}" unless file =~ /\.#{ext}$/i
|
73
|
+
|
74
|
+
f << send("compress_#{ext}", File.read(assets_dir.join(file)))
|
75
|
+
f << "\n;"
|
76
|
+
end
|
77
|
+
end
|
78
|
+
end
|
79
|
+
end
|
80
|
+
end
|
81
|
+
end
|