Class: Concerns::Storable::Collection
- Inherits:
-
Object
- Object
- Concerns::Storable::Collection
- Defined in:
- app/models/concerns/storable/collection.rb
Instance Method Summary collapse
- #define_reader_method_for_uploader_field(field, options) ⇒ Object
- #define_writer_method_for_uploader_field(field, options) ⇒ Object
- #delete ⇒ Object
-
#initialize(parent, category, config) ⇒ Collection
constructor
A new instance of Collection.
- #save ⇒ Object
- #store(name, file: false) ⇒ Object
-
#stores(file: false) ⇒ Object
You’ll find that mounting an uploader on Store will prevent the saving of other attributes.
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, ) self.class.send(:define_method, field) do StoreWithFile.mount_uploader :value, [: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, ) 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 |
#delete ⇒ Object
13 14 15 16 |
# File 'app/models/concerns/storable/collection.rb', line 13 def delete stores.each { |s| s.destroy } reset_values end |
#save ⇒ Object
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 |