dismissible_blocks 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.
- checksums.yaml +7 -0
- data/Gemfile +7 -0
- data/LICENSE.text +22 -0
- data/README.markdown +152 -0
- data/Rakefile +34 -0
- data/app/assets/javascripts/dismissible_blocks.js +37 -0
- data/app/assets/javascripts/dismissible_blocks_loader.js +3 -0
- data/app/controllers/dismissible_blocks_controller.rb +3 -0
- data/app/helpers/dismissible_blocks_helper.rb +3 -0
- data/config/routes.rb +3 -0
- data/lib/dismissible_blocks/controller.rb +18 -0
- data/lib/dismissible_blocks/engine.rb +4 -0
- data/lib/dismissible_blocks/helper.rb +34 -0
- data/lib/dismissible_blocks/version.rb +3 -0
- data/lib/dismissible_blocks.rb +13 -0
- data/test/capybara_helper.rb +33 -0
- data/test/controllers/dismissible_blocks_controller_test.rb +18 -0
- data/test/dummy/Rakefile +3 -0
- data/test/dummy/app/assets/javascripts/application.js +3 -0
- data/test/dummy/app/assets/stylesheets/application.css +17 -0
- data/test/dummy/app/controllers/application_controller.rb +29 -0
- data/test/dummy/app/controllers/landings_controller.rb +6 -0
- data/test/dummy/app/controllers/sessions_controller.rb +28 -0
- data/test/dummy/app/helpers/application_helper.rb +2 -0
- data/test/dummy/app/helpers/landings_helper.rb +2 -0
- data/test/dummy/app/helpers/sessions_helper.rb +2 -0
- data/test/dummy/app/models/user.rb +9 -0
- data/test/dummy/app/views/landings/show.html.erb +10 -0
- data/test/dummy/app/views/layouts/application.html.erb +18 -0
- data/test/dummy/app/views/sessions/new.html.erb +15 -0
- data/test/dummy/bin/bundle +3 -0
- data/test/dummy/bin/rails +4 -0
- data/test/dummy/bin/rake +4 -0
- data/test/dummy/config/application.rb +10 -0
- data/test/dummy/config/boot.rb +4 -0
- data/test/dummy/config/database.yml +11 -0
- data/test/dummy/config/environment.rb +3 -0
- data/test/dummy/config/environments/development.rb +29 -0
- data/test/dummy/config/environments/test.rb +36 -0
- data/test/dummy/config/initializers/filter_parameter_logging.rb +1 -0
- data/test/dummy/config/initializers/secret_token.rb +1 -0
- data/test/dummy/config/initializers/session_store.rb +1 -0
- data/test/dummy/config/routes.rb +7 -0
- data/test/dummy/config.ru +3 -0
- data/test/dummy/db/migrate/20140303154458_create_users.rb +10 -0
- data/test/dummy/db/migrate/20140303161500_add_dismissed_blocks_to_users.rb +9 -0
- data/test/dummy/db/schema.rb +24 -0
- data/test/dummy/db/test.sqlite3 +0 -0
- data/test/dummy/log/development.log +6003 -0
- data/test/dummy/log/test.log +21144 -0
- data/test/dummy/public/404.html +58 -0
- data/test/dummy/public/422.html +58 -0
- data/test/dummy/public/500.html +57 -0
- data/test/dummy/public/favicon.ico +0 -0
- data/test/dummy/tmp/cache/assets/development/sprockets/13fe41fee1fe35b49d145bcc06610705 +0 -0
- data/test/dummy/tmp/cache/assets/development/sprockets/2f5173deea6c795b8fdde723bb4b63af +0 -0
- data/test/dummy/tmp/cache/assets/development/sprockets/357970feca3ac29060c1e3861e2c0953 +0 -0
- data/test/dummy/tmp/cache/assets/development/sprockets/489c5c881967a5316ffa31442761ffcb +0 -0
- data/test/dummy/tmp/cache/assets/development/sprockets/48d901fc62129ded86005b628a7f0f6f +0 -0
- data/test/dummy/tmp/cache/assets/development/sprockets/58a8328804a777933d757eb9a448eaf5 +0 -0
- data/test/dummy/tmp/cache/assets/development/sprockets/96c147c638b08a4c0178d9c66c3b9c48 +0 -0
- data/test/dummy/tmp/cache/assets/development/sprockets/9708d20fa7cc14e3657195722d1a3948 +0 -0
- data/test/dummy/tmp/cache/assets/development/sprockets/996481d37a8199e763e8fcf21a801422 +0 -0
- data/test/dummy/tmp/cache/assets/development/sprockets/ac2d5385dbcd9cea11c510dc83189bba +0 -0
- data/test/dummy/tmp/cache/assets/development/sprockets/bec12e1e92fe458e19b5f9869454ac1a +0 -0
- data/test/dummy/tmp/cache/assets/development/sprockets/cffd775d018f68ce5dba1ee0d951a994 +0 -0
- data/test/dummy/tmp/cache/assets/development/sprockets/d771ace226fc8215a3572e0aa35bb0d6 +0 -0
- data/test/dummy/tmp/cache/assets/development/sprockets/ecbb0d7ff91e9d89fc001dcc70d1d48f +0 -0
- data/test/dummy/tmp/cache/assets/development/sprockets/f7cbd26ba1d28d48de824f0e94586655 +0 -0
- data/test/dummy/tmp/cache/assets/test/sprockets/13fe41fee1fe35b49d145bcc06610705 +0 -0
- data/test/dummy/tmp/cache/assets/test/sprockets/2f5173deea6c795b8fdde723bb4b63af +0 -0
- data/test/dummy/tmp/cache/assets/test/sprockets/357970feca3ac29060c1e3861e2c0953 +0 -0
- data/test/dummy/tmp/cache/assets/test/sprockets/58a8328804a777933d757eb9a448eaf5 +0 -0
- data/test/dummy/tmp/cache/assets/test/sprockets/96c147c638b08a4c0178d9c66c3b9c48 +0 -0
- data/test/dummy/tmp/cache/assets/test/sprockets/9708d20fa7cc14e3657195722d1a3948 +0 -0
- data/test/dummy/tmp/cache/assets/test/sprockets/ac2d5385dbcd9cea11c510dc83189bba +0 -0
- data/test/dummy/tmp/cache/assets/test/sprockets/bec12e1e92fe458e19b5f9869454ac1a +0 -0
- data/test/dummy/tmp/cache/assets/test/sprockets/cffd775d018f68ce5dba1ee0d951a994 +0 -0
- data/test/dummy/tmp/cache/assets/test/sprockets/d771ace226fc8215a3572e0aa35bb0d6 +0 -0
- data/test/dummy/tmp/cache/assets/test/sprockets/ecbb0d7ff91e9d89fc001dcc70d1d48f +0 -0
- data/test/dummy/tmp/cache/assets/test/sprockets/f7cbd26ba1d28d48de824f0e94586655 +0 -0
- data/test/features/dismissible_blocks_test.rb +21 -0
- data/test/fixtures/users.yml +9 -0
- data/test/helpers/dismissible_blocks_helper_test.rb +53 -0
- data/test/test_helper.rb +9 -0
- metadata +297 -0
checksums.yaml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
---
|
|
2
|
+
SHA1:
|
|
3
|
+
metadata.gz: 790003b0d118f8a317c7a4ddfd43d7492314b8b1
|
|
4
|
+
data.tar.gz: 45a0169db67ceb236117ac3cf76cfcbdf80e5d77
|
|
5
|
+
SHA512:
|
|
6
|
+
metadata.gz: c9d9e57eb204c9071580e8d405aa91b871e2cec3127cc5f9c0953afc0e0aaabd70f182650cd6d9de7af8db86ac9c16b6295e5c3c2af6df710d94b1ab598949b0
|
|
7
|
+
data.tar.gz: 41d4a657714f5df13227ac55ba2ab443dd382ef453019b869c3091280d3c2e5a5fd28375a1241186b3c316b01ded4ad9b79923c2b16959500dd35b378b84a018
|
data/Gemfile
ADDED
data/LICENSE.text
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
Copyright (c) 2014 Patrick Bougie
|
|
2
|
+
|
|
3
|
+
MIT License
|
|
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
|
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.markdown
ADDED
|
@@ -0,0 +1,152 @@
|
|
|
1
|
+
# DismissibleBlocks
|
|
2
|
+
|
|
3
|
+
## Overview
|
|
4
|
+
**DismissibleBlocks** is a Ruby on Rails engine that allows for dismissible blocks of HTML code whose state is saved in the database per user.
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
## Installation
|
|
8
|
+
### Requirements
|
|
9
|
+
The DismissibleBlocks gem has the following requirements:
|
|
10
|
+
|
|
11
|
+
- [Ruby on Rails](http://rubyonrails.org/) 3.2 or above
|
|
12
|
+
- [jQuery](http://jquery.com/)
|
|
13
|
+
|
|
14
|
+
### Gemfile
|
|
15
|
+
Add the following line to your application's Gemfile:
|
|
16
|
+
|
|
17
|
+
gem 'dismissible_blocks'
|
|
18
|
+
|
|
19
|
+
And then execute:
|
|
20
|
+
|
|
21
|
+
$ bundle install
|
|
22
|
+
|
|
23
|
+
### Manual Installation
|
|
24
|
+
Install it yourself:
|
|
25
|
+
|
|
26
|
+
$ gem install dismissible_blocks
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
## Usage
|
|
30
|
+
### Routes
|
|
31
|
+
DismissibleBlocks automatically adds the required routes when the gem is added to your project.
|
|
32
|
+
|
|
33
|
+
### Views
|
|
34
|
+
Adding dismissible blocks of HTML code is very easy. DismissibleBlocks does not generate any extra HTML and therefore gives you complete control over your code.
|
|
35
|
+
|
|
36
|
+
To add a dismissible block to a view, use the `render_dismissible_block` helper method. This will create a dismissible block named `lorem`:
|
|
37
|
+
|
|
38
|
+
<%= render_dismissible_block 'lorem' do %>
|
|
39
|
+
...
|
|
40
|
+
<% end %>
|
|
41
|
+
|
|
42
|
+
This alone isn't enough, you need to identify the container and button HTML. This is done using HTML5 `data-` attributes. There are two attributes that you must add to the HTML to make everything work as expected:
|
|
43
|
+
|
|
44
|
+
1. `data-dismissible`: attribute for the container of the HTML block.
|
|
45
|
+
2. `data-dismissible-hide`: attribute for the button to hide the HTML block.
|
|
46
|
+
|
|
47
|
+
For example:
|
|
48
|
+
|
|
49
|
+
<%= render_dismissible_block 'lorem' do %>
|
|
50
|
+
<div data-dismissible>
|
|
51
|
+
<p>...</p>
|
|
52
|
+
<a href="#" data-dismissible-hide>Hide</a>
|
|
53
|
+
</div>
|
|
54
|
+
<% end %>
|
|
55
|
+
|
|
56
|
+
Also make sure the [Cross-Site Request Forgery](http://guides.rubyonrails.org/security.html#cross-site-request-forgery-csrf) (CSRF) token is included in your layout:
|
|
57
|
+
|
|
58
|
+
<%= csrf_meta_tags %>
|
|
59
|
+
|
|
60
|
+
### JavaScript
|
|
61
|
+
Add the following JavaScript to `app/assets/javascripts/application.js`.
|
|
62
|
+
|
|
63
|
+
In its simplest form, you can require all the needed JavaScript using:
|
|
64
|
+
|
|
65
|
+
//= require jquery
|
|
66
|
+
//= require dismissible_blocks
|
|
67
|
+
//= require dismissible_blocks_loader
|
|
68
|
+
|
|
69
|
+
If you want to customize how a block of HTML is hidden using — for example — a slide up effect, you can customize the JavaScript like so:
|
|
70
|
+
|
|
71
|
+
//= require jquery
|
|
72
|
+
//= require dismissible_blocks
|
|
73
|
+
|
|
74
|
+
$(document).ready(function() {
|
|
75
|
+
$('[data-dismissible]').dismissible({
|
|
76
|
+
dismiss: function(helper) {
|
|
77
|
+
helper.slideUp();
|
|
78
|
+
}
|
|
79
|
+
});
|
|
80
|
+
});
|
|
81
|
+
|
|
82
|
+
### Model
|
|
83
|
+
|
|
84
|
+
DismissibleBlocks saves the dismissed blocks on a model. It uses the `current_user` helper method to access the current user/account.
|
|
85
|
+
|
|
86
|
+
The model must have an attributed named `dismissed_blocks` and be of type Array.
|
|
87
|
+
|
|
88
|
+
#### ActiveRecord
|
|
89
|
+
ActiveRecord's serialization feature can achieve this. First, create a database migration to add the required field:
|
|
90
|
+
|
|
91
|
+
class AddDismissedBlocksToUsers < ActiveRecord::Migration
|
|
92
|
+
def up
|
|
93
|
+
add_column :users, :dismissed_blocks, :text
|
|
94
|
+
end
|
|
95
|
+
|
|
96
|
+
def down
|
|
97
|
+
remove_column :users, :dismissed_blocks
|
|
98
|
+
end
|
|
99
|
+
end
|
|
100
|
+
|
|
101
|
+
Then add the serialization to the model:
|
|
102
|
+
|
|
103
|
+
class User < ActiveRecord::Base
|
|
104
|
+
serialize :dismissed_blocks, Array
|
|
105
|
+
end
|
|
106
|
+
|
|
107
|
+
#### ActiveRecord (using PostgreSQL)
|
|
108
|
+
If you are using [PostgreSQL](http://www.postgresql.org/) with native array support using [PostgresExt](https://github.com/dockyard/postgres_ext), your database migration would simply be:
|
|
109
|
+
|
|
110
|
+
class AddDismissedBlocksToUsers < ActiveRecord::Migration
|
|
111
|
+
def up
|
|
112
|
+
add_column :users, :dismissed_blocks, :string, :array => true
|
|
113
|
+
end
|
|
114
|
+
|
|
115
|
+
def down
|
|
116
|
+
remove_column :users, :dismissed_blocks
|
|
117
|
+
end
|
|
118
|
+
end
|
|
119
|
+
|
|
120
|
+
#### Mongoid
|
|
121
|
+
To add MongoDB support using [Mongoid](mongoid.org), add this to the user/account model:
|
|
122
|
+
|
|
123
|
+
field :dismissed_blocks, type: Array, default: []
|
|
124
|
+
|
|
125
|
+
#### Helper Method Not `current_user`
|
|
126
|
+
By default, DismissibleBlocks saves the state to the database using the `current_user` helper method. If your user/account helper method is named something else — for example `current_employee`:
|
|
127
|
+
|
|
128
|
+
def current_employee
|
|
129
|
+
...
|
|
130
|
+
end
|
|
131
|
+
helper_method :current_employee
|
|
132
|
+
|
|
133
|
+
Use `alias_method` to create an alias to your helper method; don't forget to also include `helper_method` to make your helper available from your views:
|
|
134
|
+
|
|
135
|
+
alias_method :current_user, :current_employee
|
|
136
|
+
helper_method :current_user
|
|
137
|
+
|
|
138
|
+
## Author
|
|
139
|
+
[Patrick Bougie](http://patrickbougie.com/)
|
|
140
|
+
|
|
141
|
+
|
|
142
|
+
## Contributing
|
|
143
|
+
|
|
144
|
+
1. Fork DismissibleBlocks: `https://github.com/pbougie/dismissible_blocks`
|
|
145
|
+
2. Create your feature branch: `git checkout -b new-feature`
|
|
146
|
+
3. Commit your changes: `git commit -am 'New feature description'`
|
|
147
|
+
4. Push to the branch: `git push origin new-feature`
|
|
148
|
+
5. Create a new Pull Request on [GitHub](https://github.com/)
|
|
149
|
+
|
|
150
|
+
|
|
151
|
+
## Legal
|
|
152
|
+
DismissibleBlocks is copyright © 2014 [Patrick Bougie](http://patrickbougie.com/). It is free software and may be redistributed under the terms specified in the LICENSE.text file.
|
data/Rakefile
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
begin
|
|
2
|
+
require 'bundler/setup'
|
|
3
|
+
rescue LoadError
|
|
4
|
+
puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
|
|
5
|
+
end
|
|
6
|
+
|
|
7
|
+
require 'rdoc/task'
|
|
8
|
+
|
|
9
|
+
RDoc::Task.new(:rdoc) do |rdoc|
|
|
10
|
+
rdoc.rdoc_dir = 'rdoc'
|
|
11
|
+
rdoc.title = 'DismissibleBlocks'
|
|
12
|
+
rdoc.options << '--line-numbers'
|
|
13
|
+
rdoc.rdoc_files.include('README.rdoc')
|
|
14
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
APP_RAKEFILE = File.expand_path("../test/dummy/Rakefile", __FILE__)
|
|
18
|
+
load 'rails/tasks/engine.rake'
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
Bundler::GemHelper.install_tasks
|
|
23
|
+
|
|
24
|
+
require 'rake/testtask'
|
|
25
|
+
|
|
26
|
+
Rake::TestTask.new(:test) do |t|
|
|
27
|
+
t.libs << 'lib'
|
|
28
|
+
t.libs << 'test'
|
|
29
|
+
t.pattern = 'test/**/*_test.rb'
|
|
30
|
+
t.verbose = false
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
task default: :test
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
(function() {
|
|
2
|
+
(function($) {
|
|
3
|
+
|
|
4
|
+
$.fn.dismissible = function(options) {
|
|
5
|
+
return this.each(function() {
|
|
6
|
+
var _this = $(this);
|
|
7
|
+
$('[data-dismissible-hide]', this).click(function(event) {
|
|
8
|
+
event.preventDefault();
|
|
9
|
+
return _this.dismiss(options);
|
|
10
|
+
})
|
|
11
|
+
});
|
|
12
|
+
};
|
|
13
|
+
|
|
14
|
+
return $.fn.dismiss = function(options) {
|
|
15
|
+
return $.ajax({
|
|
16
|
+
type: 'POST',
|
|
17
|
+
url: '/dismissible_blocks.json',
|
|
18
|
+
dataType: 'json',
|
|
19
|
+
contentType: 'application/json',
|
|
20
|
+
data: JSON.stringify({
|
|
21
|
+
block: $(this).attr('data-dismissible'),
|
|
22
|
+
authenticity_token: $('meta[name=csrf-token]').attr('content')
|
|
23
|
+
}),
|
|
24
|
+
success: (function(_this) {
|
|
25
|
+
return function() {
|
|
26
|
+
if ((options != null) && options.dismiss !== void 0) {
|
|
27
|
+
return options.dismiss(_this);
|
|
28
|
+
} else {
|
|
29
|
+
return $(_this).remove();
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
})(this)
|
|
33
|
+
});
|
|
34
|
+
};
|
|
35
|
+
|
|
36
|
+
})(jQuery);
|
|
37
|
+
}).call(this);
|
data/config/routes.rb
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
module DismissibleBlocks::Controller
|
|
2
|
+
|
|
3
|
+
def create
|
|
4
|
+
if current_user_available
|
|
5
|
+
current_user.dismissed_blocks += [ params[:block].to_s ]
|
|
6
|
+
current_user.save!
|
|
7
|
+
render :json => {}, :status => :ok
|
|
8
|
+
else
|
|
9
|
+
render :json => {}, :status => :unprocessable_entity
|
|
10
|
+
end
|
|
11
|
+
end
|
|
12
|
+
|
|
13
|
+
private
|
|
14
|
+
|
|
15
|
+
def current_user_available
|
|
16
|
+
respond_to?(:current_user) && current_user.respond_to?(:dismissed_blocks)
|
|
17
|
+
end
|
|
18
|
+
end
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
module DismissibleBlocks::Helper
|
|
2
|
+
|
|
3
|
+
def render_dismissible_block(name, &block)
|
|
4
|
+
unless dismissed?(name)
|
|
5
|
+
if block_given?
|
|
6
|
+
contents = capture(&block)
|
|
7
|
+
add_block_name_to_attributes(contents, name)
|
|
8
|
+
else
|
|
9
|
+
raise DismissibleBlocks::ContentMissing
|
|
10
|
+
end
|
|
11
|
+
end
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
private
|
|
15
|
+
|
|
16
|
+
def dismissed?(name)
|
|
17
|
+
if respond_to?(:current_user)
|
|
18
|
+
if !current_user.respond_to?(:dismissed_blocks)
|
|
19
|
+
raise DismissibleBlocks::AttributeUnavailable
|
|
20
|
+
elsif !current_user.dismissed_blocks.is_a?(Array)
|
|
21
|
+
raise DismissibleBlocks::AttributeNotArray
|
|
22
|
+
else
|
|
23
|
+
current_user.dismissed_blocks.include?(name)
|
|
24
|
+
end
|
|
25
|
+
else
|
|
26
|
+
raise DismissibleBlocks::ModelUnavailable
|
|
27
|
+
end
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
def add_block_name_to_attributes(contents, name)
|
|
31
|
+
contents.gsub! /(data-dismissible)(-hide)?(?!-)/, "\\1\\2='#{name}'"
|
|
32
|
+
contents.html_safe
|
|
33
|
+
end
|
|
34
|
+
end
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
require 'dismissible_blocks/engine'
|
|
2
|
+
|
|
3
|
+
module DismissibleBlocks
|
|
4
|
+
extend ActiveSupport::Autoload
|
|
5
|
+
|
|
6
|
+
class ModelUnavailable < StandardError; end
|
|
7
|
+
class AttributeUnavailable < StandardError; end
|
|
8
|
+
class AttributeNotArray < StandardError; end
|
|
9
|
+
class ContentMissing < StandardError; end
|
|
10
|
+
|
|
11
|
+
autoload :Controller
|
|
12
|
+
autoload :Helper
|
|
13
|
+
end
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
require 'test_helper'
|
|
2
|
+
require 'capybara/rails'
|
|
3
|
+
|
|
4
|
+
class ActionDispatch::IntegrationTest
|
|
5
|
+
include Capybara::DSL
|
|
6
|
+
|
|
7
|
+
def login(user)
|
|
8
|
+
visit login_path
|
|
9
|
+
fill_in 'Username', with: user.username
|
|
10
|
+
fill_in 'Password', with: user.password
|
|
11
|
+
click_button 'Login'
|
|
12
|
+
assert current_path == landing_path
|
|
13
|
+
assert page.has_content? 'You have been logged in.'
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
def logout
|
|
17
|
+
visit logout_path
|
|
18
|
+
assert current_path == login_path
|
|
19
|
+
assert page.has_content? 'You have been logged out.'
|
|
20
|
+
end
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
# Prevent database locks when testing AJAX features
|
|
24
|
+
# https://github.com/jnicklas/capybara#transactions-and-database-setup
|
|
25
|
+
class ActiveRecord::Base
|
|
26
|
+
mattr_accessor :shared_connection
|
|
27
|
+
@@shared_connection = nil
|
|
28
|
+
|
|
29
|
+
def self.connection
|
|
30
|
+
@@shared_connection || retrieve_connection
|
|
31
|
+
end
|
|
32
|
+
end
|
|
33
|
+
ActiveRecord::Base.shared_connection = ActiveRecord::Base.connection
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
require 'test_helper'
|
|
2
|
+
|
|
3
|
+
class DismissibleBlocksControllerTest < ActionController::TestCase
|
|
4
|
+
|
|
5
|
+
test "dismiss block via ajax" do
|
|
6
|
+
session[:username] = users(:one).username # Login
|
|
7
|
+
refute users(:one).dismissed_blocks.include?('lorem')
|
|
8
|
+
xhr :post, :create, block: 'lorem', format: 'json'
|
|
9
|
+
users(:one).reload
|
|
10
|
+
assert users(:one).dismissed_blocks.include?('lorem')
|
|
11
|
+
assert_response :ok
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
test "dismiss block fails via ajax; not logged in" do
|
|
15
|
+
xhr :post, :create, block: 'lorem', format: 'json'
|
|
16
|
+
assert_response :unprocessable_entity
|
|
17
|
+
end
|
|
18
|
+
end
|
data/test/dummy/Rakefile
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
/*
|
|
2
|
+
*= require_self
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
.flash, .block {
|
|
6
|
+
border: 2px solid #333;
|
|
7
|
+
padding: 10px 20px;
|
|
8
|
+
background-color: #eee;
|
|
9
|
+
}
|
|
10
|
+
.flash-alert {
|
|
11
|
+
border-color: #ebccd1;
|
|
12
|
+
background-color: #f2dede;
|
|
13
|
+
}
|
|
14
|
+
.flash-notice {
|
|
15
|
+
border-color: #bce8f1;
|
|
16
|
+
background-color: #d9edf7;
|
|
17
|
+
}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
class ApplicationController < ActionController::Base
|
|
2
|
+
protect_from_forgery with: :exception
|
|
3
|
+
|
|
4
|
+
def current_user
|
|
5
|
+
if session[:username].present?
|
|
6
|
+
@current_user ||= User.find_by(username: session[:username])
|
|
7
|
+
end
|
|
8
|
+
end
|
|
9
|
+
helper_method :current_user
|
|
10
|
+
|
|
11
|
+
def check_authentication
|
|
12
|
+
unless session[:username].present?
|
|
13
|
+
raise Dummy::Unauthorized
|
|
14
|
+
end
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
def authenticated?
|
|
18
|
+
session[:username].present?
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
def signin(username)
|
|
23
|
+
session[:username] = username
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
def signout
|
|
27
|
+
reset_session
|
|
28
|
+
end
|
|
29
|
+
end
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
class SessionsController < ApplicationController
|
|
2
|
+
before_filter :check_authentication, only: :destroy
|
|
3
|
+
|
|
4
|
+
def new
|
|
5
|
+
if authenticated?
|
|
6
|
+
flash[:notice] = 'You are already logged in.'
|
|
7
|
+
redirect_to landing_url
|
|
8
|
+
end
|
|
9
|
+
end
|
|
10
|
+
|
|
11
|
+
def create
|
|
12
|
+
user = User.authenticate params[:username], params[:password]
|
|
13
|
+
if user
|
|
14
|
+
signin user.username
|
|
15
|
+
flash[:notice] = 'You have been logged in.'
|
|
16
|
+
redirect_to landing_url
|
|
17
|
+
else
|
|
18
|
+
flash.now[:alert] = 'Your username and/or password are incorrect.'
|
|
19
|
+
render :new
|
|
20
|
+
end
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
def destroy
|
|
24
|
+
signout
|
|
25
|
+
flash[:notice] = 'You have been logged out.'
|
|
26
|
+
redirect_to login_url
|
|
27
|
+
end
|
|
28
|
+
end
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
<h1>Dummy</h1>
|
|
2
|
+
|
|
3
|
+
<%= render_dismissible_block 'lorem' do %>
|
|
4
|
+
<div class="block" data-dismissible>
|
|
5
|
+
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Cras vitae.</p>
|
|
6
|
+
<p><a href="#" data-dismissible-hide>Hide</a></p>
|
|
7
|
+
</div>
|
|
8
|
+
<% end %>
|
|
9
|
+
|
|
10
|
+
<p><%= link_to 'Logout', logout_path %></p>
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
<!doctype html>
|
|
2
|
+
<html>
|
|
3
|
+
<head>
|
|
4
|
+
<title>Dummy</title>
|
|
5
|
+
<%= stylesheet_link_tag 'application' %>
|
|
6
|
+
<%= javascript_include_tag 'application' %>
|
|
7
|
+
<%= csrf_meta_tags %>
|
|
8
|
+
</head>
|
|
9
|
+
<body>
|
|
10
|
+
<% flash.each do |name, message| %>
|
|
11
|
+
<%= content_tag :p, class: "flash flash-#{name}" do %>
|
|
12
|
+
<%= message %>
|
|
13
|
+
<% end %>
|
|
14
|
+
<% end %>
|
|
15
|
+
|
|
16
|
+
<%= yield %>
|
|
17
|
+
</body>
|
|
18
|
+
</html>
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
<%= form_tag login_path do %>
|
|
2
|
+
<p>
|
|
3
|
+
<%= label_tag :username %>
|
|
4
|
+
<%= text_field_tag :username, params[:username] %>
|
|
5
|
+
</p>
|
|
6
|
+
|
|
7
|
+
<p>
|
|
8
|
+
<%= label_tag :password %>
|
|
9
|
+
<%= password_field_tag :password %>
|
|
10
|
+
</p>
|
|
11
|
+
|
|
12
|
+
<p>
|
|
13
|
+
<%= submit_tag 'Login' %>
|
|
14
|
+
</p>
|
|
15
|
+
<% end %>
|
data/test/dummy/bin/rake
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
Dummy::Application.configure do
|
|
2
|
+
# Settings specified here will take precedence over those in config/application.rb.
|
|
3
|
+
|
|
4
|
+
# In the development environment your application's code is reloaded on
|
|
5
|
+
# every request. This slows down response time but is perfect for development
|
|
6
|
+
# since you don't have to restart the web server when you make code changes.
|
|
7
|
+
config.cache_classes = false
|
|
8
|
+
|
|
9
|
+
# Do not eager load code on boot.
|
|
10
|
+
config.eager_load = false
|
|
11
|
+
|
|
12
|
+
# Show full error reports and disable caching.
|
|
13
|
+
config.consider_all_requests_local = true
|
|
14
|
+
config.action_controller.perform_caching = false
|
|
15
|
+
|
|
16
|
+
# Don't care if the mailer can't send.
|
|
17
|
+
config.action_mailer.raise_delivery_errors = false
|
|
18
|
+
|
|
19
|
+
# Print deprecation notices to the Rails logger.
|
|
20
|
+
config.active_support.deprecation = :log
|
|
21
|
+
|
|
22
|
+
# Raise an error on page load if there are pending migrations
|
|
23
|
+
config.active_record.migration_error = :page_load
|
|
24
|
+
|
|
25
|
+
# Debug mode disables concatenation and preprocessing of assets.
|
|
26
|
+
# This option may cause significant delays in view rendering with a large
|
|
27
|
+
# number of complex assets.
|
|
28
|
+
config.assets.debug = true
|
|
29
|
+
end
|