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

Version Path
softwear-lib-1.8.6 lib/softwear/auth/belongs_to_user.rb
softwear-lib-1.8.5 lib/softwear/auth/belongs_to_user.rb
softwear-lib-1.8.3 lib/softwear/auth/belongs_to_user.rb
softwear-lib-1.8.2 lib/softwear/auth/belongs_to_user.rb
softwear-lib-1.8.1 lib/softwear/auth/belongs_to_user.rb
softwear-lib-1.8.0 lib/softwear/auth/belongs_to_user.rb
softwear-lib-1.7.20 lib/softwear/auth/belongs_to_user.rb
softwear-lib-1.7.19 lib/softwear/auth/belongs_to_user.rb
softwear-lib-1.7.18 lib/softwear/auth/belongs_to_user.rb
softwear-lib-1.7.17 lib/softwear/auth/belongs_to_user.rb
softwear-lib-1.7.16 lib/softwear/auth/belongs_to_user.rb
softwear-lib-1.7.15 lib/softwear/auth/belongs_to_user.rb
softwear-lib-1.7.13 lib/softwear/auth/belongs_to_user.rb
softwear-lib-1.7.11 lib/softwear/auth/belongs_to_user.rb
softwear-lib-1.7.10 lib/softwear/auth/belongs_to_user.rb
softwear-lib-1.7.9 lib/softwear/auth/belongs_to_user.rb
softwear-lib-1.7.8 lib/softwear/auth/belongs_to_user.rb
softwear-lib-1.7.7 lib/softwear/auth/belongs_to_user.rb
softwear-lib-1.7.6 lib/softwear/auth/belongs_to_user.rb
softwear-lib-1.7.4 lib/softwear/auth/belongs_to_user.rb