Sha256: d43c3f3459d8eb96b66f536a43d05cf8821f23d3a5ec5bd3b2291f87cb5052af
Contents?: true
Size: 1.38 KB
Versions: 4
Compression:
Stored size: 1.38 KB
Contents
module Concerns module Storable class Collection def initialize(parent, category, config) @parent = parent @category = category init_attributes(config) init_values(config) end def save attributes.each do |name,value| tmp = ::Store.find_or_initialize_by( collection: @category, storable_type: @parent.class.name, storable_id: @parent.id, name: name ) tmp.value = value tmp.save! end end def delete ::Store.where( storable_type: @parent.class, storable_id: @parent.id, collection: @category, ).each { |s| s.destroy } reset_values end private def init_attributes(config) extend(Virtus.model) config.fields.each do |field,options| attribute field, options[:type], default: options[:default], lazy: true end end def init_values(config) ::Store.where( storable_type: @parent.class, storable_id: @parent.id, collection: @category, ).pluck(:name, :value).each do |field, value| send "#{field}=", value if config.allowed?(field) end end def reset_values attributes.keys.each { |k| send("#{k}=", nil) } true end end end end
Version data entries
4 entries across 4 versions & 1 rubygems