papermill 0.14.3 → 0.16.0

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.
@@ -1,219 +0,0 @@
1
- module Papermill
2
-
3
- # Override these defaults :
4
- # - in your application (environment.rb, ..) => in your [environment, development, production].rb file with Papermill::OPTIONS = {thumbnails => {..}, :gallery => {..}, etc. }
5
- # - in your class => papermill <assoc_name>, :class_name => MySTIedPapermillAssetSubClass, thumbnails => {<my_thumbnail_parameters>}, :gallery => {<my_gallery_parameters>}, etc.
6
- # - in your helper call => images_upload :my_gallery, {thumbnails => {..}, :gallery => {..}, etc. }
7
- # Options will cascade as you expect them to.
8
-
9
- # sizes (widths, paddings, etc..) are CSS pixel values.
10
- PAPERMILL_DEFAULTS = {
11
- :thumbnail => {
12
- # you clearly want to override these two values in your templates.
13
- # the rest is very optionnal and will "cascade" nicely
14
- :width => 100, # Recommended if :gallery[:width] is nil
15
- :height => 100, # Recommended if :gallery[:height] is nil
16
- # set :width OR :height to nil to use aspect_ratio value. Remember that 4/3 == 1 => Use : 4.0/3
17
- :aspect_ratio => nil,
18
- # You can override computed ImageMagick transformation strings that defaults to "#{:width}x#{:height}>"
19
- # Note that this is required if PAPERMILL_DEFAULTS[:alias_only] is true
20
- :style => nil,
21
- # set to false if you don't want inline CSS
22
- :inline_css => true
23
- },
24
- # gallery
25
- :gallery => {
26
- # if thumbnail.inline_css is true, css will be generated automagically with these values. Great for quick scaffolding, and complete enough for real use.
27
- :width => nil, # overrides calculated width. Recommended if :thumbnail[:width] is nil.
28
- :height => nil, # overrides calculated height. Recommended if :thumbnail[:height] is nil.
29
- :columns => 8, # number of columns. If thumbnail.width has a value, sets a width for the gallery, calculated from thumbnails width multiplied by :columns.
30
- :lines => 2, # number of default lines. (height will autogrow) If thumbnail.height has a value, sets a min-height for the gallery, calculated from thumbnails height multiplied by :lines
31
- :vpadding => 0, # vertical padding around thumbnails
32
- :hpadding => 0, # horizontal padding around thumbnails
33
- :vmargin => 1, # vertical margin around thumbnails
34
- :hmargin => 1, # horizontal margin around thumbnails
35
- :border_thickness => 2, # border around thumbnails
36
- :autogrow => false # sets a min-height instead of height for the gallery
37
- },
38
- # options passed on to SWFUpload. To remove an option when overriding, set it to nil.
39
- :swfupload => {
40
- # !!! Will only work if the swf file comes from the server to where the files are sent. (Flash same origin security policy)
41
- :flash_url => '/papermill/swfupload.swf',
42
- # You can use upload-blank.png with your own wording or upload.png with default "upload" wording (looks nicer)
43
- :button_image_url => '/papermill/images/upload-blank.png',
44
- :button_width => 61,
45
- :button_height => 22,
46
- # Wording and CSS processed through an Adobe Flash styler. Result is terrible. Feel free to put a CSS button overlayed directly on the SWF button. See swfupload website.
47
- :button_text => %{<span class="button-text">#{I18n.t("papermill.upload-button-wording")}</span>},
48
- :button_text_style => %{.button-text { color: red; font-size: 12pt; font-weight: bold; }},
49
- :button_disabled => "false",
50
- :button_text_top_padding => 4,
51
- :button_text_left_padding => 4,
52
- :debug => "false",
53
- :prevent_swf_caching => "false"
54
- # See swfupload.js for details.
55
- },
56
- :images_only => false, # set to true to forbid upload of anything else than images
57
- :file_size_limit_mb => 10, # file max size
58
- :button_after_container => false, # set to true to move the upload button below the container
59
-
60
- # DO NOT CHANGE THESE IN YOUR CLASSES. Only application wide (routes or associations may depend on it)
61
-
62
- :base_association_name => 'assets',
63
- :alias_only => false, # set to true so that only aliases are authorized in url/path
64
- # aliases name must be strings!
65
- :aliases => {
66
- # 'example' => "100x100#",
67
- },
68
- # path to the root of your public directory
69
- :public_root => ":rails_root/public",
70
- # added to :public_root as the root folder for all papermill items
71
- :papermill_prefix => "system/papermill",
72
- :max_width => 1000,
73
- :max_height => 1000
74
- }.deep_merge( Papermill.const_defined?("OPTIONS") ? Papermill::OPTIONS : {} )
75
-
76
- PAPERCLIP_INTERPOLATION_STRING = ":id_partition/:style/:escaped_basename.:extension"
77
-
78
- def self.included(base)
79
- base.extend(ClassMethods)
80
- end
81
-
82
- def self.papermill_interpolated_path(replacements_pairs, up_to)
83
- replacements_pairs = {"other" => "*", ":rails_root" => RAILS_ROOT}.merge(replacements_pairs)
84
- a = "#{PAPERMILL_DEFAULTS[:public_root]}/#{PAPERMILL_DEFAULTS[:papermill_prefix]}/#{PAPERCLIP_INTERPOLATION_STRING}".split("/")
85
- "#{a[0..(up_to && a.index(up_to) || -1)].map{ |s| s.starts_with?(':') ? (replacements_pairs[s] || replacements_pairs['other'] || s) : s }.join('/')}"
86
- end
87
-
88
- module ClassMethods
89
- attr_reader :papermill_associations
90
-
91
- # papermill comes in 2 flavors:
92
- #
93
- # 1. generic declaration =>
94
- # declare associations with => papermill {my_option_hash}
95
- # create assets with => assets_upload(:my_key, {optional_option_hash})
96
- # access assets with => assetable.assets(:my_key)
97
- #
98
- # 2. association declaration =>
99
- # declare associations with => papermill :my_association, {my_option_hash}
100
- # create assets with => assets_upload(my_association, {optional_option_hash})
101
- # access assets with => assetable.my_association
102
- #
103
- # In both case, you can specify a PapermillAsset subclass to use with :class_name => MyPapermillAssetSubclass in the option hash
104
- def papermill(assoc_name = :papermill_assets, options = {})
105
- if assoc_name.is_a? Hash
106
- options = assoc_name
107
- assoc_name = :papermill_assets
108
- end
109
- base_association_name = PAPERMILL_DEFAULTS[:base_association_name]
110
- if [base_association_name.to_s.singularize, base_association_name.to_s].include?(assoc_name.to_s)
111
- raise PapermillException.new(
112
- "':#{assoc_name.to_s}' and ':#{assoc_name.to_s.singularize}' are the default papermill associations name.\n" +
113
- "You can take one of these actions: \n" +
114
- "1. use another association name instead of ':#{assoc_name.to_s}'. Eg: 'papermill :my_lovely_assets {my_options}'\n" +
115
- "2. change :base_association_name to something else than '#{base_association_name.to_s}' in the application-wide papermill option hash (change 'Papermill::OPTIONS[:base_association_name]' in your environment.rb)\n" +
116
- "3. use a catch-all 'papermill {your_options}' declaration instead of 'papermill :#{assoc_name.to_s} {your_options}'\n\n" +
117
- "If you want to take advantage of pluralized/singularized associations, always specify a singularizable name. Eg: 'my_assets' is ok, 'my_assix' is not ;). \n" +
118
- "(Anyway you can add exceptions in your config/initializer/inflections.rb)\n\n\n")
119
- end
120
- @papermill_associations ||= {}
121
- begin
122
- class_name = options.delete(:class_name)
123
- asset_class = class_name && class_name.to_s.constantize || PapermillAsset
124
- rescue NameError
125
- raise PapermillException.new("'#{class_name.to_s}' class doesn't exist.\n'#{class_name.to_s}' should be a subclass of PapermillAsset")
126
- end
127
-
128
- @papermill_associations.merge!({assoc_name.to_sym => {:class => asset_class, :options => Papermill::PAPERMILL_DEFAULTS.deep_merge(options)}})
129
-
130
- before_destroy :destroy_assets
131
- after_create :rebase_assets
132
-
133
- # Defines for catch-all association :
134
- # Assetable#assets(*options)
135
- # Assetable#asset(*options)
136
- unless self.respond_to?(base_association_name)
137
- [base_association_name.to_s.singularize, base_association_name].each_with_index do |association, index|
138
- define_method association do |*options|
139
- # case Assetable#asset<s>(:key)
140
- if (options.is_a?(Symbol) || options.is_a?(String))
141
- key = options
142
- options = {}
143
- # case Assetable#asset<s>(:key, options = {})
144
- elsif (options.first.is_a?(Symbol) || options.first.is_a?(String))
145
- key = options.first
146
- options = options[1..-1]
147
- end
148
- options = options.first || {}
149
- conditions = {
150
- :assetable_type => self.class.sti_name,
151
- :assetable_id => self.id
152
- }.merge(options.delete(:conditions) || {})
153
- conditions.merge!({:assetable_key => key.to_s}) if key
154
- hash = {
155
- :conditions => conditions,
156
- :order => options.delete(:order) || "position ASC"
157
- }.merge(options)
158
- PapermillAsset.find((index == 0 ? :first : :all), hash)
159
- end
160
- end
161
- end
162
-
163
- # Defines for declared association :
164
- # Assetable#assoc.singularize(*options)
165
- # Assetable#assoc(*options)
166
- unless assoc_name.to_sym == :papermill_assets
167
- [assoc_name.to_s.singularize, assoc_name.to_s].each_with_index do |association, index|
168
- define_method assoc_name do |*options|
169
- options = options.first || {}
170
- conditions = {
171
- :assetable_type => self.class.sti_name,
172
- :assetable_id => self.id,
173
- :assetable_key => assoc_name.to_s
174
- }.merge(options.delete(:conditions) || {})
175
- hash = {
176
- :conditions => conditions,
177
- :order => options.delete(:order) || "position ASC"
178
- }.merge(options)
179
- asset_class.find((index == 0 ? :first : :all), hash)
180
- end
181
- end
182
- end
183
-
184
- class_eval <<-EOV
185
- include Papermill::InstanceMethods
186
- EOV
187
- end
188
-
189
- def inherited(subclass)
190
- subclass.instance_variable_set("@papermill_associations", @papermill_associations)
191
- super
192
- end
193
- end
194
-
195
- module InstanceMethods
196
- attr_writer :timestamp
197
- def timestamp
198
- @timestamp ||= "-#{(Time.now.to_f * 1000).to_i.to_s[4..-1]}"
199
- end
200
-
201
- private
202
-
203
- def destroy_assets
204
- PapermillAsset.find(:all, :conditions => {:assetable_id => self.id, :assetable_type => self.class.sti_name}).each do |asset|
205
- asset.destroy
206
- end
207
- end
208
-
209
- def rebase_assets
210
- PapermillAsset.find(:all, :conditions => {:assetable_id => self.timestamp, :assetable_type => self.class.sti_name}).each do |asset|
211
- if asset.created_at < 2.hours.ago
212
- asset.destroy
213
- else
214
- asset.update_attribute(:assetable_id, self.id)
215
- end
216
- end
217
- end
218
- end
219
- end