lawrencepit-remarkable_paperclip 0.5.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 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
data/lib/en.yml ADDED
@@ -0,0 +1,40 @@
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
+ optionals:
11
+ styles:
12
+ positive: "with styles {{inspect}}"
13
+ validate_attachment_size:
14
+ description: "validate size for attached file {{attribute}}"
15
+ expectations:
16
+ is_minimum: "{{subject_name}} to be valid when size of attached file {{attribute}} is {{minimum}}"
17
+ less_than_minimum: "{{subject_name}} to be invalid when size of attached file {{attribute}} is less than {{minimum}}"
18
+ is_maximum: "{{subject_name}} to be valid when size of attached file {{attribute}} is {{maximum}}"
19
+ more_than_maximum: "{{subject_name}} to be invalid when size of attached file {{attribute}} is more than {{maximum}}"
20
+ optionals:
21
+ in:
22
+ positive: "to be in {{inspect}}"
23
+ less_than:
24
+ positive: "to be less than {{value}}"
25
+ greater_than:
26
+ positive: "to be greater than {{value}}"
27
+ validate_attachment_presence:
28
+ description: "validate attachment presence of attached file {{attribute}}"
29
+ expectations:
30
+ invalid_when_not_present: "{{subject_name}} to be invalid when attached file {{attribute}} is not present"
31
+ validate_attachment_content_type:
32
+ description: "validate attachment content type of attached file {{attribute}}"
33
+ expectations:
34
+ allows_match: "{{subject_name}} to accept content types {{allows}} for attached file {{attribute}}"
35
+ rejects_match: "{{subject_name}} to reject content types {{rejects}} for attached file {{attribute}}"
36
+ optionals:
37
+ allows:
38
+ positive: "allowing {{inspect}}"
39
+ rejects:
40
+ positive: "rejecting {{inspect}}"
@@ -0,0 +1,45 @@
1
+ module Remarkable
2
+ module Paperclip
3
+ module Matchers
4
+ class HaveAttachedFileMatcher < Remarkable::ActiveRecord::Base
5
+ arguments :attribute
6
+
7
+ optionals :styles
8
+
9
+ assertions :defined?, :has_column?, :styles_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
+ (actual.instance_of?(Hash) ? actual[:geometry] : actual) == value
28
+ end
29
+ return (match && actuals.size == 0),
30
+ :styles => @options[:styles].inspect, :actual => attachment_definition[:styles].inspect
31
+ end
32
+
33
+ private
34
+ def attachment_definition
35
+ @subject.class.attachment_definitions[@attribute]
36
+ end
37
+ end
38
+
39
+ def have_attached_file(*args)
40
+ HaveAttachedFileMatcher.new(*args).spec(self)
41
+ end
42
+
43
+ end
44
+ end
45
+ end
@@ -0,0 +1,7 @@
1
+ require 'have_attached_file_matcher'
2
+ require 'validate_attachment_size_matcher'
3
+ require 'validate_attachment_presence_matcher'
4
+ require 'validate_attachment_content_type_matcher'
5
+
6
+ Remarkable.include_matchers!(Remarkable::Paperclip, Spec::Example::ExampleGroup)
7
+ Remarkable.add_locale(File.dirname(__FILE__) + '/en.yml')
@@ -0,0 +1,47 @@
1
+ module Remarkable
2
+ module Paperclip
3
+ module Matchers
4
+ class ValidateAttachmentContentTypeMatcher < Remarkable::ActiveRecord::Base
5
+ arguments :attribute
6
+
7
+ optionals :allows, :rejects
8
+
9
+ assertions :allows_match?, :rejects_match?
10
+
11
+ protected
12
+
13
+ def allows_match?
14
+ return true unless @options.key?(:allows)
15
+ allow?(@options[:allows])
16
+ end
17
+
18
+ def rejects_match?
19
+ return true unless @options.key?(:rejects)
20
+ !allow?(@options[:rejects])
21
+ end
22
+
23
+ def interpolation_options
24
+ { :allows => @options[:allows].inspect, :rejects => @options[:rejects].inspect }
25
+ end
26
+
27
+ private
28
+
29
+ def allow?(content_types)
30
+ content_types.all? do |content_type|
31
+ file = StringIO.new
32
+ file.content_type = content_type
33
+ attachment = @subject.attachment_for(@attribute)
34
+ attachment.assign(file)
35
+ attachment.errors[:content_type].nil?
36
+ end
37
+ end
38
+
39
+ end
40
+
41
+ def validate_attachment_content_type(*args)
42
+ ValidateAttachmentContentTypeMatcher.new(*args).spec(self)
43
+ end
44
+
45
+ end
46
+ end
47
+ 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
metadata ADDED
@@ -0,0 +1,60 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: lawrencepit-remarkable_paperclip
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.5.3
5
+ platform: ruby
6
+ authors:
7
+ - Lawrence Pit
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-05-16 00:00:00 -07: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
+ - lib/en.yml
28
+ - lib/remarkable_paperclip.rb
29
+ - lib/have_attached_file_matcher.rb
30
+ - lib/validate_attachment_presence_matcher.rb
31
+ - lib/validate_attachment_size_matcher.rb
32
+ - lib/validate_attachment_content_type_matcher.rb
33
+ has_rdoc: false
34
+ homepage: http://github.com/lawrencepit/remarkable_paperclip
35
+ post_install_message:
36
+ rdoc_options: []
37
+
38
+ require_paths:
39
+ - lib
40
+ required_ruby_version: !ruby/object:Gem::Requirement
41
+ requirements:
42
+ - - ">="
43
+ - !ruby/object:Gem::Version
44
+ version: "0"
45
+ version:
46
+ required_rubygems_version: !ruby/object:Gem::Requirement
47
+ requirements:
48
+ - - ">="
49
+ - !ruby/object:Gem::Version
50
+ version: "0"
51
+ version:
52
+ requirements: []
53
+
54
+ rubyforge_project:
55
+ rubygems_version: 1.2.0
56
+ signing_key:
57
+ specification_version: 2
58
+ summary: Remarkable matchers for Paperclip.
59
+ test_files: []
60
+