papermill 1.1.2 → 1.1.3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (42) hide show
  1. data/README.rdoc +20 -34
  2. data/VERSION +1 -1
  3. data/app/controllers/papermill_controller.rb +23 -28
  4. data/app/views/papermill/_asset.html.erb +2 -2
  5. data/app/views/papermill/crop.html.erb +34 -0
  6. data/app/views/papermill/edit.html.erb +8 -1
  7. data/config/locales/papermill.yml +15 -15
  8. data/config/routes.rb +1 -1
  9. data/generators/papermill_assets/papermill_assets_generator.rb +1 -2
  10. data/generators/papermill_initializer/papermill_initializer_generator.rb +1 -1
  11. data/lib/papermill.rb +2 -6
  12. data/lib/papermill/form_builder.rb +96 -102
  13. data/lib/papermill/papermill.rb +1 -1
  14. data/lib/papermill/papermill_asset.rb +2 -0
  15. data/lib/papermill/papermill_helper.rb +9 -13
  16. data/lib/papermill/papermill_initializer.rb +132 -0
  17. data/lib/papermill/papermill_options.rb +49 -131
  18. data/lib/papermill/papermill_paperclip_processor.rb +1 -1
  19. data/public/facebox/README.txt +4 -0
  20. data/public/facebox/b.png +0 -0
  21. data/public/facebox/bl.png +0 -0
  22. data/public/facebox/br.png +0 -0
  23. data/public/facebox/closelabel.gif +0 -0
  24. data/public/facebox/facebox.css +95 -0
  25. data/public/facebox/facebox.js +319 -0
  26. data/public/facebox/fbx-border-sprite.png +0 -0
  27. data/public/facebox/loading.gif +0 -0
  28. data/public/facebox/logo.png +0 -0
  29. data/public/facebox/shadow.gif +0 -0
  30. data/public/facebox/tl.png +0 -0
  31. data/public/facebox/tr.png +0 -0
  32. data/public/jgrowl/jquery.jgrowl.css +127 -0
  33. data/public/jgrowl/jquery.jgrowl_minimized.js +4 -0
  34. data/public/papermill/images/mass-delete.png +0 -0
  35. data/public/papermill/jquery-1.3.2.min.js +19 -0
  36. data/public/papermill/jquery-ui-1.7.2.custom.min.js +34 -0
  37. data/public/papermill/jquery.Jcrop.css +35 -0
  38. data/public/papermill/jquery.Jcrop.min.js +163 -0
  39. data/public/papermill/papermill.css +15 -4
  40. data/public/papermill/papermill.js +47 -69
  41. data/test/papermill_test.rb +4 -8
  42. metadata +23 -2
data/README.rdoc CHANGED
@@ -14,62 +14,50 @@
14
14
 
15
15
  rails -m http://github.com/bbenezech/papermill/raw/master/demo.txt papermill-example
16
16
 
17
- === Out-of-the-box OPTIONAL compatibility with :
17
+ === Out-of-the-box compatibility with :
18
18
 
19
19
  * Formtastic # use :as => :[image|asset](s)_upload
20
- * JGrowl # instead of alert for notifications
21
- * FaceBox/Shadowbox # instead of a pop_up for edit form
20
+ * JGrowl # for notifications (included)
21
+ * FaceBox/Shadowbox # for popups (included)
22
22
  * Stringex # (or any String#to_url) for asset filename/url generation
23
23
 
24
24
  === Navigator minimal requirements:
25
25
 
26
26
  * IE6+
27
27
  * Flash 9+
28
- * Javascript ON.
28
+ * Javascript ON
29
29
 
30
30
  Check your audience.
31
31
 
32
32
  === Server requirements:
33
33
 
34
34
  * Makes internal use of JQuery (but loaded in compatibility mode by default, compatible with Prototype/whatever... JRails not needed.)
