Module: ExposeDB::Relations

Included in:
Model
Defined in:
lib/expose_db/model/relations.rb

Instance Method Summary collapse

Instance Method Details

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



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/expose_db/model/relations.rb', line 3

def belongs_to(name, options = {})
  if options.key?(:class_name)
    class_name = options.fetch(:class_name).to_s
  else
    class_name = name.to_s.titleize
  end

  foreign_key = (options[:foreign_key] ||
                 "#{name.to_s.underscore}_id").to_sym

  define_method(name) do
    found = instance_variable_get(:"@#{name}")
    return found if found

    if id = send(foreign_key)
      found = self.class.resolve_relation_class(class_name).find(id)
      instance_variable_set(:"@#{name}", found)
    end
    found
  end
end

#constantize_class_from_name(camel_cased_word) ⇒ Object

Selected from ActiveSupport



59
60
61
62
63
64
65
66
67
68
# File 'lib/expose_db/model/relations.rb', line 59

def constantize_class_from_name(camel_cased_word)
  names = camel_cased_word.split('::')
  names.shift if names.empty? || names.first.empty?

  constant = Object
  names.each do |name|
    constant = constant.const_defined?(name) ? constant.const_get(name) : constant.const_missing(name)
  end
  constant
end

#has_many(plural_name, options = {}) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/expose_db/model/relations.rb', line 25

def has_many(plural_name, options = {})
  singular_name = plural_name.to_s.sub(/s\Z/, '')

  if options.key?(:class_name)
    class_name = options.fetch(:class_name).to_s
  else
    class_name = singular_name.titleize
  end

  primary_key = (options[:primary_key] || :id).to_sym

  foreign_key = (options[:foreign_key] ||
                 "#{singular_name.underscore}_id").to_sym

  define_method(plural_name) do
    found = instance_variable_get(:"@#{plural_name}")
    return found if found

    relation_class = self.class.resolve_relation_class(class_name)
    found = relation_class.filter("#{foreign_key} = ?", send(primary_key))
    instance_variable_set(:"@#{plural_name}", found)
    found
  end
end

#resolve_relation_class(class_name) ⇒ Object



50
51
52
# File 'lib/expose_db/model/relations.rb', line 50

def resolve_relation_class(class_name)
  resolved_relation_classes[class_name] ||= constantize_class_from_name(class_name)
end

#resolved_relation_classesObject



54
55
56
# File 'lib/expose_db/model/relations.rb', line 54

def resolved_relation_classes
  @resolved_relation_classes ||= {}
end