module Attachy::Extension
Public Instance Methods
attachies_for(data, scope)
click to toggle source
# File lib/attachy/models/attachy/extension.rb, line 14 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
attachy_class()
click to toggle source
# File lib/attachy/models/attachy/extension.rb, line 10 def attachy_class Attachy::File end
define_attachy(scope, options)
click to toggle source
# File lib/attachy/models/attachy/extension.rb, line 42 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
fields()
click to toggle source
# File lib/attachy/models/attachy/extension.rb, line 26 def fields @fields ||= attachy_class.column_names.map(&:to_sym) end
has_attachment(scope, options = {})
click to toggle source
# File lib/attachy/models/attachy/extension.rb, line 32 def has_attachment(scope, options = {}) define_attachy scope, options.merge(multiple: false) end
has_attachments(scope, options = {})
click to toggle source
# File lib/attachy/models/attachy/extension.rb, line 36 def has_attachments(scope, options = {}) define_attachy scope, options.merge(multiple: true) end