35
- * Rails 2.3.4 (rail's I18n, engine. older 2.3.x have issues with CRSF or try to check for CRSF stamp against AJAX request, useless with AJAX same origin policy, fixed with 2.3.4)
36
- * Paperclip 2.3 branch (installed by default as a gem dependency, will be loaded internally if needed => You can require your own version of Paperclip)
35
+ * Rails 2.3.4 min (rail's I18n, engine. older 2.3.x have issues with CRSF or try to check for CRSF stamp against AJAX request, useless with AJAX same origin policy, fixed with 2.3.4)
36
+ * Paperclip 2.3 branch (installed by default as a gem dependency, will be loaded internally if needed => You can require your own version of Paperclip if you want to)
37
37
  * Front web server serving static assets if present, and forwarding demand to rails if not. (Usually a no-brainer on any classic installation. Works with Webrick)
38
-
39
-
40
- === Windows environnement :
41
-
42
- # Mime/Types library. Ex:
43
- config.gem "mime-types", :lib => "mime/types"
44
38
 
45
39
  == Features
46
40
 
47
- Loads of them
48
-
49
41
  === Ajax uploading form helpers through SWFUpload => [image|asset](s)_upload
50
42
 
43
+ See the demo to get an idea:
44
+
51
45
  * image_upload => one image, with thumbnail preview
52
46
  * images_upload => sortable image thumbnail gallery
53
47
  * asset_upload => one asset
54
48
  * assets_upload => sortable asset list
55
49
 
56
- === Choose thumbnail size for images previews :
57
-
58
- * {:thumbnail => {:width => 100, :height => 100}}
59
- * {:thumbnail => {:style => "100x100>"}}
60
- * {:thumbnail => {:width => 100, :aspect_ratio => 4.0/3.0 }}
61
-
62
50
  === Asset edit form:
63
51
 
64
- * double-click on any asset in any helper to access&edit his properties
65
- * with pop-up/shadowbox/facebox, out of the box (or use your own pop-up system, dead-easy)
52
+ * double-click on any asset in any helper to access & edit his properties
66
53
 
67
54
  === Lazy created thumbnails
68
55
 
69
56
  * thumbnails are generated the first time they are asked-for, and only in the requested size.
70
57
  * no need to register thumbnail size anywhere: my_asset.url("100x100>")
58
+ * .. but you can use application-wide aliases, see below :
71
59
 
72
- === Alias handling, declaration application-wide (in config/initializers/papermill.rb, after you do a ./script/generate papermill_initializer)
60
+ === Alias handling, declaration application-wide (in config/initializers/papermill.rb, do a ./script/generate papermill_initializer)
73
61
 
74
62
  * :big_alias => {:geometry => "1000x>"}
75
63
  * :other_alias => "100x>"
@@ -93,7 +81,7 @@ entry.rb
93
81
  papermill :diaporama, :class_name => ColorAsset, other_options..
94
82
  end
95
83
 
96
- color_asset.rb # You should add columns to papermill_assets and use STI on PapermillAsset to extend defaults capabilities (or re-open PapermillAsset and monkey-patch it..)
84
+ color_asset.rb # You should add columns to papermill_assets and use STI on PapermillAsset to extend defaults capabilities (or re-open PapermillAsset and monkey-patch it, or use a polymorphic association)
97
85
 
98
86
  class ColorAsset < PapermillAsset
99
87
  named_scope :red, :conditions => {:color => 'red'}
@@ -107,7 +95,7 @@ FormHelpers
107
95
  f.image_upload :cover_image, options_hash
108
96
  f.images_upload :illustrations, options_hash
109
97
  f.asset_upload :pdf, options_hash
110
- f.image_upload :other_ressources, options_hash
98
+ f.image_upload :other_resources, options_hash
111
99
  end
112
100
 
113
101
  Or with formtastic :
@@ -116,7 +104,7 @@ Or with formtastic :
116
104
  f.input @article, :cover_image, options_hash, :as => :image_upload
117
105
  f.input @article, :illustrations, options_hash, :as => :images_upload
118
106
  f.input @article, :pdf, options_hash, :as => :asset_upload
119
- f.input @article, :other_ressources, options_hash, :as => :image_upload
107
+ f.input @article, :other_resources, options_hash, :as => :image_upload
120
108
  end
121
109
 
122
110
  FormTagHelpers
@@ -124,14 +112,14 @@ FormTagHelpers
124
112
  image_upload_tag @article, :cover_image, options_hash
125
113
  images_upload_tag @article, :illustrations, options_hash
126
114
  asset_upload_tag @article, :pdf, options_hash
127
- image_upload_tag @article, :other_ressources, options_hash
115
+ image_upload_tag @article, :other_resources, options_hash
128
116
 
129
117
  For resources not linked to any assetable model :
130
- image_upload_tag "#{current_organization.name}_logo"
118
+ image_upload_tag "#{current_organization.name}_logo", options_hash
131
119
 
132
120
  === Resource access
133
121
 
134
- With generic papermill association, Papermill generates an #assets(:key, *args) named_scope
122
+ Papermill generates an #assets(:key, *args) named_scope
135
123
  @article.assets(:illustrations)
136
124
  @article.assets(:illustrations, :order => "created_at DESC")
137
125
  @article.assets(:illustrations).red.first
@@ -154,11 +142,11 @@ Or for non-assetable resources :
154
142
 
155
143
  @asset = @entry.mug_shot.first
156
144
  image_tag @asset.url # original
157
- image_tag @asset.url("100x>")
145
+ image_tag @asset.url("100x>") # assuming asset is an image
158
146
  image_tag @asset.url(:big) # assuming you have a :big alias
159
147
  @asset.name
160
148
  @asset.content_type
161
- @asset.path # original
149
+ @asset.path
162
150
  @asset.path("100x>")
163
151
  # etc.
164
152
 
@@ -175,11 +163,9 @@ Edit its fields and migrate :
175
163
  Copy static assets to your public directory:
176
164
  ./script/generate papermill_assets
177
165
 
178
- Create the option hash in config/initializers/papermill.rb
166
+ Create the option file config/initializers/papermill.rb
179
167
  ./script/generate papermill_initializer
180
168
 
181
- Then see config/initializers/papermill.rb for the option hash.
182
-
183
169
  === environment.rb:
184
170
 
185
171
  ...
data/VERSION CHANGED
@@ -1 +1 @@
1
- 1.1.2
1
+ 1.1.3
@@ -1,51 +1,46 @@
1
1
  class PapermillController < ApplicationController
2
- prepend_before_filter :load_asset, :only => [ "show", "destroy", "update", "edit" ]
2
+ prepend_before_filter :load_asset, :only => [ "show", "destroy", "update", "edit", "crop" ]
3
3
  prepend_before_filter :load_assets, :only => [ "sort", "mass_delete", "mass_edit" ]
4
4
 
5
5
  def show
6
6
  if @asset.create_thumb_file(params[:style])
7
7
  redirect_to @asset.url(params[:style])
8
8
  else
9
- render :nothing => true, :status => 404
9
+ render :nothing => true, :status => 500
10
10
  end
11
11
  end
12
12
 
13
- def destroy
14
- render :update do |page|
15
- if @asset.destroy
16
- page << %{ jQuery("#papermill_asset_#{params[:id]}").remove(); }
17
- else
18
- page << %{ jQuery("#papermill_asset_#{params[:id]}").show(); }
19
- page << %{ notify("#{ escape_javascript t('papermill.not-deleted', :ressource => @asset.name) }", "error"); }
20
- end
13
+ def create
14
+ @asset = params[:asset_class].constantize.new(params.reject{|k, v| !(PapermillAsset.columns.map(&:name)+["Filedata", "Filename"]).include?(k)})
15
+ if @asset.save
16
+ PapermillAsset.find(:all, :conditions => { :assetable_id => @asset.assetable_id, :assetable_type => @asset.assetable_type, :assetable_key => @asset.assetable_key }).each do |asset|
17
+ asset.destroy unless asset == @asset
18
+ end if !params[:gallery]
19
+ render :partial => "papermill/asset", :object => @asset, :locals => { :gallery => params[:gallery], :thumbnail_style => params[:thumbnail_style] }
21
20
  end
22
21
  end
23
22
 
23
+ def edit
24
+ render :action => "edit"
25
+ end
26
+
27
+ def crop
28
+ end
29
+
24
30
  def update
25
31
  render :update do |page|
26
32
  if @asset.update_attributes(params[:papermill_asset])
27
- page << %{ notify("#{ escape_javascript t("papermill.updated", :ressource => @asset.name)}", "notice"); close_popup(self); }
33
+ page << %{ notify("#{@asset.name}", "#{ escape_javascript t("papermill.updated", :resource => @asset.name)}", "notice"); close_popup(self); }
28
34
  else
29
35
  page << %{ jQuery("#error").html("#{ escape_javascript @asset.errors.full_messages.join('<br />') }"); jQuery("#error").show(); }
30
36
  end
31
37
  end
32
38
  end
33
-
34
- def edit
35
- render :action => "edit"
36
- end
37
-
38
- def create
39
- @asset = params[:asset_class].constantize.new(params.reject{|k, v| !(PapermillAsset.columns.map(&:name)+["Filedata", "Filename"]).include?(k)})
40
- if @asset.save
41
- PapermillAsset.find(:all, :conditions => { :assetable_id => @asset.assetable_id, :assetable_type => @asset.assetable_type, :assetable_key => @asset.assetable_key }).each do |asset|
42
- asset.destroy unless asset == @asset
43
- end if !params[:gallery]
44
- render :partial => "papermill/asset", :object => @asset, :locals => { :gallery => params[:gallery], :thumbnail_style => params[:thumbnail_style] }
45
- else
46
- render :update do |page|
47
- page << %{ notify("#{ escape_javascript(@asset.errors.to_sentence) }", "warning"); }
48
- end
39
+
40
+ def destroy
41
+ @asset.destroy
42
+ render :update do |page|
43
+ page << %{ jQuery("#papermill_asset_#{params[:id]}").remove(); }
49
44
  end
50
45
  end
51
46
 
@@ -67,7 +62,7 @@ class PapermillController < ApplicationController
67
62
  def mass_edit
68
63
  @assets.each { |asset| asset.update_attribute(params[:attribute], params[:value]) }
69
64
  render :update do |page|
70
- page << %{ notify("#{ escape_javascript t("papermill.updated", :ressource => @assets.map(&:name).to_sentence) }", "notice"); } unless @assets.blank?
65
+ page << %{ notify("", "#{ escape_javascript t("papermill.updated", :resource => @assets.map(&:name).to_sentence) }", "notice"); } unless @assets.blank?
71
66
  end
72
67
  end
73
68
 
@@ -1,7 +1,7 @@
1
1
  <%- dom_id = "papermill_asset_#{asset.id}" -%>
2
- <%- delete_link = %{<a onclick="if(confirm('#{escape_javascript t("papermill.delete-confirmation", :resource => asset.name)}')){ jQuery.ajax({async:true, beforeSend:function(request){jQuery('##{dom_id}').hide();}, dataType:'script', error:function(request){jQuery('##{dom_id}').show();}, type:'delete', url:'#{papermill_url(asset)}'})}; return false;" href="#" class="delete"><img title="#{escape_javascript t("papermill.delete", :ressource => asset.name)}" src="/papermill/images/delete.png" alt="delete"/></a>} %>
2
+ <%- delete_link = %{<a onclick="if(confirm('#{escape_javascript t("papermill.delete-confirmation", :resource => asset.name)}')){ jQuery.ajax({async:true, beforeSend:function(request){jQuery('##{dom_id}').hide();}, dataType:'script', error:function(request){jQuery('##{dom_id}').show();}, type:'delete', url:'#{papermill_url(asset)}'})}; return false;" href="#" class="delete"><img title="#{escape_javascript t("papermill.delete", :resource => asset.name)}" src="/papermill/images/delete.png" alt="delete"/></a>} %>
3
3
 
4
- <div id="<%= dom_id %>" class="asset" title="<%= t("papermill.#{(thumbnail_style) ? "thumbnail-" : ""}edit-title", :ressource => asset.name) %>" onDblClick="popup(jQuery(this).attr('rel')); return false;" rel="<%= edit_papermill_url(asset) %>">
4
+ <div id="<%= dom_id %>" class="asset" title="<%= t("papermill.#{(thumbnail_style) ? "thumbnail-" : ""}edit-title", :resource => asset.name) %>" onDblClick="popup(jQuery(this).attr('rel')); return false;" rel="<%= edit_papermill_url(asset) %>">
5
5
  <%= delete_link %>
6
6
  <%- if thumbnail_style -%>
7
7
  <%= render :partial => "papermill/thumbnail_asset", :object => asset, :locals => {:thumbnail_style => thumbnail_style} %>
@@ -0,0 +1,34 @@
1
+ <%= image_tag(@asset.url, :id => "cropbox") %>
2
+
3
+ <!--
4
+ <form onsubmit="jQuery.ajax({data:jQuery.param(jQuery(this).serializeArray()), dataType:'script', type:'post', url:'/papermill/<%= @asset.id %>'}); return false;" method="post">
5
+ <input type="hidden" value="put" name="_method"/>
6
+ <input type="hidden" value="<%= params[:target] %>" name="target"/>
7
+ <% fields_for :papermill_asset, @asset do |f| %>
8
+ <% for attribute in [:crop_x, :crop_y, :crop_w, :crop_h] %>
9
+ <%= f.hidden_field attribute, :id => attribute %>
10
+ <% end %>
11
+ <%= submit_tag t('papermill.save') %>
12
+ <% end -%>
13
+ </form>
14
+ -->
15
+ <div style="margin:6px"></div>
16
+ <%= link_to_function I18n.t("papermill.back"), "popup('#{edit_papermill_path(@asset)}')" %>
17
+
18
+ <script type="text/javascript" charset="utf-8">/*
19
+ jQuery(function() {
20
+ jQuery('#cropbox').Jcrop({
21
+ onChange: update_crop,
22
+ onSelect: update_crop
23
+ //setSelect: [0, 0, 100, 100]
24
+ //aspectRatio: 1
25
+ });
26
+ });
27
+
28
+ function update_crop(coords) {
29
+ jQuery("#crop_x").val(coords.x);
30
+ jQuery("#crop_y").val(coords.y);
31
+ jQuery("#crop_w").val(coords.w);
32
+ jQuery("#crop_h").val(coords.h);
33
+ }*/
34
+ </script>
@@ -1,6 +1,12 @@
1
1
  <div id="papermill-box">
2
2
  <div id="left">
3
- <%= link_to(@asset.image? ? image_tag(@asset.url("400x>")) : t("file_type", :type => @asset.content_type, :scope => 'papermill'), @asset.url, :popup => true) %>
3
+ <% if @asset.image? %>
4
+ <%= link_to image_tag(@asset.url("400x400>")), @asset.url, :popup => true %>
5
+ <%# image_tag(@asset.url("400x400>"), :onDblClick => "popup('#{crop_papermill_path(@asset, :target => "original")}'); return false;", :title => I18n.t("papermill.thumbnail-edit-title", :resource => @asset.name)) %>
6
+ <div style="clear:both;"></div>
7
+ <% else %>
8
+ <%= link_to t("file_type", :type => @asset.content_type, :scope => 'papermill'), @asset.url, :popup => true %>
9
+ <% end -%>
4
10
  </div>
5
11
  <div id="right">
6
12
  <div id="read-only">
@@ -20,6 +26,7 @@
20
26
  <%= render :partial => 'form' %>
21
27
  </div>
22
28
  </div>
29
+
23
30
  <div style="clear:both;"></div>
24
31
  <p id="footer">
25
32
  <%= t("papermill.location") + @asset.file.path %><br />
@@ -1,13 +1,12 @@
1
1
  en:
2
2
  papermill:
3
3
  not-processed: "Error/resource not processed"
4
- updated: "'{{ressource}}' updated"
5
- not-deleted: "'{{ressource}}' could not be deleted"
6
- not-found: "Asset #{{ressource}} not found"
7
- edit-title: "Click to edit '{{ressource}}'"
8
- thumbnail-edit-title: "Double-click to edit '{{ressource}}'"
4
+ updated: "'{{resource}}' updated"
5
+ not-found: "Asset #{{resource}} not found"
6
+ edit-title: "Click to edit '{{resource}}'"
7
+ thumbnail-edit-title: "Double-click to edit '{{resource}}'"
9
8
  upload-button-wording: "Upload..."
10
- delete: "Remove {{ressource}}"
9
+ delete: "Remove {{resource}}"
11
10
  delete-confirmation: "Delete '{{resource}}'?"
12
11
  SWFUPLOAD_PENDING: "Pending..."
13
12
  SWFUPLOAD_LOADING: "Loading..."
@@ -24,7 +23,8 @@ en:
24
23
  description: "Description"
25
24
  width: "Width"
26
25
  height: "Height"
27
- save: "Send to server"
26
+ save: "Save"
27
+ back: "Back"
28
28
  location: "File : "
29
29
  url: "URL : "
30
30
  modify-all: "Modify all: "
@@ -32,14 +32,13 @@ en:
32
32
  delete-all-confirmation: "Are you sure?"
33
33
  fr:
34
34
  papermill:
35
- not-processed: "Erreur/ressource non trouvée"
36
- updated: "{{ressource}} modifiés(s)"
37
- not-deleted: "{{ressource}} n'a pas pu être supprimée"
38
- not-found: "Asset #{{ressource}} non trouvé"
39
- edit-title: "Cliquer pour éditer '{{ressource}}'"
40
- thumbnail-edit-title: "Double-cliquer pour éditer '{{ressource}}'"
35
+ not-processed: "Erreur/resource non trouvée"
36
+ updated: "{{resource}} modifiés(s)"
37
+ not-found: "Asset #{{resource}} non trouvé"
38
+ edit-title: "Cliquer pour éditer '{{resource}}'"
39
+ thumbnail-edit-title: "Double-cliquer pour éditer '{{resource}}'"
41
40
  upload-button-wording: "Charger.."
42
- delete: "Supprimer {{ressource}}"
41
+ delete: "Supprimer {{resource}}"
43
42
  delete-confirmation: "Êtes-vous sûr de vouloir supprimer '{{resource}}' ?"
44
43
  SWFUPLOAD_PENDING: "En attente..."
45
44
  SWFUPLOAD_LOADING: "Chargement..."
@@ -56,7 +55,8 @@ fr:
56
55
  description: "Description"
57
56
  width: "Largeur"
58
57
  height: "Hauteur"
59
- save: "Envoyer"
58
+ save: "Modifier"
59
+ back: "Retour"
60
60
  location: "Fichier : "
61
61
  url: "Lien : "
62
62
  modify-all: "Modifier tout : "
data/config/routes.rb CHANGED
@@ -1,4 +1,4 @@
1
1
  ActionController::Routing::Routes.draw do |map|
2
- map.resources :papermill, :collection => { :sort => :post, :mass_edit => :post, :mass_delete => :post }
2
+ map.resources :papermill, :collection => { :sort => :post, :mass_edit => :post, :mass_delete => :post }, :member => { :crop => :get }
3
3
  map.connect "#{Papermill::options[:papermill_prefix]}/#{Papermill::PAPERCLIP_INTERPOLATION_STRING.gsub(":id_partition", ":id0/:id1/:id2")}", :controller => "papermill", :action => "show"
4
4
  end
@@ -4,12 +4,11 @@ class PapermillAssetsGenerator < Rails::Generator::Base
4
4
 
5
5
  def manifest
6
6
  puts "Copying papermill assets to your public directory..."
7
- FileUtils.rm_rf("#{File.join(RAILS_ROOT)}/public/papermill/")
8
7
  FileUtils.cp_r(
9
8
  Dir[File.join(File.dirname(__FILE__), '../../public')],
10
9
  File.join(RAILS_ROOT)
11
10
  )
12
- puts "Done! Check public/papermill/ for result."
11
+ puts "Done! Check public/ for result."
13
12
  exit
14
13
  end
15
14
  end
@@ -6,7 +6,7 @@ class PapermillInitializerGenerator < Rails::Generator::Base
6
6
  puts "Copying papermill initializer to config/initializers/..."
7
7
  FileUtils.rm_rf("#{RAILS_ROOT}/config/initializers/papermill.rb")
8
8
  FileUtils.cp_r(
9
- File.join(File.dirname(__FILE__), '../..', 'lib', 'papermill', 'papermill_options.rb'),
9
+ File.join(File.dirname(__FILE__), '../..', 'lib', 'papermill', 'papermill_initializer.rb'),
10
10
  "#{RAILS_ROOT}/config/initializers/papermill.rb"
11
11
  )
12
12
  puts "Done! Check config/initializer/papermill.rb for result."
data/lib/papermill.rb CHANGED
@@ -8,12 +8,8 @@ File.send :include, PapermillFileExtensions
8
8
  String.send :include, StringToUrlNotFound unless String.instance_methods.include? "to_url"
9
9
  Formtastic::SemanticFormBuilder.send(:include, PapermillFormtasticExtensions) rescue NameError
10
10
 
11
- begin
12
- require File.join(RAILS_ROOT, "config/initializers/papermill.rb")
13
- rescue LoadError
14
- require 'papermill/papermill_options.rb'
15
- end
16
-
11
+ require 'papermill/papermill_options.rb'
12
+ require File.join(RAILS_ROOT, "config/initializers/papermill.rb") rescue LoadError
17
13
  require 'paperclip' unless defined?(Paperclip)
18
14
  require 'papermill/papermill_paperclip_processor'
19
15
  require 'papermill/papermill'
@@ -34,130 +34,124 @@ module ActionView::Helpers::FormTagHelper
34
34
  end
35
35
 
36
36
  private
37
+
37
38
  def papermill_upload_tag(key, options)
38
- if key.nil? && options[:assetable]
39
- key = options[:assetable]
39
+
40
+ if key.nil? && [String, Symbol].include?(options[:assetable].class)
41
+ key = options[:assetable].to_s
40
42
  options[:assetable] = nil
41
43
  end
42
-
43
- assetable = options[:assetable] || @template.instance_variable_get("@#{@object_name}")
44
- options = PapermillAsset.assetable_papermill_options(assetable && assetable.class.name, key).deep_merge(options)
45
44
 
45
+ assetable = options[:assetable] || @template.instance_variable_get("@#{@object_name}")
46
46
  assetable_id = assetable && (assetable.id || assetable.timestamp) || nil
47
47
  assetable_type = assetable && assetable.class.base_class.name || nil
48
- id = "papermill_#{assetable_type}_#{assetable_id}_#{key ? key.to_s : 'nil'}"
49
- if options[:thumbnail]
50
- w = options[:thumbnail][:width] || options[:thumbnail][:height] && options[:thumbnail][:aspect_ratio] && (options[:thumbnail][:height] * options[:thumbnail][:aspect_ratio]).to_i || nil
51
- h = options[:thumbnail][:height] || options[:thumbnail][:width] && options[:thumbnail][:aspect_ratio] && (options[:thumbnail][:width] / options[:thumbnail][:aspect_ratio]).to_i || nil
52
- options[:thumbnail][:style] ||= (w || h) && "#{w}x#{h}>" || "original"
53
- if options[:inline_css]
54
- size = []
55
- size << "width:#{w}px" if w
56
- size << "height:#{h}px" if h
57
- size = size.join("; ")
58
- @template.content_for :papermill_inline_css do
59
- inline_css = ["\n"]
60
- if options[:gallery]
61
- vp = options[:gallery][:vpadding].to_i
62
- hp = options[:gallery][:hpadding].to_i
63
- vm = options[:gallery][:vmargin].to_i
64
- hm = options[:gallery][:hmargin].to_i
65
- b = options[:gallery][:border_thickness].to_i
66
- gallery_width = (options[:gallery][:width] || w) && "width:#{options[:gallery][:width] || options[:gallery][:columns]*(w+(hp+hm+b)*2)}px;" || ""
67
- gallery_height = (options[:gallery][:height] || h) && "min-height:#{options[:gallery][:height] || options[:gallery][:lines]*(h+(vp+vm+b)*2)}px;" || ""
68
- inline_css << %{##{id} { #{gallery_width} #{gallery_height} }}
69
- inline_css << %{##{id} .asset { margin:#{vm}px #{hm}px; border-width:#{b}px; padding:#{vp}px #{hp}px; #{size}; }}
70
- else
71
- inline_css << %{##{id}, ##{id} .asset { #{size} }}
72
- end
73
- inline_css << %{##{id} .name { width:#{w || "100"}px; }}
74
- inline_css.join("\n")
75
- end
76
- end
77
- end
78
-
79
- url_options = {
80
- :escape => false,
81
- :controller => "/papermill",
82
- :action => "create",
83
- :asset_class => (options[:class_name] || PapermillAsset).to_s,
84
- :gallery => !!options[:gallery],
85
- :thumbnail_style => options[:thumbnail] && options[:thumbnail][:style]
86
- }
87
-
88
- url_options.merge!({
89
- :assetable_id => assetable_id,
90
- :assetable_type => assetable_type
91
- }) if assetable
92
-
93
- url_options.merge!({
94
- :assetable_key => key
95
- }) if key
96
-
48
+ options = PapermillAsset.assetable_papermill_options(assetable && assetable.class.name, key).deep_merge(options)
49
+ dom_id = "papermill_#{assetable_type}_#{assetable_id}_#{key}"
97
50
 
51
+ if ot = options[:thumbnail]
52
+ w = ot[:width] || ot[:height] && ot[:aspect_ratio] && (ot[:height] * ot[:aspect_ratio]).to_i || nil
53
+ h = ot[:height] || ot[:width] && ot[:aspect_ratio] && (ot[:width] / ot[:aspect_ratio]).to_i || nil
54
+ ot[:style] ||= (w || h) && "#{w}x#{h}>" || "original"
55
+ set_papermill_inline_css(dom_id, w, h, options)
56
+ end
57
+
58
+ set_papermill_inline_js(dom_id, compute_papermill_create_url(assetable_id, assetable_type, key, options), options)
59
+
98
60
  html = {}
99
- create_url = @template.url_for(url_options)
100
- if assetable && assetable.new_record? && !@timestamped
101
- @timestamp_field = @template.hidden_field(assetable_type.underscore, :timestamp, :value => assetable.timestamp)
102
- @timestamped = true
61
+ html[:upload_button] = %{<div id="#{dom_id}-button-wrapper" class="papermill-button-wrapper" style="height: #{options[:swfupload][:button_height]}px;"><span id="browse_for_#{dom_id}" class="swf_button"></span></div>}
62
+ html[:container] = @template.content_tag(:div, :id => dom_id, :class => "papermill-#{key.to_s} #{(options[:thumbnail] ? "papermill-thumb-container" : "papermill-asset-container")} #{(options[:gallery] ? "papermill-multiple-items" : "papermill-unique-item")}") do
63
+ conditions = {:assetable_type => assetable_type, :assetable_id => assetable_id}
64
+ conditions.merge!({:assetable_key => key.to_s}) if key
65
+ @template.render :partial => "papermill/asset", :collection => PapermillAsset.all(:conditions => conditions), :locals => { :thumbnail_style => options[:thumbnail] && options[:thumbnail][:style] }
103
66
  end
104
67
 
105
- conditions = {:assetable_type => assetable_type, :assetable_id => assetable_id}
106
- conditions.merge!({:assetable_key => key.to_s}) if key
107
- collection = PapermillAsset.all(:conditions => conditions)
108
-
109
- 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>}
110
- html[:container] = @template.content_tag(:div, :id => id, :class => "#{(options[:thumbnail] ? "papermill-thumb-container" : "papermill-asset-container")} #{(options[:gallery] ? "papermill-multiple-items" : "papermill-unique-item")}") {
111
- @template.render :partial => "papermill/asset", :collection => collection, :locals => { :thumbnail_style => options[:thumbnail] && options[:thumbnail][:style] }
112
- }
113
-
114
68
  if options[:gallery]
115
69
  html[:dashboard] = {}
116
- html[:dashboard][:mass_edit] = %{<a onclick="modify_all('#{id}'); return false;" style="cursor:pointer">#{I18n.t("papermill.modify-all")}</a><select id="batch_#{id}">#{options[:mass_editable_fields].map do |field|
70
+ html[:dashboard][:mass_edit] = %{<a onclick="Papermill.modify_all('#{dom_id}'); return false;" style="cursor:pointer">#{I18n.t("papermill.modify-all")}</a><select id="batch_#{dom_id}">#{options[:mass_editable_fields].map do |field|
117
71
  %{<option value="#{field.to_s}">#{I18n.t("papermill.#{field.to_s}", :default => field.to_s)}</option>}
118
72
  end.join("\n")}</select>}
119
- 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>}
73
+ html[:dashboard][:mass_delete] = %{<a onclick="Papermill.mass_delete('#{dom_id}', '#{@template.escape_javascript I18n.t("papermill.delete-all-confirmation")}'); return false;" style="cursor:pointer">#{I18n.t("papermill.delete-all")}</a>}
120
74
  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")
121
75
  end
122
76
 
123
- @template.content_for :papermill_inline_js do %{
124
- jQuery("##{id}").sortable({
125
- update:function(){
126
- jQuery.ajax({
127
- async: true,
128
- data: jQuery(this).sortable('serialize'),
129
- dataType: 'script',
130
- type: 'post',
131
- url: '#{@template.controller.send("sort_papermill_path")}'
132
- })
133
- }
134
- })
135
- } end if options[:gallery]
77
+ if assetable && assetable.new_record? && !@timestamped
78
+ @timestamp_field = @template.hidden_field(assetable_type.underscore, :timestamp, :value => assetable.timestamp)
79
+ @timestamped = true
80
+ end
136
81
 
137
- @template.content_for :papermill_inline_js do %{
82
+ %{<div class="papermill">#{@timestamp_field.to_s + options[:form_helper_elements].map{|element| html[element] || ""}.join("\n")}</div>}
83
+ end
84
+
85
+
86
+ def compute_papermill_create_url(assetable_id, assetable_type, key, options)
87
+ create_url_options = {
88
+ :escape => false, :controller => "/papermill", :action => "create",
89
+ :asset_class => (options[:class_name] || PapermillAsset).to_s,
90
+ :gallery => !!options[:gallery], :thumbnail_style => options[:thumbnail] && options[:thumbnail][:style]
91
+ }
92
+ create_url_options.merge!({ :assetable_id => assetable_id, :assetable_type => assetable_type }) if assetable_id
93
+ create_url_options.merge!({ :assetable_key => key }) if key
94
+ @template.url_for(create_url_options)
95
+ end
96
+
97
+ def set_papermill_inline_js(dom_id, create_url, options)
98
+ return unless options[:inline_css]
99
+ @template.content_for :papermill_inline_js do
100
+ %{
101
+ jQuery("##{dom_id}").sortable({
102
+ update:function(){
103
+ jQuery.ajax({
104
+ async: true,
105
+ data: jQuery(this).sortable('serialize'),
106
+ dataType: 'script',
107
+ type: 'post',
108
+ url: '#{@template.controller.send("sort_papermill_path")}'
109
+ })
110
+ }
111
+ })
112
+ } if options[:gallery]
113
+ end
114
+ @template.content_for :papermill_inline_js do
115
+ %{
138
116
  new SWFUpload({
139
117
  post_params: {
140
- "#{ActionController::Base.session_options[:key]}": "#{@template.cookies[ActionController::Base.session_options[:key]]}",
141
- "authenticity_token": "#{@template.form_authenticity_token}"
118
+ "#{ ActionController::Base.session_options[:key] }": "#{ @template.cookies[ActionController::Base.session_options[:key]] }",
119
+ "authenticity_token": "#{ @template.form_authenticity_token }"
142
120
  },
143
- upload_id: "#{id}",
144
- upload_url: "#{@template.escape_javascript create_url}",
145
- file_types: "#{options[:images_only] ? '*.jpg;*.jpeg;*.png;*.gif' : ''}",
146
- file_queue_limit: "#{!options[:gallery] ? '1' : '0'}",
147
- file_queued_handler: Upload.file_queued,
148
- file_dialog_complete_handler: Upload.file_dialog_complete,
149
- upload_start_handler: Upload.upload_start,
150
- upload_progress_handler: Upload.upload_progress,
151
- file_queue_error_handler: Upload.file_queue_error,
152
- upload_error_handler: Upload.upload_error,
153
- upload_success_handler: Upload.upload_success,
154
- upload_complete_handler: Upload.upload_complete,
155
- button_placeholder_id: "browse_for_#{id}",
156
- #{options[:swfupload].map{ |key, value| "#{key}: #{(value.is_a?(String) ? "\"#{@template.escape_javascript(value)}\"" : @template.escape_javascript(value.to_s))}" if value }.compact.join(",\n")}
121
+ upload_id: "#{ dom_id }",
122
+ upload_url: "#{ @template.escape_javascript create_url }",
123
+ file_types: "#{ options[:images_only] ? '*.jpg;*.jpeg;*.png;*.gif' : '' }",
124
+ file_queue_limit: "#{ !options[:gallery] ? '1' : '0' }",
125
+ file_queued_handler: Papermill.file_queued,
126
+ file_dialog_complete_handler: Papermill.file_dialog_complete,
127
+ upload_start_handler: Papermill.upload_start,
128
+ upload_progress_handler: Papermill.upload_progress,
129
+ file_queue_error_handler: Papermill.file_queue_error,
130
+ upload_error_handler: Papermill.upload_error,
131
+ upload_success_handler: Papermill.upload_success,
132
+ upload_complete_handler: Papermill.upload_complete,
133
+ button_placeholder_id: "browse_for_#{dom_id}",
134
+ #{ options[:swfupload].map { |key, value| "#{key}: #{(value.is_a?(String) ? "\"#{@template.escape_javascript(value)}\"" : @template.escape_javascript(value.to_s))}" if value }.compact.join(",\n") }
157
135
  });
158
136
  }
159
- end
160
-
161
- %{<div class="papermill">#{@timestamp_field.to_s + options[:form_helper_elements].map{|element| html[element] || ""}.join("\n")}</div>}
137
+ end
138
+ end
139
+
140
+ def set_papermill_inline_css(dom_id, width, height, options)
141
+ html = ["\n"]
142
+ size = [width && "width:#{width}px", height && "height:#{height}px"].compact.join("; ")
143
+ if og = options[:gallery]
144
+ vp, hp, vm, hm, b = [og[:vpadding], og[:hpadding], og[:vmargin], og[:hmargin], og[:border_thickness]].map &:to_i
145
+ gallery_width = (og[:width] || width) && "width:#{og[:width] || og[:columns]*(width+(hp+hm+b)*2)}px;" || ""
146
+ gallery_height = (og[:height] || height) && "min-height:#{og[:height] || og[:lines]*(height+(vp+vm+b)*2)}px;" || ""
147
+ html << %{##{dom_id} { #{gallery_width} #{gallery_height} }}
148
+ html << %{##{dom_id} .asset { margin:#{vm}px #{hm}px; border-width:#{b}px; padding:#{vp}px #{hp}px; #{size}; }}
149
+ else
150
+ html << %{##{dom_id}, ##{dom_id} .asset { #{size} }}
151
+ end
152
+ html << %{##{dom_id} .name { width:#{width || "100"}px; }}
153
+ @template.content_for :papermill_inline_css do
154
+ html.join("\n")
155
+ end
162
156
  end
163
157
  end