BBenezech-papermill 0.3.0 → 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.3.0
1
+ 0.4.0
@@ -59,15 +59,18 @@ class PapermillController < ApplicationController
59
59
  end
60
60
 
61
61
  def create
62
- params[:assetable_type] = params[:assetable_type].camelize
63
- asset_class = params[:assetable_type].constantize.papermill_associations[params[:association].to_sym][:class]
62
+ params[:assetable_id] = params[:assetable_id].nie
63
+ asset_class = params[:asset_class].constantize
64
+ params[:assetable_type] = params[:assetable_type] && params[:assetable_type].to_s.camelize.nie
64
65
  params[:swfupload_file] = params.delete(:Filedata)
65
- @old_asset = asset_class.find(:first, :conditions => {:assetable_key => params[:assetable_key].to_s, :assetable_type => params[:assetable_type], :assetable_id => params[:assetable_id]}) unless params[:gallery]
66
+ unless params[:gallery]
67
+ @old_asset = asset_class.find(:first, :conditions => {:assetable_key => params[:assetable_key], :assetable_type => params[:assetable_type], :assetable_id => params[:assetable_id]})
68
+ end
66
69
  @asset = asset_class.new(params.reject{|key, value| !(PapermillAsset.columns.map(&:name)+["swfupload_file"]).include?(key.to_s)})
67
70
 
68
71
  if @asset.save
69
72
  @old_asset.destroy if @old_asset
70
- render :partial => "papermill/asset", :object => @asset, :locals => {:thumbnail => params[:thumbnail], :gallery => params[:gallery], :thumbnail_style => params[:thumbnail_style]}
73
+ render :partial => "papermill/asset", :object => @asset, :locals => {:gallery => params[:gallery], :thumbnail_style => params[:thumbnail_style]}
71
74
  else
72
75
  message = t("not-created", :scope => "papermill")
73
76
  render :text => message, :status => "500"
@@ -1,28 +1,34 @@
1
1
  class ActionView::Helpers::FormBuilder
2
2
 
3
- def assets_upload(key = nil, method = nil, options = {})
4
- papermill_upload_field key, method, { :thumbnail => false }.update(options)
3
+ def assets_upload(key = nil, options = {})
4
+ papermill_upload_field key, { :thumbnail => false }.update(options)
5
5
  end
6
- def asset_upload(key = nil, method = nil, options = {})
7
- papermill_upload_field key, method, { :gallery => false, :thumbnail => false }.update(options)
6
+ def asset_upload(key = nil, options = {})
7
+ papermill_upload_field key, { :gallery => false, :thumbnail => false }.update(options)
8
8
  end
9
- def images_upload(key = nil, method = nil, options = {})
10
- papermill_upload_field key, method, options
9
+ def images_upload(key = nil, options = {})
10
+ papermill_upload_field key, options
11
11
  end
12
- def image_upload(key = nil, method = nil, options = {})
13
- papermill_upload_field key, method, { :gallery => false }.update(options)
12
+ def image_upload(key = nil, options = {})
13
+ papermill_upload_field key, { :gallery => false }.update(options)
14
14
  end
15
15
 
16
16
  private
17
- def papermill_upload_field(key, method, options = {})
17
+ def papermill_upload_field(key, options = {})
18
+ if key.is_a? Hash
19
+ options = key
20
+ key = nil
21
+ end
18
22
  assetable = @template.instance_variable_get("@#{@object_name}")
19
- method ||= options[:association] || :papermill_assets
20
- key ||= options[:key] || nil
21
- options = assetable.class.papermill_options.deep_merge(options)
22
- assetable_id = assetable.id || assetable.timestamp
23
- assetable_type = assetable.class.to_s.underscore
24
- id = "papermill_#{assetable_type}_#{assetable_id}_#{key}_#{method}"
25
- if options[:thumbnail]
23
+ options = if assetable && (association = (assetable.class.papermill_associations[key] || assetable.class.papermill_associations[:papermill_assets]))
24
+ association[:options].deep_merge(options)
25
+ else
26
+ raise Exception.new("Papermill: can't find #{key.to_s} association for #{assetable.class.to_s}.\n\n##{assetable.class.to_s.underscore}.rb\n#set either a catchall papermill association: \npapermill {your_option_hash}\n#or this specific association: \npapermill :#{key.to_s}, {your_option_hash}")
27
+ end
28
+ assetable_id = assetable && (assetable.id || assetable.timestamp) || nil
29
+ assetable_type = assetable && assetable.class.to_s.underscore || nil
30
+ id = "papermill_#{assetable_type}_#{assetable_id}_#{key ? key.to_s : 'nil'}"
31
+ if options[:thumbnail]
26
32
  w = options[:thumbnail][:width] || options[:thumbnail][:height] && options[:thumbnail][:aspect_ratio] && (options[:thumbnail][:height] * options[:thumbnail][:aspect_ratio]).to_i || nil
