worth_saving 0.0.9
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +15 -0
- data/MIT-LICENSE +20 -0
- data/README.md +90 -0
- data/Rakefile +29 -0
- data/app/assets/javascripts/worth_saving/application.js +15 -0
- data/app/assets/stylesheets/worth_saving/application.css +13 -0
- data/app/controllers/worth_saving/application_controller.rb +4 -0
- data/app/controllers/worth_saving/drafts_controller.rb +51 -0
- data/app/helpers/worth_saving/application_helper.rb +4 -0
- data/app/models/worth_saving/draft.rb +47 -0
- data/app/views/layouts/worth_saving/application.html.erb +14 -0
- data/config/routes.rb +3 -0
- data/db/migrate/20130911055236_create_worth_saving_drafts.rb +16 -0
- data/lib/tasks/worth_saving_tasks.rake +4 -0
- data/lib/worth_saving.rb +12 -0
- data/lib/worth_saving/action_view_ext.rb +11 -0
- data/lib/worth_saving/active_record_ext.rb +39 -0
- data/lib/worth_saving/engine.rb +15 -0
- data/lib/worth_saving/form.rb +15 -0
- data/lib/worth_saving/form/base.rb +21 -0
- data/lib/worth_saving/form/record.rb +53 -0
- data/lib/worth_saving/form/rendering.rb +100 -0
- data/lib/worth_saving/form_builder.rb +57 -0
- data/lib/worth_saving/info.rb +102 -0
- data/lib/worth_saving/version.rb +3 -0
- data/spec/app/controllers/drafts_controller_spec.rb +13 -0
- data/spec/app/models/worth_saving/draft_spec.rb +156 -0
- data/spec/dummy/README.rdoc +261 -0
- data/spec/dummy/Rakefile +7 -0
- data/spec/dummy/app/assets/javascripts/application.js +15 -0
- data/spec/dummy/app/assets/stylesheets/application.css +15 -0
- data/spec/dummy/app/controllers/application_controller.rb +24 -0
- data/spec/dummy/app/controllers/login_controller.rb +11 -0
- data/spec/dummy/app/controllers/pages_controller.rb +48 -0
- data/spec/dummy/app/helpers/application_helper.rb +2 -0
- data/spec/dummy/app/models/page.rb +12 -0
- data/spec/dummy/app/models/subpage.rb +7 -0
- data/spec/dummy/app/models/user.rb +4 -0
- data/spec/dummy/app/views/layouts/application.html.erb +15 -0
- data/spec/dummy/app/views/pages/form.html.slim +53 -0
- data/spec/dummy/config.ru +4 -0
- data/spec/dummy/config/application.rb +66 -0
- data/spec/dummy/config/boot.rb +10 -0
- data/spec/dummy/config/database.yml +25 -0
- data/spec/dummy/config/environment.rb +5 -0
- data/spec/dummy/config/environments/development.rb +37 -0
- data/spec/dummy/config/environments/production.rb +67 -0
- data/spec/dummy/config/environments/test.rb +37 -0
- data/spec/dummy/config/initializers/backtrace_silencers.rb +7 -0
- data/spec/dummy/config/initializers/inflections.rb +15 -0
- data/spec/dummy/config/initializers/mime_types.rb +5 -0
- data/spec/dummy/config/initializers/secret_token.rb +7 -0
- data/spec/dummy/config/initializers/session_store.rb +8 -0
- data/spec/dummy/config/initializers/wrap_parameters.rb +14 -0
- data/spec/dummy/config/locales/en.yml +5 -0
- data/spec/dummy/config/routes.rb +8 -0
- data/spec/dummy/db/development.sqlite3 +0 -0
- data/spec/dummy/db/migrate/20130911060226_create_worth_saving_drafts.worth_saving.rb +17 -0
- data/spec/dummy/db/migrate/20130912071319_create_users.rb +9 -0
- data/spec/dummy/db/migrate/20130912071446_create_pages.rb +9 -0
- data/spec/dummy/db/migrate/20130912145831_create_subpages.rb +10 -0
- data/spec/dummy/db/migrate/20130913060709_add_columns_to_subpages.rb +5 -0
- data/spec/dummy/db/migrate/20130913060847_add_columns_to_pages.rb +7 -0
- data/spec/dummy/db/schema.rb +52 -0
- data/spec/dummy/db/test.sqlite3 +0 -0
- data/spec/dummy/log/development.log +17536 -0
- data/spec/dummy/log/test.log +76256 -0
- data/spec/dummy/public/404.html +26 -0
- data/spec/dummy/public/422.html +26 -0
- data/spec/dummy/public/500.html +25 -0
- data/spec/dummy/public/favicon.ico +0 -0
- data/spec/dummy/script/rails +6 -0
- data/spec/dummy/tmp/cache/assets/CA6/7A0/sprockets%2Ff6723fb0918cb134f62480f155a0b364 +0 -0
- data/spec/dummy/tmp/cache/assets/CD8/370/sprockets%2F357970feca3ac29060c1e3861e2c0953 +0 -0
- data/spec/dummy/tmp/cache/assets/CF8/AF0/sprockets%2F228f63c526665cc53d670799eece3b98 +0 -0
- data/spec/dummy/tmp/cache/assets/D09/190/sprockets%2F993fae97313ed7d42322c85e60b02bb1 +0 -0
- data/spec/dummy/tmp/cache/assets/D1C/CE0/sprockets%2F9a5f4484900982bd33f792ca06e7f5ea +0 -0
- data/spec/dummy/tmp/cache/assets/D31/620/sprockets%2F01f101ae68348ed1bb4e46d7f452a73a +0 -0
- data/spec/dummy/tmp/cache/assets/D32/A10/sprockets%2F13fe41fee1fe35b49d145bcc06610705 +0 -0
- data/spec/dummy/tmp/cache/assets/D3C/300/sprockets%2Ffb893496ed88af814fa985858f60ee66 +0 -0
- data/spec/dummy/tmp/cache/assets/D43/EB0/sprockets%2F43d74a38cb4f14de5087ea7f871ac166 +0 -0
- data/spec/dummy/tmp/cache/assets/D4E/1B0/sprockets%2Ff7cbd26ba1d28d48de824f0e94586655 +0 -0
- data/spec/dummy/tmp/cache/assets/D5A/EA0/sprockets%2Fd771ace226fc8215a3572e0aa35bb0d6 +0 -0
- data/spec/dummy/tmp/cache/assets/D97/C50/sprockets%2F74b5c193fc06fb3cb3c4ac12ac7589a7 +0 -0
- data/spec/dummy/tmp/cache/assets/D9E/470/sprockets%2F36bde6f05aa955c51ab2cd97b59e742a +0 -0
- data/spec/dummy/tmp/cache/assets/DCF/000/sprockets%2Fcfbeb9f4e8b3b7d1ac752d93c64017d3 +0 -0
- data/spec/dummy/tmp/cache/assets/DDC/400/sprockets%2Fcffd775d018f68ce5dba1ee0d951a994 +0 -0
- data/spec/dummy/tmp/cache/assets/E04/890/sprockets%2F2f5173deea6c795b8fdde723bb4b63af +0 -0
- data/spec/dummy/tmp/cache/assets/test/sprockets/01f101ae68348ed1bb4e46d7f452a73a +0 -0
- data/spec/dummy/tmp/cache/assets/test/sprockets/13fe41fee1fe35b49d145bcc06610705 +0 -0
- data/spec/dummy/tmp/cache/assets/test/sprockets/228f63c526665cc53d670799eece3b98 +0 -0
- data/spec/dummy/tmp/cache/assets/test/sprockets/2f5173deea6c795b8fdde723bb4b63af +0 -0
- data/spec/dummy/tmp/cache/assets/test/sprockets/357970feca3ac29060c1e3861e2c0953 +0 -0
- data/spec/dummy/tmp/cache/assets/test/sprockets/36bde6f05aa955c51ab2cd97b59e742a +0 -0
- data/spec/dummy/tmp/cache/assets/test/sprockets/43d74a38cb4f14de5087ea7f871ac166 +0 -0
- data/spec/dummy/tmp/cache/assets/test/sprockets/74b5c193fc06fb3cb3c4ac12ac7589a7 +0 -0
- data/spec/dummy/tmp/cache/assets/test/sprockets/993fae97313ed7d42322c85e60b02bb1 +0 -0
- data/spec/dummy/tmp/cache/assets/test/sprockets/9a5f4484900982bd33f792ca06e7f5ea +0 -0
- data/spec/dummy/tmp/cache/assets/test/sprockets/cfbeb9f4e8b3b7d1ac752d93c64017d3 +0 -0
- data/spec/dummy/tmp/cache/assets/test/sprockets/cffd775d018f68ce5dba1ee0d951a994 +0 -0
- data/spec/dummy/tmp/cache/assets/test/sprockets/d771ace226fc8215a3572e0aa35bb0d6 +0 -0
- data/spec/dummy/tmp/cache/assets/test/sprockets/f6723fb0918cb134f62480f155a0b364 +0 -0
- data/spec/dummy/tmp/cache/assets/test/sprockets/f7cbd26ba1d28d48de824f0e94586655 +0 -0
- data/spec/dummy/tmp/cache/assets/test/sprockets/fb893496ed88af814fa985858f60ee66 +0 -0
- data/spec/factories.rb +28 -0
- data/spec/features/worth_saving_features_spec.rb +60 -0
- data/spec/lib/active_record_ext_spec.rb +291 -0
- data/spec/lib/info_spec.rb +29 -0
- data/spec/spec_helper.rb +37 -0
- data/spec/support/features.rb +8 -0
- data/spec/support/features/helpers.rb +96 -0
- data/vendor/assets/javascripts/worth_saving.js +10 -0
- data/vendor/assets/javascripts/worth_saving/form.js +64 -0
- data/vendor/assets/stylesheets/worth_saving.css +18 -0
- metadata +424 -0
checksums.yaml
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
---
|
2
|
+
!binary "U0hBMQ==":
|
3
|
+
metadata.gz: !binary |-
|
4
|
+
ZjlmNDY3YThhOGEwMzNjOTgzYzNiNTdjNDUyMDY0ODEwODU4NTE2OA==
|
5
|
+
data.tar.gz: !binary |-
|
6
|
+
MTgzZmQyN2E2MGNhNGFiNDM3YTE0OTZjYTJiYTZjNDRjNWQxMTRlNQ==
|
7
|
+
SHA512:
|
8
|
+
metadata.gz: !binary |-
|
9
|
+
OTA2YTM1NTJlOTBkM2RhZGY1NzgxNDk2MjY4ZWE1MzJmZDQzMjg4NzBlM2Nh
|
10
|
+
NmU1MGYyYjZiZmU3ZDY4ZWNjY2EwMTE5MjVlYjI1Njk3NzA1ZjY0NTBiMzlh
|
11
|
+
NjQ1M2EwNTc3ZTVkMzk3MTNhMGZlYjQ4MjM3YjE3YzRiNTk3OWM=
|
12
|
+
data.tar.gz: !binary |-
|
13
|
+
NGRmY2QwNTU0YTI3ZGFkODcwYjJkMDY4MjYxZGY4NDI4ODJkNThjN2JhNWU4
|
14
|
+
ZGYwMjJjOWQ3NGM4NjA2OGUxYmVhM2IyNjYwZWJlYTM2ODgxNWI3M2M3YzYy
|
15
|
+
MWY5YzdiYTgwMDhhYjFhNGYzNGI5NmQwYWUxOGVjNWI4MzhmMTE=
|
data/MIT-LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright 2013 YOURNAME
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,90 @@
|
|
1
|
+
# worth_saving gem
|
2
|
+
|
3
|
+
[![Build Status](https://travis-ci.org/johnnylaw/worth_saving.png)](http://travis-ci.org/johnnylaw/worth_saving)
|
4
|
+
[![Code Climate](https://codeclimate.com/github/johnnylaw/worth_saving.png)](https://codeclimate.com/github/johnnylaw/worth_saving)
|
5
|
+
[![Coverage Status](https://coveralls.io/repos/johnnylaw/worth_saving/badge.png)](https://coveralls.io/r/johnnylaw/worth_saving)
|
6
|
+
|
7
|
+
|
8
|
+
### What is worth_saving?
|
9
|
+
|
10
|
+
The worth_saving gem is a Rails engine intended for those creating web applications whose forms are long, where editing can sometimes go on for a long period without saving. When recovery of these unsaved edits is important, the worth_saving gem's magic makes saving and recovering draft copies a cinch.
|
11
|
+
|
12
|
+
### How to start?
|
13
|
+
|
14
|
+
Install worth_saving as a gem:
|
15
|
+
|
16
|
+
gem install worth_saving
|
17
|
+
|
18
|
+
Include worth_saving in your Gemfile with `gem 'worth_saving'`.
|
19
|
+
|
20
|
+
To completely set up your application to work you will need to include the following in one of your javascript manifests that is included wherever you will use worth_saving:
|
21
|
+
|
22
|
+
//= require worth_saving
|
23
|
+
|
24
|
+
You may also include the following in a stylesheet manifest, although you may find that you want to define your own CSS:
|
25
|
+
|
26
|
+
/*= require worth_saving
|
27
|
+
|
28
|
+
By mounting the engine in your routes.rb file, you will provide the necessary routing for drafts to save automatically while editing objects whose classes use the worth_saving functionality:
|
29
|
+
|
30
|
+
mount WorthSaving::Engine => '/worth_saving'
|
31
|
+
|
32
|
+
In any model whose edits you want to draft, place the following:
|
33
|
+
|
34
|
+
class Example < ActiveRecord::Base
|
35
|
+
is_worth_saving
|
36
|
+
end
|
37
|
+
|
38
|
+
There are two options:
|
39
|
+
|
40
|
+
is_worth_saving scope: :user, except: :title
|
41
|
+
|
42
|
+
#### The :scope option
|
43
|
+
|
44
|
+
When working on a new record, the draft copy is saved without the benefit of an ID on the main record being drafted, but it is imperative for retrieving the draft later that some uniquely identifying information be saved in the draft. In the example above, perhaps the Page class is_worth_saving. If a page belongs_to :user, then a natural choice for scope would be user. In this way you ensure that two different users with unsaved changes to new Page records cannot accidentally retrieve and see one another's drafts.
|
45
|
+
|
46
|
+
#### The :except option
|
47
|
+
|
48
|
+
Certain fields may contain sensitive information that should not be drafted. Additionally any filter_parameters from your Rails app config will always be redacted from the WorthSaving::Draft records to maintain the privacy you would expect for your users.
|
49
|
+
|
50
|
+
#### Creating a form that autosaves drafts
|
51
|
+
|
52
|
+
To invoke the form that will automatically incorporate all the magic, use the following wherever you would use form_for:
|
53
|
+
|
54
|
+
worth_saving_form_for(@page, save_interval: 15, recovery_message: I18n.t("worth_saving.recovery_message")) do |f|
|
55
|
+
= f.label :title
|
56
|
+
br
|
57
|
+
= f.text_field :title
|
58
|
+
|
59
|
+
...
|
60
|
+
|
61
|
+
|
62
|
+
The :save_interval option is the number of seconds that will pass after the first unsaved change before firing a draft copy off to the engine. It is not necessary to put this option here, and it defaults to whatever you set it in initialization or 60 seconds if you do nothing:
|
63
|
+
|
64
|
+
WorthSaving.configure do |config|
|
65
|
+
config.default_save_interval = <number of seconds to wait after changes occur before saving draft>
|
66
|
+
end
|
67
|
+
|
68
|
+
The :recovery_message is a message that appears when a user visits a page where the record being edited has a draft copy. It too has a default already built in, which can be overridden in the configure block or fed as an option to #worth_saving_form_for as above.
|
69
|
+
|
70
|
+
#### Security
|
71
|
+
|
72
|
+
Not just anyone should be able to save a draft copy of a record; only someone authorized to edit that record should. Two methods can be implemented in your ApplicationController class to prevent people from saving drafts to records they do not have the authority to edit (or scopes they do have the authority to add records to). The WorthSaving::DraftsController will defer to these methods when processing draft posts/patches/puts if they are defined in ApplicationController. If you do not implement them here, it's assumed that anyone can create a draft for any record. It is recommended you implement these if the records themselves require authorization.
|
73
|
+
|
74
|
+
Example (How you might implement these methods if using CanCan; #authorized_to_draft_with_scope? will be called only if the record is not persisted):
|
75
|
+
|
76
|
+
class ApplicationController < ActionController::Base
|
77
|
+
def authorized_to_draft_record?(record)
|
78
|
+
can? :manage, record
|
79
|
+
end
|
80
|
+
|
81
|
+
def authorized_to_draft_with_scope?(scope)
|
82
|
+
can? :add_records, scope
|
83
|
+
end
|
84
|
+
end
|
85
|
+
|
86
|
+
#### Upcoming changes
|
87
|
+
* support for simple_form
|
88
|
+
* support for TinyMCE and CKEditor (currently the values in the editor window can be overlooked, as the JS simply grabs the value of the textarea or input)
|
89
|
+
* support for additional namespaced DraftsControllers that inherit from controllers in your application (e.g. class WorthSaving::Admin::DraftsController < AdminController accessed automatically when invoking worth_saving_form_for [:admin, @record] ...)
|
90
|
+
|
data/Rakefile
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
#!/usr/bin/env rake
|
2
|
+
begin
|
3
|
+
require 'bundler/setup'
|
4
|
+
rescue LoadError
|
5
|
+
puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
|
6
|
+
end
|
7
|
+
begin
|
8
|
+
require 'rdoc/task'
|
9
|
+
rescue LoadError
|
10
|
+
require 'rdoc/rdoc'
|
11
|
+
require 'rake/rdoctask'
|
12
|
+
RDoc::Task = Rake::RDocTask
|
13
|
+
end
|
14
|
+
|
15
|
+
RDoc::Task.new(:rdoc) do |rdoc|
|
16
|
+
rdoc.rdoc_dir = 'rdoc'
|
17
|
+
rdoc.title = 'WorthSaving'
|
18
|
+
rdoc.options << '--line-numbers'
|
19
|
+
rdoc.rdoc_files.include('README.rdoc')
|
20
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
21
|
+
end
|
22
|
+
|
23
|
+
APP_RAKEFILE = File.expand_path("../spec/dummy/Rakefile", __FILE__)
|
24
|
+
load 'rails/tasks/engine.rake'
|
25
|
+
|
26
|
+
|
27
|
+
|
28
|
+
Bundler::GemHelper.install_tasks
|
29
|
+
|
@@ -0,0 +1,15 @@
|
|
1
|
+
// This is a manifest file that'll be compiled into application.js, which will include all the files
|
2
|
+
// listed below.
|
3
|
+
//
|
4
|
+
// Any JavaScript/Coffee file within this directory, lib/assets/javascripts, vendor/assets/javascripts,
|
5
|
+
// or vendor/assets/javascripts of plugins, if any, can be referenced here using a relative path.
|
6
|
+
//
|
7
|
+
// It's not advisable to add code directly here, but if you do, it'll appear at the bottom of the
|
8
|
+
// the compiled file.
|
9
|
+
//
|
10
|
+
// WARNING: THE FIRST BLANK LINE MARKS THE END OF WHAT'S TO BE PROCESSED, ANY BLANK LINE SHOULD
|
11
|
+
// GO AFTER THE REQUIRES BELOW.
|
12
|
+
//
|
13
|
+
//= require jquery
|
14
|
+
//= require_self
|
15
|
+
|
@@ -0,0 +1,13 @@
|
|
1
|
+
/*
|
2
|
+
* This is a manifest file that'll be compiled into application.css, which will include all the files
|
3
|
+
* listed below.
|
4
|
+
*
|
5
|
+
* Any CSS and SCSS file within this directory, lib/assets/stylesheets, vendor/assets/stylesheets,
|
6
|
+
* or vendor/assets/stylesheets of plugins, if any, can be referenced here using a relative path.
|
7
|
+
*
|
8
|
+
* You're free to add application-wide styles to this file and they'll appear at the top of the
|
9
|
+
* compiled file, but it's generally better to create a new file per style scope.
|
10
|
+
*
|
11
|
+
*= require_self
|
12
|
+
*= require_tree .
|
13
|
+
*/
|
@@ -0,0 +1,51 @@
|
|
1
|
+
class WorthSaving::DraftsController < WorthSaving::ApplicationController
|
2
|
+
def create
|
3
|
+
@draft = WorthSaving::Draft.new draft_params
|
4
|
+
save_if_authorized_and_respond
|
5
|
+
end
|
6
|
+
|
7
|
+
def update
|
8
|
+
@draft = WorthSaving::Draft.find params[:id]
|
9
|
+
@draft.attributes = draft_params
|
10
|
+
save_if_authorized_and_respond
|
11
|
+
end
|
12
|
+
|
13
|
+
private
|
14
|
+
|
15
|
+
def draft_params
|
16
|
+
if WorthSaving::Info.attribute_whitelisting_required?
|
17
|
+
params[:draft]
|
18
|
+
else
|
19
|
+
params.require(:draft).permit :recordable_id, :recordable_type, :scopeable_id, :scopeable_type, :form_data
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
def save_if_authorized_and_respond
|
24
|
+
authorized? ? save_and_respond : render_forbidden_error
|
25
|
+
end
|
26
|
+
|
27
|
+
def save_and_respond
|
28
|
+
if @draft.save
|
29
|
+
render json: { message: 'Draft saved', worthSavingDraft: { action: worth_saving.draft_path(@draft) } }
|
30
|
+
else
|
31
|
+
render json: { error: 'Unable to save draft' }
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
def render_forbidden_error
|
36
|
+
render status: 403, text: "Not authorized to draft this #{@draft.recordable_type}"
|
37
|
+
end
|
38
|
+
|
39
|
+
def authorized?
|
40
|
+
return authorized_to_draft_record?(@draft.recordable) if @draft.recordable
|
41
|
+
authorized_to_draft_with_scope?(@draft.scopeable)
|
42
|
+
end
|
43
|
+
|
44
|
+
def authorized_to_draft_record?(recordable)
|
45
|
+
defined?(super) ? super : true
|
46
|
+
end
|
47
|
+
|
48
|
+
def authorized_to_draft_with_scope?(scopeable)
|
49
|
+
defined?(super) ? super : true
|
50
|
+
end
|
51
|
+
end
|
@@ -0,0 +1,47 @@
|
|
1
|
+
class WorthSaving::Draft < ActiveRecord::Base
|
2
|
+
if WorthSaving::Info.attribute_whitelisting_required?
|
3
|
+
attr_accessible :scopeable, :recordable_id, :recordable_type, :scopeable_id, :scopeable_type, :form_data
|
4
|
+
end
|
5
|
+
|
6
|
+
self.table_name = 'worth_saving_drafts'
|
7
|
+
belongs_to :recordable, polymorphic: true
|
8
|
+
belongs_to :scopeable, polymorphic: true
|
9
|
+
|
10
|
+
validates :recordable_type, presence: true
|
11
|
+
validate :uniquely_findable, if: ->{ recordable_type && scoped_record? }
|
12
|
+
|
13
|
+
def reconstituted_record
|
14
|
+
return unless recordable_class
|
15
|
+
record = recordable || recordable_class.new
|
16
|
+
record.attributes = recordable_params
|
17
|
+
record
|
18
|
+
end
|
19
|
+
|
20
|
+
private
|
21
|
+
|
22
|
+
def uniquely_findable
|
23
|
+
unless recordable || scopeable
|
24
|
+
errors.add :base, <<-EOS
|
25
|
+
Cannot save draft for new record without scopeable record.
|
26
|
+
Be sure to build draft from #{recordable_type} with a non-nil #{scope_name}.
|
27
|
+
EOS
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
def scope_name
|
32
|
+
recordable_class && recordable_class.worth_saving_info.scope
|
33
|
+
end
|
34
|
+
|
35
|
+
def scoped_record?
|
36
|
+
recordable_class.worth_saving_info.scope.present?
|
37
|
+
end
|
38
|
+
|
39
|
+
def recordable_class
|
40
|
+
@recordable_class ||= WorthSaving::Info.class_with_name recordable_type
|
41
|
+
end
|
42
|
+
|
43
|
+
def recordable_params
|
44
|
+
params = Rack::Utils.parse_nested_query(form_data)
|
45
|
+
params[recordable_type.underscore]
|
46
|
+
end
|
47
|
+
end
|
@@ -0,0 +1,14 @@
|
|
1
|
+
<!DOCTYPE html>
|
2
|
+
<html>
|
3
|
+
<head>
|
4
|
+
<title>WorthSaving</title>
|
5
|
+
<%= stylesheet_link_tag "worth_saving/application", :media => "all" %>
|
6
|
+
<%= javascript_include_tag "worth_saving/application" %>
|
7
|
+
<%= csrf_meta_tags %>
|
8
|
+
</head>
|
9
|
+
<body>
|
10
|
+
|
11
|
+
<%= yield %>
|
12
|
+
|
13
|
+
</body>
|
14
|
+
</html>
|
data/config/routes.rb
ADDED
@@ -0,0 +1,16 @@
|
|
1
|
+
class CreateWorthSavingDrafts < ActiveRecord::Migration
|
2
|
+
def change
|
3
|
+
create_table :worth_saving_drafts do |t|
|
4
|
+
t.integer :recordable_id
|
5
|
+
t.string :recordable_type
|
6
|
+
t.integer :scopeable_id
|
7
|
+
t.string :scopeable_type
|
8
|
+
t.text :form_data
|
9
|
+
|
10
|
+
t.timestamps
|
11
|
+
end
|
12
|
+
|
13
|
+
add_index :worth_saving_drafts, [:recordable_id, :recordable_type], name: :worth_saving_draft_recordable_index
|
14
|
+
add_index :worth_saving_drafts, [:recordable_type, :scopeable_id, :scopeable_type], name: :worth_saving_draft_scopeable_index
|
15
|
+
end
|
16
|
+
end
|
data/lib/worth_saving.rb
ADDED
@@ -0,0 +1,12 @@
|
|
1
|
+
require "worth_saving/engine"
|
2
|
+
require 'worth_saving/info'
|
3
|
+
require 'worth_saving/active_record_ext'
|
4
|
+
require 'worth_saving/form_builder'
|
5
|
+
require 'worth_saving/form'
|
6
|
+
require 'worth_saving/action_view_ext'
|
7
|
+
|
8
|
+
module WorthSaving
|
9
|
+
end
|
10
|
+
|
11
|
+
ActiveRecord::Base.send :include, WorthSaving::ActiveRecordExt
|
12
|
+
ActionView::Base.send :include, WorthSaving::ActionViewExt
|
@@ -0,0 +1,39 @@
|
|
1
|
+
module WorthSaving
|
2
|
+
module ActiveRecordExt
|
3
|
+
def self.included(base)
|
4
|
+
base.extend ClassMethods
|
5
|
+
base.send :include, InstanceMethods
|
6
|
+
end
|
7
|
+
|
8
|
+
module ClassMethods
|
9
|
+
def is_worth_saving(opts = {})
|
10
|
+
class_eval do
|
11
|
+
has_one :worth_saving_draft, class_name: 'WorthSaving::Draft', as: :recordable
|
12
|
+
before_save :destroy_worth_saving_draft
|
13
|
+
|
14
|
+
@worth_saving_info = WorthSaving::Info.new self, opts
|
15
|
+
|
16
|
+
def self.worth_saving_info
|
17
|
+
@worth_saving_info
|
18
|
+
end
|
19
|
+
|
20
|
+
private
|
21
|
+
|
22
|
+
def destroy_worth_saving_draft
|
23
|
+
worth_saving_draft && worth_saving_draft.destroy
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
def worth_saving?(field_name = nil)
|
29
|
+
respond_to?(:worth_saving_info) && worth_saving_info.saves_field?(field_name)
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
module InstanceMethods
|
34
|
+
def worth_saving?(field_name = nil)
|
35
|
+
self.class.worth_saving? field_name
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
module WorthSaving
|
2
|
+
class Engine < ::Rails::Engine
|
3
|
+
isolate_namespace WorthSaving
|
4
|
+
config.default_save_interval = 60
|
5
|
+
config.default_recovery_message = <<EOS
|
6
|
+
It appears you were working on a draft that didn't get saved.
|
7
|
+
It is recommended that you choose one now, as no more draft copies will be saved until you do.
|
8
|
+
EOS
|
9
|
+
end
|
10
|
+
|
11
|
+
def self.configure(&block)
|
12
|
+
yield Engine.config if block
|
13
|
+
Engine.config
|
14
|
+
end
|
15
|
+
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
require 'worth_saving/form/record'
|
2
|
+
require 'worth_saving/form/rendering'
|
3
|
+
require 'worth_saving/form/base'
|
4
|
+
|
5
|
+
module WorthSaving
|
6
|
+
module Form
|
7
|
+
WORTH_SAVING_DRAFT_FORM_BLOCK = proc { |f|
|
8
|
+
f.hidden_field(:recordable_id) +
|
9
|
+
f.hidden_field(:recordable_type) +
|
10
|
+
f.hidden_field(:scopeable_id) +
|
11
|
+
f.hidden_field(:scopeable_type) +
|
12
|
+
f.hidden_field(:form_data)
|
13
|
+
}
|
14
|
+
end
|
15
|
+
end
|