Class: Asset

Inherits:
ActiveRecord::Base
  • Object
show all
Includes:
Paperclip::Glue
Defined in:
app/models/asset.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.count_with_asset_types(asset_types, *args) ⇒ Object



208
209
210
# File 'app/models/asset.rb', line 208

def count_with_asset_types(asset_types, *args)
  with_asset_types(asset_types) { where(*args).count }
end

.find_all_by_asset_types(asset_types, *args) ⇒ Object

searching and pagination moved to the controller



204
205
206
# File 'app/models/asset.rb', line 204

def find_all_by_asset_types(asset_types, *args)
  with_asset_types(asset_types) { where *args }
end

.known_typesObject



198
199
200
# File 'app/models/asset.rb', line 198

def known_types
  AssetType.known_types
end

.with_asset_types(asset_types, &block) ⇒ Object



212
213
214
215
# File 'app/models/asset.rb', line 212

def with_asset_types(asset_types, &block)
  w_asset_types = AssetType.conditions_for(asset_types)
  with_scope(where(:conditions => ["#{w_asset_types} = ?", block]))
end

Instance Method Details

#aspect(style_name = 'original') ⇒ Object



129
130
131
# File 'app/models/asset.rb', line 129

def aspect(style_name='original')
  geometry(style_name).aspect
end

#asset_typeObject



72
73
74
# File 'app/models/asset.rb', line 72

def asset_type
  AssetType.for(asset)
end

#attached_to?(page) ⇒ Boolean

Returns:

  • (Boolean)


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

def attached_to?(page)
  pages.include?(page)
end

#basenameObject



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

def basename
  File.basename(asset_file_name, ".*") if asset_file_name
end

#dimensions_known?Boolean

Returns:

  • (Boolean)


167
168
169
# File 'app/models/asset.rb', line 167

def dimensions_known?
  original_width? && original_height?
end

#extension(style_name = 'original') ⇒ Object



91
92
93
94
95
96
97
98
99
# File 'app/models/asset.rb', line 91

def extension(style_name='original')
   if style_name == 'original'
     return original_extension
   elsif style = paperclip_styles[style_name.to_sym]
     return style.format
   else
     return original_extension
   end
end

#geometry(style_name = 'original') ⇒ Object

Raises:

  • (Paperclip::StyleError)


113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'app/models/asset.rb', line 113

def geometry(style_name='original')
  raise Paperclip::StyleError, "Requested style #{style_name} is not defined for this asset." unless has_style?(style_name)
  @geometry ||= {}
  begin
    @geometry[style_name] ||= if style_name.to_s == 'original'
      original_geometry
    else
      style = self.asset.styles[style_name.to_sym]
      original_geometry.transformed_by(style.geometry)    # this can return dimensions for fully specified style sizes but not for relative sizes when there are no original dimensions
    end
  rescue Paperclip::TransformationError => e
    Rails.logger.warn "geometry transformation error: #{e}"
    original_geometry                                     # returns a blank geometry if the real geometry cannot be calculated
  end
end

#has_style?(style_name = 'original') ⇒ Boolean

Returns:

  • (Boolean)


83
84
85
# File 'app/models/asset.rb', line 83

def has_style?(style_name='original')
  style_name == 'original' || paperclip_styles.keys.include?(style_name.to_sym)
end

#height(style_name = 'original') ⇒ Object



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

def height(style_name='original')
  geometry(style_name).height.to_i
end

#horizontal?(style_name = 'original') ⇒ Boolean

Returns:

  • (Boolean)


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

def horizontal?(style_name='original')
  geometry(style_name).horizontal?
end

#orientation(style_name = 'original') ⇒ Object



133
134
135
136
137
138
139
140
141
142
143
144
145
# File 'app/models/asset.rb', line 133

def orientation(style_name='original')
  a = aspect(style_name)
  case
  when a == nil?
    'unknown'
  when a < 1.0
    'vertical'
  when a > 1.0
    'horizontal'
  else
    'square'
  end
end

#original_extensionObject



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

def original_extension
  return asset_file_name.split('.').last.downcase if asset_file_name
end

#original_geometryObject



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

def original_geometry
  @original_geometry ||= Paperclip::Geometry.new(original_width, original_height)
end

#square?(style_name = 'original') ⇒ Boolean

Returns:

  • (Boolean)


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

def square?(style_name='original')
  geometry(style_name).square?
end

#thumbnail(style_name = 'original') ⇒ Object



77
78
79
80
81
# File 'app/models/asset.rb', line 77

def thumbnail(style_name='original')
  return asset.url if style_name.to_sym == :original
  return asset.url(style_name.to_sym) if has_style?(style_name)
  return asset_type.icon(style_name)
end

#vertical?(style_name = 'original') ⇒ Boolean

Returns:

  • (Boolean)


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

def vertical?(style_name='original')
  geometry(style_name).vertical?
end

#width(style_name = 'original') ⇒ Object



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

def width(style_name='original')
  geometry(style_name).width.to_i
end