pakunok 0.0.1 → 0.0.2

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.
data/.gitignore CHANGED
@@ -5,3 +5,4 @@ pkg/
5
5
  spec/dummy/db/*.sqlite3
6
6
  spec/dummy/log/*.log
7
7
  spec/dummy/tmp/
8
+ spec/dummy/public/assets/
data/Gemfile CHANGED
@@ -1,14 +1,5 @@
1
1
  source "http://rubygems.org"
2
2
 
3
- gem 'rails', '3.1.0.rc5'
4
- gem 'sprockets', '2.0.0.beta13'
3
+ gemspec
5
4
 
6
- # Bundle edge Rails instead:
7
- # gem 'rails', :git => 'git://github.com/rails/rails.git'
8
-
9
-
10
-
11
- # To use debugger
12
- # gem 'ruby-debug19', :require => 'ruby-debug'
13
-
14
- gem 'rspec', :group => :test
5
+ gem 'coffee-script', :require => false
@@ -1,3 +1,11 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ pakunok (0.0.2)
5
+ execjs
6
+ rails (>= 3.1.0.rc5)
7
+ sprockets (>= 2.0.0.beta13)
8
+
1
9
  GEM
2
10
  remote: http://rubygems.org/
3
11
  specs:
@@ -33,8 +41,14 @@ GEM
33
41
  arel (2.1.4)
34
42
  bcrypt-ruby (2.1.4)
35
43
  builder (3.0.0)
44
+ coffee-script (2.2.0)
45
+ coffee-script-source
46
+ execjs
47
+ coffee-script-source (1.1.2)
36
48
  diff-lcs (1.1.2)
37
49
  erubis (2.7.0)
50
+ execjs (1.2.4)
51
+ multi_json (~> 1.0)
38
52
  hike (1.2.0)
39
53
  i18n (0.6.0)
40
54
  mail (2.3.0)
@@ -93,6 +107,6 @@ PLATFORMS
93
107
  ruby
94
108
 
95
109
  DEPENDENCIES
96
- rails (= 3.1.0.rc5)
110
+ coffee-script
111
+ pakunok!
97
112
  rspec
98
- sprockets (= 2.0.0.beta13)
@@ -0,0 +1,7 @@
1
+ # 0.0.2 - 19 August 2011
2
+
3
+ * Added haml-js as a new asset
4
+ * Added the Tilt/Sprockets template for haml-js so it can be used from sprockets (autoregistered as `.hamljs`)
5
+
6
+ # 0.0.2 - 14 August 2011
7
+ Initial release. Includes assets that I often use.
@@ -1,4 +1,4 @@
1
- Copyright 2011 YOURNAME
1
+ Copyright 2011 Dmytrii Nagirniak
2
2
 
3
3
  Permission is hereby granted, free of charge, to any person obtaining
4
4
  a copy of this software and associated documentation files (the
data/README.md CHANGED
@@ -2,57 +2,7 @@
2
2
 
3
3
  _Pakunok_ contains a set of prepackaged assets that you can easily include into your Rails 3.1 application (using assets pipeline).
4
4
 
5
- The list of the assets included (you can reference then using the name below prefixed with `pakunok/`:
6
-
7
- - jquery (defaults to 1.6.2)
8
- - jquery/jquery-1.6.2
9
- - jquery/jquery-1.5.2
10
- - jquery.validate
11
- - colorpicker (has CSS)
12
- - fileuploader (has CSS)
13
- - innershiv
14
- - jquery.form
15
- - jquery.validate
16
- - jquery.jscrollpane (has CSS)
17
- - jquery.mousewheel (optional improvement for jquery.jscrollpane)
18
- - mwheelIntent (optional improvement for jquery.jscrollpane)
19
- - jquery.viewport
20
- - jquery-ui - standalone files (`jquery-ui/`): no dependencies tracked, serve these when part of jQueryUI has already been included elsewhere
21
- - accordion
22
- - autocomplete
23
- - button
24
- - core
25
- - datepicker
26
- - dialog
27
- - draggable
28
- - droppable
29
- - effects (all effects, does not require jQuery-UI core)
30
- - mouse
31
- - position
32
- - progressbar
33
- - resizable
34
- - selectable
35
- - slider
36
- - sortable
37
- - tabs
38
- - widget
39
- - jquery-ui - combined (`jquery-ui/pack/`): ready-to-go options, you can include multiple into only one script file
40
- - accordion
41
- - autocomplete
42
- - basic (includes core, widget, position, mouse components)
43
- - button
44
- - datepicker
45
- - dialog
46
- - draggable
47
- - droppable
48
- - effects (same as standalone)
49
- - progressbar
50
- - resizable
51
- - selectable
52
- - slider
53
- - sortable
54
- - tabs
55
-
5
+ You can see all the included [assets](assets.md). (reference those prefixed with `pakunok/`):
56
6
 
57
7
  Tested on MRI Ruby 1.9.2.
58
8
 
@@ -71,12 +21,11 @@ Then `bundle install`.
71
21
 
72
22
  # Usage
73
23
 
74
- You can reference the assets as usually using the sprockets.
75
- Let's see full example (includes everything for the sake of demo):
76
-
24
+ Reference as you normally do with Sprockets.
25
+ Let's see some examples:
77
26
 
78
27
 
79
- ## JavaScript Only libraries
28
+ ## JavaScript-only libraries
80
29
 
81
30
  You can simply reference plain JS libraries that do not require other assets (CSS, images) like this:
82
31
 
@@ -91,7 +40,7 @@ You can simply reference plain JS libraries that do not require other assets (CS
91
40
 
92
41
  //= require 'pakunok/innershiv'
93
42
 
94
- // jQuery plug-ins do no automatically include jQuery to allow to use them as a separate script tag
43
+ // jQuery plug-ins do no depend on jQuery to allow using as a separate HTTP resource
95
44
  //= require 'pakunok/jquery.form'
96
45
  //= require 'pakunok/jquery.jscrollpane'
97
46
  //= require 'pakunok/jquery.mousewheel'
@@ -103,8 +52,6 @@ You can simply reference plain JS libraries that do not require other assets (CS
103
52
  //= require 'pakunok/jquery.viewport'
104
53
  ```
105
54
 
106
- It is also possible to reference the assets directly from the views.
107
-
108
55
  ## Libraries with related resources
109
56
 
110
57
  If the library has a related CSS file, then it can be included into your CSS (or served as a separate file).
@@ -122,26 +69,23 @@ It is named after the JavaScript library.
122
69
 
123
70
  # Precompilation
124
71
  By default Rails precompiles all the assets of all included libraries.
125
- This means that ALL of the assets will be compiled (although you only use part of it).
72
+ This means that _ALL_ of the assets will be compiled (although you rarely need that).
126
73
 
127
- Please run `RAILS_ENV=production bundle exec rake assets:clean assets:precompile && tree public/assets` to verify necessary assets.
74
+ Please run `RAILS_ENV=production bundle exec rake assets:clean assets:precompile && tree public/assets` to verify necessary files.
128
75
 
129
- It is recommended to change the default behaviour so that you know which assets are compiled:
76
+ It is recommended to change the default behaviour so that you are not precompiling assets that the application will never use:
130
77
 
131
78
  ```ruby
132
79
  # config/application.rb
133
80
 
134
- # Something like this is the default
81
+ # Something like this is the Rails default
135
82
  #config.assets.precompile = [/\w+\.(?!js|css).+/, /application.(css|js)$/]
136
83
 
137
84
  # Recommended: Explicitly add assets that you use (colorpicker),
138
85
  # so that images and styles are available.
139
86
  config.assets.precompile = [/application.(css|js)$/, /pakunok\/colorpicker/]
140
87
 
141
- # Specify precompilable assets explicitly if you don't reference any assets from pakunok
142
- config.assets.precompile = [/application.(css|js)$/, 'expclicit-file.js', 'pakunok/colorpicker']
143
-
144
- # Exclude all pakunok assets from precompilation (it's ok if you reference only JS)
88
+ # Exclude all pakunok assets from precompilation (it's ok if you don't have direct HTTP request to them)
145
89
  config.assets.precompile = [/(!pakunok)\w+\.(?!js|css).+/, /application.(css|js)$/]
146
90
 
147
91
  # Exclude all pakunok assets, but explicitly add ones that you use (colorpicker),
@@ -160,9 +104,81 @@ All the files under `pakunok/jquery-ui/*` do not automatically include depndenci
160
104
  But if you want to include all the dependencies into a single file, then use `pakunok/jquery-ui/pack/*`.
161
105
 
162
106
 
107
+ # HAML for client side templating
108
+ _Pakunok_ provides a new templating engine for Rails 3.1 that can be used to produce JavsScript templates.
109
+
110
+ What you need to do is to use `.hamljs` extension on a javascript file with the HAML content.
111
+ It will generate a plain optimised JavaScipt function that you can use on the client.
112
+
113
+ For example, assuming you have a file `app/assets/javascripts/comment.js.hamljs` with the content:
114
+
115
+ ```haml
116
+ .comment
117
+ .text= text
118
+ .author= author
119
+ ```
120
+
121
+ Then you can `require comment` from the `application.js`.
122
+ This gives you access to `JST.comment` function allowing you to write JavaScript like:
123
+
124
+ ```javascript
125
+ var html = JST.comment({author: 'Dima', text: 'Awesome'});
126
+ $("#commit").append(html)
127
+ ```
128
+
129
+ *NOTE*: [HAML-JS](https://github.com/creationix/haml-js) is a little bit different from the original HAML for Ruby.
130
+
131
+ In case _pakunok_ could magically provide a good name for your template function, you can access it as `JST['what ever it is!']`.
132
+ The name of the template function is derrived from the file name. Some examples for you:
133
+
134
+ ```
135
+ file => file
136
+ file.js.erb.hamljs => file
137
+ file-with-dash.hamljs => fileWithDash
138
+ file_with_underscore => fileWithUnderscore
139
+ dir/foo_bar => dir_fooBar
140
+ win\dir\foo_bar => win_dir_fooBar
141
+ d1/d2/foo_bar.js.a.b.c.d => d1_d2_fooBar
142
+ ```
143
+
144
+ Yes, it uses one global variable `JST` to add all the functions but you can change it (see example further).
145
+
146
+ _Pakunok_ will escape the HTML using simple built-in function.
147
+ The escaping function is generated inside each template resulting in larger JavaScript code base.
148
+ It is *highly* recommended to set it to your own when you have more than a couple of templates.
149
+
150
+
151
+ ## HAML Configuration options
152
+
153
+ ```ruby
154
+ # Somewhere in your app...
155
+ require 'pakunok/haml_js_template'
156
+
157
+ # Change the escapeHTML function
158
+ Pakunok::HamlJsTemplate.custom_escape = 'YourApp.html_escape' # default is nil - built-in
159
+
160
+ # Change the global variable to attach templates to
161
+ Pakunok::HamlJsTemplate.root_variable = 'Templates' # default is 'JST'
162
+
163
+
164
+ # Change the prefix from where to start naming the templates
165
+ Pakunok::HamlJsTemplate.name_prefix = 'javascripts/templates' # default is 'javascripts/'
166
+ # This option will automatically be set to 'backbone/templates/' if back
167
+ ```
168
+
169
+ When option `name_prefix` is not set and [rails-backbone](https://github.com/codebrew/backbone-rails) is available then `backbone/templates/` is used by default.
170
+
171
+
163
172
  # Development
164
173
 
165
174
  - Source hosted at [GitHub](https://github.com/dnagir/pakunok)
166
175
  - Report issues and feature requests to [GitHub Issues](https://github.com/dnagir/pakunok/issues)
167
176
 
168
177
  Pull requests are very welcome! But please write the specs.
178
+
179
+ To start, clone the repo and then:
180
+
181
+ ```shell
182
+ bundle install
183
+ bundle exec rspec spec/
184
+ ```
@@ -0,0 +1,51 @@
1
+ List of all the assets that can be served (reference those prefixed with `pakunok/`)
2
+
3
+ - haml (also includes Rails 3.1 precompilation)
4
+ - jquery (defaults to 1.6.2)
5
+ - jquery/jquery-1.6.2
6
+ - jquery/jquery-1.5.2
7
+ - jquery.validate
8
+ - colorpicker (has CSS)
9
+ - fileuploader (has CSS)
10
+ - innershiv
11
+ - jquery.form
12
+ - jquery.validate
13
+ - jquery.jscrollpane (has CSS)
14
+ - jquery.mousewheel (optional improvement for jquery.jscrollpane)
15
+ - mwheelIntent (optional improvement for jquery.jscrollpane)
16
+ - jquery.viewport
17
+ - jquery-ui - standalone files (`jquery-ui/`): no dependencies tracked, serve these when part of jQueryUI has already been included elsewhere
18
+ - accordion
19
+ - autocomplete
20
+ - button
21
+ - core
22
+ - datepicker
23
+ - dialog
24
+ - draggable
25
+ - droppable
26
+ - effects (all effects, does not require jQuery-UI core)
27
+ - mouse
28
+ - position
29
+ - progressbar
30
+ - resizable
31
+ - selectable
32
+ - slider
33
+ - sortable
34
+ - tabs
35
+ - widget
36
+ - jquery-ui - combined (`jquery-ui/pack/`): ready-to-go options, you can include multiple into only one script file
37
+ - accordion
38
+ - autocomplete
39
+ - basic (includes core, widget, position, mouse components)
40
+ - button
41
+ - datepicker
42
+ - dialog
43
+ - draggable
44
+ - droppable
45
+ - effects (same as standalone)
46
+ - progressbar
47
+ - resizable
48
+ - selectable
49
+ - slider
50
+ - sortable
51
+ - tabs
@@ -1,4 +1,12 @@
1
1
  module Pakunok
2
2
  class Engine < Rails::Engine
3
+ initializer "pakunok.configure_rails_initialization" do |app|
4
+ next unless app.config.assets.enabled
5
+
6
+ require 'sprockets'
7
+ require 'sprockets/engines'
8
+ require 'pakunok/haml_js_template'
9
+ Sprockets.register_engine '.hamljs', ::Pakunok::HamlJsTemplate
10
+ end
3
11
  end
4
12
  end
@@ -0,0 +1,83 @@
1
+ require 'tilt/template'
2
+
3
+ module Pakunok
4
+ class HamlJsTemplate < Tilt::Template
5
+ self.default_mime_type = 'application/javascript'
6
+
7
+
8
+ def self.engine_initialized?
9
+ defined? ::ExecJS
10
+ end
11
+
12
+ def initialize_engine
13
+ require_template_library 'execjs'
14
+ end
15
+
16
+
17
+ def prepare
18
+ end
19
+
20
+
21
+ def evaluate(scope, locals, &block)
22
+ ns = self.class.root_variable || 'JST'
23
+ @output ||= <<-TEMPLATE
24
+ (function(scope){
25
+ (scope.#{ns} || (scope.#{ns} = {}))['#{client_name}'] = #{compile_to_function};
26
+ })(this);
27
+ TEMPLATE
28
+ end
29
+
30
+
31
+ def client_name
32
+ prefix = self.class.name_prefix || if defined?(BackboneRails)
33
+ 'backbone/templates/'
34
+ else
35
+ 'javascripts/'
36
+ end
37
+ path = eval_file
38
+ name_start = path.rindex(prefix)
39
+ path = file[(name_start + prefix.length)..-1] if name_start
40
+
41
+
42
+ parts = path.split(/-|_/)
43
+ res = parts[0] + (parts[1..-1] || []).map {|w| w.capitalize }.join
44
+ res = res.gsub(/\/|\\/, '_')
45
+ res.split('.').first.sub(/^_/, '') # chomp of the extensions
46
+ end
47
+
48
+
49
+ def compile_to_function
50
+ function = ExecJS.
51
+ compile(self.class.haml_source).
52
+ eval "Haml('#{js_string data}', {escapeHtmlByDefault: true, customEscape: #{js_custom_escape}}).toString()"
53
+ # make sure function is annonymous
54
+ function.sub /function \w+/, "function "
55
+ end
56
+
57
+
58
+ def js_string str
59
+ (str || '').
60
+ gsub("'") {|m| "\\'" }.
61
+ gsub("\n") {|m| "\\n" }
62
+ end
63
+
64
+
65
+ def js_custom_escape
66
+ self.class.custom_escape ? "'#{js_string self.class.custom_escape}'" : 'null'
67
+ end
68
+
69
+
70
+ class << self
71
+ attr_accessor :custom_escape
72
+ attr_accessor :root_variable
73
+ attr_accessor :name_prefix
74
+
75
+ def haml_source
76
+ # Haml source is an asset
77
+ @haml_source ||= IO.read File.expand_path('../../../vendor/assets/javascripts/pakunok/haml.js', __FILE__)
78
+ end
79
+ end
80
+
81
+ end
82
+ end
83
+
@@ -1,5 +1,5 @@
1
1
  module Pakunok
2
2
  module Version
3
- VERSION = "0.0.1"
3
+ VERSION = "0.0.2"
4
4
  end
5
5
  end
@@ -15,6 +15,7 @@ Gem::Specification.new do |s|
15
15
 
16
16
  s.add_dependency 'rails', '>= 3.1.0.rc5'
17
17
  s.add_dependency 'sprockets', '>= 2.0.0.beta13'
18
+ s.add_dependency 'execjs'
18
19
  s.add_development_dependency 'rspec'
19
20
 
20
21
  s.files = `git ls-files`.split("\n")