turboboost 0.0.6 → 0.0.8

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,15 +1,7 @@
1
1
  ---
2
- !binary "U0hBMQ==":
3
- metadata.gz: !binary |-
4
- N2VlY2M0OWI0ZjQ1MDJlYmUwYjViMDI0NmIwNTNjMzhlMGRiNjI3Nw==
5
- data.tar.gz: !binary |-
6
- NTRmNWNkOTJjN2EwYTFmZDI0ZWI1YWUyMTg0MWJiOGQ4N2E3NjhlMw==
2
+ SHA1:
3
+ metadata.gz: 73ee25271c4886c00be8630d4c0d67528d3235f8
4
+ data.tar.gz: 9de2982e9a0b21bc5995042389ee3b471b95ee19
7
5
  SHA512:
8
- metadata.gz: !binary |-
9
- ZDkwMGU2YmNiMmY0MmI1ZTlhMzkxN2M0N2ExNzY5M2JjY2Y0ZDA0ZTY3ZTEx
10
- MDY5YzQwNWJlZjM5YTE1MTdhOGJlNTljMGNlMTQ3ZjkyMDllZjE3YWQzNTgz
11
- ZWRlZmQzMGFiOWNiYTVhMWZhYTg5YjVkM2Q4OGZiODNmYTVlZGQ=
12
- data.tar.gz: !binary |-
13
- M2U1MWUzZDQ5NDRjMTI0YTRjMmRhYWEwM2RhM2UzMmY0MDE0MDhhMTNhNmJj
14
- ODhkMzhlMGY3NTAyNGE0ZWU0YzE1ZTYwNmQzNGNkNTJhMTk2YTJjMDUwNzU4
15
- NDNiNzViMzBiOWI3OGQ1ZDQ2ZDRiNGM2OGIxYTkwZTM5OWY0MjQ=
6
+ metadata.gz: 9c8e3d5fa568072f870a34570a5825b503076321e3c36c7726a2321012cd2f1ec8f2cf2d0278a53c5ef0d38fb2d4ce119f1d5ded11e096b4f91c0a2df6deb038
7
+ data.tar.gz: 9cd1f714801f13e2186752b4608ea1db6f7f38276bf9d30bb6ffdeb9cd11b7dad76bb8b72a4f3b643cb54cbf4f049ec240b27f60ad98d3d58f2fa55237e1a0c5
data/Gemfile CHANGED
@@ -6,3 +6,4 @@ gem 'rails', '>= 3.2', '< 5'
6
6
  gem 'mocha'
7
7
  gem 'turn'
8
8
  gem 'awesome_print'
9
+ gem 'sqlite3'
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- turboboost (0.0.5)
4
+ turboboost (0.0.8)
5
5
  coffee-rails
6
6
  jquery-rails
7
7
  railties
@@ -10,97 +10,102 @@ PATH
10
10
  GEM
11
11
  remote: https://rubygems.org/
12
12
  specs:
13
- actionmailer (4.0.1)
14
- actionpack (= 4.0.1)
13
+ actionmailer (4.1.0)
14
+ actionpack (= 4.1.0)
15
+ actionview (= 4.1.0)
15
16
  mail (~> 2.5.4)
16
- actionpack (4.0.1)
17
- activesupport (= 4.0.1)
18
- builder (~> 3.1.0)
19
- erubis (~> 2.7.0)
17
+ actionpack (4.1.0)
18
+ actionview (= 4.1.0)
19
+ activesupport (= 4.1.0)
20
20
  rack (~> 1.5.2)
21
21
  rack-test (~> 0.6.2)
22
- activemodel (4.0.1)
23
- activesupport (= 4.0.1)
24
- builder (~> 3.1.0)
25
- activerecord (4.0.1)
26
- activemodel (= 4.0.1)
27
- activerecord-deprecated_finders (~> 1.0.2)
28
- activesupport (= 4.0.1)
29
- arel (~> 4.0.0)
30
- activerecord-deprecated_finders (1.0.3)
31
- activesupport (4.0.1)
32
- i18n (~> 0.6, >= 0.6.4)
33
- minitest (~> 4.2)
34
- multi_json (~> 1.3)
22
+ actionview (4.1.0)
23
+ activesupport (= 4.1.0)
24
+ builder (~> 3.1)
25
+ erubis (~> 2.7.0)
26
+ activemodel (4.1.0)
27
+ activesupport (= 4.1.0)
28
+ builder (~> 3.1)
29
+ activerecord (4.1.0)
30
+ activemodel (= 4.1.0)
31
+ activesupport (= 4.1.0)
32
+ arel (~> 5.0.0)
33
+ activesupport (4.1.0)
34
+ i18n (~> 0.6, >= 0.6.9)
35
+ json (~> 1.7, >= 1.7.7)
36
+ minitest (~> 5.1)
35
37
  thread_safe (~> 0.1)
