web-connect 0.4.3 → 0.4.4

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: 1a0ddf6670c8a44de80ec1bb828a7f49c48c4602
4
- data.tar.gz: fd2e106b6ce8a0e5b203eee8859a3e2896363f03
3
+ metadata.gz: 9a78c316aa0dd58c987d6a68648bdf9d2af9f851
4
+ data.tar.gz: 41e0fb236184473185bbd711308fe1a87ed9810d
5
5
  SHA512:
6
- metadata.gz: 0b8f590197871ca9c2e75d50ad9b7e3ed47f2fb10811e7d9f1be63427b8a0b6ef06cd2d27c487d3dfee235e2380e41ce383dde148bf93b84a712147e41677e44
7
- data.tar.gz: da77c1128c56e9fd86801b319efedfc88dc79ad0817f389719f4f5b54893bbcb48e89efbb23bd78ba4a9cbfefd09da0a2c7566dc3b99e4cd3fc24ee3f4260a03
6
+ metadata.gz: 6eab39887c6b60a94689193dc3d0cb167f7e32c37d76eee2b1803b013db5819413c643b26d3bf25b6d0762f4ae83c9baae5b1c61399597cb2548fa943fa36b3e
7
+ data.tar.gz: 696ab4061e29e06d5c394c087476a3e20540c00e5b66c84a20123990771f4a2c3119be3780d58bc391d76d4b02e3af137acdaeec7f36a4a15449e63de13753f1
@@ -7,7 +7,7 @@ module Netfira::WebConnect
7
7
  def self.dispatch_event(timing, event, *args)
8
8
 
9
9
  # Allow multiple timings to be dispatched simultaneously, e.g. [:on, :after]
10
- return timing.each{ |t| dispatch_event t, event, *args } if timing.respond_to? :each
10
+ return timing.each{ |t| dispatch_event(t, event, *args) { yield } } if timing.respond_to? :each
11
11
 
12
12
  # The method to invoke, e.g. :on_update_product
13
13
  target = :"#{timing}_#{event}"
@@ -92,12 +92,14 @@ module Netfira::WebConnect
92
92
  readonly!
93
93
  yield
94
94
  instance_variable_set :@readonly, false
95
+ self
95
96
  end
96
97
 
97
98
  def as_readwrite
98
99
  instance_variable_set :@readonly, false
99
100
  yield
100
101
  readonly!
102
+ self
101
103
  end
102
104
 
103
105
  def reload
@@ -4,49 +4,80 @@ module Netfira::WebConnect
4
4
  extend ActiveSupport::Concern
5
5
 
6
6
  included do
7
- around_save :dispatch_create_or_update
8
- around_destroy :dispatch_destroy
7
+ before_save :dispatch_before_update
8
+ around_save :dispatch_around_update
9
+ after_save :dispatch_after_update
10
+ before_destroy :dispatch_before_destroy
11
+ around_destroy :dispatch_around_destroy
12
+ after_destroy :dispatch_after_destroy
9
13
  end
10
14
 
11
15
  private
12
16
 
13
- def dispatch_create_or_update
14
- previous = persisted? && changed_attributes.map do |key, value|
15
- # TODO: solve this type casting issue without guessing by attribute names!
16
- value = Checksum.new(value) if value and (key == 'checksum' or key == 'digest')
17
- [key, value]
18
- end.to_h
19
- event_name = previous ? 'update' : 'create'
20
- event_args = [event_name, self]
21
- named_event_args = ["#{event_name}_#{self.class.single_name}", self]
22
- if previous
23
- previous.merge! previous_translations if has_languages?
24
- event_args << previous
25
- named_event_args << previous
26
- end
27
- as_readonly do
28
- dispatch_event :before, *named_event_args
29
- dispatch_event :before, *event_args
30
- dispatch_event :around, *named_event_args do
31
- dispatch_event(:around, *event_args) { as_readwrite { yield } }
17
+ def update_event_args
18
+ @event_args ||= [].tap do |args|
19
+ previous = persisted? && changed_attributes.map do |key, value|
20
+ # TODO: solve this type casting issue without guessing by attribute names!
21
+ value = Checksum.new(value) if value and (key == 'checksum' or key == 'digest')
22
+ [key, value]
23
+ end.to_h
24
+ event_name = previous ? 'update' : 'create'
25
+ args << ["#{event_name}_#{self.class.single_name}", self]
26
+ args << [event_name, self]
27
+ if previous
28
+ previous.merge! previous_translations if has_languages?
29
+ args.each { |v| v << previous }
32
30
  end
