paperclip_private 0.0.2 → 0.0.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 66481098f3f82d89f0bfb3e357804c7603159aef
4
- data.tar.gz: 791cde5a10795fb9cd4faa0d3b1d16f843740c57
3
+ metadata.gz: 36298a423bc83490761e76840733815113ca63cf
4
+ data.tar.gz: a316a0e12a81321eb9f3ce0433e14770470d4011
5
5
  SHA512:
6
- metadata.gz: 390dfc6fafd5108e6d1e4088fed7db870c1bf44fa482e79d61b4be2f3747270dd1309117860cfd6505411f70e35eeb2d2243a67e2213f220dbb505be42cf7499
7
- data.tar.gz: 01c204df3132537a2e1106f14fd6936b43c12b4216ba51c34ab657f567d1a3970a06fc88e95d8529286312ea4bccd2360edafab44376aea97f30e1c8d44b7a99
6
+ metadata.gz: ce24e9f4ec483e87bdc3ca6c5636179cedb5c8705adcfb479e05cc0ae0942de9525c1b272978875de3863258167c0ee1b0985472f4177c2e9eb9ad238def0c27
7
+ data.tar.gz: 99d6769fb54991db63cc9aa93e22ba13a4181e4d00d1a9886591cca43d8111e02d460562f57e9218825f3c2bdae2ba13c630899672e6fd96e0ee36e1ef94846c
data/README.md CHANGED
@@ -10,15 +10,16 @@
10
10
  +mount PaperclipPrivate::Engine => 'paperclip/'
11
11
  +```
12
12
  +
13
- +In your ApplicationController add the method `paperclip_whitelist` which returns an array of the models with attachments you would like to serve privately.
14
- +Ex:
13
+ +You have to register class names and their attachments with the whitelist registry. This can happen in the controller or in config/paperclip_private.rb
14
+ +The following example would whitelist the class PrivateAttachment's file:
15
15
  +```ruby
