Sha256: fca19fbdb896cf2ba1a380806328199abd5c6694a4e3274432194a70b8db6c11

Contents?: true

Size: 1.75 KB

Versions: 20

Compression:

Stored size: 1.75 KB

Contents

require 'synchronisable/dsl/associations/has_one'
require 'synchronisable/dsl/associations/has_many'
require 'synchronisable/dsl/associations/belongs_to'

module Synchronisable
  module DSL
    module Associations
      extend ActiveSupport::Concern

      included do
        class_attribute :associations
        self.associations = {}
      end

      module ClassMethods
        def inherited(subclass)
          super
          subclass.associations = {}
        end

        [HasOne, HasMany, BelongsTo].each do |klass|
          macro = klass.to_s.demodulize.underscore.to_sym
          define_method(macro) do |name, options = {}|
            klass.create(self, name, options)
          end
        end

        # Builds hash with association as key and array of ids as value.
        #
        # @param attrs [Hash] local record attributes
        #
        # @return [Hash<Synchronisable::Association, Array>] associations hash
        #
        # @raise [MissedAssocationsError] raised when the given
        #   attributes hash doesn't required associations
        def associations_for(attrs)
          ensure_required_associations(attrs)

          intersection = self.associations.map { |key, _| key } & attrs.keys
          Hash[intersection.map { |key| [self.associations[key], [*attrs[key]]] }]
        end

        private

        def ensure_required_associations(attrs)
          missing = required_associations - attrs.keys
          if missing.present?
            raise MissedAssociationsError, I18n.t(
              'errors.missed_associations',
              keys: missing, attrs: attrs
            )
          end
        end

        def required_associations
          self.associations.select { |_, a| a.required }.keys
        end
      end
    end
  end
end

Version data entries

20 entries across 20 versions & 1 rubygems

Version Path
synchronisable-1.1.9 lib/synchronisable/dsl/associations.rb
synchronisable-1.1.8 lib/synchronisable/dsl/associations.rb
synchronisable-1.1.7 lib/synchronisable/dsl/associations.rb
synchronisable-1.1.6 lib/synchronisable/dsl/associations.rb
synchronisable-1.1.5 lib/synchronisable/dsl/associations.rb
synchronisable-1.1.4 lib/synchronisable/dsl/associations.rb
synchronisable-1.1.3 lib/synchronisable/dsl/associations.rb
synchronisable-1.1.2 lib/synchronisable/dsl/associations.rb
synchronisable-1.1.1 lib/synchronisable/dsl/associations.rb
synchronisable-1.1.0 lib/synchronisable/dsl/associations.rb
synchronisable-1.0.9 lib/synchronisable/dsl/associations.rb
synchronisable-1.0.8 lib/synchronisable/dsl/associations.rb
synchronisable-1.0.7 lib/synchronisable/dsl/associations.rb
synchronisable-1.0.6 lib/synchronisable/dsl/associations.rb
synchronisable-1.0.5 lib/synchronisable/dsl/associations.rb
synchronisable-1.0.4 lib/synchronisable/dsl/associations.rb
synchronisable-1.0.3 lib/synchronisable/dsl/associations.rb
synchronisable-1.0.2 lib/synchronisable/dsl/associations.rb
synchronisable-1.0.1 lib/synchronisable/dsl/associations.rb
synchronisable-1.0.0 lib/synchronisable/dsl/associations.rb