Sha256: 302ffdd2b71d99b9dc7156653d439132bc08e41b45bcd6e34ea21604226fe890
Contents?: true
Size: 950 Bytes
Versions: 24
Compression:
Stored size: 950 Bytes
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 send(:include, association_methods) end def belongs_to_user belongs_to_user_called(:user) end end included do extend ClassMethods end end end end
Version data entries
24 entries across 24 versions & 1 rubygems