Sha256: 963412c6bb2fa43ccbe9db04d87539dfb4094515c465f97a2b7caafb42351c23

Contents?: true

Size: 1.92 KB

Versions: 2

Compression:

Stored size: 1.92 KB

Contents

module Findable
  module Associations
    module ActiveRecordExt
      extend ActiveSupport::Concern

      module ClassMethods
        def has_many(*args)
          name, options = Utils.parse_args(args)
          model = Utils.model_for(name, collection: true, safe: true, **options)

          if model && model < Findable::Base
            foreign_key = options[:foreign_key].presence || model_name.name.foreign_key

            define_method(name) do
              model.where(foreign_key => public_send(self.class.primary_key))
            end

            Utils.add_reflection(:has_many, name.to_sym, options, self)
          else
            super
          end
        end

        def has_one(*args)
          name, options = Utils.parse_args(args)
          model = Utils.model_for(name, safe: true, **options)

          if model && model < Findable::Base
            foreign_key = options[:foreign_key].presence || model_name.name.foreign_key

            define_method(name) do
              model.find_by(foreign_key => public_send(self.class.primary_key))
            end

            Utils.add_reflection(:has_one, name.to_sym, options, self)
          else
            super
          end
        end

        def belongs_to(*args)
          name, options = Utils.parse_args(args)
          model = Utils.model_for(name, safe: true, **options)

          if model && model < Findable::Base
            foreign_key = options[:foreign_key].presence || name.to_s.foreign_key

            define_method(name) do
              model.find_by(model.primary_key => public_send(foreign_key))
            end

            define_method("#{name}=") do |value|
              value = value ? value.public_send(model.primary_key) : nil
              public_send("#{foreign_key}=", value)
            end

            Utils.add_reflection(:belongs_to, name.to_sym, options, self)
          else
            super
          end
        end
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
findable-0.1.5 lib/findable/associations/active_record_ext.rb
findable-0.1.4 lib/findable/associations/active_record_ext.rb