class Shrine::Plugins::Hanami::RepositoryMethods

Public Class Methods

new(name, attacher_class) click to toggle source
# File lib/shrine/plugins/hanami.rb, line 51
        def initialize(name, attacher_class)
          class_eval <<-RUBY, __FILE__, __LINE__ + 1
            def self.prepended(base)
              base.send(:include, ::Hanami::Utils::ClassAttribute)
              base.send(:class_attribute, :_attachments)
              base.class_eval do
                self._attachments ||= []
                self._attachments << { name: :#{name}, class: #{attacher_class} }
              end
            end
          RUBY

          class_eval do
            def create(entity)
              save_attachments(entity) { |new_entity| super(new_entity) }
            end

            def update(id, entity)
              save_attachments(entity) { |new_entity| super(id, new_entity) }
            end

            def persist(entity)
              save_attachments(entity) { |new_entity| super(new_entity) }
            end

            def delete(id)
              delete_attachments(id) { super(id) }
            end

            private

            def _attachments
              self.class._attachments
            end

            def save_attachments(entity)
              if !entity.is_a?(::Hanami::Entity)
                entity = self.class.entity.new(entity)
              end

              _attachments.each do |a|
                entity = save_attachment(entity, a[:name], a[:class])
              end

              yield(entity)
            end

            def save_attachment(original_entity, name, uploader_class)
              file = original_entity.send(name)

              if file
                attacher = uploader_class::Attacher.new(OpenStruct.new, name)

                attacher.assign(file)
                attacher.finalize

                original_entity_attributes = original_entity.attributes.dup
                original_entity_attributes.delete(name)

                original_entity.class.new(original_entity_attributes.merge(:"#{name}_data" => attacher.read))
              else
                original_entity
              end
            end

            def delete_attachments(id)
              entity = find(id)
              yield
              _attachments.each do |a|
                entity.send("#{a[:name]}_attacher").destroy
              end
            end
          end
        end

Public Instance Methods

_attachments() click to toggle source
# File lib/shrine/plugins/hanami.rb, line 82
def _attachments
  self.class._attachments
end
create(entity) click to toggle source
Calls superclass method
# File lib/shrine/plugins/hanami.rb, line 64
def create(entity)
  save_attachments(entity) { |new_entity| super(new_entity) }
end
delete(id) click to toggle source
Calls superclass method
# File lib/shrine/plugins/hanami.rb, line 76
def delete(id)
  delete_attachments(id) { super(id) }
end
delete_attachments(id) { || ... } click to toggle source
# File lib/shrine/plugins/hanami.rb, line 116
def delete_attachments(id)
  entity = find(id)
  yield
  _attachments.each do |a|
    entity.send("#{a[:name]}_attacher").destroy
  end
end
persist(entity) click to toggle source
Calls superclass method
# File lib/shrine/plugins/hanami.rb, line 72
def persist(entity)
  save_attachments(entity) { |new_entity| super(new_entity) }
end
save_attachment(original_entity, name, uploader_class) click to toggle source
# File lib/shrine/plugins/hanami.rb, line 98
def save_attachment(original_entity, name, uploader_class)
  file = original_entity.send(name)

  if file
    attacher = uploader_class::Attacher.new(OpenStruct.new, name)

    attacher.assign(file)
    attacher.finalize

    original_entity_attributes = original_entity.attributes.dup
    original_entity_attributes.delete(name)

    original_entity.class.new(original_entity_attributes.merge(:"#{name}_data" => attacher.read))
  else
    original_entity
  end
end
save_attachments(entity) { |entity| ... } click to toggle source
# File lib/shrine/plugins/hanami.rb, line 86
def save_attachments(entity)
  if !entity.is_a?(::Hanami::Entity)
    entity = self.class.entity.new(entity)
  end

  _attachments.each do |a|
    entity = save_attachment(entity, a[:name], a[:class])
  end

  yield(entity)
end
update(id, entity) click to toggle source
Calls superclass method
# File lib/shrine/plugins/hanami.rb, line 68
def update(id, entity)
  save_attachments(entity) { |new_entity| super(id, new_entity) }
end