spina-upgrade 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 2e6f62df1d7ca5718cb17a03faf0be690a10e2f02456dc42b34e46109f090511
4
+ data.tar.gz: 0ffcb6f05f10e10a2d13d64a2c16b41a8616793d0d6ff4dd344a385c38a3c608
5
+ SHA512:
6
+ metadata.gz: ffab776d0cf3d83fbc17d95e6c467098c042efa624baec568e653bb5df5c029b51771c0eb8e454d38b31aa4b758180cb7dcedfa89d8e6b8a67a4a2a9916bca06
7
+ data.tar.gz: 52d2d3980a318f80f2e1c6bb036278cd4bb1db5457fb065d5ce2a6ac410c953d5a691a40d7bb4492ab0859cb109a993ae44abc7b21365b1a7b0d0af0ebb8c042
@@ -0,0 +1,20 @@
1
+ Copyright 2018 Bram Jetten
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.
@@ -0,0 +1,19 @@
1
+ # Spina::Upgrade
2
+ This gem is meant for upgrading your Spina project from 0.X to 1.0 with support for ActiveStorage. All records of `Spina::Photo` will be reuploaded as `Spina::Image`. Make sure that you setup ActiveStorage before running the upgrade.
3
+
4
+ ## Installation
5
+ Add this line to your application's Gemfile:
6
+
7
+ ```ruby
8
+ gem 'spina-upgrade'
9
+ ```
10
+
11
+ ## Usage
12
+ Simply run the upgrade generator.
13
+
14
+ ```
15
+ rails g spina:upgrade
16
+ ```
17
+
18
+ ## License
19
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
@@ -0,0 +1,30 @@
1
+ begin
2
+ require 'bundler/setup'
3
+ rescue LoadError
4
+ puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
5
+ end
6
+
7
+ require 'rdoc/task'
8
+
9
+ RDoc::Task.new(:rdoc) do |rdoc|
10
+ rdoc.rdoc_dir = 'rdoc'
11
+ rdoc.title = 'Spina::Upgrade'
12
+ rdoc.options << '--line-numbers'
13
+ rdoc.rdoc_files.include('README.md')
14
+ rdoc.rdoc_files.include('lib/**/*.rb')
15
+ end
16
+
17
+ APP_RAKEFILE = File.expand_path("../test/dummy/Rakefile", __FILE__)
18
+ load 'rails/tasks/engine.rake'
19
+
20
+ Bundler::GemHelper.install_tasks
21
+
22
+ require 'rake/testtask'
23
+
24
+ Rake::TestTask.new(:test) do |t|
25
+ t.libs << 'test'
26
+ t.pattern = 'test/**/*_test.rb'
27
+ t.verbose = false
28
+ end
29
+
30
+ task default: :test
@@ -0,0 +1,14 @@
1
+ module Spina
2
+ class AttachmentCollection < ApplicationRecord
3
+ has_and_belongs_to_many :attachments, join_table: 'spina_attachment_collections_attachments'
4
+
5
+ # There's no attachment collection equivalent in Spina 2
6
+ # Instead, create a line with comma separated ID's of attachments
7
+ def convert_to_json!
8
+ json_part = Spina::Parts::Line.new
9
+ json_part.content = attachments.ids.join(", ")
10
+ json_part
11
+ end
12
+
13
+ end
14
+ end
@@ -0,0 +1,18 @@
1
+ module Spina
2
+ class ImageCollection < ApplicationRecord
3
+ has_one :page_part, as: :page_partable
4
+ has_many :image_collections_images
5
+ has_many :images, through: :image_collections_images
6
+
7
+ def convert_to_json!
8
+ json_part = Spina::Parts::ImageCollection.new
9
+ json_part.images = images.order(:position).map do |image|
10
+ image_json_part = image.convert_to_json!
11
+ image_json_part.name = page_part.name
12
+ image_json_part
13
+ end
14
+ json_part
15
+ end
16
+
17
+ end
18
+ end
@@ -0,0 +1,6 @@
1
+ module Spina
2
+ class ImageCollectionsImage < ApplicationRecord
3
+ belongs_to :image
4
+ belongs_to :image_collection
5
+ end
6
+ end
@@ -0,0 +1,12 @@
1
+ module Spina
2
+ class Line < ApplicationRecord
3
+ extend Mobility
4
+ translates :content, fallbacks: true
5
+
6
+ def convert_to_json!
7
+ text = Spina::Parts::Line.new
8
+ text.content = content
9
+ text
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,11 @@
1
+ module Spina
2
+ class Option < ApplicationRecord
3
+
4
+ def convert_to_json!
5
+ text = Spina::Parts::Option.new
6
+ text.value = value
7
+ text
8
+ end
9
+
10
+ end
11
+ end
@@ -0,0 +1,12 @@
1
+ module Spina
2
+ class PagePart < ApplicationRecord
3
+ belongs_to :page
4
+ belongs_to :page_partable, polymorphic: true
5
+
6
+ def convert_to_json!
7
+ json_part = page_partable.convert_to_json!
8
+ json_part.name = name
9
+ json_part
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,28 @@
1
+ module Spina
2
+ class Photo < ActiveRecord::Base
3
+ mount_uploader :file, PhotoUploader
4
+
5
+ belongs_to :media_folder, optional: true
6
+
7
+ has_many :page_parts, as: :page_partable
8
+ has_many :structure_parts, as: :structure_partable
9
+ has_many :photo_collections_photos
10
+ has_many :photo_collections, through: :photo_collections_photos
11
+
12
+ validates_presence_of :file
13
+
14
+ def name
15
+ file.file.filename
16
+ end
17
+
18
+ def content
19
+ self
20
+ end
21
+
22
+ def self.order_by_ids(ids)
23
+ sql = sanitize_sql_for_assignment({id: ids})
24
+ order("CASE WHEN #{sql} THEN 0 ELSE 1 END")
25
+ end
26
+
27
+ end
28
+ end
@@ -0,0 +1,10 @@
1
+ module Spina
2
+ class PhotoCollection < ActiveRecord::Base
3
+ has_one :page_part, as: :page_partable
4
+ has_many :photo_collections_photos
5
+ has_many :photos, through: :photo_collections_photos
6
+ has_many :structure_parts, as: :structure_partable
7
+
8
+ accepts_nested_attributes_for :photos, allow_destroy: true
9
+ end
10
+ end
@@ -0,0 +1,6 @@
1
+ module Spina
2
+ class PhotoCollectionsPhoto < ActiveRecord::Base
3
+ belongs_to :photo, optional: true
4
+ belongs_to :photo_collection, optional: true
5
+ end
6
+ end
@@ -0,0 +1,16 @@
1
+ module Spina
2
+ class Structure < ApplicationRecord
3
+ has_one :page_part, as: :page_partable
4
+ has_many :structure_items
5
+
6
+ def convert_to_json!
7
+ repeater = Spina::Parts::Repeater.new
8
+ repeater.content = structure_items.order(:position).map do |structure_item|
9
+ json_structure_item = structure_item.convert_to_json!
10
+ json_structure_item.name = page_part.name
11
+ json_structure_item
12
+ end
13
+ repeater
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,17 @@
1
+ module Spina
2
+ class StructureItem < ApplicationRecord
3
+ has_many :structure_parts
4
+
5
+ def convert_to_json!
6
+ repeater_content = Spina::Parts::RepeaterContent.new
7
+ repeater_content.parts = structure_parts.map do |structure_part|
8
+ next unless structure_part.structure_partable.respond_to?(:convert_to_json!)
9
+ new_part = structure_part.convert_to_json!
10
+ new_part.name = structure_part.name
11
+ new_part
12
+ end.compact
13
+ repeater_content
14
+ end
15
+
16
+ end
17
+ end
@@ -0,0 +1,11 @@
1
+ module Spina
2
+ class StructurePart < ApplicationRecord
3
+ belongs_to :structure_partable, polymorphic: true
4
+
5
+ def convert_to_json!
6
+ new_part = structure_partable.convert_to_json!
7
+ new_part.name = name
8
+ new_part
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,12 @@
1
+ module Spina
2
+ class Text < ApplicationRecord
3
+ extend Mobility
4
+ translates :content, fallbacks: true
5
+
6
+ def convert_to_json!
7
+ text = Spina::Parts::Text.new
8
+ text.content = content
9
+ text
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,15 @@
1
+ module Spina
2
+ class DefaultStoreUploader < CarrierWave::Uploader::Base
3
+
4
+ def store_dir
5
+ case ::Spina.config.storage
6
+ when :s3
7
+ "#{mounted_as}/#{model.class.to_s.underscore}/#{model.id}"
8
+ when :file
9
+ "uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
10
+ else
11
+ raise NotImplementedError, "Please set your storage preferences in config/initializers/spina.rb"
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,23 @@
1
+ # encoding: utf-8
2
+ module Spina
3
+ class PhotoUploader < Spina::DefaultStoreUploader
4
+ include CarrierWave::MiniMagick
5
+
6
+ version :image do
7
+ process resize_to_fit: [800, 800], if: :too_large?
8
+ end
9
+
10
+ version :thumb do
11
+ process resize_to_fill: [150, 150]
12
+ end
13
+
14
+ def too_large?(new_file)
15
+ new_file.size > 120 * 1000
16
+ end
17
+
18
+ def extension_white_list
19
+ %w(jpg jpeg gif png)
20
+ end
21
+
22
+ end
23
+ end
@@ -0,0 +1,15 @@
1
+ class CreateSpinaUpgradeMigration < ActiveRecord::Migration[5.2]
2
+
3
+ def up
4
+ unless column_exists? :spina_photos, :image_id
5
+ add_column :spina_photos, :image_id, :integer, foreign_key: { to_table: :spina_images }
6
+ end
7
+ end
8
+
9
+ def down
10
+ if column_exists? :spina_photos, :image_id
11
+ remove_column :spina_photos, :image_id
12
+ end
13
+ end
14
+
15
+ end
@@ -0,0 +1,22 @@
1
+ module Spina
2
+ class UpgradeGenerator < Rails::Generators::Base
3
+
4
+ def copy_migrations
5
+ return if Rails.env.production?
6
+ rake 'spina_upgrade:install:migrations'
7
+ end
8
+
9
+ def run_migrations
10
+ rake 'db:migrate'
11
+ end
12
+
13
+ def upgrade
14
+ rake 'spina:photo_to_image'
15
+ end
16
+
17
+ def done
18
+ puts "Upgrade done"
19
+ end
20
+
21
+ end
22
+ end
@@ -0,0 +1 @@
1
+ require "spina/upgrade/engine"
@@ -0,0 +1,15 @@
1
+ module Spina
2
+ module Upgrade
3
+ module AttachmentDecorator
4
+
5
+ def convert_to_json!
6
+ attachment = Spina::Parts::Attachment.new
7
+ attachment.attachment_id = id
8
+ attachment.signed_blob_id = file&.blob&.signed_id
9
+ attachment.filename = name
10
+ attachment
11
+ end
12
+
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,15 @@
1
+ module Spina
2
+ module Upgrade
3
+ module ImageDecorator
4
+
5
+ def convert_to_json!
6
+ image = Spina::Parts::Image.new
7
+ image.image_id = id
8
+ image.signed_blob_id = file&.blob&.signed_id
9
+ image.filename = name
10
+ image
11
+ end
12
+
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,22 @@
1
+ require 'spina'
2
+ require 'carrierwave'
3
+
4
+ # Decorators for convert_to_json!
5
+ require 'spina/upgrade/decorators/attachment_decorator'
6
+ require 'spina/upgrade/decorators/image_decorator'
7
+
8
+ module Spina
9
+ module Upgrade
10
+ class Engine < ::Rails::Engine
11
+ isolate_namespace Spina::Upgrade
12
+
13
+ config.autoload_paths += %W( #{config.root}/lib )
14
+
15
+ config.to_prepare do
16
+ Spina::Image.prepend(Spina::Upgrade::ImageDecorator)
17
+ Spina::Attachment.prepend(Spina::Upgrade::AttachmentDecorator)
18
+ end
19
+
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,5 @@
1
+ module Spina
2
+ module Upgrade
3
+ VERSION = '0.2.0'
4
+ end
5
+ end
@@ -0,0 +1,128 @@
1
+ def prompt(*args)
2
+ print(*args)
3
+ STDIN.gets.chomp
4
+ end
5
+
6
+ namespace :spina do
7
+
8
+ task convert_page_parts_to_json: :environment do
9
+ page_partable_types = Spina::PagePart.joins(:page).pluck(:page_partable_type).uniq
10
+ puts "Checking if parts are convertable to JSON..."
11
+
12
+ all_set = true
13
+
14
+ page_partable_types.each do |partable_type|
15
+
16
+ if partable_type.constantize.new.respond_to?(:convert_to_json!)
17
+ puts "✓ #{partable_type}"
18
+ else
19
+ puts "✗ #{partable_type}"
20
+ all_set = false
21
+ end
22
+
23
+ end
24
+
25
+ unless all_set
26
+ answer = prompt("Not all parts can be converted to JSON. Continue? (y/n) ")
27
+ answer = false if answer == "n"
28
+ all_set = ActiveRecord::Type::Boolean.new.cast(answer)
29
+ end
30
+
31
+ if all_set
32
+ Spina.config.locales.each do |locale|
33
+ I18n.with_locale(locale) do
34
+ Spina::Page.all.each do |page|
35
+ page_parts = Spina::PagePart.where(page_id: page.id)
36
+ json_content = page_parts.map do |page_part|
37
+ next if page_part.page_partable.nil? # Skip blank page parts
38
+
39
+ json_part = if page_part.page_partable.respond_to?(:convert_to_json!)
40
+ page_part.convert_to_json!
41
+ else
42
+ nil
43
+ end
44
+ end.compact
45
+
46
+ page.update("#{locale}_content".to_sym => json_content)
47
+ end
48
+ end
49
+ end
50
+
51
+ puts "Converted!"
52
+ end
53
+ end
54
+
55
+ task photo_to_image: :environment do
56
+ bar = ProgressBar.create title: "Photos", total: Spina::Photo.where(image_id: nil).count
57
+ Spina::Photo.where(image_id: nil).find_each do |photo|
58
+ image = Spina::Image.create(media_folder_id: photo.media_folder_id)
59
+ begin
60
+ image.file.attach(io: photo.file.sanitized_file.file, filename: photo.name)
61
+ photo.update_column(:image_id, image.id)
62
+ rescue Errno::ENOENT
63
+ image.destroy
64
+ end
65
+ bar.increment
66
+ end
67
+
68
+ bar = ProgressBar.create title: "PageParts", total: Spina::PagePart.where(page_partable_type: 'Spina::Photo').count
69
+ Spina::PagePart.where(page_partable_type: 'Spina::Photo').find_each do |page_part|
70
+ if page_part.page_partable.present?
71
+ page_part.update_columns(
72
+ page_partable_type: 'Spina::Image',
73
+ page_partable_id: page_part.page_partable.image_id
74
+ )
75
+ else
76
+ page_part.update_column(:page_partable_type, 'Spina::Image')
77
+ end
78
+ bar.increment
79
+ end
80
+
81
+ bar = ProgressBar.create title: "StructureParts", total: Spina::StructurePart.where(structure_partable_type: 'Spina::Photo').count
82
+ Spina::StructurePart.where(structure_partable_type: 'Spina::Photo').find_each do |structure_part|
83
+ if structure_part.structure_partable.present?
84
+ structure_part.update_columns(
85
+ structure_partable_type: 'Spina::Image',
86
+ structure_partable_id: structure_part.structure_partable.image_id
87
+ )
88
+ else
89
+ structure_part.update_column(:structure_partable_type, 'Spina::Image')
90
+ end
91
+ bar.increment
92
+ end
93
+
94
+ bar = ProgressBar.create title: "PhotoCollections", total: Spina::PagePart.where(page_partable_type: 'Spina::PhotoCollection').count
95
+ Spina::PagePart.where(page_partable_type: 'Spina::PhotoCollection').find_each do |page_part|
96
+ if page_part.partable.present?
97
+ image_collection = Spina::ImageCollection.create
98
+ page_part.partable.photos.each do |photo|
99
+ image_collection.images << Spina::Image.find(photo.image_id)
100
+ end
101
+ page_part.update_columns(
102
+ page_partable_type: 'Spina::ImageCollection',
103
+ page_partable_id: image_collection.id
104
+ )
105
+ else
106
+ page_part.update_column :page_partable_type, 'Spina::ImageCollection'
107
+ end
108
+ bar.increment
109
+ end
110
+
111
+ bar = ProgressBar.create title: "StructurePart PhotoCollections", total: Spina::StructurePart.where(structure_partable_type: 'Spina::PhotoCollection').count
112
+ Spina::StructurePart.where(structure_partable_type: 'Spina::PhotoCollection').find_each do |structure_part|
113
+ if structure_part.partable.present?
114
+ image_collection = Spina::ImageCollection.create
115
+ structure_part.partable.photos.each do |photo|
116
+ image_collection.images << Spina::Image.find(photo.image_id)
117
+ end
118
+ structure_part.update_columns(
119
+ structure_partable_type: 'Spina::ImageCollection',
120
+ structure_partable_id: image_collection.id
121
+ )
122
+ else
123
+ structure_part.update_column :structure_partable_type, 'Spina::PhotoCollection'
124
+ end
125
+ bar.increment
126
+ end
127
+ end
128
+ end
metadata ADDED
@@ -0,0 +1,111 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: spina-upgrade
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.2.0
5
+ platform: ruby
6
+ authors:
7
+ - Bram Jetten
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2020-06-09 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rails
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">"
18
+ - !ruby/object:Gem::Version
19
+ version: 5.2.0
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">"
25
+ - !ruby/object:Gem::Version
26
+ version: 5.2.0
27
+ - !ruby/object:Gem::Dependency
28
+ name: carrierwave
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: ruby-progressbar
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ description: Upgrade Spina to 2.0 from a previous version
56
+ email:
57
+ - bram@denkgroot.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - MIT-LICENSE
63
+ - README.md
64
+ - Rakefile
65
+ - app/models/spina/attachment_collection.rb
66
+ - app/models/spina/image_collection.rb
67
+ - app/models/spina/image_collections_image.rb
68
+ - app/models/spina/line.rb
69
+ - app/models/spina/option.rb
70
+ - app/models/spina/page_part.rb
71
+ - app/models/spina/photo.rb
72
+ - app/models/spina/photo_collection.rb
73
+ - app/models/spina/photo_collections_photo.rb
74
+ - app/models/spina/structure.rb
75
+ - app/models/spina/structure_item.rb
76
+ - app/models/spina/structure_part.rb
77
+ - app/models/spina/text.rb
78
+ - app/uploaders/spina/default_store_uploader.rb
79
+ - app/uploaders/spina/photo_uploader.rb
80
+ - db/migrate/1_create_spina_upgrade_migration.rb
81
+ - lib/generators/spina/upgrade_generator.rb
82
+ - lib/spina/upgrade.rb
83
+ - lib/spina/upgrade/decorators/attachment_decorator.rb
84
+ - lib/spina/upgrade/decorators/image_decorator.rb
85
+ - lib/spina/upgrade/engine.rb
86
+ - lib/spina/upgrade/version.rb
87
+ - lib/tasks/spina/upgrade_tasks.rake
88
+ homepage: https://www.spinacms.com
89
+ licenses:
90
+ - MIT
91
+ metadata: {}
92
+ post_install_message:
93
+ rdoc_options: []
94
+ require_paths:
95
+ - lib
96
+ required_ruby_version: !ruby/object:Gem::Requirement
97
+ requirements:
98
+ - - ">="
99
+ - !ruby/object:Gem::Version
100
+ version: '0'
101
+ required_rubygems_version: !ruby/object:Gem::Requirement
102
+ requirements:
103
+ - - ">="
104
+ - !ruby/object:Gem::Version
105
+ version: '0'
106
+ requirements: []
107
+ rubygems_version: 3.0.3
108
+ signing_key:
109
+ specification_version: 4
110
+ summary: Upgrade Spina to 2.0
111
+ test_files: []