27
33
  h = options[:thumbnail][:height] || options[:thumbnail][:width] && options[:thumbnail][:aspect_ratio] && (options[:thumbnail][:width] / options[:thumbnail][:aspect_ratio]).to_i || nil
28
34
  options[:thumbnail][:style] ||= (w || h) && "#{w || options[:thumbnail][:max_width]}x#{h || options[:thumbnail][:max_height]}>" || "original"
@@ -51,17 +57,21 @@ class ActionView::Helpers::FormBuilder
51
57
  end
52
58
  end
53
59
  end
54
- create_url = @template.url_for(:controller => "/papermill", :action => "create", :escape => false, :association => method.to_s, :assetable_key => key, :assetable_id => assetable_id, :assetable_type => assetable_type, :gallery => (options[:gallery] != false), :thumbnail_style => (options[:thumbnail] && options[:thumbnail][:style]))
55
60
  html = []
56
- if assetable.new_record? && !@timestamped
61
+
62
+ conditions = {:assetable_type => assetable.class.sti_name, :assetable_id => assetable_id}
63
+ conditions.merge!({:assetable_key => key.to_s}) if key
64
+ asset_class = options[:class_name] && options[:class_name].to_s.constantize || association && association[:class] || PapermillAsset
65
+ create_url = @template.url_for(:controller => "/papermill", :action => "create", :escape => false, :asset_class => asset_class.to_s, :assetable_key => key, :assetable_id => assetable_id, :assetable_type => assetable_type, :gallery => (options[:gallery] != false), :thumbnail_style => (options[:thumbnail] && options[:thumbnail][:style]))
66
+ if assetable && assetable.new_record? && !@timestamped
57
67
  html << self.hidden_field(:timestamp, :value => assetable.timestamp)
58
68
  @timestamped = true
59
69
  end
70
+ collection = asset_class.find(:all, :conditions => conditions, :order => "position")
71
+
60
72
  html << %{<div style="height: #{options[:swfupload][:button_height]}px;"><span id="browse_for_#{id}" class="swf_button"></span></div>}
61
- conditions = {:assetable_type => assetable.class.sti_name, :assetable_id => assetable_id}
62
- conditions.merge!({:assetable_key => key.to_s}) if key
63
73
  html << @template.content_tag(:ul, :id => id, :class => "papermill #{(options[:thumbnail] ? "papermill-thumb-container" : "papermill-asset-container")} #{(options[:gallery] ? "papermill-multiple-items" : "papermill-unique-item")}") {
64
- @template.render :partial => "papermill/asset", :collection => assetable.class.papermill_associations[method][:class].find(:all, :conditions => conditions, :order => "position"), :locals => { :thumbnail_style => (options[:thumbnail] && options[:thumbnail][:style]) }
74
+ @template.render :partial => "papermill/asset", :collection => collection, :locals => { :thumbnail_style => (options[:thumbnail] && options[:thumbnail][:style]) }
65
75
  }
66
76
  @template.content_for :inline_js do
