papermill 0.14.3 → 0.16.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -27,6 +27,9 @@ en:
27
27
  save: "Send to server"
28
28
  location: "File : "
29
29
  url: "URL : "
30
+ modify-all: "Modify all: "
31
+ delete-all: "Delete all"
32
+ delete-all-confirmation: "Are you sure?"
30
33
  fr:
31
34
  papermill:
32
35
  not-processed: "Erreur/ressource non trouvée"
@@ -56,3 +59,6 @@ fr:
56
59
  save: "Envoyer"
57
60
  location: "Fichier : "
58
61
  url: "Lien : "
62
+ modify-all: "Modifier tout : "
63
+ delete-all: "Supprimer tout"
64
+ delete-all-confirmation: "Êtes-vous sûr ?"
data/config/routes.rb CHANGED
@@ -1,4 +1,4 @@
1
1
  ActionController::Routing::Routes.draw do |map|
2
- map.resources :papermill, :collection => { :sort => :post }
3
- map.connect "#{Papermill::PAPERMILL_DEFAULTS[:papermill_prefix]}/#{Papermill::PAPERCLIP_INTERPOLATION_STRING.gsub(":id_partition", ":id0/:id1/:id2")}", :controller => "papermill", :action => "show"
2
+ map.resources :papermill, :collection => { :sort => :post, :mass_edit => :post, :mass_delete => :post }
3
+ map.connect "#{Papermill::options[:papermill_prefix]}/#{Papermill::PAPERCLIP_INTERPOLATION_STRING.gsub(":id_partition", ":id0/:id1/:id2")}", :controller => "papermill", :action => "show"
4
4
  end
@@ -0,0 +1,4 @@
1
+ script/generate papermill_initializer
2
+
3
+ This will copy Papermill option hash to your config/initializers directory
4
+ The old file directory will be deleted if present!
@@ -0,0 +1,15 @@
1
+ class PapermillInitializerGenerator < Rails::Generator::Base
2
+ def initialize(args, options = {})
3
+ end
4
+
5
+ def manifest
6
+ puts "Copying papermill initializer to config/initializers/..."
7
+ FileUtils.rm_rf("#{RAILS_ROOT}/config/initializers/papermill.rb")
8
+ FileUtils.cp_r(
9
+ File.join(File.dirname(__FILE__), '../..', 'lib', 'papermill', 'papermill_options.rb'),
10
+ "#{RAILS_ROOT}/config/initializers/papermill.rb"
11
+ )
12
+ puts "Done! Check config/initializer/papermill.rb for result."
13
+ exit
14
+ end
15
+ end
@@ -1,17 +1,22 @@
1
1
  class <%= migration_name %> < ActiveRecord::Migration
2
2
  def self.up
3
3
  create_table :papermill_assets do |t|
4
+ # mandatory
4
5
  t.string :file_file_name
5
6
  t.string :file_content_type
6
7
  t.integer :file_file_size
7
8
  t.integer :position
8
- t.text :description
9
- t.string :copyright
10
- t.string :title
11
9
  t.integer :assetable_id
12
10
  t.string :assetable_type
13
11
  t.string :assetable_key
14
12
  t.string :type
13
+ t.string :title
14
+
15
+
16
+ # optionnal
17
+ t.text :description
18
+ t.string :copyright
19
+ t.string :alt
15
20
  t.timestamps
16
21
  end
17
22
  end
@@ -2,6 +2,7 @@ gem 'papermill'
2
2
 
3
3
  generate :papermill_table, "PapermillMigration"
4
4
  generate :papermill_assets
5
+ generate :papermill_initializer
5
6
  generate :scaffold, "article title:string"
6
7
  rake "db:migrate"
7
8
 
@@ -62,7 +63,6 @@ file "app/views/articles/_form.html.erb", <<-END
62
63
  <% end %>
63
64
  END
64
65
 
65
-
66
66
  file "app/views/articles/show.html.erb", <<-END
67
67
  <p>
68
68
  <b>Title:</b>
