paperclip-defaults 1.0.2 → 1.0.3

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -14,8 +14,14 @@ To use Paperclip::Defaults simply add it to your Gemfile and update config/appli
14
14
  config.paperclip_defaults = {default_url: "http://www.example.com/missing.png"}
15
15
  ```
16
16
 
17
- In addition to allowing you to seet the defaults for Paperclip has_attached_file now accepts a new option: default_asset_url
18
- When default_asset_url is passed in has_attached_file will determine the URL of the image through the Rails asset pipeline (making use of an asset host if you have set one for your Rails application).
17
+ In addition to allowing you to set the defaults for Paperclip has_attached_file now accepts a new option: default_asset_url
18
+ When default_asset_url is passed in has_attached_file will determine the URL of the image through the Rails asset pipeline (making use of an asset host if you have set one for your Rails application). You can also make use of a Proc or lambda like so:
19
+
20
+ ``` ruby
21
+ class YourModel < ActiveRecord::Base
22
+ has_attached_file :logo, default_asset_url: ->(attachment) { "#{attachment.instance.your_special_field}.png" }
23
+ end
24
+ ```
19
25
 
20
26
  Development
21
27
  -----------
@@ -15,7 +15,8 @@ module Paperclip
15
15
  paperclip_options = ::Rails.application.config.paperclip_defaults.merge(options)
16
16
  if paperclip_options[:default_asset_url]
17
17
  begin
18
- paperclip_options[:default_url] = ActionController::Base.new.view_context.asset_path(paperclip_options[:default_asset_url])
18
+ default_asset_url = paperclip_options[:default_asset_url]
19
+ paperclip_options[:default_url] = ->(attachment) {ActionController::Base.new.view_context.asset_path(default_asset_url.respond_to?(:call) ? default_asset_url.call(attachment) : default_asset_url)}
19
20
  rescue StandardError => e
20
21
  ::Rails.logger.warn("Could not set default asset url for '#{paperclip_options[:default_asset_url]}'")
21
22
  end
@@ -1,5 +1,5 @@
1
1
  module Paperclip
2
2
  module Defaults
3
- VERSION = "1.0.2"
3
+ VERSION = "1.0.3"
4
4
  end
5
5
  end
@@ -9,7 +9,7 @@ class TestPaperclipDefaults < MiniTest::Unit::TestCase
9
9
 
10
10
  def test_has_attached_file_sets_defaults
11
11
  Rails.application.config.stubs(:paperclip_defaults).returns({storage: :fog})
12
- model = Class.new(ActiveRecord::Base) do
12
+ model = Class.new(ActiveRecord::Base) do
13
13
  has_attached_file :image1, default_url: "/unknown.png"
14
14
  end
15
15
 
@@ -22,17 +22,34 @@ class TestPaperclipDefaults < MiniTest::Unit::TestCase
22
22
  ActionController::Base.new.stubs(:view_context).returns(mock('view_context'))
23
23
  ActionController::Base.new.view_context.stubs(:asset_path).returns('http://www.example.com/unknown.png')
24
24
 
25
- model = Class.new(ActiveRecord::Base) do
25
+ model = Class.new(ActiveRecord::Base) do
26
26
  has_attached_file :image1, default_asset_url: "unknown.png"
27
27
  end
28
28
 
29
29
  expect = {validations: [], default_url: "http://www.example.com/unknown.png"}
30
- assert_equal expect, model.attachment_definitions[:image1]
30
+ assert_equal [], model.attachment_definitions[:image1][:validations]
31
+ assert_equal Proc, model.attachment_definitions[:image1][:default_url].class
32
+ assert_equal "http://www.example.com/unknown.png", model.attachment_definitions[:image1][:default_url].call(nil)
33
+ end
34
+
35
+ def test_default_asset_url_with_proc_will_make_use_of_it_each_time
36
+ ActionController::Base.stubs(:new).returns(mock('new_action_controller'))
37
+ ActionController::Base.new.stubs(:view_context).returns(mock('view_context'))
38
+ ActionController::Base.new.view_context.stubs(:asset_path).returns('http://www.example.com/unknown.png')
39
+
40
+ model = Class.new(ActiveRecord::Base) do
41
+ has_attached_file :image1, default_asset_url: ->(attachment){ "UNKNOWN.PNG".downcase }
42
+ end
43
+
44
+ expect = {validations: [], default_url: "http://www.example.com/unknown.png"}
45
+ assert_equal [], model.attachment_definitions[:image1][:validations]
46
+ assert_equal Proc, model.attachment_definitions[:image1][:default_url].class
47
+ assert_equal "http://www.example.com/unknown.png", model.attachment_definitions[:image1][:default_url].call(nil)
31
48
  end
32
49
 
33
50
  def test_defaults_will_not_overwrite_explicit_options
34
51
  Rails.application.config.stubs(:paperclip_defaults).returns({storage: :fog})
35
- model = Class.new(ActiveRecord::Base) do
52
+ model = Class.new(ActiveRecord::Base) do
36
53
  has_attached_file :image1, default_url: "/unknown.png", storage: :filesystem
37
54
  end
38
55
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: paperclip-defaults
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.2
4
+ version: 1.0.3
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,11 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2011-10-13 00:00:00.000000000 Z
12
+ date: 2011-12-05 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: minitest
16
- requirement: &17422820 !ruby/object:Gem::Requirement
16
+ requirement: &22097060 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
@@ -21,10 +21,10 @@ dependencies:
21
21
  version: '0'
22
22
  type: :development
23
23
  prerelease: false
24
- version_requirements: *17422820
24
+ version_requirements: *22097060
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: mocha
27
- requirement: &18029360 !ruby/object:Gem::Requirement
27
+ requirement: &21809720 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - ! '>='
@@ -32,10 +32,10 @@ dependencies:
32
32
  version: '0'
33
33
  type: :development
34
34
  prerelease: false
35
- version_requirements: *18029360
35
+ version_requirements: *21809720
36
36
  - !ruby/object:Gem::Dependency
37
37
  name: guard
38
- requirement: &18026960 !ruby/object:Gem::Requirement
38
+ requirement: &21531320 !ruby/object:Gem::Requirement
39
39
  none: false
40
40
  requirements:
41
41
  - - ! '>='
@@ -43,10 +43,10 @@ dependencies:
43
43
  version: '0'
44
44
  type: :development
45
45
  prerelease: false
46
- version_requirements: *18026960
46
+ version_requirements: *21531320
47
47
  - !ruby/object:Gem::Dependency
48
48
  name: guard-bundler
49
- requirement: &18025500 !ruby/object:Gem::Requirement
49
+ requirement: &19200420 !ruby/object:Gem::Requirement
50
50
  none: false
51
51
  requirements:
52
52
  - - ! '>='
@@ -54,10 +54,10 @@ dependencies:
54
54
  version: '0'
55
55
  type: :development
56
56
  prerelease: false
57
- version_requirements: *18025500
57
+ version_requirements: *19200420
58
58
  - !ruby/object:Gem::Dependency
59
59
  name: guard-minitest
60
- requirement: &18024680 !ruby/object:Gem::Requirement
60
+ requirement: &19069000 !ruby/object:Gem::Requirement
61
61
  none: false
62
62
  requirements:
63
63
  - - ! '>='
@@ -65,10 +65,10 @@ dependencies:
65
65
  version: '0'
66
66
  type: :development
67
67
  prerelease: false
68
- version_requirements: *18024680
68
+ version_requirements: *19069000
69
69
  - !ruby/object:Gem::Dependency
70
70
  name: rb-inotify
71
- requirement: &18023380 !ruby/object:Gem::Requirement
71
+ requirement: &19078180 !ruby/object:Gem::Requirement
72
72
  none: false
73
73
  requirements:
74
74
  - - ! '>='
@@ -76,10 +76,10 @@ dependencies:
76
76
  version: '0'
77
77
  type: :development
78
78
  prerelease: false
79
- version_requirements: *18023380
79
+ version_requirements: *19078180
80
80
  - !ruby/object:Gem::Dependency
81
81
  name: libnotify
82
- requirement: &18022480 !ruby/object:Gem::Requirement
82
+ requirement: &19074720 !ruby/object:Gem::Requirement
83
83
  none: false
84
84
  requirements:
85
85
  - - ! '>='
@@ -87,10 +87,10 @@ dependencies:
87
87
  version: '0'
88
88
  type: :development
89
89
  prerelease: false
90
- version_requirements: *18022480
90
+ version_requirements: *19074720
91
91
  - !ruby/object:Gem::Dependency
92
92
  name: activerecord
93
- requirement: &18035620 !ruby/object:Gem::Requirement
93
+ requirement: &19072900 !ruby/object:Gem::Requirement
94
94
  none: false
95
95
  requirements:
96
96
  - - ! '>='
@@ -98,10 +98,10 @@ dependencies:
98
98
  version: '0'
99
99
  type: :development
100
100
  prerelease: false
101
- version_requirements: *18035620
101
+ version_requirements: *19072900
102
102
  - !ruby/object:Gem::Dependency
103
103
  name: sqlite3
104
- requirement: &18033920 !ruby/object:Gem::Requirement
104
+ requirement: &19071180 !ruby/object:Gem::Requirement
105
105
  none: false
106
106
  requirements:
107
107
  - - ! '>='
@@ -109,10 +109,10 @@ dependencies:
109
109
  version: '0'
110
110
  type: :development
111
111
  prerelease: false
112
- version_requirements: *18033920
112
+ version_requirements: *19071180
113
113
  - !ruby/object:Gem::Dependency
114
114
  name: rake
115
- requirement: &18033120 !ruby/object:Gem::Requirement
115
+ requirement: &19084680 !ruby/object:Gem::Requirement
116
116
  none: false
117
117
  requirements:
118
118
  - - ! '>='
@@ -120,10 +120,10 @@ dependencies:
120
120
  version: '0'
121
121
  type: :development
122
122
  prerelease: false
123
- version_requirements: *18033120
123
+ version_requirements: *19084680
124
124
  - !ruby/object:Gem::Dependency
125
125
  name: paperclip
126
- requirement: &18030460 !ruby/object:Gem::Requirement
126
+ requirement: &19081500 !ruby/object:Gem::Requirement
127
127
  none: false
128
128
  requirements:
129
129
  - - ! '>='
@@ -131,10 +131,10 @@ dependencies:
131
131
  version: '0'
132
132
  type: :runtime
133
133
  prerelease: false
134
- version_requirements: *18030460
134
+ version_requirements: *19081500
135
135
  - !ruby/object:Gem::Dependency
136
136
  name: activesupport
137
- requirement: &18048320 !ruby/object:Gem::Requirement
137
+ requirement: &19094400 !ruby/object:Gem::Requirement
138
138
  none: false
139
139
  requirements:
140
140
  - - ! '>='
@@ -142,10 +142,10 @@ dependencies:
142
142
  version: '0'
143
143
  type: :runtime
144
144
  prerelease: false
145
- version_requirements: *18048320
145
+ version_requirements: *19094400
146
146
  - !ruby/object:Gem::Dependency
147
147
  name: rails
148
- requirement: &18047400 !ruby/object:Gem::Requirement
148
+ requirement: &19092260 !ruby/object:Gem::Requirement
149
149
  none: false
150
150
  requirements:
151
151
  - - ! '>='
@@ -153,7 +153,7 @@ dependencies:
153
153
  version: '0'
154
154
  type: :runtime
155
155
  prerelease: false
156
- version_requirements: *18047400
156
+ version_requirements: *19092260
157
157
  description: Configurable application wide defaults for Paperclip
158
158
  email:
159
159
  - mark@socialreferral.com
@@ -195,7 +195,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
195
195
  version: '0'
196
196
  segments:
197
197
  - 0
198
- hash: 1093818109491895370
198
+ hash: 4279530141429349155
199
199
  requirements: []
200
200
  rubyforge_project:
201
201
  rubygems_version: 1.8.10