33
- dispatch_event [:on, :after], *named_event_args
34
- dispatch_event [:on, :after], *event_args
35
31
  end
36
32
  end
37
33
 
38
- def dispatch_destroy
39
- event_args = ['delete', self]
40
- named_event_args = ["delete_#{self.class.single_name}", self]
34
+ def destroy_event_args
35
+ @event_args ||= [
36
+ ["delete_#{self.class.single_name}", self],
37
+ ['delete', self]
38
+ ]
39
+ end
40
+
41
+ def dispatch_with_args(args, *timing)
41
42
  as_readonly do
42
- dispatch_event [:before, :on], *named_event_args
43
- dispatch_event [:before, :on], *event_args
44
- dispatch_event :around, *named_event_args do
45
- dispatch_event(:around, *event_args) { as_readwrite { yield } }
43
+ if block_given?
44
+ dispatch_event timing, *args.first do
45
+ dispatch_event(timing, *args.last) { as_readwrite { yield } }
46
+ end
47
+ else
48
+ args.each { |x| dispatch_event timing, *x }
46
49
  end
47
- dispatch_event :after, *named_event_args
48
- dispatch_event :after, *event_args
49
50
  end
51
+ @event_args = nil if timing.include? :after
52
+ true
53
+ end
54
+
55
+ def dispatch_before_update
56
+ dispatch_with_args update_event_args, :before
57
+ end
58
+
59
+ def dispatch_around_update
60
+ dispatch_with_args(update_event_args, :around) { yield }
61
+ end
62
+
63
+ def dispatch_after_update
64
+ # Paperclip sets this up for us, but it runs after this handler, which
65
+ # means delegates don't get a saved image. Boo. We can save it twice,
66
+ # because the second save (the one Paperclip set up) will no-op.
67
+ attachment.save if has_file?
68
+ dispatch_with_args update_event_args, :on, :after
69
+ end
70
+
71
+ def dispatch_before_destroy
72
+ dispatch_with_args destroy_event_args, :before, :on
73
+ end
74
+
75
+ def dispatch_around_destroy
76
+ dispatch_with_args(destroy_event_args, :around) { yield }
77
+ end
78
+
79
+ def dispatch_after_destroy
80
+ dispatch_with_args destroy_event_args, :after
50
81
  end
51
82
 
52
83
  end
@@ -89,6 +89,15 @@ module Netfira::WebConnect
89
89
  self.size = value
90
90
  end
91
91
 
92
+ def url
93
+ exists? && remote_location || attachment.url
94
+ end
95
+
96
+ def exists?
97
+ !!(remote_location || attachment.exists?)
98
+ end
99
+ alias_method :exist?, :exists?
100
+
92
101
  private
93
102
 
94
103
  def set_file_defaults
@@ -1,6 +1,6 @@
1
1
  module Netfira
2
2
  module WebConnect
3
- VERSION = '0.4.3'
3
+ VERSION = '0.4.4'
4
4
  PLATFORM_AND_VERSION = 'Rack/' << VERSION
5
5
  end
6
6
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: web-connect
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.3
4
+ version: 0.4.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Neil E. Pearson
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2014-08-05 00:00:00.000000000 Z
12
+ date: 2014-08-06 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: activerecord