draft-manager 0.0.1
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.
- data/README +13 -0
- data/lib/draft.rb +61 -0
- metadata +47 -0
data/README
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
This gem provide interface of drafts. It could be using for saving unvalid records in temp table.
|
2
|
+
|
3
|
+
Usage:
|
4
|
+
|
5
|
+
#GET /humans/new
|
6
|
+
#POST /humans
|
7
|
+
me = Human.new params[:human]
|
8
|
+
me.save_draft # not valid, can�t save
|
9
|
+
# => Draft.create data: me.serialize
|
10
|
+
#GET /humans/new
|
11
|
+
me = Human.form_draft params[:draft_id]
|
12
|
+
#POST /humans
|
13
|
+
me.save # now all data is valid
|
data/lib/draft.rb
ADDED
@@ -0,0 +1,61 @@
|
|
1
|
+
class Draft < ActiveRecord::Base
|
2
|
+
attr_accessible :user_id, :data, :target_type, :name
|
3
|
+
serialize :data, Hash
|
4
|
+
validate :user_id, :data, :target_type, :precedence => true
|
5
|
+
end
|
6
|
+
|
7
|
+
module DraftManager
|
8
|
+
def self.included base
|
9
|
+
base.class_exec do
|
10
|
+
extend BaseClassMethods
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
module BaseClassMethods
|
15
|
+
def has_drafts
|
16
|
+
include DraftManager::InstanceMethods
|
17
|
+
extend DraftManager::ClassMethods
|
18
|
+
attr_accessor :draft_id
|
19
|
+
attr_accessible :draft_id
|
20
|
+
after_create :clear_draft
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
module InstanceMethods
|
25
|
+
def save_draft user
|
26
|
+
return false unless self.new_record?
|
27
|
+
draft = Draft.find_by_id(self.draft_id) || Draft.new
|
28
|
+
attrs = self.attributes.clone.delete_if { |key, value| self.class.accessible_attributes.deny?(key) }
|
29
|
+
cache_attrs = {}
|
30
|
+
attrs.each { |attr, val| cache_attrs[attr + "_cache"] = self.send(attr + "_cache") if self.send(attr).is_a?(CarrierWave::Uploader::Base) }
|
31
|
+
draft.data = attrs.merge cache_attrs
|
32
|
+
draft.name = self.to_s
|
33
|
+
draft.target_type = self.class.name
|
34
|
+
draft.user_id = user.id
|
35
|
+
res = draft.save
|
36
|
+
self.draft_id = draft.id if res
|
37
|
+
res
|
38
|
+
end
|
39
|
+
|
40
|
+
private
|
41
|
+
|
42
|
+
def clear_draft
|
43
|
+
draft = Draft.find_by_id self.draft_id
|
44
|
+
draft.destroy if draft
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
module ClassMethods
|
49
|
+
def from_draft data
|
50
|
+
draft = data.is_a?(Draft) ? data : Draft.find(data)
|
51
|
+
target = self.new draft.data
|
52
|
+
target.draft_id = draft.id
|
53
|
+
target
|
54
|
+
end
|
55
|
+
|
56
|
+
def drafts user
|
57
|
+
Draft.where(:user_id => user.id, :target_type => self.name)
|
58
|
+
end
|
59
|
+
end
|
60
|
+
|
61
|
+
end
|
metadata
ADDED
@@ -0,0 +1,47 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: draft-manager
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- mystand
|
9
|
+
autorequire: renoter
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-10-20 00:00:00.000000000 Z
|
13
|
+
dependencies: []
|
14
|
+
description: This gem provide interface of drafts. It could be using for saving unvalid
|
15
|
+
records in temp table
|
16
|
+
email: kuznetsov@mystand.ru
|
17
|
+
executables: []
|
18
|
+
extensions: []
|
19
|
+
extra_rdoc_files: []
|
20
|
+
files:
|
21
|
+
- README
|
22
|
+
- lib/draft.rb
|
23
|
+
homepage: http://github.com/lxkuz/draft
|
24
|
+
licenses: []
|
25
|
+
post_install_message:
|
26
|
+
rdoc_options: []
|
27
|
+
require_paths:
|
28
|
+
- lib
|
29
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
30
|
+
none: false
|
31
|
+
requirements:
|
32
|
+
- - ! '>='
|
33
|
+
- !ruby/object:Gem::Version
|
34
|
+
version: '0'
|
35
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
36
|
+
none: false
|
37
|
+
requirements:
|
38
|
+
- - ! '>='
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
requirements: []
|
42
|
+
rubyforge_project:
|
43
|
+
rubygems_version: 1.8.16
|
44
|
+
signing_key:
|
45
|
+
specification_version: 3
|
46
|
+
summary: Drafts interface
|
47
|
+
test_files: []
|