Sha256: fdaf2aa7b144c92156866bb04041804214302dc78164dca60e25423557a919ef

Contents?: true

Size: 1.26 KB

Versions: 38

Compression:

Stored size: 1.26 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, :reload

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

        def association
          @_her_association
        end

        # @private
        def method_missing(name, *args, &block)
          if name == :object_id # 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

38 entries across 38 versions & 2 rubygems

Version Path
test_track_rails_client-4.0.0.alpha27 vendor/gems/her/lib/her/model/associations/association_proxy.rb
test_track_rails_client-4.0.0.alpha26 vendor/gems/her/lib/her/model/associations/association_proxy.rb
test_track_rails_client-4.0.0.alpha25 vendor/gems/her/lib/her/model/associations/association_proxy.rb
test_track_rails_client-4.0.0.alpha24 vendor/gems/her/lib/her/model/associations/association_proxy.rb
test_track_rails_client-4.0.0.alpha23 vendor/gems/her/lib/her/model/associations/association_proxy.rb
test_track_rails_client-4.0.0.alpha22 vendor/gems/her/lib/her/model/associations/association_proxy.rb
test_track_rails_client-4.0.0.alpha21 vendor/gems/her/lib/her/model/associations/association_proxy.rb
test_track_rails_client-4.0.0.alpha20 vendor/gems/her/lib/her/model/associations/association_proxy.rb
test_track_rails_client-4.0.0.alpha19 vendor/gems/her/lib/her/model/associations/association_proxy.rb
test_track_rails_client-4.0.0.alpha18 vendor/gems/her/lib/her/model/associations/association_proxy.rb
test_track_rails_client-4.0.0.alpha17 vendor/gems/her/lib/her/model/associations/association_proxy.rb
her-1.1.0 lib/her/model/associations/association_proxy.rb
test_track_rails_client-4.0.0.alpha16 vendor/gems/her/lib/her/model/associations/association_proxy.rb
test_track_rails_client-4.0.0.alpha15 vendor/gems/her/lib/her/model/associations/association_proxy.rb
test_track_rails_client-4.0.0.alpha14 vendor/gems/her/lib/her/model/associations/association_proxy.rb
test_track_rails_client-4.0.0.alpha13 vendor/gems/her/lib/her/model/associations/association_proxy.rb
her-1.0.3 lib/her/model/associations/association_proxy.rb
her-1.0.2 lib/her/model/associations/association_proxy.rb