wiserespond 0.1.0 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +146 -0
- data/VERSION +1 -1
- data/app/views/{shared/wiserespond → wiserespond}/content.js.erb +0 -0
- data/app/views/{shared/wiserespond → wiserespond}/redirect.js.erb +0 -0
- data/lib/wiserespond/rails/engine.rb +2 -2
- data/lib/wiserespond/rails/railtie.rb +2 -2
- data/lib/wiserespond.rb +2 -2
- data/test/dummy/log/test.log +4 -0
- data/wiserespond.gemspec +6 -6
- metadata +17 -17
- data/README.rdoc +0 -19
data/README.md
ADDED
@@ -0,0 +1,146 @@
|
|
1
|
+
[![Build Status](https://secure.travis-ci.org/igor-alexandrov/paperclip-aws.png)](http://travis-ci.org/igor-alexandrov/paperclip-aws)
|
2
|
+
|
3
|
+
# wiserespond
|
4
|
+
|
5
|
+
Rails 3 responders to make you controllers DRY.
|
6
|
+
|
7
|
+
## How does it work?
|
8
|
+
|
9
|
+
### Without `wiserespond`
|
10
|
+
|
11
|
+
I a lot of Rails projects I see code like:
|
12
|
+
|
13
|
+
class BrandsController < ApplicationController
|
14
|
+
before_filter :fetch_brand
|
15
|
+
|
16
|
+
def edit
|
17
|
+
# Here is some code
|
18
|
+
end
|
19
|
+
|
20
|
+
def update
|
21
|
+
authorize! :update, @brand
|
22
|
+
if @brand.update_attributes(params[:brand])
|
23
|
+
respond_to do |format|
|
24
|
+
format.html{ redirect_to brand_url(@brand) }
|
25
|
+
format.js # Here will be rendered something like 'brands/update.js.erb'
|
26
|
+
end
|
27
|
+
else
|
28
|
+
respond_to do |format|
|
29
|
+
format.html{ render :action => :edit }
|
30
|
+
format.js # Here again be rendered 'brands/update.js.erb'
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
Then your `brands/update.js.erb` can look like this:
|
37
|
+
|
38
|
+
<% if @brand.errors.any? %>
|
39
|
+
$('#brand').replaceWith('<%= escape_javascript(render :partial => "brands/form", :locals => { :brand => @brand }) %>')
|
40
|
+
<% else %>
|
41
|
+
window.location.href="<%= brand_url(@brand).html_safe %>";
|
42
|
+
<% end %>
|
43
|
+
|
44
|
+
Or, if you use History API, or maybe Fancybox and you have to setup some features in you form and show flash message, it will be a bit more complex:
|
45
|
+
|
46
|
+
<% if @brand.errors.any? %>
|
47
|
+
$('#brand').replaceWith('<%= escape_javascript(render :partial => "brands/form", :locals => { :brand => @brand }) %>')
|
48
|
+
appSetupAllFeatures(#brand);
|
49
|
+
Flash.notification('An error occured while saving your brand');
|
50
|
+
<% else %>
|
51
|
+
$.fancybox.close();
|
52
|
+
History.replaceState({timestamp: (new Date().getTime()), slide: true}, document.title, "<%= brand_url(@brand).html_safe %>");
|
53
|
+
<% end %>
|
54
|
+
|
55
|
+
If you have to do this only once – this is OK. But what if out app consists of more then one controller and every controller has more then two actions?
|
56
|
+
|
57
|
+
Of course we can create
|
58
|
+
|
59
|
+
* `brands/update.js.erb`
|
60
|
+
* `brands/create.js.erb`
|
61
|
+
* `brands/some_other_action.js.erb`
|
62
|
+
* `other_model/some_other_action.js.erb`
|
63
|
+
|
64
|
+
and so on… This list can be rather long. Not a Rails-way at all.
|
65
|
+
|
66
|
+
### With `wiserespond`
|
67
|
+
|
68
|
+
Wiserespond does not try to invent some new way of writing controllers, it also does not have any magic. All is clear and easy to understand. Lets start with refactored example of out controller:
|
69
|
+
|
70
|
+
class BrandsController < ApplicationController
|
71
|
+
before_filter :fetch_brand
|
72
|
+
|
73
|
+
def edit
|
74
|
+
# Here is some code
|
75
|
+
end
|
76
|
+
|
77
|
+
def update
|
78
|
+
authorize! :update, @brand
|
79
|
+
if @brand.update_attributes(params[:brand])
|
80
|
+
respond_with_redirect :url => brand_url(@brand)
|
81
|
+
else
|
82
|
+
respond_with_content :action => :edit, :id => '#brand', :partial => 'brands/form'
|
83
|
+
end
|
84
|
+
end
|
85
|
+
end
|
86
|
+
|
87
|
+
Looks great? For me – yes! And works great too.
|
88
|
+
|
89
|
+
Wiserespond aggregates `html` and `js` content types responds, make redirection or action rendering on `html` content type and adds default templates to `js` content type. Here is `wiserespond/content.js.erb`:
|
90
|
+
|
91
|
+
<% if local_assigns[:id].present? && local_assigns[:partial].present? %>
|
92
|
+
$('#<%= id %>').replaceWith('<%= escape_javascript(render :partial => "#{partial}", :locals => locals) %>')
|
93
|
+
<% end %>
|
94
|
+
|
95
|
+
and here `wiserespond/redirect.js.erb`:
|
96
|
+
|
97
|
+
<% if local_assigns[:url].present? %>
|
98
|
+
window.location.href="<%= url.html_safe %>";
|
99
|
+
<% end %>
|
100
|
+
|
101
|
+
Of course you can redefine this templates in your application. Just create then in `app/views/wiserespond` directory.
|
102
|
+
|
103
|
+
For example, I use History API and want to pass some additional information to my JS code, here is what I have in `app/views/wiserespond/redirect.js.erb`:
|
104
|
+
|
105
|
+
History.replaceState({timestamp: (new Date().getTime()), slide: true}, document.title, "<%= url.html_safe %>");
|
106
|
+
|
107
|
+
$.fancybox.close();
|
108
|
+
|
109
|
+
<% if local_assigns[:ids].present? %>
|
110
|
+
$('.sidebar').trigger('update.cells_liens_tree', {ids: <%= ids %>});
|
111
|
+
$('.sidebar').trigger('update.cells_auctions_tree', {ids: <%= ids %>});
|
112
|
+
$('.sidebar').trigger('update.cells_liens_shared', {ids: <%= ids %>});
|
113
|
+
<% end %>
|
114
|
+
|
115
|
+
And here is how I call in controller:
|
116
|
+
|
117
|
+
if @portfolio.update_attributes(params[:portfolio])
|
118
|
+
respond_with_redirect :url => portfolio_url(@portfolio), :ids => @portfolio.affected_widget_ids, :flash => 'Portfolio was successfully updated.'
|
119
|
+
else
|
120
|
+
|
121
|
+
Make your controllers as DRY as you can!
|
122
|
+
|
123
|
+
## Production?
|
124
|
+
|
125
|
+
**wiserespond** is used at:
|
126
|
+
|
127
|
+
* [www.sdelki.ru](http://www.sdelki.ru)
|
128
|
+
* [www.lienlog.com](http://www.lienlog.com)
|
129
|
+
|
130
|
+
Know other projects? Just contact me and I will add them here.
|
131
|
+
|
132
|
+
## Contributing to wiserespond
|
133
|
+
|
134
|
+
* Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet
|
135
|
+
* Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it
|
136
|
+
* Fork the project
|
137
|
+
* Start a feature/bugfix branch
|
138
|
+
* Commit and push until you are happy with your contribution
|
139
|
+
* Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
|
140
|
+
* Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
|
141
|
+
|
142
|
+
## Copyright
|
143
|
+
|
144
|
+
Copyright (c) 2012 Igor Alexandrov. See LICENSE.txt for
|
145
|
+
further details.
|
146
|
+
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.
|
1
|
+
0.2.0
|
File without changes
|
File without changes
|
@@ -1,7 +1,7 @@
|
|
1
1
|
module Wiserespond
|
2
2
|
module Rails
|
3
|
-
class Engine < ::Rails::Engine
|
4
|
-
initializer "wiserespond.register" do
|
3
|
+
class Engine < ::Rails::Engine
|
4
|
+
initializer "wiserespond.register" do |app|
|
5
5
|
::ActionController::Base.send :include, Wiserespond::ActionController
|
6
6
|
end
|
7
7
|
end
|
data/lib/wiserespond.rb
CHANGED
@@ -12,7 +12,7 @@ module Wiserespond
|
|
12
12
|
|
13
13
|
respond_to do |format|
|
14
14
|
format.html { redirect_to options[:url] }
|
15
|
-
format.js { render :template => '
|
15
|
+
format.js { render :template => 'wiserespond/redirect', :locals => options }
|
16
16
|
end
|
17
17
|
end
|
18
18
|
|
@@ -27,7 +27,7 @@ module Wiserespond
|
|
27
27
|
|
28
28
|
respond_to do |format|
|
29
29
|
format.html { render options[:action] }
|
30
|
-
format.js { render :template => '
|
30
|
+
format.js { render :template => 'wiserespond/content', :locals => options }
|
31
31
|
end
|
32
32
|
end
|
33
33
|
|
data/test/dummy/log/test.log
CHANGED
@@ -10,3 +10,7 @@
|
|
10
10
|
[1m[35m (0.0ms)[0m rollback transaction
|
11
11
|
[1m[36m (0.0ms)[0m [1mbegin transaction[0m
|
12
12
|
[1m[35m (0.0ms)[0m rollback transaction
|
13
|
+
[1m[36m (0.2ms)[0m [1mbegin transaction[0m
|
14
|
+
[1m[35m (0.2ms)[0m rollback transaction
|
15
|
+
[1m[36m (0.1ms)[0m [1mbegin transaction[0m
|
16
|
+
[1m[35m (0.1ms)[0m rollback transaction
|
data/wiserespond.gemspec
CHANGED
@@ -5,27 +5,27 @@
|
|
5
5
|
|
6
6
|
Gem::Specification.new do |s|
|
7
7
|
s.name = "wiserespond"
|
8
|
-
s.version = "0.
|
8
|
+
s.version = "0.2.0"
|
9
9
|
|
10
10
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
11
|
s.authors = ["Igor Alexandrov"]
|
12
|
-
s.date = "2012-03-
|
12
|
+
s.date = "2012-03-17"
|
13
13
|
s.description = "With wiserespond you can easily setup your controllers to respond to :html and :js content types."
|
14
14
|
s.email = "igor.alexandrov@gmail.com"
|
15
15
|
s.extra_rdoc_files = [
|
16
16
|
"LICENSE.txt",
|
17
|
-
"README.
|
17
|
+
"README.md"
|
18
18
|
]
|
19
19
|
s.files = [
|
20
20
|
".document",
|
21
21
|
"Gemfile",
|
22
22
|
"Gemfile.lock",
|
23
23
|
"LICENSE.txt",
|
24
|
-
"README.
|
24
|
+
"README.md",
|
25
25
|
"Rakefile",
|
26
26
|
"VERSION",
|
27
|
-
"app/views/
|
28
|
-
"app/views/
|
27
|
+
"app/views/wiserespond/content.js.erb",
|
28
|
+
"app/views/wiserespond/redirect.js.erb",
|
29
29
|
"lib/wiserespond.rb",
|
30
30
|
"lib/wiserespond/rails.rb",
|
31
31
|
"lib/wiserespond/rails/engine.rb",
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: wiserespond
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,11 +9,11 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2012-03-
|
12
|
+
date: 2012-03-17 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: rails
|
16
|
-
requirement: &
|
16
|
+
requirement: &70103698722400 !ruby/object:Gem::Requirement
|
17
17
|
none: false
|
18
18
|
requirements:
|
19
19
|
- - ! '>='
|
@@ -21,10 +21,10 @@ dependencies:
|
|
21
21
|
version: 3.0.0
|
22
22
|
type: :runtime
|
23
23
|
prerelease: false
|
24
|
-
version_requirements: *
|
24
|
+
version_requirements: *70103698722400
|
25
25
|
- !ruby/object:Gem::Dependency
|
26
26
|
name: sqlite3-ruby
|
27
|
-
requirement: &
|
27
|
+
requirement: &70103698735200 !ruby/object:Gem::Requirement
|
28
28
|
none: false
|
29
29
|
requirements:
|
30
30
|
- - ! '>='
|
@@ -32,10 +32,10 @@ dependencies:
|
|
32
32
|
version: '0'
|
33
33
|
type: :development
|
34
34
|
prerelease: false
|
35
|
-
version_requirements: *
|
35
|
+
version_requirements: *70103698735200
|
36
36
|
- !ruby/object:Gem::Dependency
|
37
37
|
name: shoulda
|
38
|
-
requirement: &
|
38
|
+
requirement: &70103698753000 !ruby/object:Gem::Requirement
|
39
39
|
none: false
|
40
40
|
requirements:
|
41
41
|
- - ! '>='
|
@@ -43,10 +43,10 @@ dependencies:
|
|
43
43
|
version: '0'
|
44
44
|
type: :development
|
45
45
|
prerelease: false
|
46
|
-
version_requirements: *
|
46
|
+
version_requirements: *70103698753000
|
47
47
|
- !ruby/object:Gem::Dependency
|
48
48
|
name: bundler
|
49
|
-
requirement: &
|
49
|
+
requirement: &70103698761940 !ruby/object:Gem::Requirement
|
50
50
|
none: false
|
51
51
|
requirements:
|
52
52
|
- - ~>
|
@@ -54,10 +54,10 @@ dependencies:
|
|
54
54
|
version: 1.0.0
|
55
55
|
type: :development
|
56
56
|
prerelease: false
|
57
|
-
version_requirements: *
|
57
|
+
version_requirements: *70103698761940
|
58
58
|
- !ruby/object:Gem::Dependency
|
59
59
|
name: jeweler
|
60
|
-
requirement: &
|
60
|
+
requirement: &70103698773260 !ruby/object:Gem::Requirement
|
61
61
|
none: false
|
62
62
|
requirements:
|
63
63
|
- - ~>
|
@@ -65,7 +65,7 @@ dependencies:
|
|
65
65
|
version: 1.6.4
|
66
66
|
type: :development
|
67
67
|
prerelease: false
|
68
|
-
version_requirements: *
|
68
|
+
version_requirements: *70103698773260
|
69
69
|
description: With wiserespond you can easily setup your controllers to respond to
|
70
70
|
:html and :js content types.
|
71
71
|
email: igor.alexandrov@gmail.com
|
@@ -73,17 +73,17 @@ executables: []
|
|
73
73
|
extensions: []
|
74
74
|
extra_rdoc_files:
|
75
75
|
- LICENSE.txt
|
76
|
-
- README.
|
76
|
+
- README.md
|
77
77
|
files:
|
78
78
|
- .document
|
79
79
|
- Gemfile
|
80
80
|
- Gemfile.lock
|
81
81
|
- LICENSE.txt
|
82
|
-
- README.
|
82
|
+
- README.md
|
83
83
|
- Rakefile
|
84
84
|
- VERSION
|
85
|
-
- app/views/
|
86
|
-
- app/views/
|
85
|
+
- app/views/wiserespond/content.js.erb
|
86
|
+
- app/views/wiserespond/redirect.js.erb
|
87
87
|
- lib/wiserespond.rb
|
88
88
|
- lib/wiserespond/rails.rb
|
89
89
|
- lib/wiserespond/rails/engine.rb
|
@@ -139,7 +139,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
139
139
|
version: '0'
|
140
140
|
segments:
|
141
141
|
- 0
|
142
|
-
hash:
|
142
|
+
hash: -1867983914234975014
|
143
143
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
144
144
|
none: false
|
145
145
|
requirements:
|
data/README.rdoc
DELETED
@@ -1,19 +0,0 @@
|
|
1
|
-
= wiserespond
|
2
|
-
|
3
|
-
Description goes here.
|
4
|
-
|
5
|
-
== Contributing to wiserespond
|
6
|
-
|
7
|
-
* Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet
|
8
|
-
* Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it
|
9
|
-
* Fork the project
|
10
|
-
* Start a feature/bugfix branch
|
11
|
-
* Commit and push until you are happy with your contribution
|
12
|
-
* Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
|
13
|
-
* Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
|
14
|
-
|
15
|
-
== Copyright
|
16
|
-
|
17
|
-
Copyright (c) 2012 Igor Alexandrov. See LICENSE.txt for
|
18
|
-
further details.
|
19
|
-
|