Class: CabooseRets::Media

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
app/models/caboose_rets/media.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.rename_mediaObject



116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'app/models/caboose_rets/media.rb', line 116

def self.rename_media
  s3 = AWS::S3.new
  b = s3.buckets["advantagerealtygroup"]

  CabooseRets::Media.where(:media_type => 'Photo').reorder("mls, media_order").all.each do |m|
    puts "Renaming #{m.mls}_#{m.id}..."
    if m.image_file_name && m.image
      ext = File.extname(m.image_file_name)
      (m.image.styles.keys+[:original]).each do |style|
        b.objects[m.image.path(style)].copy_to "rets/media_new/#{m.id}_#{style}.#{ext}" if b.objects[m.image.path(style)].exists?
      end
    end
  end

  return true
end

.reorder(media_ids, bucket_name) ⇒ Object

before_post_process :rename_avatar def rename_image

extension = File.extname(image_file_name).downcase
self.image.instance_write :file_name, "#{self.mls}_#{self.media_order}.#{extension}"

end



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'app/models/caboose_rets/media.rb', line 48

def self.reorder(media_ids, bucket_name)

  s3 = AWS::S3.new
  b = s3.buckets[bucket_name]

  # Rename the s3 assets to temp names
  media_ids.each do |id|
    m = CabooseRets::Media.find(id)
    (m.image.styles.keys+[:original]).each { |style| b.objects[m.image.path(style)].rename_to "#{m.image.path(style)}.temp" } if m.media_type == 'Photo'
    b.objects[m.file.path].rename_to "#{m.file.path}.temp" if m.media_type == 'File'
  end

  # Rename the assets to their new names
  i = 1
  j = 1
  media_ids.each do |id|
    m = CabooseRets::Media.find(id)
    if m.media_type == 'Photo'
      orig_name = "#{m.image.path}.temp"
      m.media_order = i
      (m.image.styles.keys+[:original]).each { |style| b.objects[orig_name.gsub("original", style.to_s)].rename_to m.image.path(style) }
      m.save
      i = i + 1
    elsif m.media_type == 'File'
      b.objects["#{m.file.path}.temp"].rename_to m.file.path
      m.media_order = j
      m.save
      j = j + 1
    end
  end

  return true
end

Instance Method Details

#image_url(style) ⇒ Object



133
134
135
136
137
138
139
# File 'app/models/caboose_rets/media.rb', line 133

def image_url(style)
  if CabooseRets::use_hosted_images == true
    return "#{CabooseRets::media_base_url}/#{self.file_name}"
  end
  return "" if self.image.nil?
  return self.image.url(style)
end

#lowercase_file_namesObject



37
38
39
40
# File 'app/models/caboose_rets/media.rb', line 37

def lowercase_file_names
  self.image.instance_write(:file_name, self.image_file_name.downcase) if self.image_file_name
  self.file.instance_write(:file_name, self.file_file_name.downcase)   if self.file_file_name
end

#parse(data, content) ⇒ Object

attr_accessible :date_modified, :file_name, :media_id, :media_order, :media_remarks, :media_type, :mls, :url



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'app/models/caboose_rets/media.rb', line 18

def parse(data,content)
   # self.date_modified  = data['DATE_MODIFIED']
   # self.file_name 		  = data['FILE_NAME']
   # self.media_id 		  = data['MEDIA_ID']
   # self.media_order 	  = data['MEDIA_ORDER'].to_i
   # self.media_remarks  = data['MEDIA_REMARKS']    
   # self.mls 		        = data['MLS']
   self.media_type      = data['MediaCategory']
   self.media_mui       = data['content-id']
   self.media           = Caboose::Media.create(
                 :image => content,
   #               :image => content,
                 :image_content_type => data['content-type']
         )
   # self.media.save      
end