remarkable_paperclip 0.6.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.
- data/MIT-LICENSE +20 -0
- data/README.markdown +59 -0
- data/lib/matchers/allow_attachment_content_type_for_matcher.rb +46 -0
- data/lib/matchers/have_attached_file_matcher.rb +55 -0
- data/lib/matchers/validate_attachment_presence_matcher.rb +25 -0
- data/lib/matchers/validate_attachment_size_matcher.rb +63 -0
- data/lib/paperclip/extension.rb +28 -0
- data/lib/remarkable_paperclip.rb +13 -0
- data/locale/en.yml +37 -0
- metadata +63 -0
data/MIT-LICENSE
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
Copyright (c) 2009 Lawrence Pit
|
|
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.markdown
ADDED
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
Remarkable Paperclip
|
|
2
|
+
====================
|
|
3
|
+
|
|
4
|
+
Test matchers to test Paperclip with Remarkable.
|
|
5
|
+
|
|
6
|
+
Usage
|
|
7
|
+
-----
|
|
8
|
+
|
|
9
|
+
it { should have_attached_file :avatar, :styles => { :ico => "16x16", :normal => "48x48" } }
|
|
10
|
+
it { should validate_attachment_presence :avatar }
|
|
11
|
+
it { should validate_attachment_size :avatar, :less_than => 1.megabyte }
|
|
12
|
+
it { should validate_attachment_content_type :avatar, :allows => ["image/png", "image/jpg"], :rejects => ["video/mpeg"] }
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
Installation
|
|
16
|
+
------------
|
|
17
|
+
|
|
18
|
+
### As a Gem
|
|
19
|
+
|
|
20
|
+
Use this if you prefer to use versioned releases of remarkable_paperclip.
|
|
21
|
+
Specify the gem dependency in your config/environment.rb file:
|
|
22
|
+
|
|
23
|
+
Rails::Initializer.run do |config|
|
|
24
|
+
config.gem "lawrencepit-remarkable_paperclip", :lib => false, :source => "http://gems.github.com"
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
Then:
|
|
28
|
+
|
|
29
|
+
$ rake gems:install
|
|
30
|
+
$ rake gems:unpack
|
|
31
|
+
|
|
32
|
+
Then require remarkable_paperclip from your spec/spec_helper.rb file, before Spec::Runner is configured:
|
|
33
|
+
|
|
34
|
+
# requires for RSpec
|
|
35
|
+
require 'remarkable_paperclip'
|
|
36
|
+
Spec::Runner.configure do |config|
|
|
37
|
+
# ...
|
|
38
|
+
|
|
39
|
+
|
|
40
|
+
### As a Plugin
|
|
41
|
+
|
|
42
|
+
Use this if you prefer to use the edge version of remarkable_paperclip:
|
|
43
|
+
|
|
44
|
+
$ script/plugin install git://github.com/lawrencepit/remarkable_paperclip.git
|
|
45
|
+
|
|
46
|
+
|
|
47
|
+
Credits
|
|
48
|
+
-------
|
|
49
|
+
|
|
50
|
+
Written by [Lawrence Pit](http://lawrencepit.com).
|
|
51
|
+
|
|
52
|
+
Thanks to
|
|
53
|
+
[Remarkable](http://github.com/carlosbrando/remarkable) and
|
|
54
|
+
[Paperclip](http://github.com/thoughtbot/paperclip).
|
|
55
|
+
|
|
56
|
+
|
|
57
|
+
|
|
58
|
+
----
|
|
59
|
+
Copyright (c) 2009 Lawrence Pit, released under the MIT license
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
module Remarkable
|
|
2
|
+
module Paperclip
|
|
3
|
+
module Matchers
|
|
4
|
+
class AllowAttachmentContentTypeForMatcher < Remarkable::ActiveRecord::Base
|
|
5
|
+
arguments :attribute
|
|
6
|
+
|
|
7
|
+
optional :in, :splat => true
|
|
8
|
+
|
|
9
|
+
assertions :is_valid?
|
|
10
|
+
|
|
11
|
+
protected
|
|
12
|
+
|
|
13
|
+
def is_valid?
|
|
14
|
+
@options[:in].each do |value|
|
|
15
|
+
return false, :value => value.inspect unless allow?(value)
|
|
16
|
+
end
|
|
17
|
+
true
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
def interpolation_options
|
|
21
|
+
{ :in => array_to_sentence(@options[:in].map{|i| i.inspect}) }
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
private
|
|
25
|
+
|
|
26
|
+
def allow?(content_types)
|
|
27
|
+
content_types.all? do |content_type|
|
|
28
|
+
file = StringIO.new
|
|
29
|
+
file.content_type = content_type
|
|
30
|
+
attachment = @subject.attachment_for(@attribute)
|
|
31
|
+
attachment.assign(file)
|
|
32
|
+
attachment.errors[:content_type].nil?
|
|
33
|
+
end
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
def allow_attachment_content_type_for(attribute, *args, &block)
|
|
39
|
+
options = args.extract_options!
|
|
40
|
+
AllowAttachmentContentTypeForMatcher.new(attribute, options.merge!(:in => args), &block).spec(self)
|
|
41
|
+
end
|
|
42
|
+
alias_method :validate_attachment_content_type, :allow_attachment_content_type_for
|
|
43
|
+
|
|
44
|
+
end
|
|
45
|
+
end
|
|
46
|
+
end
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
module Remarkable
|
|
2
|
+
module Paperclip
|
|
3
|
+
module Matchers
|
|
4
|
+
class HaveAttachedFileMatcher < Remarkable::ActiveRecord::Base
|
|
5
|
+
arguments :attribute
|
|
6
|
+
|
|
7
|
+
optionals :styles, :default_style
|
|
8
|
+
|
|
9
|
+
assertions :defined?, :has_column?, :styles_match?, :default_style_match?
|
|
10
|
+
|
|
11
|
+
protected
|
|
12
|
+
def defined?
|
|
13
|
+
!attachment_definition.nil?
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
def has_column?
|
|
17
|
+
column_name = "#{@attribute}_file_name"
|
|
18
|
+
return @subject.class.column_names.include?(column_name),
|
|
19
|
+
:table_name => @subject.class.table_name, :column_name => column_name
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
def styles_match?
|
|
23
|
+
return true unless @options.key?(:styles)
|
|
24
|
+
actuals = attachment_definition[:styles].clone
|
|
25
|
+
match = @options[:styles].all? do |key, value|
|
|
26
|
+
actual = actuals.delete(key)
|
|
27
|
+
value == actual || value == case actual
|
|
28
|
+
when Hash then actual[:geometry]
|
|
29
|
+
when Array then actual[0]
|
|
30
|
+
else actual
|
|
31
|
+
end
|
|
32
|
+
end
|
|
33
|
+
return (match && actuals.size == 0),
|
|
34
|
+
:styles => @options[:styles].inspect, :actual => attachment_definition[:styles].inspect
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
def default_style_match?
|
|
38
|
+
return true unless @options.key?(:default_style)
|
|
39
|
+
return @options[:default_style] == attachment_definition[:default_style],
|
|
40
|
+
:style => @options[:default_style].inspect, :actual => attachment_definition[:default_style].inspect
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
private
|
|
44
|
+
def attachment_definition
|
|
45
|
+
@subject.class.attachment_definitions[@attribute]
|
|
46
|
+
end
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
def have_attached_file(*args)
|
|
50
|
+
HaveAttachedFileMatcher.new(*args).spec(self)
|
|
51
|
+
end
|
|
52
|
+
|
|
53
|
+
end
|
|
54
|
+
end
|
|
55
|
+
end
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
module Remarkable
|
|
2
|
+
module Paperclip
|
|
3
|
+
module Matchers
|
|
4
|
+
class ValidateAttachmentPresenceMatcher < Remarkable::ActiveRecord::Base
|
|
5
|
+
arguments :attribute
|
|
6
|
+
|
|
7
|
+
assertions :invalid_when_not_present?
|
|
8
|
+
|
|
9
|
+
protected
|
|
10
|
+
|
|
11
|
+
def invalid_when_not_present?
|
|
12
|
+
attachment = @subject.attachment_for(@attribute)
|
|
13
|
+
attachment.assign(nil)
|
|
14
|
+
!attachment.errors[:presence].nil?
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
def validate_attachment_presence(*args)
|
|
20
|
+
ValidateAttachmentPresenceMatcher.new(*args).spec(self)
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
end
|
|
24
|
+
end
|
|
25
|
+
end
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
module Remarkable
|
|
2
|
+
module Paperclip
|
|
3
|
+
module Matchers
|
|
4
|
+
class ValidateAttachmentSizeMatcher < Remarkable::ActiveRecord::Base
|
|
5
|
+
Infinity = (1.0/0)
|
|
6
|
+
|
|
7
|
+
arguments :attribute
|
|
8
|
+
|
|
9
|
+
optionals :in, :less_than, :greater_than
|
|
10
|
+
|
|
11
|
+
assertions :is_minimum?, :less_than_minimum?, :is_maximum?, :more_than_maximum?
|
|
12
|
+
|
|
13
|
+
before_assert do
|
|
14
|
+
if @options[:in]
|
|
15
|
+
@minimum, @maximum = @options[:in].first, @options[:in].last
|
|
16
|
+
elsif @options[:less_than]
|
|
17
|
+
@minimum, @maximum = 0, @options[:less_than]
|
|
18
|
+
elsif @options[:greater_than]
|
|
19
|
+
@minimum, @maximum = @options[:greater_than], Infinity
|
|
20
|
+
end
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
protected
|
|
24
|
+
|
|
25
|
+
def is_minimum?
|
|
26
|
+
valid_with_size?(@minimum)
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
def less_than_minimum?
|
|
30
|
+
!valid_with_size?(@minimum - 1)
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
def is_maximum?
|
|
34
|
+
valid_with_size?(@maximum == Infinity ? 2**256 : @maximum)
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
def more_than_maximum?
|
|
38
|
+
return true if @maximum == Infinity
|
|
39
|
+
!valid_with_size?(@maximum + 1)
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
def interpolation_options
|
|
43
|
+
{ :minimum => @minimum, :maximum => @maximum }
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
private
|
|
47
|
+
def valid_with_size?(size)
|
|
48
|
+
file = StringIO.new
|
|
49
|
+
file.instance_variable_set(:@stubbed_size, size)
|
|
50
|
+
def file.size ; @stubbed_size ; end
|
|
51
|
+
attachment = @subject.attachment_for(@attribute)
|
|
52
|
+
attachment.assign(file)
|
|
53
|
+
attachment.errors[:size].nil?
|
|
54
|
+
end
|
|
55
|
+
end
|
|
56
|
+
|
|
57
|
+
def validate_attachment_size(*args)
|
|
58
|
+
ValidateAttachmentSizeMatcher.new(*args).spec(self)
|
|
59
|
+
end
|
|
60
|
+
|
|
61
|
+
end
|
|
62
|
+
end
|
|
63
|
+
end
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
require 'paperclip' unless defined?(Paperclip)
|
|
2
|
+
|
|
3
|
+
# Always test using filesystem storage for speedy tests.
|
|
4
|
+
module Remarkable
|
|
5
|
+
module Paperclip
|
|
6
|
+
module Extensions
|
|
7
|
+
private
|
|
8
|
+
|
|
9
|
+
def self.included(base)
|
|
10
|
+
base.class_eval do
|
|
11
|
+
alias_method :initialize_without_remarkable, :initialize
|
|
12
|
+
alias_method :initialize, :initialize_with_remarkable
|
|
13
|
+
end
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
def initialize_with_remarkable(name, instance, options = {})
|
|
17
|
+
options[:storage] = :filesystem
|
|
18
|
+
initialize_without_remarkable(name, instance, options)
|
|
19
|
+
end
|
|
20
|
+
end
|
|
21
|
+
end
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
if defined?(Paperclip::PaperclipAttachment)
|
|
25
|
+
Paperclip::PaperclipAttachment.send(:include, Remarkable::Paperclip::Extensions)
|
|
26
|
+
else
|
|
27
|
+
Paperclip::Attachment.send(:include, Remarkable::Paperclip::Extensions)
|
|
28
|
+
end
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
require 'paperclip/extension'
|
|
2
|
+
require 'matchers/have_attached_file_matcher'
|
|
3
|
+
require 'matchers/validate_attachment_size_matcher'
|
|
4
|
+
require 'matchers/validate_attachment_presence_matcher'
|
|
5
|
+
require 'matchers/allow_attachment_content_type_for_matcher'
|
|
6
|
+
|
|
7
|
+
if defined?(Spec::Rails)
|
|
8
|
+
Remarkable.include_matchers!(Remarkable::Paperclip, Spec::Rails::Example::ModelExampleGroup)
|
|
9
|
+
else
|
|
10
|
+
Remarkable.include_matchers!(Remarkable::Paperclip, Spec::Example::ExampleGroup)
|
|
11
|
+
end
|
|
12
|
+
|
|
13
|
+
Remarkable.add_locale(File.dirname(__FILE__) + '/../locale/en.yml')
|
data/locale/en.yml
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
en:
|
|
2
|
+
remarkable:
|
|
3
|
+
paperclip:
|
|
4
|
+
have_attached_file:
|
|
5
|
+
description: "have attached file {{attribute}}"
|
|
6
|
+
expectations:
|
|
7
|
+
defined: "{{subject_name}} has {{attribute}} as an attached file"
|
|
8
|
+
has_column: "table {{table_name}} to have column {{column_name}}, but it does not"
|
|
9
|
+
styles_match: "{{subject_name}} to have attached file {{attribute}} with styles {{styles}}, got {{actual}}"
|
|
10
|
+
default_style_match: "{{subject_name}} to have attached file {{attribute}} with default style {{style}}, got {{actual}}"
|
|
11
|
+
optionals:
|
|
12
|
+
styles:
|
|
13
|
+
positive: "with styles {{inspect}}"
|
|
14
|
+
default_style:
|
|
15
|
+
positive: "with default style {{value}}"
|
|
16
|
+
validate_attachment_size:
|
|
17
|
+
description: "validate size for attached file {{attribute}}"
|
|
18
|
+
expectations:
|
|
19
|
+
is_minimum: "{{subject_name}} to be valid when size of attached file {{attribute}} is {{minimum}}"
|
|
20
|
+
less_than_minimum: "{{subject_name}} to be invalid when size of attached file {{attribute}} is less than {{minimum}}"
|
|
21
|
+
is_maximum: "{{subject_name}} to be valid when size of attached file {{attribute}} is {{maximum}}"
|
|
22
|
+
more_than_maximum: "{{subject_name}} to be invalid when size of attached file {{attribute}} is more than {{maximum}}"
|
|
23
|
+
optionals:
|
|
24
|
+
in:
|
|
25
|
+
positive: "to be in {{inspect}}"
|
|
26
|
+
less_than:
|
|
27
|
+
positive: "to be less than {{value}}"
|
|
28
|
+
greater_than:
|
|
29
|
+
positive: "to be greater than {{value}}"
|
|
30
|
+
validate_attachment_presence:
|
|
31
|
+
description: "validate attachment presence of attached file {{attribute}}"
|
|
32
|
+
expectations:
|
|
33
|
+
invalid_when_not_present: "{{subject_name}} to be invalid when attached file {{attribute}} is not present"
|
|
34
|
+
allow_attachment_content_type_for:
|
|
35
|
+
description: "allow {{in}} as content type for attachment {{attribute}}"
|
|
36
|
+
expectations:
|
|
37
|
+
is_valid: "{{subject_name}} to be valid when content type for attachment {{attribute}} is set to {{value}}"
|
metadata
ADDED
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: remarkable_paperclip
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 0.6.3
|
|
5
|
+
platform: ruby
|
|
6
|
+
authors:
|
|
7
|
+
- Lawrence Pit
|
|
8
|
+
autorequire:
|
|
9
|
+
bindir: bin
|
|
10
|
+
cert_chain: []
|
|
11
|
+
|
|
12
|
+
date: 2009-10-09 00:00:00 +11:00
|
|
13
|
+
default_executable:
|
|
14
|
+
dependencies: []
|
|
15
|
+
|
|
16
|
+
description:
|
|
17
|
+
email: lawrence.pit@gmail.com
|
|
18
|
+
executables: []
|
|
19
|
+
|
|
20
|
+
extensions: []
|
|
21
|
+
|
|
22
|
+
extra_rdoc_files: []
|
|
23
|
+
|
|
24
|
+
files:
|
|
25
|
+
- README.markdown
|
|
26
|
+
- MIT-LICENSE
|
|
27
|
+
- locale/en.yml
|
|
28
|
+
- lib/paperclip/extension.rb
|
|
29
|
+
- lib/remarkable_paperclip.rb
|
|
30
|
+
- lib/matchers/have_attached_file_matcher.rb
|
|
31
|
+
- lib/matchers/validate_attachment_presence_matcher.rb
|
|
32
|
+
- lib/matchers/validate_attachment_size_matcher.rb
|
|
33
|
+
- lib/matchers/allow_attachment_content_type_for_matcher.rb
|
|
34
|
+
has_rdoc: true
|
|
35
|
+
homepage: http://github.com/lawrencepit/remarkable_paperclip
|
|
36
|
+
licenses: []
|
|
37
|
+
|
|
38
|
+
post_install_message:
|
|
39
|
+
rdoc_options: []
|
|
40
|
+
|
|
41
|
+
require_paths:
|
|
42
|
+
- lib
|
|
43
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
44
|
+
requirements:
|
|
45
|
+
- - ">="
|
|
46
|
+
- !ruby/object:Gem::Version
|
|
47
|
+
version: "0"
|
|
48
|
+
version:
|
|
49
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
50
|
+
requirements:
|
|
51
|
+
- - ">="
|
|
52
|
+
- !ruby/object:Gem::Version
|
|
53
|
+
version: "0"
|
|
54
|
+
version:
|
|
55
|
+
requirements: []
|
|
56
|
+
|
|
57
|
+
rubyforge_project:
|
|
58
|
+
rubygems_version: 1.3.5
|
|
59
|
+
signing_key:
|
|
60
|
+
specification_version: 3
|
|
61
|
+
summary: Remarkable matchers for Paperclip.
|
|
62
|
+
test_files: []
|
|
63
|
+
|