Class: Concerns::Storable::Collection

Inherits:
Object
  • Object
show all
Defined in:
app/models/concerns/storable/collection.rb

Instance Method Summary collapse

Constructor Details

#initialize(parent, category, config) ⇒ Collection

Returns a new instance of Collection.



4
5
6
7
8
9
10
11
# File 'app/models/concerns/storable/collection.rb', line 4

def initialize(parent, category, config)
  @parent = parent
  @category = category
  @config = config

  init_attributes
  init_values
end

Instance Method Details

#define_reader_method_for_uploader_field(field, options) ⇒ Object



37
38
39
40
41
42
# File 'app/models/concerns/storable/collection.rb', line 37

def define_reader_method_for_uploader_field(field, options)
  self.class.send(:define_method, field) do
    StoreWithFile.mount_uploader :value, options[:type]
    store(field, file: true).value
  end
end

#define_writer_method_for_uploader_field(field, options) ⇒ Object



44
45
46
47
48
49
50
51
52
53
# File 'app/models/concerns/storable/collection.rb', line 44

def define_writer_method_for_uploader_field(field, options)
  self.class.send(:define_method, "#{field}=") do |value|
    # Right now, when we save a form with a filefield, you will
    # notice in the database that the store value keeps updating
    # with a new filename hash. It always "reuploads" the file.
    s = store(field, file: true)
    s.value = value
    s.save
  end
end

#deleteObject



13
14
15
16
# File 'app/models/concerns/storable/collection.rb', line 13

def delete
  stores.each { |s| s.destroy }
  reset_values
end

#saveObject



18
19
20
21
22
23
24
# File 'app/models/concerns/storable/collection.rb', line 18

def save
  attributes.each do |name,value|
    tmp = store(name)
    tmp.value = value
    tmp.save!
  end
end

#store(name, file: false) ⇒ Object



26
27
28
# File 'app/models/concerns/storable/collection.rb', line 26

def store(name, file: false)
  stores(file: file).find_or_initialize_by(name: name)
end

#stores(file: false) ⇒ Object

You’ll find that mounting an uploader on Store will prevent the saving of other attributes.



32
33
34
35
# File 'app/models/concerns/storable/collection.rb', line 32

def stores(file: false)
  klass = file ? StoreWithFile : Store
  klass.where(storable: @parent, collection: @category)
end