Class: Asset

Inherits:
Object
  • Object
show all
Includes:
MongoSearch::Searchable, Mongoid::Document, Mongoid::Paperclip, Mongoid::Timestamps
Defined in:
app/models/asset.rb

Constant Summary collapse

IMAGE_REGEX =
/(jpg|jpeg|gif|png)/
NORMALIZED_EXTENSIONS =
{ ".jpeg" => ".jpg" }

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.make(*args) ⇒ Object



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

def self.make(*args)
  Slices::Asset::Maker.run(*args)
end

.ordered_activeObject



61
62
63
# File 'app/models/asset.rb', line 61

def self.ordered_active
  where(destroyed_at: nil).desc(:created_at)
end

.search_for(term = nil) ⇒ Object



53
54
55
56
57
58
59
# File 'app/models/asset.rb', line 53

def self.search_for(term = nil)
  if term.present?
    all.ordered_active.text_search term
  else
    all.ordered_active
  end
end

Instance Method Details

#admin_image_urlObject



136
137
138
139
140
# File 'app/models/asset.rb', line 136

def admin_image_url
  if !new_record? && is_image?
    url_for(:admin)
  end
end

#as_json(options = nil) ⇒ Object



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'app/models/asset.rb', line 65

def as_json(options = nil)
  {
    id:                id.to_s,
    name:              name,
    file_file_name:    file_file_name,
    file_file_size:    file_file_size,
    file_content_type: file_content_type,
    asset_url:         admin_image_url,
    original_url:      file.url(:original),
    errors:            errors,
    created_at:        created_at,
    tags:              tags,
    pages:             page_cache
  }
end

#dimensions_for(style) ⇒ Object

Returns the dimensions the asset style. The style is created if it has not already been generated

asset.dimensions_for(:main)
"100x100"

Parameters:

  • style (Symbol)

    The asset style is defined in Slice::Config.asset_styles



97
98
99
100
# File 'app/models/asset.rb', line 97

def dimensions_for(style)
  reprocess_for(style)
  file_dimensions[style.to_s]
end

#file_extensionObject



128
129
130
131
132
133
134
# File 'app/models/asset.rb', line 128

def file_extension
  if file_file_name.present?
    file_file_name.split('.').last.downcase
  else
    ''
  end
end

#is_image?Boolean

Returns:

  • (Boolean)


124
125
126
# File 'app/models/asset.rb', line 124

def is_image?
  file_content_type.present? && file_content_type.match(IMAGE_REGEX).present?
end

#nameObject



154
155
156
# File 'app/models/asset.rb', line 154

def name
  @new_name || file_file_name
end

#name=(new_name) ⇒ Object



158
159
160
# File 'app/models/asset.rb', line 158

def name=(new_name)
  @new_name = new_name
end

#reprocess_for(style) ⇒ Object



102
103
104
105
106
107
108
# File 'app/models/asset.rb', line 102

def reprocess_for(style)
  if file.styles.has_key?(style) && ! file_dimensions.has_key?(style.to_s)
    file.reprocess!(style)
    save
  end
rescue Errors::NotIdentifiedByImageMagickError, Errno::ENOENT
end

#reprocess_for!(style) ⇒ Object



110
111
112
113
114
115
# File 'app/models/asset.rb', line 110

def reprocess_for!(style)
  file.clear(style)
  file_dimensions.delete(style.to_s)
  file.flush_deletes
  reprocess_for(style)
end

#reset_file_dimensions!Object

Reset all stored file dimensions except for the origin and admin styles, this is used when a new version of an asset is uploaded



178
179
180
181
182
183
# File 'app/models/asset.rb', line 178

def reset_file_dimensions!
  self.file_dimensions.keys.each do |key|
    next if key == 'original' || key == 'admin'
    self.file_dimensions.delete key
  end
end

#search_page_cacheObject



168
169
170
171
172
173
# File 'app/models/asset.rb', line 168

def search_page_cache
  page_cache.inject([]) do |memo, page|
    memo << page['name']
    memo << page['path']
  end.join ' '
end

#soft_destroy!Object



142
143
144
# File 'app/models/asset.rb', line 142

def soft_destroy!
  update_attributes!(destroyed_at: Time.now)
end

#soft_destroyed?Boolean

Returns:

  • (Boolean)


146
147
148
# File 'app/models/asset.rb', line 146

def soft_destroyed?
  destroyed_at.present?
end

#soft_restore!Object



150
151
152
# File 'app/models/asset.rb', line 150

def soft_restore!
  update_attributes!(destroyed_at: nil)
end

#store_dimensionsObject



117
118
119
120
121
122
# File 'app/models/asset.rb', line 117

def store_dimensions
  file.queued_for_write.each do |style, adapter|
    geometry = Paperclip::Geometry.from_file(adapter)
    self.file_dimensions[style.to_s] = geometry.to_s
  end
end

#update_page_cacheObject



162
163
164
165
166
# File 'app/models/asset.rb', line 162

def update_page_cache
  self.page_cache = pages.collect do |page|
    { id: page.id, name: page.name, path: page.path }
  end
end

#url_for(style) ⇒ Object

Returns the url for the asset style. The style is created if it has not already been generated

Parameters:

  • style (Symbol)

    The asset style is defined in Slice::Config.asset_styles



85
86
87
88
# File 'app/models/asset.rb', line 85

def url_for(style)
  reprocess_for(style)
  file.url(style)
end