16
- +def paperclip_whitelist
17
- + [PrivateAttachment]
18
- +end
16
+ +require 'paperclip_private'
17
+ +PaperclipPrivate::Whitelist.register({PrivateAttachment: :file})
19
18
  +```
19
+ +You can also pass an array of names like `{PrivateAttachment: [:file, :avatar]}`.
20
20
  +
21
- +Then in your model add `privacy: :private` to the has_attached_file options. Then add the method `can_download_attachment?`.
21
+ +Then in your model add `privacy: :private` to the has_attached_file options. This can also accept a lambda that gets passed the attachment instance and expects back either `:private` or `:public`.
22
+ +Then add the method `can_download_attachment?`.
22
23
  +The method `can_download_attachment?` gets passed the controller instance and the params and is expected to return true, false, or raise Paperclip::Errors::AccessDeniedError.
23
24
  +The controller is passed so that methods like current_user can be run on it to get the user instance for validation puprposes. Duplicated params gets passed to do things like allow anyone if the style is :thumb or to only allow :original to paid users.
24
25
  +Ex:
@@ -17,16 +17,17 @@ module PaperclipPrivate
17
17
  protected
18
18
  # Return the private arratcment registry
19
19
  def paperclip_whitelist
20
- super if self.class.superclass.instance_methods.include?(:paperclip_whitelist) # Call classes in an array so they appear in the registry.
21
- ::PaperclipPrivate::AttachmentRegistry.registry
20
+ # super if self.class.superclass.instance_methods.include?(:paperclip_whitelist) # Call classes in an array so they appear in the registry.
21
+ # ::PaperclipPrivate::AttachmentRegistry.registry
22
+ ::PaperclipPrivate::Whitelist.registry
22
23
  end
23
24
 
24
25
  private
25
26
  # Confirm class name is valid and attachment name is valid before calling constantize in set_attachment method.
26
27
  def validate_params!
27
28
  attachments = paperclip_whitelist[params[:class_name]]
28
- raise ::PaperclipPrivate::Errors::ControllerValidationError if attachments.nil? || !attachments.keys.map(&:to_s).include?(params[:attachment])
29
- @options = attachments[params[:attachment].to_sym]
29
+ raise ::PaperclipPrivate::Errors::ControllerValidationError if !attachments.include?(params[:attachment])
30
+ # @options = attachments[params[:attachment].to_sym]
30
31
  end
31
32
 
32
33
  # Sets object, attachment, and styles
@@ -35,6 +36,8 @@ module PaperclipPrivate
35
36
  object_id = params[:id]
36
37
  @object = klass.find(object_id)
37
38
  @attachment = @object.send(params[:attachment])
39
+ raise ActiveRecord::RecordNotFound if @attachment.nil?
40
+ @options = @attachment.options
38
41
  @styles = @options[:styles] || {}
39
42
  @styles = @styles.call(@attachment) if @styles.respond_to?(:call)
40
43
  end
@@ -2,10 +2,9 @@ require 'paperclip'
2
2
  require 'paperclip_private/interpolations'
3
3
  require 'paperclip_private/attachment'
4
4
  require 'paperclip_private/errors'
5
- require 'paperclip_private/has_attached_file'
6
- require 'paperclip_private/attachment_registry'
5
+ require 'paperclip_private/whitelist'
7
6
  require 'paperclip_private/engine'
8
7
 
8
+
9
9
  Paperclip::Attachment.include PaperclipPrivate::Attachment
10
- Paperclip::HasAttachedFile.prepend PaperclipPrivate::HasAttachedFile
11
10
  Paperclip::Interpolations.extend PaperclipPrivate::Interpolations
@@ -17,7 +17,9 @@ module PaperclipPrivate
17
17
  module Initializer
18
18
  def initialize(name, instance, options = {})
19
19
  defaults = self.class.default_options.dup
20
- if options[:privacy] == :private # change default options if private before merging with options.
20
+ privacy = options[:privacy]
21
+ privacy = privacy.call(instance) if privacy.respond_to?(:call)
22
+ if privacy == :private # change default options if private before merging with options.
21
23
  defaults[:path] = ":rails_root/:privacy#{defaults[:url]}"
22
24
  defaults[:url] = "#{Rails.application.routes.url_helpers.paperclip_private_engine_path}/private/:klass/:id/:attachment_singular/:style"
23
25
  end
@@ -7,9 +7,11 @@ module PaperclipPrivate
7
7
  end
8
8
  end
9
9
 
10
- # Returns the style, or the default style if nil is supplied.
10
+ # Returns the privacy
11
11
  def privacy(attachment, style_name)
12
- attachment.options[:privacy]
12
+ # attachment.options[:privacy]
13
+ privacy = attachment.options[:privacy]
14
+ privacy.respond_to?(:call) ? privacy.call(attachment.instance) : privacy
13
15
  end
14
16
 
15
17
  # Returns the class name without pluarlizing it.
@@ -1,3 +1,3 @@
1
1
  module PaperclipPrivate
2
- VERSION = '0.0.2'
2
+ VERSION = '0.0.3'
3
3
  end
@@ -0,0 +1,29 @@
1
+ module PaperclipPrivate
2
+ module Whitelist
3
+ def self.registry
4
+ @registry ||= {}
5
+ end
6
+
7
+ def self.names_for_class(klass)
8
+ registry[klass] || []
9
+ end
10
+
11
+ def self.register_class(klass, name)
12
+ return if klass.nil? || name.nil?
13
+ class_name = klass.to_s
14
+ registry[class_name] ||= []
15
+ registry[class_name] << name.to_s
16
+ end
17
+
18
+ def self.register(hash = {})
19
+ hash.each do |k, v|
20
+ if v.is_a? Array
21
+ v.each { |name| register_class(k, name) }
22
+ else
23
+ register_class(k, v)
24
+ end
25
+ end
26
+ end
27
+
28
+ end
29
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: paperclip_private
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - jose
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-09-06 00:00:00.000000000 Z
11
+ date: 2017-09-22 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -70,12 +70,11 @@ files:
70
70
  - config/routes.rb
71
71
  - lib/paperclip_private.rb
72
72
  - lib/paperclip_private/attachment.rb
73
- - lib/paperclip_private/attachment_registry.rb
74
73
  - lib/paperclip_private/engine.rb
75
74
  - lib/paperclip_private/errors.rb
76
- - lib/paperclip_private/has_attached_file.rb
77
75
  - lib/paperclip_private/interpolations.rb
78
76
  - lib/paperclip_private/version.rb
77
+ - lib/paperclip_private/whitelist.rb
79
78
  - lib/tasks/paperclip_private_tasks.rake
80
79
  - paperclip_private.gemspec
81
80
  - test/dummy/Rakefile
@@ -1,13 +0,0 @@
1
- module PaperclipPrivate
2
- module AttachmentRegistry
3
- def self.registry
4
- @registry ||= {}
5
- end
6
-
7
- def self.register(klass, name, options)
8
- class_name = klass.to_s
9
- self.registry[class_name] ||= {}
10
- self.registry[class_name][name] = options
11
- end
12
- end
13
- end
@@ -1,10 +0,0 @@
1
- module PaperclipPrivate
2
- module HasAttachedFile
3
-
4
- def register_new_attachment
5
- super
6
- PaperclipPrivate::AttachmentRegistry.register(@klass, @name, @options) if @options[:privacy] == :private
7
- end
8
-
9
- end
10
- end