action-draft 0.1.0 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 5b73826fe9719191f155c5e59f291f250baf2180c1c690d801dfccd82d9960f6
4
- data.tar.gz: 29d365a7dfbfb663474e68d096437de5b5d30df26eab7a29e7fe150ba0f10186
3
+ metadata.gz: 6fde164f418f2264692ee13820be77814869782461b521635871c132946f4e74
4
+ data.tar.gz: 206426857fa67eac4317bf72747a7517c8fe15097308c1620b790c044fbd1a88
5
5
  SHA512:
6
- metadata.gz: c348a4e420d9ecc51d9eb201188e0c7f2ba67e1c95872877f45a8d1fe26ae9287e9fcf4c841964eb6da184ec1a43463a413d0e27a152b1e8d0f66430e12348c6
7
- data.tar.gz: 914c21a224f2ba53a37786a18820580b9b67b207a2c1a6cdf9d07d8f94f8addf8f9c79489e49aed4a0237df3294957e109766f694a591b56df44acaf17bec83c
6
+ metadata.gz: 7f39f613f05c12f53448f5e17ed92bc68866db93840e937390384b52b86ec5895509a0a0d626e341ae13642fcad182f9866ca85091ab0710fd60ba64a8627c64
7
+ data.tar.gz: 596647366cde77653b62189c50bb86ce12aa462ddc2e06b840882e12e36382c1620f96fe7ffe31ca6255202a6bfaceb688b979382ed1717bbfcb79600d191e19
data/CHANGELOG.md CHANGED
@@ -1,3 +1,9 @@
1
+ 0.2.0
2
+ -----
3
+
4
+ - A `publish` method for assignment the draft values to actual attributes.
5
+ - Fallback to actual attribute value when draft is nil.
6
+
1
7
  0.1.0
2
8
  -----
3
9
 
data/README.md CHANGED
@@ -1,9 +1,16 @@
1
1
  # Action Draft
2
2
 
3
- Action Draft brings your ActiveRecord model to storage multiple draft attributes without add any columns to table.
3
+ Action Draft brings your ActiveRecord model to storage multiple draft attributes without add columns to the business table.
4
4
 
5
5
  [![Gem Version](https://badge.fury.io/rb/action-draft.svg)](https://rubygems.org/gems/action-draft) [![Build Status](https://travis-ci.org/rails-engine/action-draft.svg?branch=master)](https://travis-ci.org/rails-engine/action-draft)
6
6
 
7
+ ## Features
8
+
9
+ - Save drafts without add columns to the business table.
10
+ - Work any ActiveRecord model, just add `has_draft :field_name`.
11
+ - A `publish` method for assignment the draft values to actual attributes.
12
+ - Fallback to actual attribute value when draft is nil.
13
+
7
14
  ## Installation
8
15
 
9
16
  ```ruby
@@ -67,6 +74,21 @@ irb> message.draft_content.to_s
67
74
  "Draft message content"
68
75
  ```
69
76
 
77
+ Publish draft content:
78
+
79
+ ```rb
80
+ irb> message = Message.new
81
+ irb> message.draft_title = "Message title"
82
+ irb> message.publish
83
+
84
+ irb> message.new_record?
85
+ false
86
+ irb> message.title
87
+ "Message title"
88
+ irb> message.draft_title
89
+ "Message title"
90
+ ```
91
+
70
92
  ## License
71
93
 
72
94
  The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
@@ -5,7 +5,19 @@ module ActionDraft
5
5
  extend ActiveSupport::Concern
6
6
 
7
7
  included do
8
- class_attribute :__action_draft_names
8
+ mattr_accessor :action_draft_attributes
9
+ end
10
+
11
+ def publish
12
+ self.action_draft_attributes ||= []
13
+ self.action_draft_attributes.each do |_name|
14
+ self.send("#{_name}=", self.send("draft_#{_name}").to_s)
15
+ end
16
+ self.save
17
+ end
18
+
19
+ def publish!
20
+ publish ? true : raise_validation_error
9
21
  end
10
22
 
11
23
  class_methods do
@@ -24,14 +36,14 @@ module ActionDraft
24
36
  #
25
37
  # Message.all.with_draft_title # Avoids N+1 queries when you just want the draft fields.
26
38
  def has_draft(*names)
27
- __action_draft_names ||= []
39
+ self.action_draft_attributes ||= []
28
40
  names = Array(names)
29
41
  names.each do |name|
30
- __action_draft_names << name
42
+ self.action_draft_attributes << name.to_sym
31
43
 
32
44
  class_eval <<-CODE, __FILE__, __LINE__ + 1
33
45
  def draft_#{name}
34
- self.draft_#{name}_content ||= ActionDraft::Content.new(name: "#{name}", record: self)
46
+ self.draft_#{name}_content ||= ActionDraft::Content.new(name: "#{name}", record: self, content: self.send("#{name}"))
35
47
  end
36
48
 
37
49
  def draft_#{name}=(content)
@@ -43,12 +55,12 @@ module ActionDraft
43
55
  end
44
56
 
45
57
  scope :with_drafts, -> do
46
- names = __action_draft_names.map { |name| "draft_#{name}_content" }
58
+ names = self.action_draft_attributes.map { |name| "draft_#{name}_content" }
47
59
  includes(*names)
48
60
  end
49
61
 
50
62
  after_save do
51
- __action_draft_names.each do |name|
63
+ self.action_draft_attributes.each do |name|
52
64
  public_send("draft_#{name}").save if public_send("draft_#{name}").changed?
53
65
  end
54
66
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module ActionDraft
4
- VERSION = "0.1.0"
4
+ VERSION = "0.2.0"
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: action-draft
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jason Lee
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2019-07-24 00:00:00.000000000 Z
11
+ date: 2019-07-25 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -38,7 +38,8 @@ dependencies:
38
38
  - - ">="
39
39
  - !ruby/object:Gem::Version
40
40
  version: '0'
41
- description: Help your ActiveRecord model to storage draft field.
41
+ description: Action Draft brings your ActiveRecord model to storage multiple draft
42
+ attributes without add columns to the business table.
42
43
  email:
43
44
  - huacnlee@gmail.com
44
45
  executables: []
@@ -78,5 +79,6 @@ requirements: []
78
79
  rubygems_version: 3.0.3
79
80
  signing_key:
80
81
  specification_version: 4
81
- summary: Help your ActiveRecord model to storage draft field.
82
+ summary: Action Draft brings your ActiveRecord model to storage multiple draft attributes
83
+ without add columns to the business table.
82
84
  test_files: []