Module: Plaza::RestfulModel
- Defined in:
- lib/plaza/models/restful_model.rb
Defined Under Namespace
Modules: ClassMethods
Classes: ErrorResponse
Class Method Summary
collapse
Instance Method Summary
collapse
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method_name, *args, &block) ⇒ Object
142
143
144
145
146
147
148
149
150
151
152
|
# File 'lib/plaza/models/restful_model.rb', line 142
def method_missing(method_name, *args, &block)
method_name = method_name.to_s
if self.respond_to?(method_name + '_id')
obj_id = self.send(method_name + '_id')
class_name = Plaza::Inflector.classify(method_name)
klass = (self.class.name.split('::')[0..-2] + [class_name]).reduce(Module, :const_get)
return klass.find(obj_id)
else
raise NoMethodError.new "undefined method '#{method_name}' for #{self.class}"
end
end
|
Class Method Details
.included(base) ⇒ Object
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
|
# File 'lib/plaza/models/restful_model.rb', line 9
def self.included(base)
base.class_eval do
include Virtus.model
include Plaza::BaseModel
attribute :id, Integer
attribute :errors, Hash
def serialize
attrs = attributes.delete_if{|k, v| k.to_sym == :id && v.nil?}
attrs = attrs.delete_if{|k, v| [:updated_at, :created_at].include?(k.to_sym)}
attrs.delete(:errors)
{singular_name => attrs}
end
end
base.extend ClassMethods
end
|
Instance Method Details
#delete ⇒ Object
96
97
98
|
# File 'lib/plaza/models/restful_model.rb', line 96
def delete
self.class.adapter.delete(self.id)
end
|
#error_messages ⇒ Object
100
101
102
103
104
105
106
|
# File 'lib/plaza/models/restful_model.rb', line 100
def error_messages
if errors.empty?
[]
else
errors.collect{|k, v| v.collect{|val| "#{k} #{val}"}}.flatten
end
end
|
#new_record? ⇒ Boolean
108
109
110
|
# File 'lib/plaza/models/restful_model.rb', line 108
def new_record?
!persisted?
end
|
#persisted? ⇒ Boolean
112
113
114
|
# File 'lib/plaza/models/restful_model.rb', line 112
def persisted?
self.id.present?
end
|
#plaza_config ⇒ Object
92
93
94
|
# File 'lib/plaza/models/restful_model.rb', line 92
def plaza_config
self.class.plaza_config
end
|
#save ⇒ Object
116
117
118
119
120
121
122
123
124
125
126
127
|
# File 'lib/plaza/models/restful_model.rb', line 116
def save
begin
if self.id
self.attributes = self.class.adapter.update(self.id, self.serialize)
else
self.attributes = self.class.adapter.create(self.serialize)
end
rescue Plaza::ResourceInvalid => e
self.errors.merge!(e.errors)
end
self.errors.empty?
end
|
#symbolize_keys(hash) ⇒ Object
129
130
131
|
# File 'lib/plaza/models/restful_model.rb', line 129
def symbolize_keys(hash)
hash.inject({}){|sym_hash,(k,v)| sym_hash[k.to_sym] = v; sym_hash}
end
|
#to_param ⇒ Object
133
134
135
|
# File 'lib/plaza/models/restful_model.rb', line 133
def to_param
self.id
end
|
#update_attributes(attributes_hash) ⇒ Object
137
138
139
140
|
# File 'lib/plaza/models/restful_model.rb', line 137
def update_attributes(attributes_hash)
self.attributes = self.attributes.merge(self.symbolize_keys(attributes_hash))
self.save
end
|