Class: Asset

Inherits:
ActiveRecord::Base
  • Object
show all
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



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

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



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

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

.known_typesObject



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

def known_types
  AssetType.known_types
end

.ransackable_attributes(auth_object = nil) ⇒ Object



59
60
61
# File 'app/models/asset.rb', line 59

def self.ransackable_attributes(auth_object = nil)
  %w[asset_content_type asset_file_name asset_file_size caption created_at created_by_id id original_extension original_height original_width title updated_at updated_by_id uuid]
end

.with_asset_types(asset_types, &block) ⇒ Object



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

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



131
132
133
# File 'app/models/asset.rb', line 131

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

#asset_typeObject



45
46
47
# File 'app/models/asset.rb', line 45

def asset_type
  AssetType.for(asset)
end

#asset_variant(style_name) ⇒ Object



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

def asset_variant(style_name)
  case style_name
  when 'thumbnail'
    asset.variant(gravity: 'Center', resize: '100x100^', crop: '100x100+0+0')
  when 'small'
    asset.variant(gravity: 'Center', resize: '320x320^')
  when 'normal'
    asset.variant(gravity: 'Center', resize_to_limit: [asset.[:width], asset.[:height]])
  when 'icon'
    asset.variant(gravity: 'Center', resize: '50x50^')
  end
end

#attached_to?(page) ⇒ Boolean

Returns:

  • (Boolean)


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

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

#basenameObject



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

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

#dimensions_known?Boolean

Returns:

  • (Boolean)


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

def dimensions_known?
  original_width? && original_height?
end

#extension(style_name = 'original') ⇒ Object



88
89
90
91
92
93
94
95
96
# File 'app/models/asset.rb', line 88

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

#geometry(style_name = 'original') ⇒ Object



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

def geometry(style_name = 'original')
  unless style?(style_name)
    raise Paperclip::StyleError,
          "Requested style #{style_name} is not defined for this asset."
  end

  @geometry ||= {}
  begin
    @geometry[style_name] ||= if style_name.to_s == 'original'
                                original_geometry
                              else
                                style = 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

#height(style_name = 'original') ⇒ Object



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

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

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

Returns:

  • (Boolean)


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

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

#orientation(style_name = 'original') ⇒ Object



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

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

#original_extensionObject



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

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

#original_geometryObject



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

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

#render_original(style_name) ⇒ Object



63
64
65
# File 'app/models/asset.rb', line 63

def render_original(style_name)
  style_name.to_s == 'original' && asset.key.include?('culturaldistrict')
end

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

Returns:

  • (Boolean)


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

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

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

Returns:

  • (Boolean)


80
81
82
# File 'app/models/asset.rb', line 80

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

#thumbnail(style_name = 'original') ⇒ Object



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

def thumbnail(style_name = 'original')
  return asset.url if style_name.to_s == 'original' || render_original(style_name)
  return asset_variant(style_name.to_s).processed.url if asset.variable?

  asset_type.icon(style_name.to_s)
end

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

Returns:

  • (Boolean)


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

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

#width(style_name = 'original') ⇒ Object



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

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