Sha256: fb4fb4ceb7c9c4288c07027ae719d04ce1a40b7b6bdf629394c37aa8fafe1a14

Contents?: true

Size: 1.21 KB

Versions: 12

Compression:

Stored size: 1.21 KB

Contents

module Soulless
  module Associations
    def self.included(base)
      base.instance_eval do
        @association_attributes = []
        
        class << self
          attr_reader :association_attributes
        end
        
        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)
          @association_attributes << name
          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.send(:attr_accessor, :parent)
          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

12 entries across 12 versions & 1 rubygems

Version Path
soulless-0.6.0 lib/soulless/associations.rb
soulless-0.5.2 lib/soulless/associations.rb
soulless-0.5.1 lib/soulless/associations.rb
soulless-0.5.0 lib/soulless/associations.rb
soulless-0.5.0.rc5 lib/soulless/associations.rb
soulless-0.5.0.rc4 lib/soulless/associations.rb
soulless-0.5.0.rc3 lib/soulless/associations.rb
soulless-0.5.0.rc2 lib/soulless/associations.rb
soulless-0.5.0.rc1 lib/soulless/associations.rb
soulless-0.4.3 lib/soulless/associations.rb
soulless-0.4.1 lib/soulless/associations.rb
soulless-0.4.0 lib/soulless/associations.rb