hanami-shrine 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
  SHA1:
3
- metadata.gz: 0a4b2d769b9c4ae74aa70fb0123ec6c946d99c9a
4
- data.tar.gz: 7697d45f9ece1f8c340135b90499448646427d99
3
+ metadata.gz: e247ae2c7d1155f30389af1800709dd2eca26692
4
+ data.tar.gz: b9cce6169be65754c1e79f3b2b14557c7eebbae1
5
5
  SHA512:
6
- metadata.gz: ce937ea318c92b34618915753a11d39f6244838675bb931f0f14b45df641c3a571d39b23ee56556495fde30e1dff0ddb7c30030144527292e191f9638338ed8a
7
- data.tar.gz: 0cd4719b771e3810eb7c651244ff77560077477ce12aeae2781da312701c1e51f44add62a54c8801d9c1f5800dbdfbebda8e5d668a995810b908ca32d48633f4
6
+ metadata.gz: 79e3315c7a4cb0a14898b17eb67eaf995419971472fdb0b3f447158dc46b758d3c03ed5d7d01bed6730249ceefa1181a8e061cd3803184499d1dd23358168f13
7
+ data.tar.gz: fbf4bb823e49230e23ea3dee488b691e05ffe3ff86342406e8da955d6ae428d7c02e2483c8a8777b10e674c13e7ad63b729840442074e535517a7a2f4956cdfc
data/.travis.yml CHANGED
@@ -1,4 +1,4 @@
1
1
  language: ruby
2
2
  rvm:
3
- - 2.2.3
3
+ - 2.3.3
4
4
  before_install: gem install bundler -v 1.10.6
data/Gemfile CHANGED
@@ -2,3 +2,6 @@ source 'https://rubygems.org'
2
2
 
3
3
  # Specify your gem's dependencies in hanami-shrine.gemspec
4
4
  gemspec
