carrierwave-serializable 0.0.2 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 705005ed9d28d521d499fe829da9b35d085b1fc73a48040b63441a36674b4d60
4
+ data.tar.gz: 96d2e100737e15ca6ec83a37b59eea0c809d2fbcdb0280e5cca56fb1c867358a
5
+ SHA512:
6
+ metadata.gz: deef3d01ef6cf03e2a8df75578860e0f12b6e9fd1cf2cb028d1287774bdc569689b3af67fcfc6bea5daf60cd1beff06486a7f0668626852440c8088a63c9bfae
7
+ data.tar.gz: 69ec95ecbcc7ebda4e616f3296a21196627e1e7b3a74b866ff3da25bc7095c3238d2d32c1a773455279e99923792cab65725093df248ad90e6c09fb5fb413893
@@ -0,0 +1,23 @@
1
+ name: CI RSpec Tests
2
+
3
+ on:
4
+ push:
5
+ branches: [main]
6
+ pull_request:
7
+ branches: ['**']
8
+
9
+ jobs:
10
+ test:
11
+ strategy:
12
+ fail-fast: false
13
+ matrix:
14
+ os: [ubuntu-latest, macos-latest]
15
+ ruby: [2.6, 2.7, 3.0, 'head']
16
+ runs-on: ${{ matrix.os }}
17
+ steps:
18
+ - uses: actions/checkout@v1
19
+ - uses: ruby/setup-ruby@v1
20
+ with:
21
+ ruby-version: ${{ matrix.ruby }}
22
+ bundler-cache: true # runs 'bundle install' and caches installed gems automatically
23
+ - run: bundle exec rake
data/.gitignore CHANGED
@@ -15,3 +15,4 @@ spec/reports
15
15
  test/tmp
16
16
  test/version_tmp
17
17
  tmp
18
+ uploads
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --require spec_helper
data/Gemfile CHANGED
@@ -2,3 +2,6 @@ source 'https://rubygems.org'
2
2
 
3
3
  # Specify your gem's dependencies in carrierwave-serializable.gemspec
4
4
  gemspec
5
+
6
+ gem 'activerecord'
7
+ gem 'sqlite3'
data/LICENSE.txt CHANGED
@@ -1,4 +1,4 @@
1
- Copyright (c) 2013 Tima Maslyuchenko
1
+ Copyright (c) 2013 Tim Masliuchenko
2
2
 
3
3
  MIT License
4
4
 
@@ -19,4 +19,4 @@ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
19
  NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
20
  LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
21
  OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
- WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md CHANGED
@@ -1,4 +1,4 @@
1
- # Carrierwave::Serializable
1
+ # Carrierwave::Serializable ![Build Status](https://github.com/timfjord/carrierwave-serializable/actions/workflows/test.yml/badge.svg)
2
2
 
3
3
  Carrierwave plugin that allow mount uploader to serialized field
4
4
 
@@ -14,13 +14,15 @@ Or install it yourself as:
14
14
 
15
15
  ## Usage
16
16
 
17
- If you need mount uploader to serialized field, set `serialize_to` option
18
- to serialized field where you want to store your uplaoder.
17
+ To mount an uploader to the serialized field, set the `serialize_to` option
18
+ to the serialized field where you want to store the uploader.
19
19
 
20
- class User < ActiveRecord::Base
21
- serialize :documents, Hash
22
- mount_uploader :document1, DocumentUploader, serialize_to: :documents
23
- end
20
+ ```ruby
21
+ class User < ActiveRecord::Base
22
+ serialize :documents, Hash
23
+ mount_uploader :document1, DocumentUploader, serialize_to: :documents
24
+ end
25
+ ```
24
26
 
25
27
  ## Contributing
26
28
 
data/Rakefile CHANGED
@@ -1 +1,5 @@
1
- require "bundler/gem_tasks"
1
+ require 'bundler/gem_tasks'
2
+ require 'rspec/core/rake_task'
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+ task default: :spec
@@ -4,18 +4,22 @@ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
4
  require 'carrierwave-serializable/version'
5
5
 
6
6
  Gem::Specification.new do |gem|
