Class Image
In: app/models/image.rb
Parent: ActiveRecord::Base

Images are referred to as "Pictures" throughout the views. They could further be divided into Photos, Artworks, Documents etc. using STI if there was need to deal with these things in different ways.

Using the file_column plugin thumbnails, and a reduced size diplay image are automatically when the file is uploaded created. A customized hack on this plugin also converts any TIFFs into JPEGs so that they can be viewed in the browser. file_column is no longer supported and it would be worth thinking about moving to acts_as_attachment which is easier to maintain and customize.

The files are currently stored in the filesystem in public/images/url/ according to the default file_column locations.

schema.rb snippet:

  create_table "images", :force => true do |t|
    t.column "url",                  :string,   :null => false
    t.column "format",               :string
    t.column "type",                 :string
    t.column "description",          :text
    t.column "created_at",           :datetime
    t.column "updated_at",           :datetime
    t.column "recorded_on",          :date
    t.column "restriction",          :string
    t.column "original_format",      :string
    t.column "location_of_original", :string
    t.column "catalogue_ref",        :string
    t.column "width",                :integer
    t.column "height",               :integer
    t.column "copyright_notice_id",  :integer
  end

Methods

Public Instance methods

Used to add a subject to the image. Optionally a hash of floats representing percentages for left, top, width and height specifies which region of the image contains the subject.

[Source]

     # File app/models/image.rb, line 99
 99:   def add_subject subject_to_add, box = {}
100:       unless subject_to_add.new_record?
101:           subjects.each do |subject| 
102:               if subject == subject_to_add 
103:                   return false
104:               end
105:           end
106:       end
107:       
108:       params = {:subject => subject_to_add}
109:       if box.size == 4
110:         params.merge({:left => box[:left], :top => box[:top], :width => box[:width], :height => box[:height]})
111:       end
112:       return image_notes.create(params)
113:   end

Returns the apect ratio (width:height) of the image. This can be used to calculate one dimension of the image if the other is known. Or to create a scaling div with the right shape.

[Source]

    # File app/models/image.rb, line 88
88:   def aspect_ratio
89:     self.width.to_f / self.height
90:   end

Before saving the dimesions of the image are stored in the width and height fields. No restriction must be represented as empty string rather than null.

[Source]

    # File app/models/image.rb, line 76
76:   def before_save
77:     self.width = Magick::ImageList.new(self.url(nil)).columns
78:     self.height = Magick::ImageList.new(self.url(nil)).rows
79:     
80:     # month = self.recorded_on_month.nil? ? 1 : self.recorded_on_month
81:     # day = self.recorded_on_day.nil? ? 1 : self.recorded_on_day
82:     # self.recorded_on = Date.new(self.recorded_on_year, , self.recorded_on_day.to_i) rescue nil
83:   end

Could be more direct by using CRUD on ImageNote instead of this call.

[Source]

     # File app/models/image.rb, line 116
116:   def delete_subject subject_to_remove
117:     if subject_to_remove.class == ImageNote
118:       subject_to_remove.destroy
119:     else
120:       subject = ImageNote.find_by_image_id_and_subject_id_and_subject_type(self.id, subject_to_remove.id, subject_to_remove.class.to_s)
121:       subject.destroy
122:     end
123:   end

Returns a list of those who are featured in this image.

[Source]

    # File app/models/image.rb, line 93
93:   def subjects
94:         image_notes.sort.collect {|note| note.subject}
95:   end

[Validate]