lib/soulless/associations.rb in soulless-0.3.0 vs lib/soulless/associations.rb in soulless-0.4.0

- old
+ new

@@ -1,9 +1,15 @@ module Soulless module Associations def self.included(base) - base.instance_eval do |object| + 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 @@ -12,12 +18,14 @@ 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 \ No newline at end of file