7
- gem.name = "carrierwave-serializable"
7
+ gem.name = 'carrierwave-serializable'
8
8
  gem.version = CarrierWave::Serializable::VERSION
9
- gem.authors = ["Tima Maslyuchenko"]
10
- gem.email = ["insside@gmail.com"]
9
+ gem.authors = ['Tim Masliuchenko']
10
+ gem.email = ['timfjord@hey.com']
11
11
  gem.description = %q{Carrierwave plugin that allow mount uploader to serialized field}
12
12
  gem.summary = %q{Carrierwave plugin that allow mount uploader to serialized field}
13
- gem.homepage = "https://github.com/timsly/carrierwave-serializable"
13
+ gem.homepage = 'https://github.com/timfjord/carrierwave-serializable'
14
+ gem.licenses = 'MIT'
14
15
 
15
16
  gem.files = `git ls-files`.split($/)
16
17
  gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
17
18
  gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
18
- gem.require_paths = ["lib"]
19
-
19
+ gem.require_paths = ['lib']
20
+
20
21
  gem.add_dependency 'carrierwave'
22
+
23
+ gem.add_development_dependency 'rspec'
24
+ gem.add_development_dependency 'rake'
21
25
  end
@@ -1,5 +1,5 @@
1
- require "carrierwave"
2
- require "carrierwave-serializable/version"
3
- require "carrierwave-serializable/orm/activerecord"
1
+ require 'carrierwave'
2
+ require 'carrierwave-serializable/version'
3
+ require 'carrierwave-serializable/orm/activerecord'
4
4
 
5
- ActiveRecord::Base.extend CarrierWave::ActiveRecord::Serializable if defined?(Rails)
5
+ ActiveRecord::Base.extend CarrierWave::ActiveRecord::Serializable if defined?(ActiveRecord)
@@ -1,59 +1,72 @@
1
1
  # encoding: utf-8
2
2
 
3
- require "carrierwave/orm/activerecord"
3
+ require 'carrierwave/orm/activerecord'
4
4
 
5
5
  module CarrierWave
6
6
  module ActiveRecord
7
7
  module Serializable
8
8
  def serialized_uploaders
9
- @serialized_uploaders ||= {}
9
+ @serialized_uploaders ||= read_from_superclass? ? superclass.serialized_uploaders.dup : {}
10
10
  end
11
-
11
+
12
12
  def serialized_uploader?(column)
13
- serialized_uploaders.key?(column) && serialized_attributes.key?(serialized_uploaders[column].to_s)
13
+ serialized_uploaders.key?(column)
14
14
  end
15
-
15
+
16
16
  ##
17
17
  # See +CarrierWave::Mount#mount_uploader+ for documentation
18
18
  #
19
- def mount_uploader(column, uploader=nil, options={}, &block)
19
+ def mount_uploader(column, uploader = nil, options = {}, &block)
20
20
  super
21
-
21
+
22
22
  serialize_to = options.delete :serialize_to
23
23
  if serialize_to
24
- serialized_uploaders[column] = serialize_to
24
+ serialization_column = options[:mount_on] || column
25
+ serialized_uploaders[serialization_column] = serialize_to
25
26
  class_eval <<-RUBY, __FILE__, __LINE__+1
26
- def #{column}_will_change!
27
+ def #{serialization_column}_will_change!
27
28
  #{serialize_to}_will_change!
28
- @#{column}_changed = true
29
+ @#{serialization_column}_changed = true
29
30
  end
30
31
 
31
- def #{column}_changed?
32
- @#{column}_changed
32
+ def #{serialization_column}_changed?
33
+ @#{serialization_column}_changed
33
34
  end
34
35
  RUBY
35
36
  end
36
-
37
- class_eval <<-RUBY, __FILE__, __LINE__+1
37
+
38
+ class_eval <<-RUBY, __FILE__, __LINE__ + 1
38
39
  def write_uploader(column, identifier)
39
40
  if self.class.serialized_uploader?(column)
40
- serialized_field = self.send self.class.serialized_uploaders[column]
41
- serialized_field[column.to_s] = identifier
41
+ serialized_field_name = self.class.serialized_uploaders[column].to_s
42
+
43
+ if serialized_field = self.send(serialized_field_name)
44
+ serialized_field[column.to_s] = identifier
45
+ else
46
+ self.send("\#{serialized_field_name}=", column.to_s => identifier)
47
+ end
42
48
  else