36
- tzinfo (~> 0.3.37)
38
+ tzinfo (~> 1.1)
37
39
  ansi (1.4.3)
38
- arel (4.0.1)
39
- atomic (1.1.14)
40
+ arel (5.0.1.20140414130214)
40
41
  awesome_print (1.2.0)
41
- builder (3.1.4)
42
+ builder (3.2.2)
42
43
  coffee-rails (4.0.1)
43
44
  coffee-script (>= 2.2.0)
44
45
  railties (>= 4.0.0, < 5.0)
45
46
  coffee-script (2.2.0)
46
47
  coffee-script-source
47
48
  execjs
48
- coffee-script-source (1.6.3)
49
+ coffee-script-source (1.7.0)
49
50
  erubis (2.7.0)
50
51
  execjs (2.0.2)
51
52
  hike (1.2.3)
52
- i18n (0.6.5)
53
- jquery-rails (3.0.4)
53
+ i18n (0.6.9)
54
+ jquery-rails (3.1.0)
54
55
  railties (>= 3.0, < 5.0)
55
56
  thor (>= 0.14, < 2.0)
57
+ json (1.8.1)
56
58
  mail (2.5.4)
57
59
  mime-types (~> 1.16)
58
60
  treetop (~> 1.4.8)
59
61
  metaclass (0.0.1)
60
- mime-types (1.25)
61
- minitest (4.7.5)
62
+ mime-types (1.25.1)
63
+ minitest (5.3.3)
62
64
  mocha (0.14.0)
63
65
  metaclass (~> 0.0.1)
64
- multi_json (1.8.2)
65
- polyglot (0.3.3)
66
+ multi_json (1.9.2)
67
+ polyglot (0.3.4)
66
68
  rack (1.5.2)
67
69
  rack-test (0.6.2)
68
70
  rack (>= 1.0)
69
- rails (4.0.1)
70
- actionmailer (= 4.0.1)
71
- actionpack (= 4.0.1)
72
- activerecord (= 4.0.1)
73
- activesupport (= 4.0.1)
71
+ rails (4.1.0)
72
+ actionmailer (= 4.1.0)
73
+ actionpack (= 4.1.0)
74
+ actionview (= 4.1.0)
75
+ activemodel (= 4.1.0)
76
+ activerecord (= 4.1.0)
77
+ activesupport (= 4.1.0)
74
78
  bundler (>= 1.3.0, < 2.0)
75
- railties (= 4.0.1)
76
- sprockets-rails (~> 2.0.0)
77
- railties (4.0.1)
78
- actionpack (= 4.0.1)
79
- activesupport (= 4.0.1)
79
+ railties (= 4.1.0)
80
+ sprockets-rails (~> 2.0)
81
+ railties (4.1.0)
82
+ actionpack (= 4.1.0)
83
+ activesupport (= 4.1.0)
80
84
  rake (>= 0.8.7)
81
85
  thor (>= 0.18.1, < 2.0)
82
- rake (10.1.0)
83
- sprockets (2.10.0)
86
+ rake (10.3.1)
87
+ sprockets (2.12.1)
84
88
  hike (~> 1.2)
85
89
  multi_json (~> 1.0)
86
90
  rack (~> 1.0)
87
91
  tilt (~> 1.1, != 1.3.0)
88
- sprockets-rails (2.0.1)
92
+ sprockets-rails (2.1.3)
89
93
  actionpack (>= 3.0)
90
94
  activesupport (>= 3.0)
91
95
  sprockets (~> 2.8)
92
- thor (0.18.1)
93
- thread_safe (0.1.3)
94
- atomic
96
+ sqlite3 (1.3.8)
97
+ thor (0.19.1)
98
+ thread_safe (0.3.3)
95
99
  tilt (1.4.1)
96
100
  treetop (1.4.15)
97
101
  polyglot
98
102
  polyglot (>= 0.3.1)
99
- turbolinks (1.3.0)
103
+ turbolinks (2.2.2)
100
104
  coffee-rails
101
105
  turn (0.9.6)
102
106
  ansi
103
- tzinfo (0.3.38)
107
+ tzinfo (1.1.0)
108
+ thread_safe (~> 0.1)
104
109
 
