lib/active_model/associations.rb in activemodel-associations-0.1.2 vs lib/active_model/associations.rb in activemodel-associations-0.2.0.beta1
- old
+ new
@@ -2,27 +2,30 @@
require 'active_model/associations/active_record_reflection'
require 'active_model/associations/autosave_association'
require 'active_model/associations/override_methods'
require 'active_record/associations/builder/has_many_for_active_model'
require 'active_record/associations/has_many_for_active_model_association'
+require 'active_support/core_ext/module'
module ActiveModel
module Associations
extend ActiveSupport::Concern
include InitializeExtension
include AutosaveAssociation
include ActiveRecordReflection
include OverrideMethods
+ included do
+ mattr_accessor :belongs_to_required_by_default, instance_accessor: false
+ end
+
module ClassMethods
# define association like ActiveRecord
def belongs_to(name, scope = nil, options = {})
reflection = ActiveRecord::Associations::Builder::BelongsTo.build(self, name, scope, options)
- if ActiveRecord.version >= Gem::Version.new("4.1")
- ActiveRecord::Reflection.add_reflection self, name, reflection
- end
+ ActiveRecord::Reflection.add_reflection self, name, reflection
end
# define association like ActiveRecord
def has_many(name, scope = nil, options = {}, &extension)
options.reverse_merge!(active_model: true, target_ids: "#{name.to_s.singularize}_ids")
@@ -30,12 +33,10 @@
options.merge!(scope)
scope = nil
end
reflection = ActiveRecord::Associations::Builder::HasManyForActiveModel.build(self, name, scope, options, &extension)
- if ActiveRecord.version >= Gem::Version.new("4.1")
- ActiveRecord::Reflection.add_reflection self, name, reflection
- end
+ ActiveRecord::Reflection.add_reflection self, name, reflection
mixin = generated_association_methods
mixin.class_eval <<-CODE, __FILE__, __LINE__ + 1
def #{options[:target_ids]}=(other_ids)
@#{options[:target_ids]} = other_ids