43
- super
49
+ write_attribute(column, identifier)
44
50
  end
45
51
  end
46
52
 
47
53
  def read_uploader(column)
48
54
  if self.class.serialized_uploader?(column)
49
55
  serialized_field = self.send self.class.serialized_uploaders[column]
50
- serialized_field[column.to_s]
56
+ serialized_field ? serialized_field[column.to_s] : nil
51
57
  else
52
- super
58
+ read_attribute(column)
53
59
  end
54
60
  end
55
61
  RUBY
62
+
63
+ end
64
+
65
+ private
66
+
67
+ def read_from_superclass?
68
+ superclass != ::ActiveRecord::Base && superclass.respond_to?(:serialized_uploaders)
56
69
  end
57
- end # Serializable
58
- end # ActiveRecord
59
- end # CarrierWave
70
+ end
71
+ end
72
+ end
@@ -1,5 +1,5 @@
1
1
  module CarrierWave
2
2
  module Serializable
3
- VERSION = "0.0.2"
3
+ VERSION = '0.1.0'
4
4
  end
5
5
  end
Binary file
@@ -0,0 +1,82 @@
1
+ RSpec.describe 'Integration' do
2
+ ActiveRecord::Base.establish_connection adapter: 'sqlite3', database: ':memory:'
3
+ ActiveRecord::Migration.verbose = false
4
+
5
+ class TestMigration < ActiveRecord::Migration[4.2]
6
+ def self.up
7
+ create_table :avatars, force: true do |t|
8
+ t.column :image1, :text
9
+ end
10
+
11
+ create_table :images, force: true do |t|
12
+ t.column :data, :text
13
+ end
14
+ end
15
+
16
+ def self.down
17
+ drop_table :avatars
18
+ drop_table :images
19
+ end
20
+ end
21
+
22
+ class ImageUploader < CarrierWave::Uploader::Base; end
23
+
24
+ class ApplicationRecord < ActiveRecord::Base
25
+ self.abstract_class = true
26
+
27
+ mount_uploader :base_image, ImageUploader, serialize_to: :data
28
+ end
29
+
30
+ class Avatar < ApplicationRecord
31
+ mount_uploader :image1, ImageUploader
32
+ end
33
+
34
+ class Image < ApplicationRecord
35
+ serialize :data, Hash
36
+ mount_uploader :image1, ImageUploader, serialize_to: :data
37
+ end
38
+
39
+ before(:all) { TestMigration.up }
40
+ after(:all) { TestMigration.down }
41
+
42
+ after do
43
+ Avatar.destroy_all
44
+ Image.destroy_all
45
+ end
46
+
47
+ let(:image) { Image.new }
48
+
49
+ it 'should mount uploader to serialized field' do
50
+ expect(image.image1).to be_instance_of ImageUploader
51
+ end
52
+
53
+ it 'should upload image and store it to the serialized field' do
54
+ with_image do |file|
55
+ image.image1 = file
56
+ image.save!
57
+
58
+ expect(image.data['image1']).to be_present
59
+ end
60
+ end
61
+
62
+ it 'inherits superclass uploaders' do
63
+ with_image do |file|
64
+ image.base_image = file
65
+ image.save!
66
+
67
+ expect(image.data['base_image']).to be_present
68
+ end
69
+ end
70
+
71
+ describe 'model with the regular uploader and same name as serialized uploader' do
72
+ before do
73
+ Image.new
74
+ end
75
+
76
+ it 'mounts uploader to regular field' do
77
+ avatar = Avatar.new
78
+
79
+ expect(avatar.image1).to be_instance_of ImageUploader
80
+ end
81
+ end
82
+ end
@@ -0,0 +1,18 @@
1
+ require 'bundler/setup'
2
+ Bundler.setup
3
+
4
+ require 'carrierwave-serializable'
5
+
6
+ Dir[File.expand_path('../support/**/*.rb', __FILE__)].each { |f| require f }
7
+
8
+ RSpec.configure do |config|
9
+ config.include FileHelper
10
+
11
+ config.expect_with :rspec do |expectations|
12
+ expectations.include_chain_clauses_in_custom_matcher_descriptions = true
13
+ end
14
+
15
+ config.mock_with :rspec do |mocks|
16
+ mocks.verify_partial_doubles = true
17
+ end
18
+ end
@@ -0,0 +1,5 @@
1
+ module FileHelper
2
+ def with_image(name = 'blank.gif', &block)
3
+ File.open File.expand_path("../../fixtures/#{name}", __FILE__), &block
4
+ end
5
+ end
metadata CHANGED
@@ -1,40 +1,67 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: carrierwave-serializable
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
5
- prerelease:
4
+ version: 0.1.0
6
5
  platform: ruby
