Module: Meek::Concern

Defined in:
lib/meek/concern.rb

Instance Method Summary collapse

Instance Method Details

#belongs_to(name, args = {}) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/meek/concern.rb', line 30

def belongs_to(name, args={})
  @@active_workaround_args ||= {}
  @@active_workaround_args[:belongs_to] ||= {}
  @@active_workaround_args[:belongs_to][name] = args
  @@active_workaround_args[:belongs_to][name][:_attribute_name] = name

  define_method(name) do

    args= @@active_workaround_args[:belongs_to][name]
    msg = {}

    foreign_key = args[:foreign_key] || :id
    msg[foreign_key] = respond_to?(:to_param) ? to_param : @id

    klass_title = ActiveSupport::Inflector.classify(name)
    klass = args[:class] || ActiveSupport::Inflector.constantize(klass_title)

    klass.find(msg)
  end
end

#has_many(name, args = {}) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/meek/concern.rb', line 6

def has_many(name, args={})
  @@active_workaround_args ||= {}
  @@active_workaround_args[:has_many] ||= {}
  @@active_workaround_args[:has_many][name] = args
  @@active_workaround_args[:has_many][name][:_attribute_name] = name

  define_method(name) do
    #return instance_variable_get(name) if does_cache && !instance_variable_get(name).nil?

    args= @@active_workaround_args[:has_many][name]
    msg = {}

    foreign_key = args[:foreign_key] || ActiveSupport::Inflector.foreign_key(self.class.name)
    msg[foreign_key] = respond_to?(:to_param) ? to_param : @id

    klass_title = ActiveSupport::Inflector.classify( msg[:class_name] || name )
    klass = args[:class] || ActiveSupport::Inflector.constantize(klass_title)

    response = klass.find(:all, msg)
    #instance_variable_set(name, response) if does_cache
    response
  end
end