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

Version Path
softwear-lib-3.3.7 lib/softwear/auth/belongs_to_user.rb
softwear-lib-3.3.6 lib/softwear/auth/belongs_to_user.rb
softwear-lib-3.3.5 lib/softwear/auth/belongs_to_user.rb
softwear-lib-3.1.5 lib/softwear/auth/belongs_to_user.rb
softwear-lib-3.1.4 lib/softwear/auth/belongs_to_user.rb
softwear-lib-3.1.3 lib/softwear/auth/belongs_to_user.rb
softwear-lib-3.1.2 lib/softwear/auth/belongs_to_user.rb
softwear-lib-3.1.1 lib/softwear/auth/belongs_to_user.rb
softwear-lib-3.1.0 lib/softwear/auth/belongs_to_user.rb
softwear-lib-3.0.0 lib/softwear/auth/belongs_to_user.rb
softwear-lib-2.1.7 lib/softwear/auth/belongs_to_user.rb
softwear-lib-2.1.6 lib/softwear/auth/belongs_to_user.rb
softwear-lib-2.1.4 lib/softwear/auth/belongs_to_user.rb
softwear-lib-2.1.3 lib/softwear/auth/belongs_to_user.rb
softwear-lib-2.1.2 lib/softwear/auth/belongs_to_user.rb
softwear-lib-2.1.1 lib/softwear/auth/belongs_to_user.rb
softwear-lib-2.1.0 lib/softwear/auth/belongs_to_user.rb
softwear-lib-2.0.13 lib/softwear/auth/belongs_to_user.rb
softwear-lib-2.0.12 lib/softwear/auth/belongs_to_user.rb
softwear-lib-2.0.11 lib/softwear/auth/belongs_to_user.rb