7
6
  authors:
8
- - Tima Maslyuchenko
9
- autorequire:
7
+ - Tim Masliuchenko
8
+ autorequire:
10
9
  bindir: bin
11
10
  cert_chain: []
12
- date: 2013-02-18 00:00:00.000000000 Z
11
+ date: 2021-04-14 00:00:00.000000000 Z
13
12
  dependencies:
14
13
  - !ruby/object:Gem::Dependency
15
14
  name: carrierwave
16
15
  requirement: !ruby/object:Gem::Requirement
17
- none: false
18
16
  requirements:
19
- - - ! '>='
17
+ - - ">="
20
18
  - !ruby/object:Gem::Version
21
19
  version: '0'
22
20
  type: :runtime
23
21
  prerelease: false
24
22
  version_requirements: !ruby/object:Gem::Requirement
25
- none: false
26
23
  requirements:
27
- - - ! '>='
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rspec
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
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: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
28
53
  - !ruby/object:Gem::Version
29
54
  version: '0'
30
55
  description: Carrierwave plugin that allow mount uploader to serialized field
31
56
  email:
32
- - insside@gmail.com
57
+ - timfjord@hey.com
33
58
  executables: []
34
59
  extensions: []
35
60
  extra_rdoc_files: []
36
61
  files:
37
- - .gitignore
62
+ - ".github/workflows/test.yml"
63
+ - ".gitignore"
64
+ - ".rspec"
38
65
  - Gemfile
39
66
  - LICENSE.txt
40
67
  - README.md
@@ -43,28 +70,35 @@ files:
43
70
  - lib/carrierwave-serializable.rb
44
71
  - lib/carrierwave-serializable/orm/activerecord.rb
45
72
  - lib/carrierwave-serializable/version.rb
46
- homepage: https://github.com/timsly/carrierwave-serializable
47
- licenses: []
48
- post_install_message:
73
+ - spec/fixtures/blank.gif
74
+ - spec/integration_spec.rb
75
+ - spec/spec_helper.rb
76
+ - spec/support/file_helpder.rb
77
+ homepage: https://github.com/timfjord/carrierwave-serializable
78
+ licenses:
79
+ - MIT
80
+ metadata: {}
81
+ post_install_message:
49
82
  rdoc_options: []
50
83
  require_paths:
51
84
  - lib
52
85
  required_ruby_version: !ruby/object:Gem::Requirement
53
- none: false
54
86
  requirements:
55
- - - ! '>='
87
+ - - ">="
56
88
  - !ruby/object:Gem::Version
57
89
  version: '0'
58
90
  required_rubygems_version: !ruby/object:Gem::Requirement
59
- none: false
60
91
  requirements:
61
- - - ! '>='
92
+ - - ">="
62
93
  - !ruby/object:Gem::Version
63
94
  version: '0'
64
95
  requirements: []
65
- rubyforge_project:
66
- rubygems_version: 1.8.23
67
- signing_key:
68
- specification_version: 3
96
+ rubygems_version: 3.1.4
97
+ signing_key:
98
+ specification_version: 4
69
99
  summary: Carrierwave plugin that allow mount uploader to serialized field
70
- test_files: []
100
+ test_files:
101
+ - spec/fixtures/blank.gif
102
+ - spec/integration_spec.rb
103
+ - spec/spec_helper.rb
104
+ - spec/support/file_helpder.rb