Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Renamed image class according to convention

  • Loading branch information...
commit 747cd864f55543e3cdbc9a77cb52fcb7d6d8ae12 1 parent a969d90
Can authored
View
113 lib/digineo/image.rb
@@ -1,110 +1,5 @@
-#require 'RMagick'
-class Digineo::Image < ActiveRecord::Base
-
- set_table_name :digineo_images
- store_full_sti_class = true
-
- belongs_to :parentmodel, :polymorphic => true
- belongs_to :gallery, :class_name => "Digineo::ImageGallery"
- belongs_to :image_type, :class_name => "Digineo::ImageType"
- belongs_to :user
- attr_accessor :file_url
- attr_accessor :gallery_name
- attr_accessor :image_type_name
-
- before_create :should_be_avatar?
- before_destroy :unset_avatar if :avatar
-
- extend HasImages::Scope
-
- scope_method :not_avatar, :conditions => "avatar=0"
- scope_method :without_gallery, :conditions => "gallery_id IS NULL"
-
- scope_method :image_type, lambda { |*types|
- { :conditions => "image_type_id IN (" + types.collect do |type|
- begin
- type.is_a?(Integer) ? type : Digineo::ImageType.find_by_name(type).id
- rescue
- raise Digineo::ImageType::Exception, "Could not find ImageType with name #{type}"
- end
- end.compact.join(",") + ")"}
- }
-
- has_attached_file :file
-
- after_save :set_gallery, :set_image_type
-
- validates_attachment_presence :file, :unless => :file_url_provided?, :on => :create
- validates_presence_of :parentmodel
- before_validation :download_remote_file, :if => :file_url_provided?
-
- validates_presence_of :file_remote_url, :if => :file_url_provided?, :message => 'is invalid or inaccessible'
-
- # sets the avatar flag on this image
- def set_avatar
- parentmodel.avatar.unset_avatar if parentmodel.avatar
- update_attribute(:avatar, true)
- end
-
- # removes avatar flag
- def unset_avatar
- update_attribute(:avatar, false)
- end
-
- def set_gallery
- return if gallery_id or gallery_name.to_s.empty?
- self.gallery_id = parentmodel.find_or_create_gallery(gallery_name).id
- save
- end
-
- def set_image_type
- return if image_type_id or image_type_name.to_s.empty?
- self.image_type_id = ImageType.find_or_create_by_name(image_type_name).id
- save
- end
-
-# def crop(params = {:style => :original})
-#
-# args = [params[:x1].to_i, params[:y1].to_i, params[:width].to_i, params[:height].to_i]
-#
-# if old_original = file.to_file(params[:style])
-# orig_img = Magick::ImageList.new
-# orig_img.from_blob(old_original.read)
-# orig_img.crop!(*args)
-#
-# new_img = File.open(file.path(:original), "w")
-# new_img.write(orig_img.to_blob)
-# new_img.close
-#
-#
-# file.reprocess!
-# save
-# else
-# true
-# end
-# end
-
- private
-
- def should_be_avatar?
- self.avatar = !parentmodel.avatar
- true # returns true because it's called by before_create
- end
-
- def file_url_provided?
- !self.file_url.blank?
- end
-
- def download_remote_file
- self.file = do_download_remote_file
- self.file_remote_url = file_url
- end
-
- def do_download_remote_file
- io = open(URI.parse(file_url))
- def io.original_filename; base_uri.path.split('/').last; end
- io.original_filename.blank? ? nil : io
- rescue # TODO catch url errors with validations instead of exceptions (Errno::ENOENT, OpenURI::HTTPError, etc...)
- end
-
+module Digineo
+ module Image
+ autoload :Base, 'digineo/image/base'
end
+end
View
112 lib/digineo/image/base.rb
@@ -0,0 +1,112 @@
+#require 'RMagick'
+module Digineo
+ module Image
+ class Base < ActiveRecord::Base
+ set_table_name :digineo_images
+ store_full_sti_class = true
+
+ belongs_to :parentmodel, :polymorphic => true
+ belongs_to :gallery, :class_name => "Digineo::ImageGallery"
+ belongs_to :image_type, :class_name => "Digineo::ImageType"
+ belongs_to :user
+ attr_accessor :file_url
+ attr_accessor :gallery_name
+ attr_accessor :image_type_name
+
+ before_create :should_be_avatar?
+ before_destroy :unset_avatar if :avatar
+
+ extend HasImages::Scope
+
+ scope_method :not_avatar, :conditions => "avatar=0"
+ scope_method :without_gallery, :conditions => "gallery_id IS NULL"
+
+ scope_method :image_type, lambda { |*types|
+ { :conditions => "image_type_id IN (" + types.collect do |type|
+ begin
+ type.is_a?(Integer) ? type : Digineo::ImageType.find_by_name(type).id
+ rescue
+ raise Digineo::ImageType::Exception, "Could not find ImageType with name #{type}"
+ end
+ end.compact.join(",") + ")"}
+ }
+
+ has_attached_file :file
+
+ after_save :set_gallery, :set_image_type
+
+ validates_attachment_presence :file, :unless => :file_url_provided?, :on => :create
+ validates_presence_of :parentmodel
+ before_validation :download_remote_file, :if => :file_url_provided?
+
+ validates_presence_of :file_remote_url, :if => :file_url_provided?, :message => 'is invalid or inaccessible'
+
+ # sets the avatar flag on this image
+ def set_avatar
+ parentmodel.avatar.unset_avatar if parentmodel.avatar
+ update_attribute(:avatar, true)
+ end
+
+ # removes avatar flag
+ def unset_avatar
+ update_attribute(:avatar, false)
+ end
+
+ def set_gallery
+ return if gallery_id or gallery_name.to_s.empty?
+ self.gallery_id = parentmodel.find_or_create_gallery(gallery_name).id
+ save
+ end
+
+ def set_image_type
+ return if image_type_id or image_type_name.to_s.empty?
+ self.image_type_id = ImageType.find_or_create_by_name(image_type_name).id
+ save
+ end
+
+ # def crop(params = {:style => :original})
+ #
+ # args = [params[:x1].to_i, params[:y1].to_i, params[:width].to_i, params[:height].to_i]
+ #
+ # if old_original = file.to_file(params[:style])
+ # orig_img = Magick::ImageList.new
+ # orig_img.from_blob(old_original.read)
+ # orig_img.crop!(*args)
+ #
+ # new_img = File.open(file.path(:original), "w")
+ # new_img.write(orig_img.to_blob)
+ # new_img.close
+ #
+ #
+ # file.reprocess!
+ # save
+ # else
+ # true
+ # end
+ # end
+
+ private
+
+ def should_be_avatar?
+ self.avatar = !parentmodel.avatar
+ true # returns true because it's called by before_create
+ end
+
+ def file_url_provided?
+ !self.file_url.blank?
+ end
+
+ def download_remote_file
+ self.file = do_download_remote_file
+ self.file_remote_url = file_url
+ end
+
+ def do_download_remote_file
+ io = open(URI.parse(file_url))
+ def io.original_filename; base_uri.path.split('/').last; end
+ io.original_filename.blank? ? nil : io
+ rescue # TODO catch url errors with validations instead of exceptions (Errno::ENOENT, OpenURI::HTTPError, etc...)
+ end
+ end
+ end
+end
View
4 lib/digineo/image_gallery.rb
@@ -3,8 +3,8 @@ class Digineo::ImageGallery < ActiveRecord::Base
set_table_name :digineo_image_galleries
belongs_to :parentmodel, :polymorphic => true
- has_many :images, :class_name => 'Digineo::Image', :foreign_key => :gallery_id
- has_one :avatar, :foreign_key => :gallery_id, :class_name => "Digineo::Image"
+ has_many :images, :class_name => 'Digineo::Image::Base', :foreign_key => :gallery_id
+ has_one :avatar, :foreign_key => :gallery_id, :class_name => "Digineo::Image::Base"
validates_presence_of :name
View
2  lib/digineo/image_type.rb
@@ -1,7 +1,7 @@
class Digineo::ImageType < ActiveRecord::Base
set_table_name :digineo_image_types
- has_many :images, :class_name => "Digineo::Image"
+ has_many :images, :class_name => "Digineo::Image::Base"
validates_presence_of :name
validates_uniqueness_of :name
View
25 lib/has_images.rb
@@ -19,22 +19,19 @@ def has_images(options={})
options.merge! :use_timestamp => false
# eval is not always evil ;)
# we generate a Digineo::Model::Image clase to store the given paperclip configuration in it
- eval <<-EOF
- module Digineo::#{self.name}
- class Digineo::#{self.name}::Image < Digineo::Image
- self.attachment_definitions = {}
- has_attached_file :file, #{options.inspect}
- belongs_to :parentmodel, :polymorphic => true, :counter_cache => #{counter_cache.inspect}
- end
- end
+ ::Digineo::Image.const_set(self.name, Class.new(Digineo::Image::Base){
+ has_attached_file :file, options
+ belongs_to :parentmodel, :polymorphic => true, :counter_cache => counter_cache
+ })
+ eval %Q{
def digineo_image_class
- Digineo::#{self.name}::Image
+ Digineo::Image::#{self.name}
end
- EOF
-
+ }
+
belongs_to :parentmodel, :polymorphic => true, :counter_cache => counter_cache
- has_many :images, :as => :parentmodel, :dependent => :destroy, :order => 'id ASC', :class_name => "Digineo::#{self.name}::Image"
- has_one :avatar, :as => :parentmodel, :conditions => { :avatar => 1 }, :class_name => "Digineo::#{self.name}::Image"
+ has_many :images, :as => :parentmodel, :dependent => :destroy, :order => 'id ASC', :class_name => "Digineo::Image::#{self.name}"
+ has_one :avatar, :as => :parentmodel, :conditions => { :avatar => 1 }, :class_name => "Digineo::Image::#{self.name}"
has_many :galleries, :as => :parentmodel, :dependent => :destroy, :class_name => 'Digineo::ImageGallery'
after_create :save_avatar
@@ -80,7 +77,7 @@ def create_image_by_url(url, image_type = nil)
end
def avatar_with_autobuild=(image_or_upload)
- if image_or_upload.kind_of? Digineo::Image
+ if image_or_upload.kind_of? Digineo::Image::Base
super
else
avatar.update_attribute :avatar, 0 if avatar
Please sign in to comment.
Something went wrong with that request. Please try again.