lib/carrierwave/compatibility/paperclip.rb in carrierwave-0.9.0 vs lib/carrierwave/compatibility/paperclip.rb in carrierwave-0.10.0

- old
+ new

@@ -44,15 +44,35 @@ # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN # THE SOFTWARE. # module Paperclip + extend ActiveSupport::Concern + DEFAULT_MAPPINGS = { + :rails_root => lambda{|u, f| Rails.root.to_s }, + :rails_env => lambda{|u, f| Rails.env }, + :id_partition => lambda{|u, f| ("%09d" % u.model.id).scan(/\d{3}/).join("/")}, + :id => lambda{|u, f| u.model.id }, + :attachment => lambda{|u, f| u.mounted_as.to_s.downcase.pluralize }, + :style => lambda{|u, f| u.paperclip_style }, + :basename => lambda{|u, f| u.filename.gsub(/#{File.extname(u.filename)}$/, "") }, + :extension => lambda{|u, d| File.extname(u.filename).gsub(/^\.+/, "")}, + :class => lambda{|u, f| u.model.class.name.underscore.pluralize} + } + + included do + attr_accessor :filename + class_attribute :mappings + self.mappings ||= DEFAULT_MAPPINGS.dup + end + def store_path(for_file=filename) path = paperclip_path + self.filename = for_file path ||= File.join(*[store_dir, paperclip_style.to_s, for_file].compact) - interpolate_paperclip_path(path, for_file) + interpolate_paperclip_path(path) end def store_dir ":rails_root/public/system/:attachment/:id" end @@ -66,30 +86,20 @@ def paperclip_style version_name || paperclip_default_style end - private - - def interpolate_paperclip_path(path, filename) - mappings.inject(path) do |agg, pair| - agg.gsub(":#{pair[0]}") { pair[1].call(self, filename).to_s } + module ClassMethods + def interpolate(sym, &block) + mappings[sym] = block end end - def mappings - [ - [:rails_root , lambda{|u, f| Rails.root }], - [:rails_env , lambda{|u, f| Rails.env }], - [:class , lambda{|u, f| u.model.class.name.underscore.pluralize}], - [:id_partition , lambda{|u, f| ("%09d" % u.model.id).scan(/\d{3}/).join("/")}], - [:id , lambda{|u, f| u.model.id }], - [:attachment , lambda{|u, f| u.mounted_as.to_s.downcase.pluralize }], - [:style , lambda{|u, f| u.paperclip_style }], - [:basename , lambda{|u, f| f.gsub(/#{File.extname(f)}$/, "") }], - [:extension , lambda{|u, f| File.extname(f).gsub(/^\.+/, "")}] - ] + private + def interpolate_paperclip_path(path) + mappings.each_pair.inject(path) do |agg, pair| + agg.gsub(":#{pair[0]}") { pair[1].call(self, self.paperclip_style).to_s } + end end - end # Paperclip end # Compatibility end # CarrierWave