paloma 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.
Files changed (38) hide show
  1. data/.gitignore +18 -0
  2. data/Gemfile +3 -0
  3. data/Gemfile.lock +148 -0
  4. data/License +22 -0
  5. data/README.md +240 -0
  6. data/Rakefile +16 -0
  7. data/app/views/paloma/_callback_hook.html.erb +33 -0
  8. data/lib/paloma.rb +14 -0
  9. data/lib/paloma/action_controller_extension.rb +52 -0
  10. data/lib/paloma/action_controller_filters.rb +53 -0
  11. data/lib/paloma/paloma_generator.rb +94 -0
  12. data/lib/paloma/templates/_callbacks.js +2 -0
  13. data/lib/paloma/templates/_local.js +20 -0
  14. data/lib/paloma/templates/action.js +5 -0
  15. data/lib/paloma/templates/index.js +1 -0
  16. data/lib/paloma/templates/paloma.js +22 -0
  17. data/paloma.gemspec +23 -0
  18. data/spec/callback_spec.rb +65 -0
  19. data/spec/sample_app/app/assets/javascripts/application.js +4 -0
  20. data/spec/sample_app/app/assets/javascripts/paloma/articles/callbacks.js +1 -0
  21. data/spec/sample_app/app/assets/javascripts/paloma/articles/create.js +3 -0
  22. data/spec/sample_app/app/assets/javascripts/paloma/articles/edit.js +3 -0
  23. data/spec/sample_app/app/assets/javascripts/paloma/articles/index.js +3 -0
  24. data/spec/sample_app/app/assets/javascripts/paloma/articles/new.js +3 -0
  25. data/spec/sample_app/app/assets/javascripts/paloma/articles/show.js +3 -0
  26. data/spec/sample_app/app/assets/javascripts/paloma/articles/update.js +3 -0
  27. data/spec/sample_app/app/assets/javascripts/paloma/index.js +2 -0
  28. data/spec/sample_app/app/assets/javascripts/paloma/paloma.js +1 -0
  29. data/spec/sample_app/app/views/articles/index.html.erb +5 -0
  30. data/spec/sample_app/app/views/articles/new.html.erb +9 -0
  31. data/spec/sample_app/app/views/articles/show.html.erb +2 -0
  32. data/spec/sample_app/app/views/layouts/application.html.erb +12 -0
  33. data/spec/sample_app/controllers.rb +54 -0
  34. data/spec/sample_app/init.rb +33 -0
  35. data/spec/sample_app/model.rb +28 -0
  36. data/spec/sample_app/paloma_test +0 -0
  37. data/spec/spec_helper.rb +21 -0
  38. metadata +215 -39
