Sha256: 708e066dfb0943c669c012b0f44cde05066ebee2224a30138f36e3c5d1eb375c
Contents?: true
Size: 1.79 KB
Versions: 3
Compression:
Stored size: 1.79 KB
Contents
# frozen_string_literal: true module Attachy module Extension extend ActiveSupport::Concern included do private def attachy_class Attachy::File end def attachies_for(data, scope) json = JSON.parse(data, symbolize_names: true) [json].flatten.map do |data| if data[:id] attachy_class.find data[:id] else attachy_class.new data.slice(*fields).merge(scope: scope) end end end def fields @fields ||= attachy_class.column_names.map(&:to_sym) end end class_methods do def has_attachment(scope, options = {}) define_attachy scope, options.merge(multiple: false) end def has_attachments(scope, options = {}) define_attachy scope, options.merge(multiple: true) end private def define_attachy(scope, options) association = "#{scope}_files" has_many association.to_sym, -> { where scope: scope }, as: :attachable, class_name: 'Attachy::File', dependent: :destroy define_method scope do value = send(association) return value if options[:multiple] return Attachy::File.default if value.blank? value.last end define_method "#{scope}=" do |data| return if data.blank? attachies = attachies_for(data, scope) if attachies.present? send "#{association}=", attachies else send(association).destroy_all end end define_method "#{scope}?" do send("#{scope}_files").present? end define_method "#{scope}_metadata" do options.merge scope: scope end end end end end
Version data entries
3 entries across 3 versions & 1 rubygems
Version | Path |
---|---|
attachy-0.4.1 | lib/attachy/models/attachy/extension.rb |
attachy-0.4.0 | lib/attachy/models/attachy/extension.rb |
attachy-0.3.0 | lib/attachy/models/attachy/extension.rb |