Module: AbAdmin::Models::Asset

Extended by:
ActiveSupport::Concern
Included in:
Asset
Defined in:
lib/ab_admin/models/asset.rb

Defined Under Namespace

Modules: ClassMethods

Instance Method Summary collapse

Instance Method Details

#as_json(options = nil) ⇒ Object


63
64
65
66
67
68
69
70
71
72
# File 'lib/ab_admin/models/asset.rb', line 63

def as_json(options = nil)
  options = {
      only: [:id, :guid, :assetable_id, :assetable_type, :user_id, :data_file_size, :data_content_type, :is_main,
             :original_name, :cached_alt, :cached_title],
      root: 'asset',
      methods: [:filename, :url, :thumb_url, :width, :height]
  }.merge(options || {})

  super
end

#base_filenameObject


82
83
84
85
# File 'lib/ab_admin/models/asset.rb', line 82

def base_filename
  base = File.basename(data_file_name, '.*')
  base == data_secure_token ? File.basename(original_name, '.*') : base
end

#base_filename=(value) ⇒ Object


87
88
89
90
91
# File 'lib/ab_admin/models/asset.rb', line 87

def base_filename=(value)
  return false if value.blank? || File.basename(data_file_name, '.*') == value
  self.original_name = value + File.extname(data_file_name)
  rename!(value)
end

#build_filename(base_filename, record = nil) ⇒ Object

allow to build custom human file name


46
47
48
# File 'lib/ab_admin/models/asset.rb', line 46

def build_filename(base_filename, record=nil)
  nil
end

#crop!(geometry) ⇒ Object


112
113
114
115
116
117
118
# File 'lib/ab_admin/models/asset.rb', line 112

def crop!(geometry)
  rename!
  self.cropper_geometry = geometry
  save!
  refresh_assetable
  self
end

#cropper_geometry=(value) ⇒ Object


143
144
145
146
147
148
149
150
# File 'lib/ab_admin/models/asset.rb', line 143

def cropper_geometry=(value)
  geometry = (value || '').to_s.split(',')

  unless geometry.map(&:blank?).any?
    @cropper_geometry_changed = true
    @cropper_geometry = geometry
  end
end

#cropper_geometry_changed?Boolean

Returns:

  • (Boolean)

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

def cropper_geometry_changed?
  @cropper_geometry_changed === true
end

#format_created_atObject


55
56
57
# File 'lib/ab_admin/models/asset.rb', line 55

def format_created_at
  I18n.l(created_at, format: '%d.%m.%Y %H:%M')
end

#full_url(*args) ⇒ Object


133
134
135
# File 'lib/ab_admin/models/asset.rb', line 133

def full_url(*args)
  AbAdmin.full_url url(*args)
end

#has_dimensions?Boolean

Returns:

  • (Boolean)

74
75
76
# File 'lib/ab_admin/models/asset.rb', line 74

def has_dimensions?
  respond_to?(:width) && respond_to?(:height)
end

#human_nameObject


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

def human_name
  original_name.presence || data_file_name
end

#image?Boolean

Returns:

  • (Boolean)

78
79
80
# File 'lib/ab_admin/models/asset.rb', line 78

def image?
  AbAdmin.image_types.include?(self.data_content_type)
end

#main!Object


93
94
95
96
97
98
99
100
101
102
# File 'lib/ab_admin/models/asset.rb', line 93

def main!
  cond = {assetable_type: assetable_type, type: type}
  if assetable_id.to_i.nonzero? || guid.presence
    cond.merge!(assetable_id.to_i.zero? ? {guid: guid} : {assetable_id: assetable_id})
    self.class.where(cond).update_all(is_main: false)
  end
  update_column(:is_main, true)
  refresh_assetable
  self
end

#refresh_assetableObject


126
127
128
129
130
131
# File 'lib/ab_admin/models/asset.rb', line 126

def refresh_assetable
  return unless assetable.try(:persisted?)
  assetable.touch
  assetable.tire.update_index if assetable.respond_to?(:tire)
  true
end

#rename!(name = nil) ⇒ Object


120
121
122
123
124
# File 'lib/ab_admin/models/asset.rb', line 120

def rename!(name=nil)
  normalized_name = name ? data.normalize_filename(name) : rand(9999)
  return false if normalized_name.blank?
  data.rename_via_move "#{normalized_name}#{File.extname(data_file_name)}"
end

#rotate!Object


104
105
106
107
108
109
110
# File 'lib/ab_admin/models/asset.rb', line 104

def rotate!
  rename!
  self.rotate_degrees = 90
  save!
  refresh_assetable
  self
end

#rotate_degrees=(value) ⇒ Object


156
157
158
159
160
161
# File 'lib/ab_admin/models/asset.rb', line 156

def rotate_degrees=(value)
  unless value.blank?
    @rotate_degrees_changed = true
    @rotate_degrees = value.to_s
  end
end

#rotate_degrees_changed?Boolean

Returns:

  • (Boolean)

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

def rotate_degrees_changed?
  @rotate_degrees_changed === true
end

#store_model_filename(record) ⇒ Object


41
42
43
# File 'lib/ab_admin/models/asset.rb', line 41

def store_model_filename(record)
  data.store_model_filename(record) and save!(validate: false)
end

#thumb_urlObject


50
51
52
53
# File 'lib/ab_admin/models/asset.rb', line 50

def thumb_url
  return unless image?
  data.versions[thumb_size] ? url(thumb_size) : url
end

#url_on_fly(version) ⇒ Object


137
138
139
140
141
# File 'lib/ab_admin/models/asset.rb', line 137

def url_on_fly(version)
  file_url = url(version)
  data.recreate_versions!(version) unless Rails.root.join("public/#{file_url}").exist?
  file_url
end