Class: Correlate::Relationships::ActiveRecord
- Inherits:
-
Object
- Object
- Correlate::Relationships::ActiveRecord
- 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.
Defined Under Namespace
Classes: CollectionProxy
Class Method Summary collapse
Instance Method Summary collapse
-
#a(name, options = {}) ⇒ Object
Specify that this model will have a single document of said relationship.
-
#initialize(klass) ⇒ ActiveRecord
constructor
A new instance of ActiveRecord.
-
#some(*args) ⇒ Object
Specify that this model will have multiple documents of said relationship.
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.
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, = {} ) [:source] = @klass @klass.correlations << Correlate::Relationships.build_correlation( name, :a, ) @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
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 |