105
110
  PLATFORMS
106
111
  ruby
@@ -111,5 +116,6 @@ DEPENDENCIES
111
116
  mocha
112
117
  rails (>= 3.2, < 5)
113
118
  rake
119
+ sqlite3
114
120
  turboboost!
115
121
  turn
data/README.md CHANGED
@@ -1,47 +1,22 @@
1
1
  ## Turboboost
2
2
 
3
- Turboboost extends the power of Turbolinks into the forms of your Rails app and provides convenient success and error handlers. It aims to be a seemless and logical addition to any Turbolinks-rocking Rails 3.2+/4+ app. The main features are:
3
+ Turboboost extends the power of Turbolinks into the forms of your Rails app and provides additional convenient AJAX handlers for forms and links. It aims to be a seemless and logical addition to any Turbolinks-rocking Rails 3.2+/4+ app. Currently it depends on jQuery. The main features are:
4
4
 
5
- * Response redirection is handled by Turbolinks.
6
-
7
- * Customizable success and error handling through registered JavaScript events, with optional error rendering built-in.
8
-
9
- * Responses can also be rendered within the DOM using jQuery selectors.
10
-
11
- <!-- The main features are: -->
12
-
13
- <!-- * Integration into Turbolinks-managed browser history states. -->
14
-
15
- <!-- * Faster loading upon successful form submissions with redirects, as only the body is swapped out. -->
16
-
17
- <!-- * Customizable success and error handling through registered JavaScript events, with optional error rendering built-in. -->
18
-
19
- <!-- As a bonus: since failed form submissions are caught and returned with JavaScript, you can cache your views harder since you don't have to re-render your form view with the model in an invalid state. -->
20
-
21
- <!-- ### Design Pattern -->
22
-
23
- <!-- In order to bring AJAX control over your Rails app's forms in a Turbolinks compatible way, you have to define some assumptions. The way Turboboost currently works is: -->
24
-
25
- <!-- * For GET requests, visit the form's action with the serialized data appended to as a query string with Turbolinks. This will preserve navigable history states for things like search filter forms. -->
26
- <!-- * For other request types, hit your Rails controllers then: -->
27
- <!-- - If the response has a `redirect_to` declaration, do not reload. Instead, visit that route with Turbolinks. -->
28
- <!-- - If there is an error, don't visit anything with Turbolinks. Instead, the errors will be sent through the global document event `turboboost:error`. Optionally, the errors can be prepended to the form as HTML. -->
29
- <!-- * Turboboost only works on forms that you define with `turboboost: true` in your Rails form helper options or manually with a `data-turboboost` attribute. -->
30
- <!-- * When a Turboboost has an AJAX request in process, do sensible things like disable that form's submit button. -->
31
-
32
- <!-- These are definitely open to discussion. The goal here is to be Rails 3.2+ and Rails 4+ compatible. -->
5
+ * Form response redirection is handled by Turbolinks.
6
+ * Customizable success and error handling through registered JavaScript, with support for Rails' Flash and optional error rendering built-in.
7
+ * Responses can also be rendered within a scoped DOM target using jQuery selectors.
33
8
 
34
9
  ### Installation
35
10
 
11
+ In your `Gemfile`:
12
+
36
13
  ``` ruby
37
14
  gem "turboboost"
38
15
  ```
39
16
 
40
- Put that in your `Gemfile` and `bundle install`. In your `application.js` require it after `jquery_ujs` and `turbolinks`:
17
+ In your `application.js`:
41
18
 
42
19
  ``` javascript
43
- //= require jquery_ujs
44
- //= require turbolinks
45
20
  //= require turboboost
46
21
  ```
47
22
 
@@ -65,32 +40,33 @@ In its simplest server-side implementation, a basic Turboboost controller action
65
40
 
66
41
  ``` ruby
67
42
  def create
68
- post = Post.create!(params[:post]) <- trigger exception if model is invalid
43
+ post = Post.create!(params[:post]) # <- trigger exception if model is invalid
69
44
  redirect_to post, notice: 'Post was successfully created.'
70
45
  end
71
46
  ```
72
47
 
73
- If the post is successfully created, the app will visit the `post_url` with Turbolinks if it was sent from a Turboboost. Otherwise, the redirect will happen like normal.
48
+ If the post is successfully created through a Turboboost-ed form, the app will visit the post's URL with Turbolinks. Otherwise, the redirect will happen like normal.
74
49
 
75
50
  #### Error Handling and Flash Messages
76
51
 
