Sha256: 8df505b46b2f25a23d7ac42509e4e7ca523f659d291ee50131dde1d67c24d276

Contents?: true

Size: 995 Bytes

Versions: 6

Compression:

Stored size: 995 Bytes

Contents

module RailsOps
  module ModelMixins
    module VirtualHasOne
      extend ActiveSupport::Concern

      module ClassMethods
        # TODO: Passing type Fixnum currently requires a monkey-patch of ActiveType.
        # This would need to be changed when releasing this functionality as a Gem.
        # See config/initializers/patch_active_type.rb and
        # https://github.com/remofritzsche/active_type/commit/fb8c2cb4cccaaec
        #
        # TODO: Document.
        def virtual_has_one(name, base_class, required: false, accessible: true, default: nil, type: Integer)
          fk = "#{name}_id"
          attribute fk, type, default: default

          if required
            validates fk, presence: true
          end

          if accessible
            attr_accessible name, fk
          end

          belongs_to name, anonymous_class: base_class, foreign_key: fk, class_name: base_class.name

          return reflect_on_association(name)
        end
      end
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
rails_ops-1.0.0.beta6 lib/rails_ops/model_mixins/virtual_has_one.rb
rails_ops-1.0.0.beta5 lib/rails_ops/model_mixins/virtual_has_one.rb
rails_ops-1.0.0.beta4 lib/rails_ops/model_mixins/virtual_has_one.rb
rails_ops-1.0.0.beta3 lib/rails_ops/model_mixins/virtual_has_one.rb
rails_ops-1.0.0.beta2 lib/rails_ops/model_mixins/virtual_has_one.rb
rails_ops-1.0.0.beta1 lib/rails_ops/model_mixins/virtual_has_one.rb