Sha256: 14c6a06e6a20a2c1e06de60744297c47caebd8b9b3fb5db3188e629afd303b28

Contents?: true

Size: 1.48 KB

Versions: 2

Compression:

Stored size: 1.48 KB

Contents

module HasUuid
  module ActiveRecord
    module Associations
      module Builder
        module SingularAssociation
          extend ActiveSupport::Concern

          def self.included(base)
            if ::ActiveRecord::VERSION::STRING >= "4.1"
              base.extend RedefinedWriter
              base.class_eval do
                class << self
                  alias_method_chain :define_writers, :uuid_args
                end
              end
            else
              base.class_eval do
                include RedefinedWriter
                alias_method_chain :define_writers, :uuid_no_args
              end
            end
          end

          module RedefinedWriter
            def define_writers_with_uuid(mixin, name)
              mixin.class_eval <<-CODE, __FILE__, __LINE__ + 1
                def #{name.to_s.singularize}_uuid=(uuids)
                  association(:#{name}).uuid_writer(uuids)
                end

                def #{name.to_s.singularize}_uuid
                  association(:#{name}).uuid_reader
                end
              CODE
            end

            def define_writers_with_uuid_args(mixin, name)
              define_writers_without_uuid_args(mixin, name)
              define_writers_with_uuid(mixin, name)
            end

            def define_writers_with_uuid_no_args
              define_writers_without_uuid_no_args
              define_writers_with_uuid(mixin, name)
            end
          end
        end
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
rails_has_uuid-0.3.0 lib/has_uuid/active_record/associations/builder/singular_association.rb
rails_has_uuid-0.2.0 lib/has_uuid/active_record/associations/builder/singular_association.rb