Sha256: 1393cbbc574f3932b20fc4cdb3a4773a510b33bd83ddff3ea51a91103ae12cc4

Contents?: true

Size: 1.24 KB

Versions: 22

Compression:

Stored size: 1.24 KB

Contents

module Her
  module Model
    module Associations
      class AssociationProxy < (ActiveSupport.const_defined?('ProxyObject') ? ActiveSupport::ProxyObject : ActiveSupport::BasicObject)

        # @private
        def self.install_proxy_methods(target_name, *names)
          names.each do |name|
            module_eval <<-RUBY, __FILE__, __LINE__ + 1
              def #{name}(*args, &block)
                #{target_name}.send(#{name.inspect}, *args, &block)
              end
            RUBY
          end
        end

        install_proxy_methods :association,
          :build, :create, :where, :find, :all, :assign_nested_attributes

        # @private
        def initialize(association)
          @_her_association = association
        end

        def association
          @_her_association
        end

        # @private
        def method_missing(name, *args, &block)
          if :object_id == name # avoid redefining object_id
            return association.fetch.object_id
          end

          # create a proxy to the fetched object's method
          AssociationProxy.install_proxy_methods 'association.fetch', name

          # resend message to fetched object
          __send__(name, *args, &block)
        end

      end
    end
  end
end

Version data entries

22 entries across 22 versions & 4 rubygems

Version Path
test_track_rails_client-4.0.0.alpha12 vendor/gems/her/lib/her/model/associations/association_proxy.rb
test_track_rails_client-4.0.0.alpha11 vendor/gems/her/lib/her/model/associations/association_proxy.rb
test_track_rails_client-4.0.0.alpha10 vendor/gems/her/lib/her/model/associations/association_proxy.rb
test_track_rails_client-4.0.0.alpha9 vendor/gems/her/lib/her/model/associations/association_proxy.rb
test_track_rails_client-4.0.0.alpha8 vendor/gems/her/lib/her/model/associations/association_proxy.rb
test_track_rails_client-4.0.0.alpha7 vendor/gems/her/lib/her/model/associations/association_proxy.rb
test_track_rails_client-4.0.0.alpha6 vendor/gems/her/lib/her/model/associations/association_proxy.rb
test_track_rails_client-4.0.0.alpha5 vendor/gems/her/lib/her/model/associations/association_proxy.rb
test_track_rails_client-4.0.0.alpha4 vendor/gems/her/lib/her/model/associations/association_proxy.rb
test_track_rails_client-4.0.0.alpha3 vendor/gems/her/lib/her/model/associations/association_proxy.rb
test_track_rails_client-4.0.0.alpha2 vendor/gems/her/lib/her/model/associations/association_proxy.rb
test_track_rails_client-4.0.0.alpha1 vendor/gems/her/lib/her/model/associations/association_proxy.rb
test_track_rails_client-3.0.1 vendor/gems/her/lib/her/model/associations/association_proxy.rb
test_track_rails_client-3.0.0 vendor/gems/her/lib/her/model/associations/association_proxy.rb
her-0.8.6 lib/her/model/associations/association_proxy.rb
her-0.8.5 lib/her/model/associations/association_proxy.rb
her-0.8.4 lib/her/model/associations/association_proxy.rb
her-0.8.3 lib/her/model/associations/association_proxy.rb
her-0.8.2 lib/her/model/associations/association_proxy.rb
her5-0.8.2 lib/her/model/associations/association_proxy.rb