67
77
  %{
@@ -1,8 +1,5 @@
1
- require "acts_as_list"
2
- require "paperclip"
3
-
4
1
  class PapermillAsset < ActiveRecord::Base
5
- acts_as_list :scope => 'assetable_key=\'#{assetable_key.simple_sql_sanitizer}\' AND assetable_id=#{assetable_id} AND assetable_type=\'#{assetable_type}\''
2
+ acts_as_list :scope => 'assetable_key=\'#{assetable_key.to_s.simple_sql_sanitizer}\' AND assetable_id=#{assetable_id} AND assetable_type=\'#{assetable_type}\''
6
3
 
7
4
  belongs_to :assetable, :polymorphic => true
8
5
  before_destroy :destroy_files
@@ -50,10 +47,6 @@ class PapermillAsset < ActiveRecord::Base
50
47
  content_type && content_type.first == "image" && content_type[1]
51
48
  end
52
49
 
53
- def interpolated_path(with = {}, up_to = nil)
54
- Papermill::papermill_interpolated_path({":id_partition" => self.id_partition}.merge(with), up_to)
55
- end
56
-
57
50
  # before_filter
58
51
  def destroy_files
59
52
  system "rm -rf #{Papermill::papermill_interpolated_path({":id_partition" => self.id_partition}, ':id_partition')}/" if image?
@@ -1,5 +1,11 @@
1
1
  module PapermillHelper
2
2
 
3
+ # Sets all the javascript needed for papermill.
4
+ # If you already loaded jQuery and JQueryUI, call papermill_javascript_tag
5
+ # If you don't use jQuery or use some other library, call papermill_javascript_tag(:with_jquery => "no_conflict")
6
+ # If you want to rely on this helper to load jQuery/jQueryUI and use it, call papermill_javascript_tag(:with_jquery => true)
7
+ # If you loaded jQuery and need to load only jQueryUI, call papermill_javascript_tag(:with_jqueryui_only => true)
8
+ # If you changed the location of papermill.js, you'll need to set :root_folder (defaults to "javascripts")
3
9
  def papermill_javascript_tag(options = {})
4
10
  html = []
5
11
  root_folder = options[:path] || "javascripts"
@@ -7,7 +13,8 @@ module PapermillHelper
7
13
  html << %{<script src="http://www.google.com/jsapi"></script>}
8
14
  html << %{<script type="text/javascript">\n//<![CDATA[}
9
15
  html << %{google.load("jquery", "1");} if options[:with_jquery]
10
- html << %{google.load("jqueryui", "1");} if options[:with_jquery] || options[:with_jqueryui]
16
+ html << %{google.load("jqueryui", "1");} if options[:with_jquery] || options[:with_jqueryui_only]
17
+ html << %{jQuery.noConflict();} if options[:with_jquery] == "no_conflict"
11
18
  html << %{</script>}
12
19
  end
13
20
  html << %{<script src="http://swfupload.googlecode.com/svn/swfupload/tags/swfupload_v2.2.0_core/swfupload.js"></script>}
@@ -23,6 +30,8 @@ module PapermillHelper
23
30
  html.join("\n")
24
31
  end
25
32
 
33
+ # Sets the css tags needed for papermill.
34
+ # If you changed the location of papermill.css, you'll need to set :root_folder (defaults to "stylesheets")
26
35
  def papermill_stylesheet_tag(options = {})
27
36
  html = []
28
37
  root_folder = options[:path] || "stylesheets"
@@ -84,53 +84,56 @@ module Papermill
84
84
  end
85
85
 
86
86
  module ClassMethods
87
- attr_reader :papermill_options
88
87
  attr_reader :papermill_associations
89
88
 
90
- # Dealing with STIed Asset table in papermill declaration is dead easy, since papermill follows ActiveRecord :has_many global syntax and the Convention over configuration concept :
91
- # papermill <association_name>, :class_name => MySTIedPapermillAssetClassName
92
- # class MyAsset < PapermillAsset
93
- # ...
94
- # end
95
- # class MyAssetableClass < ActiveRecord::Base
96
- # 1 -> papermill :my_association, :class_name => MyAsset
97
- # 2 -> papermill :class_name => MyAsset
98
- # 3 -> papermill :my_assets
99
- # 4 -> papermill :my_other_assets
100
- # 5 -> papermill
101
- # end
102
- # assetable = MyAssetableClass.new
103
- # 1 -> assetable.my_association attached MyAsset objects (no magic here, association and class_name both specified)
104
- # 2 -> assetable.my_assets attached MyAsset objects (association infered from :class_name as expected)
105
- # 3 -> assetable.my_assets attached MyAsset objects (class_name guessed from association name)
106
- # 4 -> assetable.my_other_assets attached PapermillAssets objects (couldn't find MyOtherAsset class or MyOtherAsset is not a PapermillAsset subclass, use default PapermillAsset superclass)
107
- # 5 -> assetable.papermill_assets attached PapermillAssets objects (defaults)
89
+ # papermill comes in 2 flavors:
90
+ #
91
+ # 1. generic declaration =>
92
+ # declare associations with => papermill {my_option_hash}
93
+ # create assets with => assets_upload(:my_key, {optional_option_hash})
94
+ # access assets with => assetable.papermill_assets(:key => :my_key)
95
+ #
96
+ # 2. association declaration =>
97
+ # declare associations with => papermill :my_association, {my_option_hash}
98
+ # create assets with => assets_upload(my_association, {optional_option_hash})
99
+ # access assets with => assetable.my_association
100
+ #
101
+ # In both case, you can specify a PapermillAsset subclass to use with :class_name => MyPapermillAssetSubclass in the option hash
102
+ def papermill(assoc_name = :papermill_assets, options = {})
103
+ if assoc_name.is_a? Hash
104
+ options = assoc_name
105
+ assoc_name = :papermill_assets
106
+ end
108
107
 
109
- def papermill(assoc = nil, options = {})
110
108
  @papermill_associations ||= {}
111
- asset_class = ((klass = options.delete(:class_name)) && (klass = (klass.to_s.singularize.camelize.constantize rescue nil)) && klass.superclass == PapermillAsset && klass || assoc && (klass = (assoc.to_s.singularize.camelize.constantize rescue nil)) && klass.superclass == PapermillAsset && klass || PapermillAsset)
112
- assoc ||= asset_class.to_s.pluralize.underscore.to_sym
109
+ begin
110
+ asset_class = (class_name = options.delete(:class_name)).to_s.constantize || PapermillAsset
111
+ rescue
112
+ raise Exception.new("Papermill: can't find class #{class_name.to_s}.\n#{class_name.to_s} should be a subclass of PapermillAsset")
113
+ end
114
+ assoc_name ||= asset_class.to_s.pluralize.underscore.to_sym
113
115
 
114
- @papermill_associations.merge!({assoc => {:class => asset_class}})
115
- @papermill_options = Papermill::PAPERMILL_DEFAULTS.deep_merge(options)
116
+ @papermill_associations.merge!({assoc_name => {:class => asset_class, :options => Papermill::PAPERMILL_DEFAULTS.deep_merge(options)}})
116
117
  before_destroy :destroy_assets
117
118
  after_create :rebase_assets
118
- # reinventing the wheel because ActiveRecord chokes on :finder_sql with associations
119
- # TODO Clean the mess
120
- define_method assoc do |*options|
121
- klass = self.class.papermill_associations[assoc.to_sym][:class]
119
+
120
+ define_method assoc_name do |*options|
121
+ klass = self.class.papermill_associations[assoc_name.to_sym][:class]
122
122
  options = options.first || {}
123
123
  conditions = {
124
124
  :assetable_type => self.class.sti_name,
125
- :assetable_id => self.id,
125
+ :assetable_id => self.id
126
126
  }.merge(options.delete(:conditions) || {})
127
- order = (options.delete(:order) || "position ASC")
128
- conditions.merge!({:type => klass.to_s}) unless klass == PapermillAsset
129
- conditions.merge!({:assetable_key => options[:key].to_s}) if options[:key]
127
+ conditions.merge!({:type => klass.to_s}) unless assoc_name == PapermillAsset
128
+ conditions.merge!({:assetable_key => assoc_name.to_s}) if assoc_name != :papermill_assets
129
+ conditions.merge!({:assetable_key => options.delete(:key)}) if options.has_key?(:key)
130
130
  conditions.merge!({:type => options[:class_name]}) if options[:class_name]
131
- asset_class.find(:all, :conditions => conditions, :order => order)
131
+ hash = {
132
+ :conditions => conditions,
133
+ :order => options.delete(:order) || "position ASC"
134
+ }.merge(options)
135
+ asset_class.find(:all, hash)
132
136
  end
133
-
134
137
 
135
138
  class_eval <<-EOV
136
139
  include Papermill::InstanceMethods
@@ -138,7 +141,6 @@ module Papermill
138
141
  end
139
142
 
140
143
  def inherited(subclass)
141
- subclass.instance_variable_set("@papermill_options", @papermill_options)
142
144
  subclass.instance_variable_set("@papermill_associations", @papermill_associations)
143
145
  super
144
146
  end
@@ -162,7 +164,7 @@ module Papermill
162
164
  PapermillAsset.find(:all, :conditions => {:assetable_id => self.timestamp, :assetable_type => self.class.sti_name}).each do |asset|
163
165
  if asset.created_at < 2.hours.ago
164
166
  asset.destroy
165
- else
167
+ else
166
168
  asset.update_attribute(:assetable_id, self.id)
167
169
  end
168
170
  end
data/papermill.gemspec CHANGED
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{papermill}
8
- s.version = "0.3.0"
8
+ s.version = "0.4.0"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Benoit B\303\251n\303\251zech"]
12
- s.date = %q{2009-08-31}
12
+ s.date = %q{2009-09-01}
13
13
  s.description = %q{Paperclip wrapper}
14
14
  s.email = %q{benoit.benezech@gmail.com}
15
15
  s.extra_rdoc_files = [
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: BBenezech-papermill
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - "Benoit B\xC3\xA9n\xC3\xA9zech"
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-08-31 00:00:00 -07:00
12
+ date: 2009-09-01 00:00:00 -07:00
13
13
  default_executable:
14
14
  dependencies: []
15
15
 
@@ -63,7 +63,6 @@ files:
63
63
  - uninstall.rb
64
64
  has_rdoc: false
65
65
  homepage: http://github.com/BBenezech/papermill
66
- licenses:
67
66
  post_install_message:
68
67
  rdoc_options:
69
68
  - --charset=UTF-8
@@ -84,7 +83,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
84
83
  requirements: []
85
84
 
86
85
  rubyforge_project:
87
- rubygems_version: 1.3.5
86
+ rubygems_version: 1.2.0
88
87
  signing_key:
89
88
  specification_version: 3
90
89
  summary: Paperclip wrapper