Module: DaisybillApi::Ext::Associations::ClassMethods

Defined in:
lib/daisybill_api/ext/associations.rb

Overview

TODO: Think about creating Association classes or Routing class

Instance Method Summary collapse

Instance Method Details

#foreign_keyObject



10
11
12
# File 'lib/daisybill_api/ext/associations.rb', line 10

def foreign_key
  @foreign_key
end

#foreign_key=(value) ⇒ Object



6
7
8
# File 'lib/daisybill_api/ext/associations.rb', line 6

def foreign_key=(value)
  @foreign_key = value
end

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



14
15
16
17
18
19
20
21
22
23
# File 'lib/daisybill_api/ext/associations.rb', line 14

def has_many(name, options = {})
  clazz = modulize options[:class]
  define_method name do |params = {}|
    params.merge!(:"#{self.class.singular_key}_id" => self.id)
    params.merge!(path: "#{show_path}/#{name}") if options[:set_path]
    params.merge!(path: clazz.constantize.index_path(id)) if options[:set_path] && options[:use_path_prefix]
    params.merge!(collection_key: options[:collection_key]) if options[:collection_key]
    clazz.constantize.all(params)
  end
end