Class: ActiveRecord::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/asset_manager/active_record.rb

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.am_fileObject (readonly)

Returns the value of attribute am_file.



4
5
6
# File 'lib/asset_manager/active_record.rb', line 4

def am_file
  @am_file
end

.am_file_optionsObject (readonly)

Returns the value of attribute am_file_options.



4
5
6
# File 'lib/asset_manager/active_record.rb', line 4

def am_file_options
  @am_file_options
end

.am_filesObject (readonly)

Returns the value of attribute am_files.



4
5
6
# File 'lib/asset_manager/active_record.rb', line 4

def am_files
  @am_files
end

.am_files_optionsObject (readonly)

Returns the value of attribute am_files_options.



4
5
6
# File 'lib/asset_manager/active_record.rb', line 4

def am_files_options
  @am_files_options
end

Class Method Details

.am_field_name(field, multiple) ⇒ Object



84
85
86
# File 'lib/asset_manager/active_record.rb', line 84

def am_field_name(field, multiple)
  (multiple ? "#{field.to_s.singularize}_ids" : "#{field.to_s}_id").to_sym
end

.am_field_option(field, key) ⇒ Object



79
80
81
82
# File 'lib/asset_manager/active_record.rb', line 79

def am_field_option(field, key)
  options = field_options(field)
  options[field.to_sym][key] rescue nil
end

.am_file_fieldsObject



59
60
61
# File 'lib/asset_manager/active_record.rb', line 59

def am_file_fields
  @am_file.nil? ? [] : @am_file
end

.am_files_fieldsObject



63
64
65
# File 'lib/asset_manager/active_record.rb', line 63

def am_files_fields
  @am_files.nil? ? [] : @am_files
end

.am_has_field?(field) ⇒ Boolean

Returns:

  • (Boolean)


75
76
77
# File 'lib/asset_manager/active_record.rb', line 75

def am_has_field?(field)
  am_file_fields.include?(field.to_sym) || am_files_fields.include?(field.to_sym)
end

.has_file(field, options = {}) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/asset_manager/active_record.rb', line 6

def has_file(field, options = {})
  options.assert_valid_keys(:accepted, :type, :mode)
  options.reverse_merge!(type: :public, max: 1, mode: :file)

  @am_file = [] if @am_file.nil?
  @am_file.push(field) unless @am_file.include?(field)
  @am_file_options = {} if @am_file_options.nil?
  @am_file_options[field] = options

  attr_accessible am_field_name(field, false)
  attr_accessor am_field_name(field, false)

  has_one "asset_association_#{field}".to_sym, as: :owner, class_name: 'AssetManager::AssetAssociation', conditions: { context: "#{field}" }
  has_one "#{field}".to_sym, through: "asset_association_#{field}".to_sym, source: :asset, class_name: 'AssetManager::Asset'

  after_save :save_file_associations
  after_destroy :destroy_file_and_files_associations
end

.has_files(field, options = {}) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/asset_manager/active_record.rb', line 33

def has_files(field, options = {})
  options.assert_valid_keys(:accepted, :type, :mode, :max)
  options.reverse_merge!(type: :public, mode: :files, max: 999)

  @am_files = [] if @am_files.nil?
  @am_files.push(field) unless @am_files.include?(field)
  @am_files_options = {} if @am_files_options.nil?
  @am_files_options[field] = options

  attr_accessible am_field_name(field, true)

  has_many "asset_association_#{field}".to_sym, as: :owner, class_name: 'AssetManager::AssetAssociation', conditions: { context: "#{field}" }
  has_many "#{field}".to_sym, through: "asset_association_#{field}".to_sym, source: :asset, class_name: 'AssetManager::Asset'

  after_save :save_files_associations
  after_destroy :destroy_file_and_files_associations
end

.has_image(field, options = {}) ⇒ Object



29
30
31
# File 'lib/asset_manager/active_record.rb', line 29

def has_image(field, options = {})
  has_file(field, options.merge(accepted: AssetManager.default_image_formats, mode: :image))
end

.has_images(field, options = {}) ⇒ Object



55
56
57
# File 'lib/asset_manager/active_record.rb', line 55

def has_images(field, options = {})
  has_files(field, options.merge(accepted: AssetManager.default_image_formats, mode: :images))
end

.has_private_file(field, options = {}) ⇒ Object



25
26
27
# File 'lib/asset_manager/active_record.rb', line 25

def has_private_file(field, options = {})
  has_file(field, options.merge(type: :private))
end

.has_private_files(field, options = {}) ⇒ Object



51
52
53
# File 'lib/asset_manager/active_record.rb', line 51

def has_private_files(field, options = {})
  has_files(field, options.merge(type: :private))
end