hanami-shrine 0.2.0 → 0.2.1

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
  SHA1:
3
- metadata.gz: e247ae2c7d1155f30389af1800709dd2eca26692
4
- data.tar.gz: b9cce6169be65754c1e79f3b2b14557c7eebbae1
3
+ metadata.gz: 775327ea4bdbd83afc7f6b957027d11e9e04449b
4
+ data.tar.gz: d8d444a0ba3d009b0709e82ad4223b9af80fe1dd
5
5
  SHA512:
6
- metadata.gz: 79e3315c7a4cb0a14898b17eb67eaf995419971472fdb0b3f447158dc46b758d3c03ed5d7d01bed6730249ceefa1181a8e061cd3803184499d1dd23358168f13
7
- data.tar.gz: fbf4bb823e49230e23ea3dee488b691e05ffe3ff86342406e8da955d6ae428d7c02e2483c8a8777b10e674c13e7ad63b729840442074e535517a7a2f4956cdfc
6
+ metadata.gz: 9722d16cd6a1f67e1ce58332043d604fd77dbbb2f37014084a0a132274ddfc0fca202be7bbecda778ecc02936193a7b1508072329e3cbb91a70910a6662e1ea4
7
+ data.tar.gz: 01d0e893e9f70607be1c540bd075224aa6f489aa383fa0059012b08215d9ef12a549d7b9b5b8a804d394c0ad639b27e3f672b1f499e717b388093b01981e00fd
data/Gemfile CHANGED
@@ -5,3 +5,6 @@ gemspec
5
5
  gem 'shrine', '>= 2.0.0'
6
6
  gem 'sqlite3'
7
7
  gem 'pry'
8
+ gem 'image_processing'
9
+ gem "mini_magick", ">= 4.3.5"
10
+ gem 'fastimage'
@@ -21,10 +21,10 @@ Gem::Specification.new do |spec|
21
21
 
22
22
  spec.add_dependency 'shrine'
23
23
  spec.add_dependency 'hanami-model', '>= 0.7'
24
+ spec.add_dependency "hanami-utils"
24
25
 
25
26
  spec.add_development_dependency "bundler", "~> 1.10"
26
27
  spec.add_development_dependency "rake", "~> 10.0"
27
28
  spec.add_development_dependency "rspec"
28
29
  spec.add_development_dependency "json"
29
- spec.add_development_dependency "hanami-validations"
30
30
  end
@@ -1,5 +1,5 @@
1
1
  module Hanami
2
2
  module Shrine
3
- VERSION = "0.2.0"
3
+ VERSION = "0.2.1"
4
4
  end
5
5
  end
@@ -1,4 +1,6 @@
1
1
  require 'shrine'
2
+ require 'hanami/utils/class_attribute'
3
+ require 'ostruct'
2
4
 
3
5
  class Shrine
4
6
  module Plugins
@@ -47,68 +49,72 @@ class Shrine
47
49
 
48
50
  class RepositoryMethods < Module
49
51
  def initialize(name, attacher_class)