data/.gitignore ADDED
@@ -0,0 +1,18 @@
1
+ *.rbc
2
+ *.sassc
3
+ .sass-cache
4
+ capybara-*.html
5
+ .rspec
6
+ /.bundle
7
+ /vendor/bundle
8
+ /log/*
9
+ /tmp/*
10
+ /db/*.sqlite3
11
+ /public/system/*
12
+ /coverage/
13
+ /spec/tmp/*
14
+ *.gem
15
+ /spec/sample_app/log
16
+ **.orig
17
+ rerun.txt
18
+ pickle-email-*.html
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source "http://rubygems.org"
2
+
3
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,148 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ paloma (0.0.2)
5
+ jquery-rails
6
+
7
+ GEM
8
+ remote: http://rubygems.org/
9
+ specs:
10
+ actionmailer (3.2.9)
11
+ actionpack (= 3.2.9)
12
+ mail (~> 2.4.4)
13
+ actionpack (3.2.9)
14
+ activemodel (= 3.2.9)
15
+ activesupport (= 3.2.9)
16
+ builder (~> 3.0.0)
17
+ erubis (~> 2.7.0)
18
+ journey (~> 1.0.4)
19
+ rack (~> 1.4.0)
20
+ rack-cache (~> 1.2)
21
+ rack-test (~> 0.6.1)
22
+ sprockets (~> 2.2.1)
23
+ activemodel (3.2.9)
24
+ activesupport (= 3.2.9)
25
+ builder (~> 3.0.0)
26
+ activerecord (3.2.9)
27
+ activemodel (= 3.2.9)
28
+ activesupport (= 3.2.9)
29
+ arel (~> 3.0.2)
30
+ tzinfo (~> 0.3.29)
31
+ activeresource (3.2.9)
32
+ activemodel (= 3.2.9)
33
+ activesupport (= 3.2.9)
34
+ activesupport (3.2.9)
35
+ i18n (~> 0.6)
36
+ multi_json (~> 1.0)
37
+ addressable (2.3.2)
38
+ arel (3.0.2)
39
+ builder (3.0.4)
40
+ capybara (2.0.1)
41
+ mime-types (>= 1.16)
42
+ nokogiri (>= 1.3.3)
43
+ rack (>= 1.0.0)
44
+ rack-test (>= 0.5.4)
45
+ selenium-webdriver (~> 2.0)
46
+ xpath (~> 1.0.0)
47
+ childprocess (0.3.6)
48
+ ffi (~> 1.0, >= 1.0.6)
49
+ database_cleaner (0.9.1)
50
+ diff-lcs (1.1.3)
51
+ erubis (2.7.0)
52
+ ffi (1.2.0)
53
+ hike (1.2.1)
54
+ i18n (0.6.1)
55
+ journey (1.0.4)
56
+ jquery-rails (2.1.4)
57
+ railties (>= 3.0, < 5.0)
58
+ thor (>= 0.14, < 2.0)
59
+ json (1.7.5)
60
+ launchy (2.1.2)
61
+ addressable (~> 2.3)
62
+ libwebsocket (0.1.7.1)
63
+ addressable
64
+ websocket
65
+ mail (2.4.4)
66
+ i18n (>= 0.4.0)
67
+ mime-types (~> 1.16)
68
+ treetop (~> 1.4.8)
69
+ mime-types (1.19)
70
+ multi_json (1.5.0)
71
+ nokogiri (1.5.5)
72
+ polyglot (0.3.3)
73
+ rack (1.4.1)
74
+ rack-cache (1.2)
75
+ rack (>= 0.4)
76
+ rack-ssl (1.3.2)
77
+ rack
78
+ rack-test (0.6.2)
79
+ rack (>= 1.0)
80
+ rails (3.2.9)
81
+ actionmailer (= 3.2.9)
82
+ actionpack (= 3.2.9)
83
+ activerecord (= 3.2.9)
84
+ activeresource (= 3.2.9)
85
+ activesupport (= 3.2.9)
86
+ bundler (~> 1.0)
87
+ railties (= 3.2.9)
88
+ railties (3.2.9)
89
+ actionpack (= 3.2.9)
90
+ activesupport (= 3.2.9)
91
+ rack-ssl (~> 1.3.2)
92
+ rake (>= 0.8.7)
93
+ rdoc (~> 3.4)
94
+ thor (>= 0.14.6, < 2.0)
95
+ rake (10.0.3)
96
+ rdoc (3.12)
97
+ json (~> 1.4)
98
+ rspec (2.12.0)
99
+ rspec-core (~> 2.12.0)
100
+ rspec-expectations (~> 2.12.0)
101
+ rspec-mocks (~> 2.12.0)
102
+ rspec-core (2.12.2)
103
+ rspec-expectations (2.12.1)
104
+ diff-lcs (~> 1.1.3)
105
+ rspec-mocks (2.12.0)
106
+ rspec-rails (2.12.0)
107
+ actionpack (>= 3.0)
108
+ activesupport (>= 3.0)
109
+ railties (>= 3.0)
110
+ rspec-core (~> 2.12.0)
111
+ rspec-expectations (~> 2.12.0)
112
+ rspec-mocks (~> 2.12.0)
113
+ rubyzip (0.9.9)
114
+ selenium-webdriver (2.27.2)
115
+ childprocess (>= 0.2.5)
116
+ libwebsocket (~> 0.1.3)
117
+ multi_json (~> 1.0)
118
+ rubyzip
119
+ sprockets (2.2.2)
120
+ hike (~> 1.2)
121
+ multi_json (~> 1.0)
122
+ rack (~> 1.0)
123
+ tilt (~> 1.1, != 1.3.0)
124
+ sqlite3 (1.3.6)
125
+ thor (0.16.0)
126
+ tilt (1.3.3)
127
+ treetop (1.4.12)
128
+ polyglot
129
+ polyglot (>= 0.3.1)
130
+ tzinfo (0.3.35)
131
+ websocket (1.0.4)
132
+ xpath (1.0.0)
133
+ nokogiri (~> 1.3)
134
+
135
+ PLATFORMS
136
+ ruby
137
+
138
+ DEPENDENCIES
139
+ bundler (>= 1.0.0)
140
+ capybara (>= 1.0)
141
+ database_cleaner
142
+ launchy
143
+ paloma!
144
+ rails (>= 3.1.0)
145
+ rake
146
+ rspec
147
+ rspec-rails (~> 2.0)
148
+ sqlite3
data/License ADDED
@@ -0,0 +1,22 @@
1
+ (The MIT License)
2
+
3
+ Copyright (c) 2012-2013 Karl Paragua, Bia Esmero
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ 'Software'), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19
+ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
20
+ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
21
+ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
22
+ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,240 @@
1
+ Paloma
2
+ ======
3
+ Paloma provides a sexy way to organize javascript files using Rails' asset pipeline.
4
+ It adds the capability to execute specific javascript code after rendering the controller's response.
5
+
6
+ Advantages
7
+ -
8
+ * Javascript files are organized per controller just like app/views folder of Rails.
9
+ * Javascript file per controller's action.
10
+ * The ability to choose what specific javascript code to run on a specific action.
11
+
12
+ Quick Example
13
+ -
14
+ The javascript callback file `paloma/users/new.js`:
15
+
16
+ ```javascript
17
+ Paloma.callbacks['users/new'] = function(params){
18
+ // This will only run after executing users/new action
19
+ alert('Hello New Sexy User');
20
+ };
21
+ ```
22
+
23
+ The Rails controller `app/controllers/users_controller.rb`:
24
+
25
+ ```ruby
26
+ def UsersController < ApplicationController
27
+ def new
28
+ @user = User.new
29
+ # No special function to call, the javascript callback will be executed automatically
30
+ end
31
+ end
32
+ ```
33
+
34
+ That's it! Simply Sexy!
35
+
36
+ Minimum Requirements
37
+ -
38
+ * jQuery 1.7 or higher
39
+ * Rails 3.1 or higher
40
+
41
+
42
+ Install
43
+ -
44
+ Without bundler:
45
+ ```
46
+ sudo gem install paloma
47
+ ```
48
+
49
+ With bundler, add this to your Gemfile:
50
+ ```
51
+ gem paloma
52
+ ```
53
+
54
+ Setup
55
+ -
56
+ On setup, the `paloma` folder will be generated in `app/assets/javascripts/` containing its required files. Run:
57
+ ```
58
+ rails g paloma:setup
59
+ ```
60
+
61
+ Directory Structure
62
+ -
63
+ `paloma` folder contains the javascript callbacks.
64
+
65
+ * paloma
66
+ * [controllers]
67
+ * [action].js
68
+ * [other_action].js
69
+ * [other_controllers]
70
+ * [action].js
71
+ * [other_action].js
72
+ * [more_action].js
73
+
74
+ Generators
75
+ -
76
+ 1. Generate a controller folder containing its required files:
77
+ ```
78
+ rails g paloma:add [controllers]
79
+ ```
80
+ **Example:**
81
+ ```
82
+ rails g paloma:add users
83
+ ```
84
+
85
+ **Generates:**
86
+ * /paloma
87
+ * /users
88
+
89
+
90
+ 2. Generate a callback file for a controller's action:
91
+ ```
92
+ rails g paloma:add [controllers]/[action]
93
+ ```
94
+ **Example:**
95
+ ```
96
+ rails g paloma:add users/new
97
+ ```
98
+
99
+ **Generates:**
100
+ * /paloma
101
+ * /users
102
+ * new.js
103
+
104
+
105
+ **Note:** You can directly run `rails g paloma:add [controllers]/[action]` even the controller folder is not yet
106
+ existing on `paloma` folder. It will be created automatically.
107
+
108
+
109
+ Advanced Callbacks
110
+ -
111
+ By default Paloma will execute the callback that matches the response's current controller and action if it finds one.
112
+ For instance if the current response is from the `new` action of the `Users` controller, then Paloma will execute the callback
113
+ named `users/new`.
114
+
115
+ You can manipulate callback behavior by using the `js_callback` command before the `render` or `redirect_to` command in your controllers.
116
+
117
+ 1. Preventing the Callback to execute.
118
+
119
+ ```ruby
120
+ def destroy
121
+ user = User.find params[:id]
122
+ user.destroy
123
+
124
+ js_callback false
125
+ end
126
+ ```
127
+
128
+ `[controllers]/destroy` callback will not be executed.
129
+
130
+ 2. Using other action's callback from the same controller.
131
+
132
+ ```ruby
133
+ def edit
134
+ @user = User.find params[:id]
135
+ js_callback :new
136
+ end
137
+ ```
138
+
139
+ This will execute `[controller]/new` callback instead of `[controller]/edit`.
140
+
141
+ 3. Using other action's callback from other controller.
142
+
143
+ ```ruby
144
+ def index
145
+ @users = User.all
146
+ js_callback :controller => 'clients', :action => 'index'
147
+ end
148
+ ```
149
+
150
+ This will execute `clients/index` callback instead of `[controller]/index`.
151
+
152
+ Passing Parameters
153
+ -
154
+ You can also pass parameters to the callback by passing a `:params` key to `js_callback`. The passed parameters
155
+ will be available on the callback by the `params` object.
156
+
157
+ **Example:**
158
+
159
+ `users_controller.rb`
160
+ ```ruby
161
+ def destroy
162
+ user = User.find params[:id]
163
+ user.destroy
164
+
165
+ js_callback :params => {:user_id => params[:id]}
166
+ end
167
+ ```
168
+
169
+ `/paloma/users/destroy.js`
170
+ ```javascript
171
+ Paloma.callbacks['users/destroy'] = function(params){
172
+ var id = params['user_id'];
173
+ alert('User ' + id + ' deleted.');
174
+ };
175
+ ```
176
+
177
+ Callback Helpers
178
+ -
179
+ Callback helpers are inside Paloma objects in order to prevent conflicts from different functions and variables.
180
+
181
+ 1. Global
182
+
183
+ Helper functions and variables can defined in `paloma/paloma.js` inside the `Paloma.g` object.
184
+
185
+ **Example:**
186
+ ```javascript
187
+ Paloma.g = {
188
+ helper: function(){
189
+ // do something sexy
190
+ },
191
+
192
+ helper_variable: 1000
193
+ };
194
+ ```
195
+
196
+ 2. Controller's Scope
197
+
198
+ Helper functions that you will only use for a certain controller can be defined in `paloma/[controllers]/_local.js` inside
199
+ the `Paloma.[controllers]` object.
200
+
201
+ **Example:**
202
+ ```javascript
203
+ Paloma.users = {
204
+ local_helper: function(){
205
+ // do something sexy
206
+ },
207
+
208
+ helper_variable: 1
209
+ };
210
+ ```
211
+
212
+ Callback Chains
213
+ -
214
+ Callback chains are created after a redirect action. The chain will continue to increase its length until a render action is detected.
215
+
216
+ **Example:**
217
+
218
+ ```ruby
219
+ def first_action
220
+ redirect_to second_action_path
221
+ end
222
+
223
+ def second_action
224
+ redirect_to third_action_path
225
+ end
226
+
227
+ def third_action
228
+ render :template => 'third_action_view'
229
+ end
230
+ ```
231
+
232
+ A request for `first_action` will lead to 2 redirects until it reaches the `third_action` and renders a result on the browser. When the `third_action` renders its response, Paloma will execute the callbacks for all the 3 actions.
233
+
234
+ The order of execution will be `[controller]/first_action` first, then `[controller]/second_action`, and finally `[controller]/third_action`.
235
+
236
+
237
+ Gotchas
238
+ -
239
+ * Callbacks will not be executed if the response is `js`, `json`, `xml` or any other format except `html`.
240
+ This will not work: `render "something.js.erb"`
data/Rakefile ADDED
@@ -0,0 +1,16 @@
1
+ require 'bundler'
2
+ Bundler::GemHelper.install_tasks
3
+
4
+ require 'rspec/core'
5
+ require 'rspec/core/rake_task'
6
+
7
+ RSpec::Core::RakeTask.new(:spec) do |spec|
8
+ spec.pattern = FileList['spec/*_spec.rb']
9
+ end
10
+
11
+
12
+ desc "Run tests"
13
+ task :test do
14
+ puts "Testing..."
15
+ sh "bundle exec rake spec"
16
+ end