77
- If the post in our example above is invalid, no redirect will happen and a `rescue_from` handler will pass off the errors to JavaScript through the `turboboost:error` event:
52
+ If the post in our example above is invalid, no redirect will happen and a `rescue_from` handler will pass the errors to JavaScript through the `turboboost:error` event:
78
53
 
79
54
  ``` coffeescript
80
55
  $(document).on "turboboost:error", (e, errors) ->
81
56
  console.log(errors) # <- JSON array of errors messages
82
57
  ```
83
58
 
84
- You can also trigger the JSON error messages explicitly with the method `render_turboboost_errors_for(record)` if you don't want to use the `rescue_from` handler:
59
+ You can also trigger the JSON error messages explicitly with the method `render_turboboost_errors_for(record)` if you don't want to use the default `rescue_from` handler:
85
60
 
86
61
  ``` ruby
87
62
  def create
88
63
  @post = Post.new(post_params)
89
64
  if @post.save
90
- redirect_to @post, notice: 'Post was successfully created.'
65
+ flash[:notice] = 'Post was successfully created.'
66
+ redirect_to @post
91
67
  else
92
68
  respond_to do |format|
93
- format.html { render 'new' }
69
+ format.html { render :new }
94
70
  format.js { render_turboboost_errors_for(@post) }
95
71
  end
96
72
  end
@@ -124,11 +100,9 @@ $(document).on "turboboost:success", (e, flash) ->
124
100
  console.log(flash) # -> {'notice': 'Post was successfully created.'}
125
101
  ```
126
102
 
127
- You may also prevent redirecting on any Turboboost by adding the attribute `data-no-turboboost-redirect` to your form element if you just want to handle the response and returned flash messages manually:
128
-
129
103
  #### Scoped response rendering
130
104
 
131
- Turboboost also provides some extra rendering options for letting you render your form responses at specific locations in the DOM:
105
+ Turboboost also provides some options for rendering AJAX responses at specific locations in the DOM:
132
106
 
133
107
  |Rails controller render option | jQuery function|
134
108
  |-------------------------------|:---------------|
@@ -137,23 +111,12 @@ Turboboost also provides some extra rendering options for letting you render you
137
111
  |`:prepend` |`prepend()` |
138
112
  |`:append` |`append()` |
139
113
 
140
- The value can be any jQuery selector. Example usage:
114
+ The value can be any jQuery selector. Examples usage:
141
115
 
142
116
  ``` ruby
143
117
  respond_to do |format|
118
+ format.html
144
119
  format.js { render partial: 'task', object: @task, prepend: "#todo-list" }
145
120
  end
146
121
  ```
147
122
 
148
- or:
149
-
150
- ``` ruby
151
- respond_to do |format|
152
- format.js { render partial: 'task', object: @task, replace: "#todo-item#{@task.id}" }
153
- end
154
- ```
155
-
156
- ### Todo
157
-
158
- * More tests, obviously.
159
-
data/Rakefile CHANGED
@@ -12,4 +12,4 @@ Rake::TestTask.new(:test) do |t|
12
12
  end
13
13
 
14
14
  # Make test the default task.
15
- task :default => :test
15
+ task default: :test
@@ -1,3 +1,7 @@
1
+ #= require jquery
2
+ #= require jquery_ujs
3
+ #= require turbolinks
4
+
1
5
  @Turboboost =
2
6
  insertErrors: false
3
7
  defaultError: "Sorry, there was an error."
@@ -15,6 +19,7 @@ disableForm = ($form) ->
15
19
 
16
20
  turboboostFormError = (e, errors) ->
17
21
  return if !Turboboost.insertErrors
22
+ errors = tryJSONParse errors
18
23
  errors = [Turboboost.defaultError] if !errors.length
19
24
  $form = $(e.target)
20
25
  $el = $form.find(errID)
@@ -36,9 +41,14 @@ turboboostComplete = (e, resp) ->
36
41
 
37
42
  if resp.status in [400..599]
38
43
  enableForm $el if isForm
39
- $el.trigger "turboboost:error", tryJSONParse resp.responseText
44
+ $el.trigger "turboboost:error", resp.responseText
45
+
46
+ $el.trigger "turboboost:complete"
40
47
 
41
- turboboostFormBeforeSend = (e, xhr, settings) ->
48
+ turboboostBeforeSend = (e, xhr, settings) ->
49
+ xhr.setRequestHeader('X-Turboboost', '1')
50
+ isForm = @nodeName is "FORM"
51
+ return e.stopPropagation() unless isForm
42
52
  disableForm $(@)
