app/models/logo.rb in social_stream-0.4.0 vs app/models/logo.rb in social_stream-0.4.1
- old
+ new
@@ -1,23 +1,60 @@
require 'RMagick'
class Logo < ActiveRecord::Base
- has_attached_file :logo,
- :styles => { :tie => "30x30>",
- :actor => '35x35>',
- :profile => '94x94' },
- :default_url => "/images/:attachment/:style/:subtype_class.png"
+ has_attached_file :logo,
+ :styles => { :tie => "30x30>",
+ :actor => '35x35>',
+ :profile => '94x94' },
+ :default_url => "/images/:attachment/:style/:subtype_class.png"
+
before_post_process :process_precrop
- attr_accessor :crop_x, :crop_y, :crop_w, :crop_h
- validates_attachment_presence :logo
+# before_post_process :copy_temp_file
+ attr_accessor :crop_x, :crop_y, :crop_w, :crop_h, :name
+ validates_attachment_presence :logo, :if => :uploading_file?
+
+ after_validation :precrop_done
+# after_validation :mylog
+
+
+ def uploading_file?
+ return @name.blank?
+ end
+
+ def precrop_done
+ return if @name.blank?
+
+ images_path = File.join(RAILS_ROOT, "public", "images")
+ tmp_path = FileUtils.mkdir_p(File.join(images_path, "tmp"))
+ precrop_path = File.join(tmp_path,@name)
+
+ make_precrop(precrop_path,@crop_x.to_i,@crop_y.to_i,@crop_w.to_i,@crop_h.to_i)
+ @logo = Logo.new :logo => File.open(precrop_path), :name => @name
+
+ self.logo = @logo.logo
+
+ FileUtils.remove_file(precrop_path)
+ end
+
+ def copy_temp_file
+ images_path = File.join(RAILS_ROOT, "public", "images")
+ tmp_path = FileUtils.mkdir_p(File.join(images_path, "tmp"))
+ end
def process_precrop
+
+ if @name.blank? && ( logo.content_type.present? && !logo.content_type.start_with?("image/"))
+ logo.errors['invalidType'] = "The file you uploaded isn't valid"
+ return false
+ end
+
+ return if !@name.blank?
logo.errors['precrop'] = "You have to make precrop"
images_path = File.join(RAILS_ROOT, "public", "images")
tmp_path = FileUtils.mkdir_p(File.join(images_path, "tmp"))
-
- resize_image(logo.queued_for_write[:original].path,600,600)
+
+ resize_image(logo.queued_for_write[:original].path,500,500)
my_file_name = File.basename(logo.queued_for_write[:original].path)
FileUtils.cp(logo.queued_for_write[:original].path,tmp_path)
temp_file = File.open(logo.queued_for_write[:original].path, "w+")
end