PaperclipAttachmentRemover 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2010 [name of plugin creator]
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.rdoc ADDED
@@ -0,0 +1,42 @@
1
+ = PaperclipAttachmentRemover
2
+
3
+ This plugin used for delete attachment created by paperclip.
4
+ It provides two methods for this:
5
+
6
+ +attachment_remover_observe+::
7
+ used for to point out that attachment attributes needs to be observed for removing. You can point out multiple attributes with paperclip attachment
8
+
9
+ +attachment_remover_name_for+::
10
+ used for getting correct form field name (usually checkbox) that should trigger attachment removing
11
+
12
+ == Installation
13
+
14
+ sudo gem install paperclip_attachment_remover
15
+
16
+ == Usage
17
+
18
+ In your model:
19
+
20
+ SomeThing << ActiveRecord::Base
21
+
22
+ has_attachment :icon
23
+
24
+ attachment_remover_observe :icon
25
+
26
+ end
27
+
28
+ In your update form:
29
+
30
+ form_for @something do |f|
31
+
32
+ f.label :icon, 'Icon picture'
33
+ f.file_field :icon
34
+
35
+ f.label attachment_remover_name_for(:icon), 'Remove existing icon picture'
36
+ f.check_box attachment_remover_name_for(:icon)
37
+
38
+ end
39
+
40
+
41
+
42
+ Copyright (c) 2010 v.valgis, released under the MIT license
data/Rakefile ADDED
@@ -0,0 +1,24 @@
1
+ require 'rake'
2
+ require 'rake/testtask'
3
+ require 'rake/rdoctask'
4
+
5
+ desc 'Default: run unit tests.'
6
+ task :default => :test
7
+
8
+ desc 'Test the paperclip_attachment_remover plugin.'
9
+ Rake::TestTask.new(:test) do |t|
10
+ t.libs << 'lib'
11
+ t.libs << 'test'
12
+ t.pattern = 'test/*_test.rb'
13
+ t.verbose = true
14
+ end
15
+
16
+ desc 'Generate documentation for the paperclip_attachment_remover plugin.'
17
+ Rake::RDocTask.new(:rdoc) do |rdoc|
18
+ rdoc.rdoc_dir = 'rdoc'
19
+ rdoc.title = 'PaperclipAttachmentRemover'
20
+ rdoc.options << '--line-numbers' << '--inline-source'
21
+ rdoc.rdoc_files.include('README.rdoc')
22
+ rdoc.rdoc_files.include('lib/**/*.rb')
23
+ end
24
+
data/init.rb ADDED
@@ -0,0 +1,2 @@
1
+ require 'paperclip_attachment_remover'
2
+ ActiveRecord::Base.send(:include, PaperclipAttachmentRemover)
@@ -0,0 +1,51 @@
1
+ module PaperclipAttachmentRemover #:nodoc:
2
+
3
+ def self.included(base) #:nodoc:
4
+ base.send :extend, ClassMethods
5
+ end
6
+
7
+ module ClassMethods
8
+ # list of attributes for observing by remover
9
+ attr_reader :attributes_for_removal_observation
10
+
11
+ # prefix for remover attribute
12
+ attr_reader :remove_name_prefix
13
+
14
+ # Set list of attributes for observing by remover
15
+ def attachment_remover_observe *attribute_names
16
+ send :include, InstanceMethods
17
+
18
+ options = attribute_names.extract_options!
19
+ @remove_name_prefix = options.fetch(:remove_name_prefix, 'remove')
20
+
21
+ existing_columns = column_names.select { |cn| cn.match('_file_name') }
22
+ attribute_names.map{|a| "#{a}_file_name"}.each do |attribute_name|
23
+ raise ActiveRecord::UnknownAttributeError unless existing_columns.include? attribute_name
24
+ end
25
+
26
+ attr_accessor *(attribute_names.map { |an| "#{@remove_name_prefix}_#{an}".to_sym })
27
+ before_save :paperclip_attachment_remover_proceed
28
+
29
+ @attributes_for_removal_observation = attribute_names
30
+ end
31
+ end
32
+
33
+ module InstanceMethods
34
+ # return name of form field that trigger deletion of attachment
35
+ def attachment_remover_name_for attribute
36
+ "#{self.class.remove_name_prefix}_#{attribute}"
37
+ end
38
+
39
+ private
40
+
41
+ def paperclip_attachment_remover_proceed #:nodoc:
42
+ unless self.class.attributes_for_removal_observation.empty?
43
+ self.class.attributes_for_removal_observation.each do |attribute|
44
+ self.send("#{attribute}=", nil) if self.send("#{self.class.remove_name_prefix}_#{attribute}") == "1" && !self.send(attribute).dirty?
45
+ end
46
+ end
47
+ true
48
+ end
49
+
50
+ end
51
+ end
@@ -0,0 +1,57 @@
1
+ require 'test_helper'
2
+ require File.expand_path(File.dirname(__FILE__) + '/../init')
3
+
4
+ # stub for paperclip
5
+ class Attachment
6
+ attr_accessor :dirty
7
+ def dirty?; @dirty; end
8
+ end
9
+
10
+ class ExampleModelWithAttachment < ActiveRecord::Base
11
+ attr_accessor :attachment
12
+ attachment_remover_observe :attachment
13
+ end
14
+
15
+ class PaperclipAttachmentRemoverTest < Test::Unit::TestCase
16
+
17
+ def setup
18
+ a = Attachment.new
19
+ a.dirty = true
20
+ @emwa = ExampleModelWithAttachment.new(:attachment_file_name => 'test.txt', :attachment => a)
21
+ end
22
+
23
+ def test_return_remover_field_name
24
+ assert_equal 'remove_attachment', @emwa.attachment_remover_name_for(:attachment)
25
+ assert_equal 'remove_attachment', @emwa.attachment_remover_name_for('attachment')
26
+ assert_not_equal 'remove_attachment', @emwa.attachment_remover_name_for('rose')
27
+ end
28
+
29
+ def test_return_remover_field_name_with_prefix_option
30
+ @emwa.class.send(:attachment_remover_observe, :attachment, :remove_name_prefix => :delete)
31
+ assert_equal 'delete_attachment', @emwa.attachment_remover_name_for(:attachment)
32
+ end
33
+
34
+ def test_setting_attribute_to_list_for_observation
35
+ assert @emwa.class.attributes_for_removal_observation.include?(:attachment)
36
+ end
37
+
38
+ def test_removing_attachment_when_remover_field_set_to_1_and_attachment_is_not_new
39
+ check false
40
+ end
41
+
42
+ def test_removing_attachment_when_remover_field_set_to_1_and_attachment_is_new
43
+ check true
44
+ end
45
+
46
+ def check(dirtyness)
47
+ @emwa.attachment.dirty = dirtyness
48
+ assert_not_nil @emwa.attachment
49
+ @emwa.update_attributes({:remove_attachment => "1"})
50
+ if dirtyness
51
+ assert_not_nil @emwa.attachment
52
+ else
53
+ assert_nil @emwa.attachment
54
+ end
55
+ end
56
+
57
+ end
@@ -0,0 +1,15 @@
1
+ require 'rubygems'
2
+ require 'test/unit'
3
+ require 'active_record'
4
+
5
+
6
+ ActiveRecord::Base.establish_connection({
7
+ :adapter => 'sqlite3',
8
+ :database => ':memory:'
9
+ })
10
+
11
+ ActiveRecord::Schema.define do
12
+ create_table "example_model_with_attachments", :force => true do |t|
13
+ t.string :attachment_file_name
14
+ end
15
+ end
metadata ADDED
@@ -0,0 +1,68 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: PaperclipAttachmentRemover
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: false
5
+ segments:
6
+ - 1
7
+ - 0
8
+ - 0
9
+ version: 1.0.0
10
+ platform: ruby
11
+ authors:
12
+ - v.valgis
13
+ autorequire:
14
+ bindir: bin
15
+ cert_chain: []
16
+
17
+ date: 2010-07-20 00:00:00 +04:00
18
+ default_executable:
19
+ dependencies: []
20
+
21
+ description:
22
+ email: vladimir.valgis@gmail.com
23
+ executables: []
24
+
25
+ extensions: []
26
+
27
+ extra_rdoc_files:
28
+ - README.rdoc
29
+ files:
30
+ - README.rdoc
31
+ - MIT-LICENSE
32
+ - Rakefile
33
+ - init.rb
34
+ - lib/paperclip_attachment_remover.rb
35
+ - test/paperclip_attachment_remover_test.rb
36
+ - test/test_helper.rb
37
+ has_rdoc: true
38
+ homepage: http://github.com/vvalgis/paperclip_attachment_remover
39
+ licenses: []
40
+
41
+ post_install_message:
42
+ rdoc_options: []
43
+
44
+ require_paths:
45
+ - lib
46
+ required_ruby_version: !ruby/object:Gem::Requirement
47
+ requirements:
48
+ - - ">="
49
+ - !ruby/object:Gem::Version
50
+ segments:
51
+ - 0
52
+ version: "0"
53
+ required_rubygems_version: !ruby/object:Gem::Requirement
54
+ requirements:
55
+ - - ">="
56
+ - !ruby/object:Gem::Version
57
+ segments:
58
+ - 0
59
+ version: "0"
60
+ requirements: []
61
+
62
+ rubyforge_project:
63
+ rubygems_version: 1.3.6
64
+ signing_key:
65
+ specification_version: 3
66
+ summary: Plugin that observe attachments and delete them when you trigger deletion by checking box in update form
67
+ test_files:
68
+ - test/paperclip_attachment_remover_test.rb