43
53
  if settings.type == "GET"
44
54
  Turbolinks.visit [@action, $(@).serialize()].join("?")
@@ -61,8 +71,6 @@ maybeInsertSuccessResponseBody = (resp) ->
61
71
  $(scope).prepend(resp.responseText)
62
72
 
63
73
  $(document)
64
- .on("ajax:beforeSend", turboboostable, (e, xhr, settings) ->
65
- xhr.setRequestHeader('X-Turboboost', '1'))
66
- .on("ajax:beforeSend", "form#{turboboostable}", turboboostFormBeforeSend)
74
+ .on("ajax:beforeSend", turboboostable, turboboostBeforeSend)
67
75
  .on("ajax:complete", turboboostable, turboboostComplete)
68
76
  .on("turboboost:error", "form#{turboboostable}", turboboostFormError)
@@ -60,6 +60,7 @@ module Turboboost
60
60
  [:replace, :within, :append, :prepend].each do |h|
61
61
  response.headers["X-#{h.capitalize}"] = options[h] if options[h]
62
62
  end
63
+ response.headers["X-Flash"] = flash.to_hash.to_json if !flash.empty?
63
64
  self.response_body = render_to_body(options)
64
65
  end
65
66
 
@@ -88,6 +89,9 @@ module Turboboost
88
89
  turboboost_flash = {}
89
90
  flash_types = defined?(self.class._flash_types) ? self.class._flash_types : [:alert, :notice]
90
91
  flash_types.each do |flash_type|
92
+ if type = flash.delete(type)
93
+ turboboost_flash.update(type)
94
+ end
91
95
  if type = response_status_and_flash.delete(flash_type)
92
96
  turboboost_flash[flash_type] = type
93
97
  end
@@ -1,3 +1,3 @@
1
1
  module Turboboost
2
- VERSION = '0.0.6'
2
+ VERSION = '0.0.8'
3
3
  end
@@ -0,0 +1,124 @@
1
+ require 'test_helper'
2
+
3
+ class PostsController < ApplicationController
4
+
5
+ respond_to :html
6
+ respond_to :js, only: [:create, :update, :destroy]
7
+
8
+ def create
9
+ post = Post.create!(post_params)
10
+ redirect_to posts_url, notice: "Post was successfully created."
11
+ end
12
+
13
+ def update
14
+ post = Post.find(params[:id])
15
+ post.update_attributes!(post_params)
16
+ redirect_to post_url(post), notice: "Post was successfully updated."
17
+ end
18
+
19
+ def destroy
20
+ post = Post.find(params[:id])
21
+ post.destroy!
22
+ redirect_to posts_url
23
+ end
24
+
25
+ private
26
+
27
+ def post_params
28
+ params.require(:post).permit(:title, :user_id)
29
+ end
30
+
31
+ end
32
+
33
+ class UsersController < ApplicationController
34
+
35
+ def create
36
+ user = User.new(user_params)
37
+ if user.save
38
+ flash[:notice] = "User was successfully created."
39
+ redirect_to user_url(user)
40
+ else
41
+ respond_to do |format|
42
+ format.html { render :new }
43
+ format.js { render_turboboost_errors_for(user) }
44
+ end
45
+ end
46
+ end
47
+
48
+ def update
49
+ user = User.find(params[:id])
50
+ if user.update_attributes(user_params)
51
+ redirect_to user_url(user)
52
+ else
53
+ respond_to do |format|
54
+ format.html { redirect_to users_url }
55
+ format.js { render_turboboost_errors_for(user) }
56
+ end
57
+ end
58
+ end
59
+
60
+ private
61
+
62
+ def user_params
63
+ params.require(:user).permit(:name, :email)
64
+ end
65
+
66
+ end
67
+
68
+ class PostsControllerTest < ActionController::TestCase
69
+
70
+ tests PostsController
71
+
72
+ setup do
73
+ @request.headers["X-Turboboost"] = "1"
74
+ end
75
+
76
+ test "On a successful turboboost request, return an empty response with headers containing the redirect location and flash message" do
77
+ xhr :post, :create, post: { title: "Foobar", user_id: "1" }
78
+
79
+ assert @response.body.strip.blank?
80
+ assert_equal flash[:notice], 'Post was successfully created.'
81
+ assert_equal @response.headers["Location"], posts_url
82
+ assert_equal JSON.parse(@response.headers["X-Flash"])["notice"], 'Post was successfully created.'
83
+ end
84
+
85
+ test "On an unsuccessful turboboost request, catch and return the error message(s) as an array" do
86
+ xhr :post, :create, post: { title: "Title", user_id: nil }
87
+
88
+ assert_equal @response.status, 422
89
+ assert_equal @response.body.strip, ["User can't be blank"].to_json
90
+
91
+ xhr :post, :create, post: { title: "Tit", user_id: nil }
92
+
93
+ assert_equal @response.status, 422
94
+ assert_equal @response.body.strip, ["Title is too short.", "User can't be blank"].to_json
95
+ end
96
+
97
+ end
98
+
99
+
100
+ class UsersControllerTest < ActionController::TestCase
101
+
102
+ tests UsersController
103
+
104
+ setup do
105
+ @request.headers["X-Turboboost"] = "1"
106
+ end
107
+
108
+ test "On a successful turboboost request, return an empty response with headers containing the redirect location and flash message" do
109
+ xhr :post, :create, user: { name: "Mike", email: "mike@mike.com" }
110
+
111
+ assert @response.body.strip.blank?
112
+ assert_equal flash[:notice], 'User was successfully created.'
113
+ assert_equal @response.headers["Location"], user_url(1)
114
+ assert_equal JSON.parse(@response.headers["X-Flash"])["notice"], 'User was successfully created.'
115
+ end
116
+
117
+ test "On an unsuccessful turboboost request, explicitly render the error message(s)" do
118
+ xhr :post, :create, user: { name: "Mike", email: "mike at mike.com" }
119
+
120
+ assert_equal @response.status, 422
121
+ assert_equal @response.body.strip, ["Email is invalid"].to_json
122
+ end
123
+
124
+ end
@@ -9,12 +9,12 @@ require "active_record"
9
9
  require "action_controller"