50
- module_eval <<-RUBY, __FILE__, __LINE__ + 1
52
+ class_eval <<-RUBY, __FILE__, __LINE__ + 1
53
+ def self.prepended(base)
54
+ base.send(:include, ::Hanami::Utils::ClassAttribute)
55
+ base.send(:class_attribute, :_attachments)
56
+ base.class_eval do
57
+ self._attachments ||= []
58
+ self._attachments << { name: :#{name}, class: #{attacher_class} }
59
+ end
60
+ end
61
+ RUBY
62
+
63
+ class_eval do
51
64
  def create(entity)
52
- save_#{name}_attachment(entity) { |new_entity| super(new_entity) }
65
+ save_attachments(entity) { |new_entity| super(new_entity) }
53
66
  end
67
+
54
68
  def update(id, entity)
55
- save_#{name}_attachment(entity) { |new_entity| super(id, new_entity) }
69
+ save_attachments(entity) { |new_entity| super(id, new_entity) }
56
70
  end
71
+
57
72
  def persist(entity)
58
- save_#{name}_attachment(entity) { |new_entity| super(new_entity) }
73
+ save_attachments(entity) { |new_entity| super(new_entity) }
59
74
  end
75
+
60
76
  def delete(id)
61
- delete_#{name}_attachment(id) { super(id) }
77
+ delete_attachments(id) { super(id) }
62
78
  end
63
79
 
64
80
  private
65
- def save_#{name}_attachment(original_entity)
66
- attacher_proxy = Entity.attacher(:#{name}, #{attacher_class})
81
+ def _attachments
82
+ self.class._attachments
83
+ end
84
+
85
+ def save_attachments(entity)
86
+ _attachments.each do |a|
87
+ entity = save_attachment(entity, a[:name], a[:class])
88
+ end
89
+ yield(entity)
90
+ end
67
91
 
68
- if original_entity.#{name}
69
- attacher_proxy.#{name} = original_entity.#{name}
70
- attacher_proxy.#{name}_attacher.save
92
+ def save_attachment(original_entity, name, uploader_class)
93
+ file = original_entity.send(name)
71
94
 
72
- attacher_proxy.#{name}_attacher.replace
73
- attacher_proxy.#{name}_attacher._promote
95
+ if file
96
+ attacher = uploader_class::Attacher.new(OpenStruct.new, name)
97
+
98
+ attacher.assign(file)
99
+ attacher.finalize
74
100
 
75
101
  original_entity_attributes = original_entity.attributes
76
- original_entity_attributes.delete(:#{name})
102
+ original_entity_attributes.delete(name)
77
103
 
78
- entity = original_entity.class.new(original_entity_attributes.merge(#{name}_data: attacher_proxy.#{name}_data))
104
+ entity = original_entity.class.new(original_entity_attributes.merge(:"#{name}_data" => attacher.read))
79
105
  else
80
106
  entity = original_entity
81
107
  end
82
-
83
- yield(entity)
84
108
  end
85
109
 
86
- def delete_#{name}_attachment(id)
110
+ def delete_attachments(id)
87
111
  entity = find(id)
88
112
  yield
89
- entity.#{name}_attacher.destroy
90
- end
91
- RUBY
92
- end
93
- end
94
-
95
- module Entity
96
- def self.attacher(name, attacher)
97
- attachment_proxy ||= Class.new
98
- attachment_proxy.send(:include, attacher[name])
99
- attachment_proxy.class_eval do
100
- define_method :initialize do |attributes|
101
- end
102
-
103
- define_method :"#{name}_data" do
104
- @attachment_data
105
- end
106
-
107
- define_method :"#{name}_data=" do |data|
108
- @attachment_data = data
113
+ _attachments.each do |a|
114
+ entity.send("#{a[:name]}_attacher").destroy
115
+ end
109
116
  end
110
117
  end
111
- attachment_proxy.new({})
112
118
  end
113
119
  end
114
120
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: hanami-shrine
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.2.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Paweł Świątkowski
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2016-12-29 00:00:00.000000000 Z
11
+ date: 2017-01-20 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: shrine
@@ -38,6 +38,20 @@ dependencies:
38
38
  - - ">="
39
39
  - !ruby/object:Gem::Version
40
40
  version: '0.7'
41
+ - !ruby/object:Gem::Dependency
42
+ name: hanami-utils
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'
41
55
  - !ruby/object:Gem::Dependency
42
56
  name: bundler
43
57
  requirement: !ruby/object:Gem::Requirement
@@ -94,20 +108,6 @@ dependencies:
94
108
  - - ">="
95
109
  - !ruby/object:Gem::Version
96
110
  version: '0'
97
- - !ruby/object:Gem::Dependency
98
- name: hanami-validations
99
- requirement: !ruby/object:Gem::Requirement
100
- requirements:
101
- - - ">="
102
- - !ruby/object:Gem::Version
103
- version: '0'
104
- type: :development
105
- prerelease: false
106
- version_requirements: !ruby/object:Gem::Requirement
107
- requirements:
108
- - - ">="
109
- - !ruby/object:Gem::Version
110
- version: '0'
111
111
  description: Support for Shrine gem in Hanami framework
112
112
  email:
113
113
  - inquebrantable@gmail.com
@@ -153,4 +153,3 @@ signing_key:
153
153
  specification_version: 4
154
154
  summary: Support for Shrine gem in Hanami framework
155
155
  test_files: []
156
- has_rdoc: