Sha256: 3134ab261b96c167965c3746f128552f3852ea3ea70cfebdfab2302d5236ab4e

Contents?: true

Size: 1020 Bytes

Versions: 2

Compression:

Stored size: 1020 Bytes

Contents

module Soulless
  module Associations
    def self.included(base)
      base.instance_eval do |object|
        def has_one(name, superclass = Object, &block)
          klass = define_virtus_class(name, superclass, &block)
          send(:attribute, name, klass)
        end
        
        def has_many(name, superclass = Object, &block)
          klass = define_virtus_class(name, superclass, &block)
          send(:attribute, name, Array[klass])
        end
        
        private
        def define_virtus_class(name, superclass, &block)
          klass_name = name.to_s.singularize.classify + '_' + SecureRandom.hex
          klass = const_set(klass_name, Class.new(superclass))
          klass.send(:include, Soulless.model) unless klass.included_modules.include?(Model)
          klass.instance_eval(&block) if block_given?
          klass.model_name.instance_variable_set(:@i18n_key, klass.model_name.i18n_key.to_s.gsub(/_[^_]+$/, '').underscore.to_sym)
          klass
        end
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
soulless-0.3.0 lib/soulless/associations.rb
soulless-0.2.0 lib/soulless/associations.rb