Sha256: 65528e1ae3399cb76f22cc80dbe94df788771e30a66e24e809301d7a5cf167b0

Contents?: true

Size: 1.25 KB

Versions: 6

Compression:

Stored size: 1.25 KB

Contents

module Basepack
  module Import
    module Importable
      extend ActiveSupport::Concern

      included do
        association_name = Basepack::Settings.import.association_name
        association_name_join_table = Basepack::Settings.import.association_name_join_table
        if association_name
          has_many association_name_join_table,
                   as: :importable, inverse_of: :importable, dependent: :destroy, class_name: 'Basepack::ImportImportable'
          has_many association_name, through: association_name_join_table
        end
      end

      module ClassMethods
        def find_or_initialize_for_import(attrs)
          Basepack::Import::Importable.find_or_initialize_for_import(self, attrs)
        end
        alias :find_or_initialize_for_import! :find_or_initialize_for_import
      end

      def around_import(import, &block)
        yield
      end
      alias :around_import! :around_import

      def self.find_or_initialize_for_import(model, attrs, key = nil)
        key ||= model.primary_key
        if attrs[key]
          object = model.where(key => attrs[key]).first_or_initialize
          object.assign_attributes(attrs.except(key))
          object
        else
          model.new(attrs)
        end
      end
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
basepack-1.0.1 lib/basepack/import/importable.rb
basepack-1.0.0 lib/basepack/import/importable.rb
basepack-1.0.0.pre.0 lib/basepack/import/importable.rb
basepack-0.2.0 lib/basepack/import/importable.rb
basepack-0.1.0 lib/basepack/import/importable.rb
basepack-0.0.2 lib/basepack/import/importable.rb