rails_admin_draft 0.1.1 → 0.1.3

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: a0536b226a2009c0cea3563c66e3eb6e200a7b006cc211206a71c311f00e3964
4
- data.tar.gz: ac2c6be6868bb2099ee3b8d68a3cb19efa5ce4e6e76850d164a1959cd9c17527
3
+ metadata.gz: 50f58bc7bf92dc6416841d6c1f6d45ec06d10c08c8c2c73c6ef44d9a992c0fc0
4
+ data.tar.gz: e9c3a67f666a61995fd37c687b9b2a1b35d0cfde738e905b6f2d5af2e47a19b6
5
5
  SHA512:
6
- metadata.gz: 0c539d9f9642a1e39aff7fae1f90d406b30380f1b0d52392cb4ad9e447fcc2a9bf9693d3386454814340ef98404f843108b096acb05ac7f61acb53e27781c4eb
7
- data.tar.gz: 78dd9721239eb16052a5f44ba685d37d9f915c6b923945b7dfa0ac7b379139ed2259644bf5213b6bf9abe2fe3b68119467ef78637a84f4969a2ec946ea4853fc
6
+ metadata.gz: d727b538cd1d7c1ee072fd03575215df9c651f9396f31dc5f58af2c2246e55f13ff49f5741f460a37351221351877dc0dfbbc6693def7bde51607b996b50b639
7
+ data.tar.gz: 845c88469340f36465b6b71f91dbd2e5ca1b7afc69edb15e06b0a22e3fd5abd4ca4dfc35edd7f1a324d63c9c1255db67ffd1bae83532a18cc170ce69462f4451
data/README.md CHANGED
@@ -43,7 +43,7 @@ $ rake db:migrate
43
43
  ```ruby
44
44
  class MyModel < ApplicationRecord
45
45
  include DraftConcern
46
- set_draft_relation(klass_name: "MyModel")
46
+ has_draft
47
47
  end
48
48
  ```
49
49
 
@@ -1,6 +1,9 @@
1
1
  <% if @draft_conf.draft? && @object.is_draft? %>
2
2
  <div class="alert alert-dark">
3
- <b><%=I18n.t('admin.form.draft_header')%></b>
3
+ <b><%= I18n.t('admin.form.draft_header') %></b>
4
+ <% if @object.draft_source_id.present? %>
5
+ <%= I18n.t('admin.form.of' )%> <%= link_to "#{@object.class.name} ID:1", edit_path(@abstract_model, @object.draft_source_id) %>
6
+ <% end %>
4
7
  </div>
5
8
  <% end %>
6
9
 
@@ -18,6 +18,7 @@ en:
18
18
 
19
19
  form:
20
20
  draft_header: Draft
21
+ of: of
21
22
  buttons:
22
23
  save_draft:
23
24
  title: Save as draft
@@ -18,6 +18,7 @@ fr:
18
18
 
19
19
  form:
20
20
  draft_header: Brouillon
21
+ of: de
21
22
  buttons:
22
23
  save_draft:
23
24
  title: Enregistrer au statut brouillon. Ne sera pas visible sur le site public.
@@ -55,22 +55,27 @@ module DraftConcern
55
55
  return if !self.is_draft?
56
56
  model_to_publish = self
57
57
 
58
- ActiveRecord::Base.transaction do
59
- if self.draft_source_id.present?
60
- model_to_publish = Draft::OverwriteService.new(source: self, destination: self.draft_source).perform
61
- model_to_publish.slug = self.slug # Keep same slug
62
- if Object.const_defined?('DiscardConcern') && self.class.include?('DiscardConcern'.constantize)
63
- self.destroy(true)
64
- else
65
- self.destroy
66
- end
67
- remove_draft_on_unique_text_fields(model_to_publish)
58
+ if self.draft_source_id.present?
59
+ # Save new image if we publish directly the draft
60
+ self.image.store! if self.image.present?
61
+ self.save!
62
+
63
+ model_to_publish = Draft::OverwriteService.new(source: self, destination: self.draft_source).perform
64
+ model_to_publish.slug = self.slug # Keep same slug
65
+
66
+ if Object.const_defined?('DiscardConcern') && self.class.include?('DiscardConcern'.constantize)
67
+ self.image.remove!
68
+ self.destroy(true)
69
+ else
70
+ self.image.remove!
71
+ self.destroy
68
72
  end
69
-
70
- model_to_publish.draft_status = DraftStatus::PUBLISHED
71
- model_to_publish.save!
73
+ remove_draft_on_unique_text_fields(model_to_publish)
72
74
  end
73
75
 
76
+ model_to_publish.draft_status = DraftStatus::PUBLISHED
77
+ model_to_publish.save!
78
+
74
79
  return model_to_publish
75
80
  end
76
81
 
@@ -98,8 +103,8 @@ module DraftConcern
98
103
 
99
104
  class_methods do
100
105
  # Creates bi-directional association for draft source.
101
- def set_draft_relation(klass_name:)
102
- association_class_name = klass_name.to_s
106
+ def has_draft
107
+ association_class_name = self.name.to_s
103
108
  class_eval do
104
109
  has_one :draft, class_name: "#{association_class_name}", foreign_key: :draft_source_id, inverse_of: :draft_source
105
110
  belongs_to :draft_source, class_name: "#{association_class_name}", optional: true
@@ -1,3 +1,5 @@
1
+ require 'fileutils'
2
+
1
3
  module Draft
2
4
  class OverwriteService
3
5
  DRAFT_ASSOCIATIONS = [:draft, :draft_source]
@@ -46,8 +48,26 @@ module Draft
46
48
  return if attributes.blank?
47
49
 
48
50
  attributes.each do |attribute|
49
- @destination.send(attribute).remove! if @destination.send(attribute).present?
51
+ if @destination.send(attribute).present?
52
+ destination_path = File.dirname(File.open(@destination.send(attribute).file.path))
53
+ @destination.send(attribute).remove!
54
+
55
+ end
56
+
50
57
  new_upload = @source.send(attribute).present? ? File.open(@source.send(attribute).file.path) : nil
58
+
59
+ if @source.image.present?
60
+ dir_source = @source.send(attribute).file.path.remove(@source.send(attribute).identifier)
61
+
62
+ Dir[File.join(dir_source, "*#{@source.send(attribute).identifier}")].each do | filename |
63
+ # copy if image destination is present
64
+ FileUtils.mv(filename, destination_path) if destination_path.present?
65
+ end
66
+ end
67
+
68
+ # Delete folder
69
+ FileUtils.remove_dir(dir_source, true) if dir_source.present?
70
+
51
71
  @destination.send("#{attribute}=", new_upload)
52
72
  end
53
73
  end
@@ -1,3 +1,3 @@
1
1
  module RailsAdminDraft
2
- VERSION = '0.1.1'.freeze
2
+ VERSION = '0.1.3'.freeze
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rails_admin_draft
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.1.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Grégory Huet
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2023-02-01 00:00:00.000000000 Z
12
+ date: 2023-02-14 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rails