@@ -78,7 +78,7 @@ file "app/views/articles/show.html.erb", <<-END
78
78
  <br /><br />
79
79
  <b>@article.assets(:my_other_image).first :</b>
80
80
  <p>
81
- <% image = @article.asset(:my_other_image) %>
81
+ <% image = @article.assets(:my_other_image).first %>
82
82
  <%= link_to(image_tag(image.url("100x100#")), image.url) if image %>
83
83
  </p>
84
84
  <br /><br />
@@ -91,9 +91,9 @@ file "app/views/articles/show.html.erb", <<-END
91
91
  </ul>
92
92
  </p>
93
93
  <br /><br />
94
- <b>@article.asset(:my_other_asset) :</b>
94
+ <b>@article.assets(:my_other_asset).first :</b>
95
95
  <p>
96
- <% asset = @article.asset(:my_other_asset) %>
96
+ <% asset = @article.assets(:my_other_asset).first %>
97
97
  <%= link_to(asset.name, asset.url) if asset %>
98
98
  </p>
99
99
 
@@ -13,24 +13,15 @@ module PapermillHashExtensions
13
13
  target
14
14
  end
15
15
  end
16
- module PapermillStringExtensions
17
- def simple_sql_sanitizer
18
- gsub(/\\/, '\&\&').gsub(/'/, "''")
19
- end
20
- end
21
- module PapermillStringToUrlNotFound
22
- def to_url
23
- gsub(/[^a-zA-Z0-9]/, "-").gsub(/-+/, "-").gsub(/^-|-$/, "").downcase
24
- end
25
- end
16
+
26
17
  module PapermillObjectExtensions
27
18
  # Nil if empty.
28
19
  def nie
29
20
  self.blank? ? nil : self
30
21
  end
31
22
  end
23
+
32
24
  module PapermillFileExtensions
33
-
34
25
  def get_content_type
35
26
  begin
36
27
  MIME::Types.type_for(self.original_filename).to_s
@@ -41,10 +32,27 @@ module PapermillFileExtensions
41
32
  end
42
33
 
43
34
  module PapermillFormtasticExtensions
44
- def method_missing(input_type, method, options)
45
- return super unless input_type.to_s.match("_input")
46
- input_type = input_type.to_s.split("_input").first
35
+ def image_upload_input(method, options)
36
+ self.label(method, options_for_label(options)) +
37
+ self.send(:image_upload, method, options)
38
+ end
39
+ def images_upload_input(method, options)
47
40
  self.label(method, options_for_label(options)) +
48
- self.send(input_type, method, options)
41
+ self.send(:images_upload, method, options)
42
+ end
43
+ def asset_upload_input(method, options)
44
+ self.label(method, options_for_label(options)) +
45
+ self.send(:asset_upload, method, options)
46
+ end
47
+ def assets_upload_input(method, options)
48
+ self.label(method, options_for_label(options)) +
49
+ self.send(:assets_upload, method, options)
49
50
  end
50
51
  end
52
+
53
+
54
+ module StringToUrlNotFound
55
+ def to_url
56
+ gsub(/[^a-zA-Z0-9]/, "-").gsub(/-+/, "-").gsub(/^-|-$/, "")
57
+ end
58
+ end
data/lib/papermill.rb CHANGED
@@ -1,12 +1,19 @@
1
1
  I18n.load_path = [File.join(File.dirname(__FILE__), "../config/locales/papermill.yml")] + I18n.load_path
2
- require 'core_extensions'
2
+ require 'extensions'
3
3
  Object.send :include, PapermillObjectExtensions
4
4
  Hash.send :include, PapermillHashExtensions
5
5
  File.send :include, PapermillFileExtensions
6
- String.send :include, PapermillStringExtensions
7
- String.send :include, PapermillStringToUrlNotFound unless String.instance_methods.include? "to_url"
6
+ String.send :include, StringToUrlNotFound unless String.instance_methods.include? "to_url"
8
7
  Formtastic::SemanticFormBuilder.send(:include, PapermillFormtasticExtensions) rescue NameError
9
- require 'papermill/papermill_module'
8
+
9
+ begin
10
+ require File.join(File.dirname(RAILS_ROOT), "config/initializers/papermill.rb")
11
+ rescue LoadError
12
+ require 'papermill/papermill_options'
13
+ end
14
+
15
+ require 'paperclip' unless defined?(Paperclip)
16
+ require 'papermill/papermill'
10
17
  require 'papermill/papermill_asset'
11
18
  require 'papermill/form_builder'
12
19
  require 'papermill/papermill_helper'
@@ -1,3 +1,20 @@
1
+ class ActionView::Helpers::FormBuilder
2
+ include ActionView::Helpers::FormTagHelper
3
+
4
+ def assets_upload(key = nil, options = {})
5
+ papermill_upload_tag key, { :thumbnail => false }.update(options)
6
+ end
7
+ def asset_upload(key = nil, options = {})
8
+ papermill_upload_tag key, { :gallery => false, :thumbnail => false }.update(options)
9
+ end
10
+ def images_upload(key = nil, options = {})
11
+ papermill_upload_tag key, options
12
+ end
13
+ def image_upload(key = nil, options = {})
14
+ papermill_upload_tag key, { :gallery => false }.update(options)
15
+ end
16
+ end
17
+
1
18
  module ActionView::Helpers::FormTagHelper
2
19
 
3
20
  def assets_upload_tag(assetable, key = nil, options = {})
@@ -24,21 +41,24 @@ module ActionView::Helpers::FormTagHelper
24
41
  end
25
42
 
26
43
  assetable = options[:assetable] || @template.instance_variable_get("@#{@object_name}")
27
- options = if assetable && (association = (assetable.class.papermill_associations[key] || assetable.class.papermill_associations[:papermill_assets]))
28
- association[:options].deep_merge(options)
29
- elsif assetable.nil?
30
- Papermill::PAPERMILL_DEFAULTS.deep_merge(options)
31
- else
32
- raise PapermillException.new("Can't find '#{key.to_s}' association for '#{assetable.class.to_s}'.\n\n##{assetable.class.to_s.underscore}.rb\nYou can take on of these actions: \n1. set either a catchall papermill association: 'papermill {your_option_hash}'\n2. or a specific association: 'papermill :#{key.to_s}, {your_option_hash}'")
33
- end
44
+ options = (
45
+ if assetable && (association = (assetable.class.papermill_associations[key] || assetable.class.papermill_associations[Papermill::options[:base_association_name]]))
46
+ association[:options].deep_merge(options)
47
+ elsif assetable.nil?
48
+ Papermill::options.deep_merge(options)
49
+ else
50
+ raise PapermillException.new("Can't find '#{key.to_s}' association for '#{assetable.class.to_s}'.\n\n##{assetable.class.to_s.underscore}.rb\nYou can take on of these actions: \n1. set either a catchall papermill association: 'papermill {your_option_hash}'\n2. or a specific association: 'papermill :#{key.to_s}, {your_option_hash}'")
51
+ end
52
+ )
53
+
34
54
  assetable_id = assetable && (assetable.id || assetable.timestamp) || nil
35
- assetable_type = assetable && assetable.class.to_s || nil
55
+ assetable_type = assetable && assetable.class.base_class.name || nil
36
56
  id = "papermill_#{assetable_type}_#{assetable_id}_#{key ? key.to_s : 'nil'}"
37
57
  if options[:thumbnail]
38
58
  w = options[:thumbnail][:width] || options[:thumbnail][:height] && options[:thumbnail][:aspect_ratio] && (options[:thumbnail][:height] * options[:thumbnail][:aspect_ratio]).to_i || nil
39
59
  h = options[:thumbnail][:height] || options[:thumbnail][:width] && options[:thumbnail][:aspect_ratio] && (options[:thumbnail][:width] / options[:thumbnail][:aspect_ratio]).to_i || nil
40
- options[:thumbnail][:style] ||= (w || h) && "#{w || options[:thumbnail][:max_width]}x#{h || options[:thumbnail][:max_height]}>" || "original"
41
- if options[:thumbnail][:inline_css]
60
+ options[:thumbnail][:style] ||= (w || h) && "#{w}x#{h}>" || "original"
61
+ if options[:inline_css]
42
62
  size = []
43
63
  size << "width:#{w}px" if w
44
64
  size << "height:#{h}px" if h
@@ -52,7 +72,7 @@ module ActionView::Helpers::FormTagHelper
52
72
  hm = options[:gallery][:hmargin].to_i
53
73
  b = options[:gallery][:border_thickness].to_i
54
74
  gallery_width = (options[:gallery][:width] || w) && "width:#{options[:gallery][:width] || options[:gallery][:columns]*(w+(hp+hm+b)*2)}px;" || ""
55
- gallery_height = (options[:gallery][:height] || h) && "#{options[:gallery][:autogrow] ? "" : "min-"}height:#{options[:gallery][:height] || options[:gallery][:lines]*(h+(vp+vm+b)*2)}px;" || ""
75
+ gallery_height = (options[:gallery][:height] || h) && "min-height:#{options[:gallery][:height] || options[:gallery][:lines]*(h+(vp+vm+b)*2)}px;" || ""
56
76
  inline_css << %{##{id} { #{gallery_width} #{gallery_height} }}
57
77
  inline_css << %{##{id} li { margin:#{vm}px #{hm}px; border-width:#{b}px; padding:#{vp}px #{hp}px; #{size}; }}
58
78
  else
@@ -63,46 +83,58 @@ module ActionView::Helpers::FormTagHelper
63
83
  end
64
84
  end
65
85
  end
66
- html = []
67
-
68
- asset_class = options[:class_name] && options[:class_name].to_s.constantize || association && association[:class] || PapermillAsset
69
-
86
+
70
87
  url_options = {
71
88
  :controller => "/papermill",
72
89
  :action => "create",
73
- :escape => false,
74
- :asset_class => asset_class.to_s,
75
- :assetable_key => key,
76
- :gallery => (options[:gallery] != false),
77
- :thumbnail_style => (options[:thumbnail] && options[:thumbnail][:style])
90
+ :asset_class => (options[:class_name] && options[:class_name].to_s.constantize || association && association[:class] || PapermillAsset).to_s,
91
+ :gallery => !!options[:gallery],
92
+ :thumbnail_style => options[:thumbnail] && options[:thumbnail][:style]
78
93
  }
94
+
79
95
  url_options.merge!({
80
96
  :assetable_id => assetable_id,
81
- :assetable_type => assetable_type.underscore,
97
+ :assetable_type => assetable_type
82
98
  }) if assetable
99
+
100
+ url_options.merge!({
101
+ :assetable_key => key
102
+ }) if key
103
+
104
+
105
+ html = {}
83
106
  create_url = @template.url_for(url_options)
84
107
  if assetable && assetable.new_record? && !@timestamped
85
- html << @template.hidden_field(assetable.class.to_s.underscore, :timestamp, :value => assetable.timestamp)
108
+ @timestamp_field = @template.hidden_field(assetable_type.underscore, :timestamp, :value => assetable.timestamp)
86
109
  @timestamped = true
87
110
  end
88
111
 
89
112
  conditions = {:assetable_type => assetable_type, :assetable_id => assetable_id}
90
113
  conditions.merge!({:assetable_key => key.to_s}) if key
91
- collection = asset_class.find(:all, :conditions => conditions, :order => "position")
114
+ collection = PapermillAsset.all(:conditions => conditions, :order => "position")
92
115
 
93
- html << %{<div id="#{id}-button-wrapper" class="papermill-button-wrapper" style="height: #{options[:swfupload][:button_height]}px;"><span id="browse_for_#{id}" class="swf_button"></span></div>}
94
- html << @template.content_tag(:ul, :id => id, :class => "#{(options[:thumbnail] ? "papermill-thumb-container" : "papermill-asset-container")} #{(options[:gallery] ? "papermill-multiple-items" : "papermill-unique-item")}") {
116
+ html[:upload_button] = %{<div id="#{id}-button-wrapper" class="papermill-button-wrapper" style="height: #{options[:swfupload][:button_height]}px;"><span id="browse_for_#{id}" class="swf_button"></span></div>}
117
+ html[:container] = @template.content_tag(:ul, :id => id, :class => "#{(options[:thumbnail] ? "papermill-thumb-container" : "papermill-asset-container")} #{(options[:gallery] ? "papermill-multiple-items" : "papermill-unique-item")}") {
95
118
  @template.render :partial => "papermill/asset", :collection => collection, :locals => { :thumbnail_style => (options[:thumbnail] && options[:thumbnail][:style]) }
96
- }
119
+ }
120
+
121
+ if options[:gallery]
122
+ html[:dashboard] = {}
123
+ html[:dashboard][:mass_edit] = %{<select id="batch_#{id}">#{options[:mass_editable_fields].map do |field|
124
+ %{<option value="#{field.to_s}">#{I18n.t("papermill.#{field.to_s}", :default => field.to_s)}</option>}
125
+ end.join("\n")}</select>
126
+ <a onclick="modify_all('#{id}'); return false;" style="cursor:pointer">#{I18n.t("papermill.modify-all")}</a>}
127
+ html[:dashboard][:mass_delete] = %{<a onclick="mass_delete('#{id}', '#{@template.escape_javascript I18n.t("papermill.delete-all-confirmation")}'); return false;" style="cursor:pointer">#{I18n.t("papermill.delete-all")}</a>}
128
+ html[:dashboard] = @template.content_tag(:ul, options[:dashboard].map{|action| @template.content_tag(:li, html[:dashboard][action], :class => action.to_s) }.join("\n"), :class => "dashboard")
129
+ end
130
+
97
131
  @template.content_for :papermill_inline_js do
98
132
  %{
99
133
  #{%{(jQuery("##{id}")).sortable({update:function(){jQuery.ajax({async:true, data:jQuery(this).sortable('serialize'), dataType:'script', type:'post', url:'#{@template.controller.send("sort_papermill_path")}'})}})} if options[:gallery]}
100
134
  new SWFUpload({
101
135
  upload_id: "#{id}",
102
136
  upload_url: "#{@template.escape_javascript create_url}",
103
- file_size_limit: "#{options[:file_size_limit_mb].megabytes}",
104
137
  file_types: "#{options[:images_only] ? '*.jpg;*.jpeg;*.png;*.gif' : ''}",
105
- file_types_description: "#{options[:thumbnail] ? 'Images' : 'Files'}",
106
138
  file_queue_limit: "#{!options[:gallery] ? '1' : '0'}",
107
139
  file_queued_handler: Upload.file_queued,
108
140
  file_dialog_complete_handler: Upload.file_dialog_complete,
@@ -112,29 +144,10 @@ module ActionView::Helpers::FormTagHelper
112
144
  upload_success_handler: Upload.upload_success,
113
145
  upload_complete_handler: Upload.upload_complete,
114
146
  button_placeholder_id : "browse_for_#{id}",
115
- #{options[:swfupload].map{ |key, value| ["false", "true"].include?(value.to_s) ? "#{key.to_s}: #{value.to_s}" : "#{key.to_s}: '#{value.to_s}'" }.compact.join(", ")}
147
+ #{options[:swfupload].map{ |key, value| (["false", "true"].include?(value.to_s) ? "#{key.to_s}: #{value.to_s}" : "#{key.to_s}: '#{value.to_s}'") if value }.compact.join(", ")}
116
148
  });
117
149
  }
118
150
  end
119
- html.reverse! if options[:button_after_container]
120
- %{<div class="papermill">#{html.join("\n")}</div>}
121
- end
122
- end
123
-
124
-
125
- class ActionView::Helpers::FormBuilder
126
- include ActionView::Helpers::FormTagHelper
127
-
128
- def assets_upload(key = nil, options = {})
129
- papermill_upload_tag key, { :thumbnail => false }.update(options)
130
- end
131
- def asset_upload(key = nil, options = {})
132
- papermill_upload_tag key, { :gallery => false, :thumbnail => false }.update(options)
133
- end
134
- def images_upload(key = nil, options = {})
135
- papermill_upload_tag key, options
136
- end
137
- def image_upload(key = nil, options = {})
138
- papermill_upload_tag key, { :gallery => false }.update(options)
151
+ %{<div class="papermill">#{@timestamp_field.to_s + options[:form_helper_elements].map{|element| html[element] || ""}.join("\n")}</div>}
139
152
  end
140
153
  end
@@ -0,0 +1,70 @@
1
+ module Papermill
2
+ PAPERCLIP_INTERPOLATION_STRING = ":id_partition/:style/:escaped_basename.:extension"
3
+
4
+ def self.included(base)
5
+ base.extend(ClassMethods)
6
+ end
7
+
8
+ def self.options
9
+ defined?(OPTIONS) ? OPTIONS : {}
10
+ end
11
+
12
+
13
+ module ClassMethods
14
+ attr_reader :papermill_associations
15
+
16
+ def papermill(*args)
17
+ assoc_name = (!args.first.is_a?(Hash) && args.shift.try(:to_sym) || Papermill::options[:base_association_name])
18
+ options = args.first || {}
19
+
20
+ (@papermill_associations ||= {}).merge!({ assoc_name => {
21
+ :class => (class_name = options.delete(:class_name)) && class_name.to_s.constantize || PapermillAsset,
22
+ :options => Papermill::options.deep_merge(options)
23
+ }})
24
+
25
+ include Papermill::InstanceMethods
26
+ before_destroy :destroy_assets
27
+ after_create :rebase_assets
28
+ has_many :papermill_assets, :as => "Assetable"
29
+
30
+ define_method assoc_name do |*options|
31
+ scope = PapermillAsset.scoped(:conditions => {:assetable_id => self.id, :assetable_type => self.class.base_class.name})
32
+ if assoc_name != Papermill::options[:base_association_name]
33
+ scope = scope.scoped(:conditions => { :assetable_key => assoc_name.to_s })
34
+ elsif options.first && !options.first.is_a?(Hash)
35
+ scope = scope.scoped(:conditions => { :assetable_key => options.shift.to_s.nie })
36
+ end
37
+ scope = scope.scoped(options.shift) if options.first
38
+ scope
39
+ end
40
+ end
41
+
42
+ def inherited(subclass)
43
+ subclass.instance_variable_set("@papermill_associations", @papermill_associations)
44
+ super
45
+ end
46
+ end
47
+
48
+ module InstanceMethods
49
+ attr_writer :timestamp
50
+ def timestamp
51
+ @timestamp ||= "-#{(Time.now.to_f * 1000).to_i.to_s[4..-1]}"
52
+ end
53
+
54
+ private
55
+
56
+ def destroy_assets
57
+ papermill_assets.each &:destroy
58
+ end
59
+
60
+ def rebase_assets
61
+ PapermillAsset.all(:conditions => { :assetable_id => self.timestamp, :assetable_type => self.class.base_class.name }).each do |asset|
62
+ if asset.created_at < 2.hours.ago
63
+ asset.destroy
64
+ else
65
+ asset.update_attribute(:assetable_id, self.id)
66
+ end
67
+ end
68
+ end
69
+ end
70
+ end
@@ -1,37 +1,53 @@
1
- require 'paperclip'
2
-
3
1
  class PapermillAsset < ActiveRecord::Base
4
- belongs_to :assetable, :polymorphic => true
2
+
5
3
  before_destroy :destroy_files
6
-
4
+ before_create :set_position
5
+
6
+ has_attached_file :file,
7
+ :path => "#{Papermill::options[:public_root]}/#{Papermill::options[:papermill_prefix]}/#{Papermill::PAPERCLIP_INTERPOLATION_STRING}",
8
+ :url => "/#{Papermill::options[:papermill_prefix]}/#{Papermill::PAPERCLIP_INTERPOLATION_STRING}"
9
+
7
10
  Paperclip.interpolates :escaped_basename do |attachment, style|
8
11
  Paperclip::Interpolations[:basename].call(attachment, style).to_url
9
12
  end
10
13
 
11
- has_attached_file :file,
12
- :path => "#{Papermill::PAPERMILL_DEFAULTS[:public_root]}/#{Papermill::PAPERMILL_DEFAULTS[:papermill_prefix]}/#{Papermill::PAPERCLIP_INTERPOLATION_STRING}",
13
- :url => "/#{Papermill::PAPERMILL_DEFAULTS[:papermill_prefix]}/#{Papermill::PAPERCLIP_INTERPOLATION_STRING}"
14
14
  validates_attachment_presence :file
15
-
16
- def swfupload_file=(data)
17
- data.content_type = data.get_content_type
15
+
16
+ belongs_to :assetable, :polymorphic => true
17
+ default_scope :order => 'position'
18
+
19
+ def Filedata=(data)
18
20
  self.file = data
21
+ self.file_content_type = data.get_content_type
22
+ end
23
+
24
+ def Filename=(name)
25
+ self.title = name
26
+ end
27
+
28
+ def create_thumb_file(style_name)
29
+ FileUtils.mkdir_p File.dirname(file.path(style_name))
30
+ FileUtils.mv(Paperclip::Thumbnail.make(file, self.class.compute_style(style_name)).path, file.path(style_name))
19
31
  end
20
32
 
21
33
  def id_partition
22
34
  ("%09d" % self.id).scan(/\d{3}/).join("/")
23
35
  end
24
36
 
37
+ def self.find_by_id_partition(params)
38
+ self.find((params[:id0] + params[:id1] + params[:id2]).to_i)
39
+ end
40
+
25
41
  def name
26
42
  file_file_name
27
43
  end
28
44
 
29
45
  def width
30
- image? && Paperclip::Geometry.from_file(file).width
46
+ Paperclip::Geometry.from_file(file).width
31
47
  end
32
48
 
33
49
  def height
34
- image? && Paperclip::Geometry.from_file(file).height
50
+ Paperclip::Geometry.from_file(file).height
35
51
  end
36
52
 
37
53
  def size
@@ -51,11 +67,37 @@ class PapermillAsset < ActiveRecord::Base
51
67
  end
52
68
 
53
69
  def image?
54
- content_type && content_type.split("/")[0] == "image" && (content_type.split("/")[1] || "unknown")
70
+ content_type.split("/")[0] == "image"
71
+ end
72
+
73
+ def save(*params)
74
+ if super(*params)
75
+ if params.last.is_a?(Hash) && params.last[:unique] && assetable_key
76
+ PapermillAsset.find(:all, :conditions => {:assetable_id => assetable_id, :assetable_type => assetable_type, :assetable_key => assetable_key }).each do |asset|
77
+ asset.destroy unless asset == self
78
+ end
79
+ end
80
+ true
81
+ else
82
+ false
83
+ end
84
+ end
85
+
86
+ def self.cleanup
87
+ PapermillAsset.all(:conditions => ["id < 0 AND created_at < ?", DateTime.now.yesterday]).each &:destroy
88
+ end
89
+
90
+ private
91
+ def set_position
92
+ self.position ||= PapermillAsset.first(:conditions => {:assetable_key => assetable_key, :assetable_type => assetable_type, :assetable_id => assetable_id}, :order => "position DESC" ).try(:position).to_i + 1
55
93
  end
56
94
 
57
95
  def destroy_files
58
- FileUtils.rm_r "#{Papermill::papermill_interpolated_path({":id_partition" => self.id_partition}, ':id_partition')}/"
59
- true
96
+ FileUtils.rm_r(File.dirname(path).chomp("original")) rescue true
60
97
  end
98
+
99
+ def self.compute_style(style, compatibility_mode = nil)
100
+ style = Papermill::options[:aliases][style.to_sym] || Papermill::options[:aliases][style.to_s] || !Papermill::options[:alias_only] && style
101
+ [Symbol, String].include?(style.class) ? {:geometry => style.to_s} : style
102
+ end
61
103
  end