10
10
  require "action_controller"
11
11
  require "action_controller/railtie"
12
-
13
12
  require 'rails/test_help'
14
13
  require 'awesome_print'
15
-
16
14
  require 'turboboost'
17
15
 
16
+ ActiveRecord::Base.establish_connection adapter: "sqlite3", database: ":memory:"
17
+
18
18
  class TestApp < Rails::Application; end
19
19
  Rails.application = TestApp
20
20
  Rails.configuration.secret_key_base = "abc123"
@@ -22,6 +22,7 @@ Rails.configuration.secret_key_base = "abc123"
22
22
  Turboboost::Routes = ActionDispatch::Routing::RouteSet.new
23
23
  Turboboost::Routes.draw do
24
24
  resources 'posts'
25
+ resources 'users'
25
26
  end
26
27
 
27
28
  class ApplicationController < ActionController::Base
@@ -33,3 +34,40 @@ class ActiveSupport::TestCase
33
34
  @routes = Turboboost::Routes
34
35
  end
35
36
  end
37
+
38
+ class ActionView::TestCase
39
+
40
+ def default_url_options; {}; end
41
+
42
+ include Turboboost::Routes.url_helpers
43
+ include Turboboost::FormHelper
44
+
45
+ setup do
46
+ @controller = ApplicationController
47
+ end
48
+
49
+ end
50
+
51
+ posts_table = %{CREATE TABLE posts (id INTEGER PRIMARY KEY, title VARCHAR(5), user_id INTEGER);}
52
+ ActiveRecord::Base.connection.execute(posts_table)
53
+
54
+ class Post < ActiveRecord::Base
55
+
56
+ attr_accessor :title, :user_id
57
+
58
+ validates :title, length: { minimum: 5, message: "is too short." }
59
+ validates :user_id, presence: true
60
+
61
+ end
62
+
63
+ users_table = %{CREATE TABLE users (id INTEGER PRIMARY KEY, name VARCHAR(5), email VARCHAR(255));}
64
+ ActiveRecord::Base.connection.execute(users_table)
65
+
66
+ class User < ActiveRecord::Base
67
+
68
+ attr_accessor :name, :email
69
+
70
+ validates :name, presence: true
71
+ validates :email, format: { with: /\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\z/i, allow_blank: false }
72
+
73
+ end
@@ -0,0 +1,34 @@
1
+ require 'test_helper'
2
+
3
+ class PostViewTest < ActionView::TestCase
4
+
5
+ setup do
6
+ @post = Post.new
7
+ end
8
+
9
+ def with_concat_form_for(*args, &block)
10
+ concat form_for(*args, &(block || proc {}))
11
+ end
12
+
13
+ def with_concat_form_tag(*args, &block)
14
+ concat form_tag(*args, &(block || proc {}))
15
+ end
16
+
17
+ def test_form_for
18
+ with_concat_form_for(@post, turboboost: true, html: { data: { foo: "bar" } })
19
+ assert_select "form[data-remote]"
20
+ assert_select "form[data-turboboost]"
21
+ assert_select "form[data-foo='bar']"
22
+ assert_select "form.new_post"
23
+ assert_select "form#new_post"
24
+ end
25
+
26
+ def test_form_tag
27
+ with_concat_form_tag("/posts", turboboost: true, class: "post-form", data: { bar: "baz" })
28
+ assert_select "form[data-remote]"
29
+ assert_select "form.post-form"
30
+ assert_select "form[data-bar='baz']"
31
+ assert_select "form[data-turboboost]"
32
+ end
33
+
34
+ end
metadata CHANGED
@@ -1,97 +1,97 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: turboboost
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.6
4
+ version: 0.0.8
5
5
  platform: ruby
