Module: Subroutine::Association

Defined in:
lib/subroutine/association.rb

Defined Under Namespace

Modules: ClassMethods

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



4
5
6
7
8
9
10
11
# File 'lib/subroutine/association.rb', line 4

def self.included(base)
  base.send :extend, ::Subroutine::Association::ClassMethods
  class << base
    alias_method :field_without_associations, :field
    alias_method :field, :field_with_associations
  end

end

Instance Method Details

#initialize(*args) ⇒ Object



107
108
109
110
111
112
113
114
115
# File 'lib/subroutine/association.rb', line 107

def initialize(*args)
  super(*args)

  self._fields.each_pair do |field, config|
    next unless config[:association]
    next unless @original_params.has_key?(field)
    send("#{field}=", @original_params[field]) # this gets the _id and _type into the params hash
  end
end

#polymorphic_instance(_type, _id, _unscoped = false) ⇒ Object



117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/subroutine/association.rb', line 117

def polymorphic_instance(_type, _id, _unscoped = false)
  return nil unless _type && _id

  klass = _type
  klass = klass.classify.constantize if klass.is_a?(String)

  return nil unless klass

  scope = klass.all
  scope = scope.unscoped if _unscoped

  scope.find(_id)
end