Sha256: e504ec8e908bf1f72f9e9257599e281e3cc14401db2fe64770150a7e8877a73f
Contents?: true
Size: 1.05 KB
Versions: 48
Compression:
Stored size: 1.05 KB
Contents
module Softwear module Auth module BelongsToUser extend ActiveSupport::Concern module ClassMethods def belongs_to_user_called(name, options = {}) foreign_key = "#{name}_id" # Create an anonymous module and include it, so that we can # override the generated association methods and call `super` # in the method body. association_methods = Module.new association_methods.module_eval <<-RUBY, __FILE__, __LINE__ + 1 def #{name} @#{name} ||= User.find(#{name}_id) end def #{name}=(new) self.#{foreign_key} = new.id @#{name} = new end RUBY self.user_associations ||= [] user_associations << name.to_sym send(:include, association_methods) end def belongs_to_user belongs_to_user_called(:user) end end included do extend ClassMethods cattr_accessor :user_associations end end end end
Version data entries
48 entries across 48 versions & 2 rubygems