6
6
  authors:
7
7
  - Justin Talbott
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-12-04 00:00:00.000000000 Z
11
+ date: 2014-04-22 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: railties
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
- - - ! '>='
17
+ - - ">="
18
18
  - !ruby/object:Gem::Version
19
19
  version: '0'
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
- - - ! '>='
24
+ - - ">="
25
25
  - !ruby/object:Gem::Version
26
26
  version: '0'
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: turbolinks
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
- - - ! '>='
31
+ - - ">="
32
32
  - !ruby/object:Gem::Version
33
33
  version: '0'
34
34
  type: :runtime
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
- - - ! '>='
38
+ - - ">="
39
39
  - !ruby/object:Gem::Version
40
40
  version: '0'
41
41
  - !ruby/object:Gem::Dependency
42
42
  name: coffee-rails
43
43
  requirement: !ruby/object:Gem::Requirement
44
44
  requirements:
45
- - - ! '>='
45
+ - - ">="
46
46
  - !ruby/object:Gem::Version
47
47
  version: '0'
48
48
  type: :runtime
49
49
  prerelease: false
50
50
  version_requirements: !ruby/object:Gem::Requirement
51
51
  requirements:
52
- - - ! '>='
52
+ - - ">="
53
53
  - !ruby/object:Gem::Version
54
54
  version: '0'
55
55
  - !ruby/object:Gem::Dependency
56
56
  name: jquery-rails
57
57
  requirement: !ruby/object:Gem::Requirement
58
58
  requirements:
59
- - - ! '>='
59
+ - - ">="
60
60
  - !ruby/object:Gem::Version
61
61
  version: '0'
62
62
  type: :runtime
63
63
  prerelease: false
64
64
  version_requirements: !ruby/object:Gem::Requirement
65
65
  requirements:
66
- - - ! '>='
66
+ - - ">="
67
67
  - !ruby/object:Gem::Version
68
68
  version: '0'
69
69
  - !ruby/object:Gem::Dependency
70
70
  name: bundler
71
71
  requirement: !ruby/object:Gem::Requirement
72
72
  requirements:
73
- - - ~>
73
+ - - "~>"
74
74
  - !ruby/object:Gem::Version
75
75
  version: '1.3'
76
76
  type: :development
77
77
  prerelease: false
78
78
  version_requirements: !ruby/object:Gem::Requirement
79
79
  requirements:
80
- - - ~>
80
+ - - "~>"
81
81
  - !ruby/object:Gem::Version
82
82
  version: '1.3'
83
83
  - !ruby/object:Gem::Dependency
84
84
  name: rake
85
85
  requirement: !ruby/object:Gem::Requirement
86
86
  requirements:
87
- - - ! '>='
87
+ - - ">="
88
88
  - !ruby/object:Gem::Version
89
89
  version: '0'
90
90
  type: :development
91
91
  prerelease: false
92
92
  version_requirements: !ruby/object:Gem::Requirement
93
93
  requirements:
94
- - - ! '>='
94
+ - - ">="
95
95
  - !ruby/object:Gem::Version
96
96
  version: '0'
97
97
  description: Turbolinks and AJAX enhancements for your Rails forms and links.
@@ -101,7 +101,7 @@ executables: []
101
101
  extensions: []
102
102
  extra_rdoc_files: []
103
103
  files:
104
- - .gitignore
104
+ - ".gitignore"
105
105
  - Gemfile
106
106
  - Gemfile.lock
107
107
  - README.md
@@ -109,8 +109,9 @@ files:
109
109
  - app/assets/javascripts/turboboost.js.coffee
110
110
  - lib/turboboost.rb
