Sha256: 2beaccc07d2dd4269b5868b9a4bda9849cd36143ef12d7df29452a3f709d3e37

Contents?: true

Size: 1.85 KB

Versions: 2

Compression:

Stored size: 1.85 KB

Contents

module Refinery
  module PhotoGallery
    class Photo < ActiveRecord::Base
      belongs_to :album

      mount_uploader :file, Refinery::PhotoGallery::Admin::FileUploader

      validates :title, :presence => true
      #TODO validate latitude/longitude - convert from nondecimal to decimal using inspiration from https://github.com/airblade/geo_tools/tree/master/lib/geo_tools

      before_validation :set_title
      before_create :exif_read
      #before_update :exif_write #TODO or use it after update?
      #TODO delete photo path from db? is it used?

      self.per_page = Refinery::PhotoGallery.photos_per_page

      def to_param
        "#{id}-#{title.parameterize}"
      end

      def link_url
        self.url.blank? ? file.single.url : self.url
      end

      def preview_link(ext_type = :album)
        begin
          type = self.preview_type.blank? ? ext_type : self.preview_type
          file.url(type)
        rescue Exception => e
          file.url(:error_wrong_preview_version)
        end
      end

      private

      def set_title
        self.title = self.file.file.basename.titleize unless self.title
      end

      def exif_read
        begin
          photo = MiniExiftool.new(self.file.file.file, {:numerical=> true})

          self.title = photo.DocumentName  if !photo.DocumentName.nil?
          self.longitude = photo.GPSLongitude if self.longitude.nil?
          self.latitude = photo.GPSLatitude if self.latitude.nil?
          self.description = photo.ImageDescription if self.description.nil? && photo.ImageDescription != 'Exif_JPEG_PICTURE'
            # TODO read keywords from exif
        rescue
          p "ERROR raised exception during MiniExiftool reading"

        ensure
          self.description = "" if self.description.nil?  # Because description is concating in helpers, this can't be null
        end
      end


    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
refinerycms-photo-gallery-0.3.0 app/models/refinery/photo_gallery/photo.rb
refinerycms-photo-gallery-0.2.0 app/models/refinery/photo_gallery/photo.rb