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-2.0.10 lib/softwear/auth/belongs_to_user.rb
softwear-lib-2.0.9.2 lib/softwear/auth/belongs_to_user.rb
softwear-lib-2.0.9.1 lib/softwear/auth/belongs_to_user.rb
softwear-lib-2.0.9 lib/softwear/auth/belongs_to_user.rb
softwear-lib-2.0.8 lib/softwear/auth/belongs_to_user.rb
softwear-lib-2.0.7 lib/softwear/auth/belongs_to_user.rb
softwear-lib-2.0.6 lib/softwear/auth/belongs_to_user.rb
softwear-lib-2.0.5 lib/softwear/auth/belongs_to_user.rb
softwear-lib-2.0.4 lib/softwear/auth/belongs_to_user.rb
softwear-lib-2.0.3 lib/softwear/auth/belongs_to_user.rb
softwear-lib-2.0.2 lib/softwear/auth/belongs_to_user.rb
softwear-lib-2.0.1 lib/softwear/auth/belongs_to_user.rb
softwear-lib-2.0.0 lib/softwear/auth/belongs_to_user.rb
softwear-2.0.7 lib/softwear/auth/belongs_to_user.rb
softwear-2.0.6 lib/softwear/auth/belongs_to_user.rb
softwear-2.0.5 lib/softwear/auth/belongs_to_user.rb
softwear-2.0.4 lib/softwear/auth/belongs_to_user.rb
softwear-2.0.3 lib/softwear/auth/belongs_to_user.rb
softwear-2.0.0 lib/softwear/auth/belongs_to_user.rb
softwear-lib-1.10.3 lib/softwear/auth/belongs_to_user.rb