111
111
  - lib/turboboost/version.rb
112
- - test/posts_test.rb
112
+ - test/controller_test.rb
113
113
  - test/test_helper.rb
114
+ - test/view_test.rb
114
115
  - turboboost.gemspec
115
116
  homepage: ''
116
117
  licenses:
@@ -122,21 +123,21 @@ require_paths:
122
123
  - lib
123
124
  required_ruby_version: !ruby/object:Gem::Requirement
124
125
  requirements:
125
- - - ! '>='
126
+ - - ">="
126
127
  - !ruby/object:Gem::Version
127
128
  version: '0'
128
129
  required_rubygems_version: !ruby/object:Gem::Requirement
129
130
  requirements:
130
- - - ! '>='
131
+ - - ">="
131
132
  - !ruby/object:Gem::Version
132
133
  version: '0'
133
134
  requirements: []
134
135
  rubyforge_project:
135
- rubygems_version: 2.1.11
136
+ rubygems_version: 2.2.0
136
137
  signing_key:
137
138
  specification_version: 4
138
139
  summary: Turbolinks and AJAX enhancements for your Rails forms and links.
139
140
  test_files:
140
- - test/posts_test.rb
141
+ - test/controller_test.rb
141
142
  - test/test_helper.rb
142
- has_rdoc:
143
+ - test/view_test.rb
@@ -1,115 +0,0 @@
1
- require 'test_helper'
2
-
3
- class Post
4
- extend ActiveModel::Naming
5
-
6
- include ActiveModel::Conversion
7
- include ActiveModel::Model
8
- include ActiveModel::Validations
9
-
10
- attr_accessor :title
11
-
12
- validates! :title, length: { minimum: 1, message: "is too short." }
13
-
14
- def self.create!(attributes)
15
- post = self.new(attributes)
16
- post.save
17
- end
18
-
19
- def save
20
- valid?
21
- end
22
-
23
- def update_attributes!(attributes)
24
- attributes = attributes
25
- valid?
26
- end
27
-
28
- end
29
-
30
- class PostsController < ApplicationController
31
-
32
- respond_to :html
33
- respond_to :js, only: [:create, :update, :destroy]
34
-
35
- def index; end
36
- def show; end
37
- def new; end
38
- def edit; end
39
-
40
- def create
41
- post = Post.create!(params[:post])
42
- redirect_to posts_url, notice: "Post was successfully created."
43
- end
44
-
45
- def update
46
- post = Post.find(params[:id])
47
- post.update_attributes!
48
- redirect_to post_url(post), notice: "Post was successfully updated."
49
- end
50
-
51
- def destroy
52
- post = Post.find(params[:id])
53
- post.destroy!
54
- redirect_to posts_url
55
- end
56
-
57
- end
58
-
59
- class PostsVerboseController < PostsController
60
-
61
- def create
62
- post = post.new(params[:post])
63
- if post.save
64
- redirect_to post_url(post)
65
- else
66
- respond_to do |format|
67
- format.html { redirect_to posts_url }
68
- format.js { render_turboboost_errors_for(post) }
69
- end
70
- end
71
- end
72
-
73
- def update
74
- post = post.find(params[:id])
75
- if post.update_attributes(params[:post])
76
- redirect_to post_url(post)
77
- else
78
- respond_to do |format|
79
- format.html { redirect_to posts_url }
80
- format.js { render_turboboost_errors_for(post) }
81
- end
82
- end
83
- end
84
-
85
- end
86
-
87
- class PostsControllerTest < ActionController::TestCase
88
-
89
- tests PostsController
90
-
91
- setup do
92
- @request.headers["X-Turboboost"] = "1"
93
- end
94
-
95
- test "On a successful turboboost request, return an empty response with headers containing the redirect location and flash message" do
96
- xhr :post, :create, post: { title: "Foo" }
97
-
98
- assert @response.body.strip.blank?
99
- assert_equal flash[:notice], 'Post was successfully created.'
100
- assert_equal @response.headers["Location"], posts_url
101
- assert_equal JSON.parse(@response.headers["X-Flash"])["notice"], 'Post was successfully created.'
102
- end
103
-
104
- test "On an unsuccessful turboboost request, catch and return the error message(s) as an array" do
105
- xhr :post, :create, post: { title: "" }
106
-
107
- assert_equal @response.status, 422
108
- assert_equal @response.body.strip, ["Title is too short."].to_json
109
- end
110
-
111
- test "On an unsuccessful turboboost request, explicitly render the error message(s)" do
112
-
113
- end
114
-
115
- end