drafting 0.1.0 → 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +65 -6
- data/lib/drafting/base_class_methods.rb +4 -2
- data/lib/drafting/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 88b15d49a289bae4e3b54fd33020792a23c8383e
|
4
|
+
data.tar.gz: 3a23d7a0924d227f79df9dcdc2c486ab14763160
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: cc5a190a3a9d2219f8fd410bb7bfedce0128e21611812d63a1c3c5e07a532be390cf3d583e6e674e7321641ace0daf0e090d68f7d35647dad7918d863b8975e3
|
7
|
+
data.tar.gz: c0e3576d2e7b78b7042a9e7e0af119290ab3131e10585661de9bf35bbfdf3a512b35cef8380c22063bd851d8bc2abbccb6274925be619c1c4196f5fa939e7c83
|
data/README.md
CHANGED
@@ -2,14 +2,19 @@
|
|
2
2
|
|
3
3
|
This Ruby gem enhances `ActiveRecord::Base` to save a draft version of the current instance.
|
4
4
|
|
5
|
-
|
5
|
+
[![Build Status](https://travis-ci.org/ledermann/drafting.svg?branch=master)](https://travis-ci.org/ledermann/drafting)
|
6
|
+
[![Code Climate](https://codeclimate.com/github/ledermann/drafting/badges/gpa.svg)](https://codeclimate.com/github/ledermann/drafting)
|
7
|
+
[![Coverage Status](https://coveralls.io/repos/ledermann/drafting/badge.svg?branch=master)](https://coveralls.io/r/ledermann/drafting?branch=master)
|
8
|
+
|
9
|
+
## Features
|
6
10
|
|
7
11
|
* The gem stores all the data in **one** separate table and does not need to modify the existing tables
|
8
12
|
* It handles drafts for different models
|
9
|
-
* It allows saving draft for an
|
10
|
-
*
|
13
|
+
* It allows saving draft for an instance which does not pass the validations
|
14
|
+
* A draft stores associations and virtual attributes, too
|
11
15
|
* A draft is optionally linked to a given user, so every user can manage his own drafts (invisible for the other users)
|
12
|
-
* A draft is optionally linked to a parent
|
16
|
+
* A draft is optionally linked to a parent instance. This helps showing existing drafts in a context (e.g. message drafts for a given topic)
|
17
|
+
* Only 80 lines of code
|
13
18
|
|
14
19
|
|
15
20
|
## Installation
|
@@ -28,9 +33,62 @@ Or install it yourself as:
|
|
28
33
|
|
29
34
|
$ gem install drafting
|
30
35
|
|
36
|
+
Finally, generate and run the migration:
|
37
|
+
|
38
|
+
$ rails g drafting:migration
|
39
|
+
$ rake db:migrate
|
40
|
+
|
41
|
+
|
31
42
|
## Usage
|
32
43
|
|
33
|
-
|
44
|
+
### Simple example
|
45
|
+
|
46
|
+
```ruby
|
47
|
+
class Message < ActiveRecord::Base
|
48
|
+
has_drafts
|
49
|
+
end
|
50
|
+
|
51
|
+
message = Message.new content: "Let's start with a simple example."
|
52
|
+
message.save_draft(current_user)
|
53
|
+
|
54
|
+
# Time passes ...
|
55
|
+
|
56
|
+
draft = Message.drafts(current_user).first
|
57
|
+
message = draft.restore
|
58
|
+
message.save!
|
59
|
+
```
|
60
|
+
|
61
|
+
### Linking to parent instance
|
62
|
+
|
63
|
+
```ruby
|
64
|
+
class Topic < ActiveRecord::Base
|
65
|
+
has_many :messages
|
66
|
+
end
|
67
|
+
|
68
|
+
class Message < ActiveRecord::Base
|
69
|
+
belongs_to :topic
|
70
|
+
has_drafts parent: :topic
|
71
|
+
end
|
72
|
+
|
73
|
+
topic = Topic.create! title: 'World domination'
|
74
|
+
message = topic.messages.build content: 'First step: Get some coffee.'
|
75
|
+
message.save_draft(current_user)
|
76
|
+
|
77
|
+
# Time passes ...
|
78
|
+
|
79
|
+
draft = topic.drafts(current_user).first
|
80
|
+
message = draft.restore
|
81
|
+
message.save!
|
82
|
+
```
|
83
|
+
|
84
|
+
### Hints
|
85
|
+
|
86
|
+
* `save_draft` is allowed only if the instance is not persisted yet
|
87
|
+
* Doing `save_draft` overwrites a previous draft (if there is one)
|
88
|
+
* After doing a `save`, the draft (if there is one) will be deleted
|
89
|
+
* The `user` argument can be nil if you don't want to distinguish between multiple users
|
90
|
+
* Saving draft stores the data via `Marshal.dump` and `Marshal.load`. If you don't like this or need some customization, you can override the instance methods `dump_to_draft` and `load_from_draft` (see source)
|
91
|
+
|
34
92
|
|
35
93
|
## Development
|
36
94
|
|
@@ -38,6 +96,7 @@ After checking out the repo, run `bin/setup` to install dependencies. Then, run
|
|
38
96
|
|
39
97
|
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
40
98
|
|
99
|
+
|
41
100
|
## Contributing
|
42
101
|
|
43
102
|
Bug reports and pull requests are welcome on GitHub at https://github.com/ledermann/drafting. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](https://contributor-covenant.org) code of conduct.
|
@@ -47,4 +106,4 @@ Bug reports and pull requests are welcome on GitHub at https://github.com/lederm
|
|
47
106
|
|
48
107
|
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
49
108
|
|
50
|
-
Based on some code from the
|
109
|
+
Based on some code from the outdated gem [drafts](https://rubygems.org/gems/drafts). Used with kind permission of Alexey Kuznetsov (@lxkuz).
|
@@ -12,8 +12,10 @@ module Drafting
|
|
12
12
|
parent_class = self.reflect_on_all_associations(:belongs_to).find { |a| a.name == options[:parent] }.try(:klass)
|
13
13
|
raise ArgumentError unless parent_class
|
14
14
|
|
15
|
-
parent_class.
|
16
|
-
|
15
|
+
unless parent_class.method_defined? :drafts
|
16
|
+
parent_class.send :define_method, :drafts do |user|
|
17
|
+
Draft.where(:user => user, :parent => self)
|
18
|
+
end
|
17
19
|
end
|
18
20
|
|
19
21
|
self.draft_parent = options[:parent]
|
data/lib/drafting/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: drafting
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Georg Ledermann
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-08-
|
11
|
+
date: 2015-08-04 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activerecord
|