Class: MediaItem

Inherits:
Forest::ApplicationRecord show all
Includes:
Attachable, Forest::CacheBuster, Sluggable
Defined in:
app/models/media_item.rb

Constant Summary collapse

DATE_FILTER_CACHE_KEY =
'forest_media_item_dates_for_filter'
CONTENT_TYPE_CACHE_KEY =
'forest_media_item_content_types_for_filter'

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Forest::ApplicationRecord

cache_key, cache_key_name, csv_columns, #expire_cache_key, expire_cache_key, statusable?, #statusable?, to_csv_template, #to_friendly_param, #to_label, versionable, #versionable

Class Method Details

.content_types_for_filterObject



42
43
44
45
46
# File 'app/models/media_item.rb', line 42

def self.content_types_for_filter
  Rails.cache.fetch CONTENT_TYPE_CACHE_KEY do
    self.grouped_by_content_type.collect { |x| x.attachment_content_type }
  end
end

.dates_for_filterObject



36
37
38
39
40
# File 'app/models/media_item.rb', line 36

def self.dates_for_filter
  Rails.cache.fetch DATE_FILTER_CACHE_KEY do
    self.grouped_by_year_month.collect { |x| [x.created_at.strftime('%B %Y'), x.created_at.strftime('%d-%m-%Y')] }.reverse
  end
end

.expire_cache!Object



48
49
50
51
# File 'app/models/media_item.rb', line 48

def self.expire_cache!
  Rails.cache.delete self::DATE_FILTER_CACHE_KEY
  Rails.cache.delete self::CONTENT_TYPE_CACHE_KEY
end

.resource_descriptionObject



32
33
34
# File 'app/models/media_item.rb', line 32

def self.resource_description
  'Media items consist of image, video and other file uploads.'
end

Instance Method Details

#aspect_ratioObject

Portrait images have a lower aspect ratio



101
102
103
# File 'app/models/media_item.rb', line 101

def aspect_ratio
  dimensions[:width].to_f / dimensions[:height].to_f
end

#generate_slugObject



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'app/models/media_item.rb', line 53

def generate_slug
  if self.slug.blank? || changed.include?('slug')
    if title.present?
      slug_attribute = title
    elsif attachment_file_name.present?
      slug_attribute = attachment_file_name
    else
      slug_attribute = SecureRandom.uuid
    end

    slug_attribute = slug_attribute.parameterize

    if MediaItem.where(slug: slug_attribute).present?
      slug_attribute = slug_attribute + '-' + SecureRandom.uuid
    end

    self.slug = slug_attribute
  end
end

#glyphiconObject



73
74
75
76
77
78
79
80
81
82
83
# File 'app/models/media_item.rb', line 73

def glyphicon
  if image?
    'glyphicon-picture'
  elsif video?
    'glyphicon-play'
  elsif attachment_content_type == 'application/zip'
    'glyphicon-folder-close'
  else
    'glyphicon-file'
  end
end

#landscape?(ratio = 1) ⇒ Boolean

Returns:

  • (Boolean)


105
106
107
# File 'app/models/media_item.rb', line 105

def landscape?(ratio = 1)
  aspect_ratio > ratio
end

#portrait?(ratio = 1) ⇒ Boolean

Returns:

  • (Boolean)


109
110
111
# File 'app/models/media_item.rb', line 109

def portrait?(ratio = 1)
  !landscape?(ratio)
end

#to_jq_uploadObject



85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'app/models/media_item.rb', line 85

def to_jq_upload
  {
    'id': self.id,
    'name': read_attribute(:title),
    'file_name': attachment_file_name,
    'is_image': image?,
    'glyphicon': glyphicon,
    'size': attachment.size,
    'url': edit_admin_media_item_path(self),
    'thumbnail_url': attachment.url(:medium),
    'delete_url': admin_media_item_path(id: id),
    'delete_type': 'DELETE'
  }
end

#to_select2_responseObject



113
114
115
116
# File 'app/models/media_item.rb', line 113

def to_select2_response
  img_tag = "<img src='#{attachment.url(:thumb)}' style='height: 50px;'> " if image? && attachment.present?
  "#{img_tag}<span class='select2-response__id'>#{id}</span> #{to_label}"
end

#to_select2_selectionObject



118
119
120
121
# File 'app/models/media_item.rb', line 118

def to_select2_selection
  img_tag = "<img src='#{attachment.url(:thumb)}' style='height: 20px; display: inline-block; vertical-align: top;'> " if image? && attachment.present?
  "#{img_tag}<span class='select2-response__id'>#{id}</span> #{to_label}"
end