Class: Correlate::Relationships::ActiveRecord

Inherits:
Object
  • Object
show all
Defined in:
lib/correlate/relationships/active_record.rb,
lib/correlate/relationships/active_record/collection_proxy.rb

Overview

Used to define relationships between ActiveRecord::Base models and CouchRest::ExtendedDocument classes.

Important note

Unlike normal correlations, when correlating an ActiveRecord model with a CouchRest document, the ‘reverse correlation’ needs to be specified in in the CouchRest model.

See Also:

Defined Under Namespace

Classes: CollectionProxy

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(klass) ⇒ ActiveRecord

Returns a new instance of ActiveRecord.



30
31
32
# File 'lib/correlate/relationships/active_record.rb', line 30

def initialize( klass )
  @klass = klass
end

Class Method Details

.configure!(klass, &block) ⇒ Object



21
22
23
24
25
26
# File 'lib/correlate/relationships/active_record.rb', line 21

def configure!( klass, &block )

  # Setup our conveniences
  relationships = new( klass )
  relationships.instance_eval( &block )
end

Instance Method Details

#a(name, options = {}) ⇒ Object

Specify that this model will have a single document of said relationship. This is akin to ActiveRecord’s belongs_to or has_one associations.

Examples:

Defining correlations and using them

class MyModel < ActiveRecord::Base
  include Correlate

  related_to do
    a :journal, :class => 'Journal', :rel => 'my_model'
  end
end

class Journal < CouchRest::ExtendedDocument
  include Correlate

  related_to do
    some :my_models, :class => 'MyModel', :rel => 'my_model'
  end
end

doc = Journal.new
doc.my_models # => []

me = MyModel.find( 1 )
doc.my_models << me

doc.my_models # => [ <MyModel#12121212> ]

me.journal # => <Journal#121212121>

Parameters:

  • name (Symbol)

    of the relationship

  • options (Hash) (defaults to: {})

    for the relationship

Options Hash (options):

  • :class (String)

    the class of the related document/model

  • :rel (String) — default: name

    the value of the rel key in the related hash

  • :id_method (Symbol) — default: :id

    name of a method use to retrieve the ‘foreign key’ value from documents added to the relationship

  • :load_via (Symbol) — default: :get

    name of the class method used to retreive related documents



123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# File 'lib/correlate/relationships/active_record.rb', line 123

def a( name, options = {} )
  options[:source] = @klass

  @klass.correlations << Correlate::Relationships.build_correlation( name, :a, options )

  @klass.class_eval <<-EOF, __FILE__, __LINE__
    def #{name}=( object )
      self.links.replace( object )
    end

    def #{name}( raw = false )
      correlation = self.links.rel( '#{name}' ).first
      return if correlation.nil?

      correlation = self.class.correlation_for( correlation ).correlate( correlation ) unless raw

      correlation
    end
  EOF
end

#some(*args) ⇒ Object

Specify that this model will have multiple documents of said relationship. This is akin to ActiveRecord’s has_many associations

Examples:

Defining correlations and using them

class MyModel < ActiveRecord::Base
  include Correlate

  related_to do
    some :other_documents, :class => 'OtherDocument', :rel => 'my_model'
  end
end

class OtherDocument < CouchRest::ExtendedDocument
  include Correlate

  related_to do
    a :my_model, :class => 'MyModel'
  end
end

doc = OtherDocument.new
doc.my_model # => nil

me = MyModel.find( 1 )
doc.my_model = me

doc.me_model # => <MyModel#12121212>

me.other_documents # => [ <OtherDocument#121212121> ]

Parameters:

  • name (Symbol)

    of the relationship

  • options (Hash)

    for the relationship



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/correlate/relationships/active_record.rb', line 70

def some( *args )
  name = args.shift
  opts = args.empty? ? {} : args.last
  opts[:source] = @klass

  correlation = Correlate::Relationships.build_correlation( name, :some, opts )
  @klass.correlations << correlation

  @klass.class_eval <<-EOF, __FILE__, __LINE__
    def #{name}( raw = false )
      local_correlation = self.class.correlations.detect { |c| c.name == :#{name} }

      Correlate::Relationships::ActiveRecord::CollectionProxy.new self, local_correlation
    end
  EOF
end