Class: ShipStation::Model
- Inherits:
-
Object
- Object
- ShipStation::Model
show all
- Includes:
- Her::Model
- Defined in:
- lib/ship_station/api/model.rb
Class Method Summary
collapse
Instance Method Summary
collapse
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
permalink
#method_missing(name, *args, &block) ⇒ Object
[View source]
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
|
# File 'lib/ship_station/api/model.rb', line 40
def method_missing(name, *args, &block)
association = self.class.associated.fetch(name)
if association
case association[:relationship]
when :has_many
foreign_key = association[:foreign_key]
resource = name.to_s.singularize.capitalize.classify
"ShipStation::#{resource}".constantize.where(foreign_key => self.send(foreign_key)).fetch
when :belongs_to
primary_key = association[:primary_key]
resource = name.to_s.singularize.capitalize.classify
"ShipStation::#{resource}".constantize.find(self.send(primary_key))
else
super
end
end
end
|
Class Method Details
permalink
.associated ⇒ Object
[View source]
27
28
29
|
# File 'lib/ship_station/api/model.rb', line 27
def associated
@associated ||= {}
end
|
permalink
.shipstation_belongs_to(name, opts = {}) ⇒ Object
[View source]
35
36
37
|
# File 'lib/ship_station/api/model.rb', line 35
def shipstation_belongs_to(name, opts={})
associated[name] = opts.merge(:relationship => :belongs_to)
end
|
permalink
.shipstation_has_many(name, opts = {}) ⇒ Object
[View source]
31
32
33
|
# File 'lib/ship_station/api/model.rb', line 31
def shipstation_has_many(name, opts={})
associated[name] = opts.merge(:relationship => :has_many)
end
|