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



96
97
98
# File 'lib/asset_manager/active_record.rb', line 96

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



91
92
93
94
# File 'lib/asset_manager/active_record.rb', line 91

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

.am_file_fieldsObject



66
67
68
# File 'lib/asset_manager/active_record.rb', line 66

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

.am_files_fieldsObject



70
71
72
# File 'lib/asset_manager/active_record.rb', line 70

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

.am_has_field?(field) ⇒ Boolean

Returns:

  • (Boolean)


82
83
84
# File 'lib/asset_manager/active_record.rb', line 82

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

.am_multiple_field?(field) ⇒ Boolean

Returns:

  • (Boolean)


86
87
88
89
# File 'lib/asset_manager/active_record.rb', line 86

def am_multiple_field?(field)
  raise "undefined field #{field}" unless am_has_field?(field)
  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
50
51
52
53
54
55
56
# 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'

  define_method "#{field.to_s.singularize}_ids=" do |ids|
    super(ids)
    ids.each_with_index do |value, index|
      aa = send("asset_association_#{field}").where(asset_id: value).first
      aa.update_attributes(position: (index+1))
    end
  end

  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



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

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



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

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