5
+ gem 'shrine', '>= 2.0.0'
6
+ gem 'sqlite3'
7
+ gem 'pry'
data/README.md CHANGED
@@ -4,6 +4,13 @@ This gem aims at providing support for [Shrine](https://github.com/janko-m/shrin
4
4
 
5
5
  [![Build Status](https://travis-ci.org/katafrakt/hanami-shrine.svg)](https://travis-ci.org/katafrakt/hanami-shrine)
6
6
  [![Gem Version](https://badge.fury.io/rb/hanami-shrine.svg)](https://badge.fury.io/rb/hanami-shrine)
7
+ [![Code Climate](https://codeclimate.com/github/katafrakt/hanami-shrine/badges/gpa.svg)](https://codeclimate.com/github/katafrakt/hanami-shrine)
8
+
9
+ ### Current compatibility status
10
+
11
+ * working with Hanami version 0.7.x
12
+ * working with Hanami version 0.8.x without validations (which have been extracted to separate gem)
13
+ * does not work work with Hanami 0.9.x (work in progress, see [#1](https://github.com/katafrakt/hanami-shrine/issues/1))
7
14
 
8
15
  ## Installation
9
16
 
@@ -30,7 +37,7 @@ end
30
37
  Then, in your repository add (assuming your attachment is `avatar`):
31
38
 
32
39
  ```ruby
33
- extend ImageAttachment.repository(:avatar)
40
+ prepend ImageAttachment.repository(:avatar)
34
41
  ```
35
42
 
36
43
  And in your entity:
@@ -39,32 +46,14 @@ And in your entity:
39
46
  include ImageAttachment[:avatar]
40
47
  ```
41
48
 
42
- To use validations, enable them during setup of the plugin:
43
-
44
- ```ruby
45
- class ImageAttachment < Shrine
46
- plugin :hanami, validations: true
47
- end
48
- ```
49
-
50
- And you can write some validation code. For example:
51
-
52
- ```ruby
53
- class ImageAttachment < Shrine
54
- plugin :validation_helpers
55
- plugin :determine_mime_type
56
- plugin :hanami, validations: true
57
-
58
- Attacher.validate do
59
- validate_max_size 180_000, message: "is too large (max is 2 MB)"
60
- validate_mime_type_inclusion ["image/jpg", "image/jpeg"]
61
- end
62
- end
63
- ```
49
+ For inspiration read a [blog post](http://katafrakt.me/2016/02/04/shrine-hanami-uploads/), look at [the specs](https://github.com/katafrakt/hanami-shrine/tree/master/spec/hanami) or [example repo](https://github.com/katafrakt/hanami-shrine-example).
64
50
 
65
- Remember that you have to call `valid?` or `validate` yourself. There is not as much magic in Hanami as it is in Rails :wink:
51
+ ## Important changes since 0.1 version
66
52
 
67
- For inspiration look at [the specs](https://github.com/katafrakt/hanami-shrine/tree/master/spec/hanami) or [example repo](https://github.com/katafrakt/hanami-shrine-example).
53
+ As Hanami has been upgraded to 0.9, it started to use new engine for the entities in `hanami-model`. It required heavy changes in `hanami-shrine` to accomodate to new paradigm. Unfortunately, some of them are breaking. Here's a summary list:
54
+ * Validations are gone (for now, hopefully)
55
+ * You need to use `prepend` instead of `extend` in your repository
56
+ * Entities are now read-only. You need to initialize them with the attachment, not add it later: `MyEntity.new(avatar: File.open('my_avatar.png')`. This is a Hanami change, but it's worth mentioning here as well.
68
57
 
69
58
  ## Development
70
59
 
@@ -20,7 +20,7 @@ Gem::Specification.new do |spec|
20
20
  spec.require_paths = ["lib"]
21
21
 
22
22
  spec.add_dependency 'shrine'
23
- spec.add_dependency 'hanami-model'
23
+ spec.add_dependency 'hanami-model', '>= 0.7'
24
24
 
25
25
  spec.add_development_dependency "bundler", "~> 1.10"
26
26
  spec.add_development_dependency "rake", "~> 10.0"
@@ -1,5 +1,5 @@
1
1
  module Hanami
2
2
  module Shrine
3
- VERSION = "0.1.0"
3
+ VERSION = "0.2.0"
4
4
  end
5
5
  end
@@ -3,65 +3,114 @@ require 'shrine'
3
3
  class Shrine
4
4
  module Plugins
5
5
  module Hanami
6
- def self.configure(uploader, validations: nil)
7
- uploader.opts[:hanami_validations] = validations
8
- end
9
-
10
6
  module AttachmentMethods
11
7
  def initialize(name)
12
8
  super
13
9
 
14
- if shrine_class.opts[:hanami_validations]
15
- module_eval <<-RUBY, __FILE__, __LINE__ + 1
16
- def validate
17
- super
18
- #{name}_attacher.errors.each do |message|
19
- errors.add(:#{name}, message)
20
- end
10
+ module_eval <<-RUBY, __FILE__, __LINE__ + 1
11
+ module EntitySupport
12
+ attr_reader :attributes
13
+ def initialize(attributes)
14
+ attachment = attributes[:#{name}]
15
+ @_#{name} = attachment
16
+ self.#{name}_attacher
17
+ super(attributes)
21
18
  end
22
- RUBY
23
- end
19
+
20
+ def #{name}_data=(data)
21
+ @#{name}_data = data
22
+ end
23
+
24
+ def #{name}_data
25
+ super || @#{name}_data
26
+ end
27
+
28
+ def #{name}
29
+ @_#{name} || super
30
+ end
31
+
32
+ def attributes
33
+ @_#{name} ? super.merge(#{name}: @_#{name}) : super
34
+ end
35
+ end
36
+
37
+ prepend EntitySupport
38
+ RUBY
24
39
  end
25
40
  end
26
41
 
27
42
  module ClassMethods
28
43
  def repository(name)
29
- RepositoryMethods.new(name)
44
+ RepositoryMethods.new(name, self)
30
45
  end
31
46
  end
32
47
 
33
48
  class RepositoryMethods < Module
34
- def initialize(name)
49
+ def initialize(name, attacher_class)
35
50
  module_eval <<-RUBY, __FILE__, __LINE__ + 1
36
51
  def create(entity)
37
- save_#{name}_attachment(entity) { super }
52
+ save_#{name}_attachment(entity) { |new_entity| super(new_entity) }
38
53
  end
39
- def update(entity)
40
- save_#{name}_attachment(entity) { super }
54
+ def update(id, entity)
55
+ save_#{name}_attachment(entity) { |new_entity| super(id, new_entity) }
41
56
  end
42
57
  def persist(entity)
43
- save_#{name}_attachment(entity) { super }
58
+ save_#{name}_attachment(entity) { |new_entity| super(new_entity) }
44
59
  end
45
- def delete(entity)
46
- delete_#{name}_attachment(entity) { super }
60
+ def delete(id)
61
+ delete_#{name}_attachment(id) { super(id) }
47
62
  end
48
63
 
49
64
  private
50
- def save_#{name}_attachment(entity)
51
- entity.#{name}_attacher.save
65
+ def save_#{name}_attachment(original_entity)
66
+ attacher_proxy = Entity.attacher(:#{name}, #{attacher_class})
52
67
 
53
- entity.#{name}_attacher.replace
54
- entity.#{name}_attacher._promote
55
- yield
68
+ if original_entity.#{name}
69
+ attacher_proxy.#{name} = original_entity.#{name}
70
+ attacher_proxy.#{name}_attacher.save
71
+
72
+ attacher_proxy.#{name}_attacher.replace
73
+ attacher_proxy.#{name}_attacher._promote
74
+
75
+ original_entity_attributes = original_entity.attributes
76
+ original_entity_attributes.delete(:#{name})
77
+
78
+ entity = original_entity.class.new(original_entity_attributes.merge(#{name}_data: attacher_proxy.#{name}_data))
79
+ else
80
+ entity = original_entity
81
+ end
82
+
83
+ yield(entity)
56
84
  end
57
85
 
58
- def delete_#{name}_attachment(entity)
86
+ def delete_#{name}_attachment(id)
87
+ entity = find(id)
59
88
  yield
60
89
  entity.#{name}_attacher.destroy
61
90
  end
62
91
  RUBY
63
92
  end
64
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
109
+ end
110
+ end
111
+ attachment_proxy.new({})
112
+ end
113
+ end
65
114
  end
66
115
 
67
116
  register_plugin(:hanami, Hanami)
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.1.0
4
+ version: 0.2.0
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-01-25 00:00:00.000000000 Z
11
+ date: 2016-12-29 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: shrine
@@ -30,14 +30,14 @@ dependencies:
30
30
  requirements:
31
31
  - - ">="
32
32
  - !ruby/object:Gem::Version
33
- version: '0'
33
+ version: '0.7'
34
34
  type: :runtime
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
38
  - - ">="
39
39
  - !ruby/object:Gem::Version
40
- version: '0'
40
+ version: '0.7'
41
41
  - !ruby/object:Gem::Dependency
42
42
  name: bundler
43
43
  requirement: !ruby/object:Gem::Requirement
@@ -148,7 +148,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
148
148
  version: '0'
149
149
  requirements: []
150
150
  rubyforge_project:
151
- rubygems_version: 2.4.5
151
+ rubygems_version: 2.5.1
152
152
  signing_key:
153
153
  specification_version: 4
154
154
  summary: Support for Shrine gem in Hanami framework