Sha256: 8505a03c043c8e3ad2a8d1da1785757ca9226bd0c8db69e1a894256c820827a0

Contents?: true

Size: 901 Bytes

Versions: 27

Compression:

Stored size: 901 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, default: nil, type: Integer)
          fk = "#{name}_id"
          attribute fk, type, default: default

          if required
            validates fk, presence: true
          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

27 entries across 27 versions & 1 rubygems

Version Path
rails_ops-1.0.17.1 lib/rails_ops/model_mixins/virtual_has_one.rb
rails_ops-1.0.17 lib/rails_ops/model_mixins/virtual_has_one.rb
rails_ops-1.0.16 lib/rails_ops/model_mixins/virtual_has_one.rb
rails_ops-1.0.15 lib/rails_ops/model_mixins/virtual_has_one.rb
rails_ops-1.0.14 lib/rails_ops/model_mixins/virtual_has_one.rb
rails_ops-1.0.13 lib/rails_ops/model_mixins/virtual_has_one.rb
rails_ops-1.0.12 lib/rails_ops/model_mixins/virtual_has_one.rb
rails_ops-1.0.11 lib/rails_ops/model_mixins/virtual_has_one.rb
rails_ops-1.0.10 lib/rails_ops/model_mixins/virtual_has_one.rb
rails_ops-1.0.9 lib/rails_ops/model_mixins/virtual_has_one.rb
rails_ops-1.0.7 lib/rails_ops/model_mixins/virtual_has_one.rb
rails_ops-1.0.6 lib/rails_ops/model_mixins/virtual_has_one.rb
rails_ops-1.0.5 lib/rails_ops/model_mixins/virtual_has_one.rb
rails_ops-1.0.4 lib/rails_ops/model_mixins/virtual_has_one.rb
rails_ops-1.0.3 lib/rails_ops/model_mixins/virtual_has_one.rb
rails_ops-1.0.2 lib/rails_ops/model_mixins/virtual_has_one.rb
rails_ops-1.0.1 lib/rails_ops/model_mixins/virtual_has_one.rb
rails_ops-1.0.0 lib/rails_ops/model_mixins/virtual_has_one.rb
rails_ops-1.0.0.beta15 lib/rails_ops/model_mixins/virtual_has_one.rb
rails_ops-1.0.0.beta14 lib/rails_ops/model_mixins/virtual_has_one.rb