Class: Appcelerator::CrudService

Inherits:
Service
  • Object
show all
Defined in:
lib/appcelerator/service.rb

Overview

TODO: spin this into another file when we can resolve dependecy issues

Instance Attribute Summary collapse

Attributes inherited from Service

#after_filters, #before_filters, #message_type, #params, #request, #response, #response_type, #session

Instance Method Summary collapse

Methods inherited from Service

Service, #clear_listeners, #do_after_filters, #do_before_filters, #handle, #initialize, #register, #respond_with, service_scaffold

Constructor Details

This class inherits a constructor from Appcelerator::Service

Instance Attribute Details

#model_classObject

Returns the value of attribute model_class.



123
124
125
# File 'lib/appcelerator/service.rb', line 123

def model_class
  @model_class
end

Instance Method Details

#assembly(request, message) ⇒ Object



125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
# File 'lib/appcelerator/service.rb', line 125

def assembly(request,message)
  if self.respond_to?(:before_assembly)
    request,message = send(:before_assembly,request,message)
  end
  data = Array.new
  @model_class.columns.each do |column|
      data << {'name'=>column.name,'type'=>column.type, 'limit'=>column.limit, 
             'nullable'=>column.null, 'primary'=>column.primary, 
             'default'=>column.default}
  end
  response = {'success'=>true,'columns'=>data}
  if self.respond_to?(:after_assembly)
    response = send(:after_assembly,response)
  end
  response
end

#create(request, message) ⇒ Object



142
143
144
145
146
147
148
149
150
151
152
153
# File 'lib/appcelerator/service.rb', line 142

def create(request,message)
  if self.respond_to?(:before_create)
    request,message = send(:before_create,request,message)
  end
  o = @model_class.new(message)
  o.save
  response = {'success'=>true,'id'=>o.id}
  if self.respond_to?(:after_create)
    response = send(:after_create,response)
  end
  response
end

#delete(request, message) ⇒ Object



192
193
194
195
196
197
198
199
200
201
202
# File 'lib/appcelerator/service.rb', line 192

def delete(request,message)
  if self.respond_to?(:before_delete)
  request,message = send(:before_delete,request,message)
  end
  @model_class.delete(message['id'])
  response = {'success'=>true,'id'=>message['id']}
  if self.respond_to?(:after_delete)
    response = send(:after_delete,response)
  end
  response
end

#list(request, message) ⇒ Object



228
229
230
231
232
233
234
235
236
237
238
239
240
241
# File 'lib/appcelerator/service.rb', line 228

def list(request,message)
  pk = message['id']
  limit = message['limit'] || 100
  if pk
      objs = [@model_class.find_by_id(pk)]
    else
      objs = @model_class.find(:all,:limit=>limit)
      end
  response = {'success'=>true,'rows'=>objs, 'total'=>@model_class.count}
  if self.respond_to?(:after_list)
    response = send(:after_list,response)
  end
  response
end

#retrieve(request, message) ⇒ Object



155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
# File 'lib/appcelerator/service.rb', line 155

def retrieve(request,message)
  if self.respond_to?(:before_retrieve)
  request,message = send(:before_retrieve,request,message)
  end
  o = @model_class.find(message['id'])
  response = {'success'=>true}
  o.attributes.each do |key,val|
    response[key]=val
  end
  response
  if self.respond_to?(:after_retrieve)
    response = send(:after_retrieve,response)
  end
  response
end

#search(request, message) ⇒ Object



204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
# File 'lib/appcelerator/service.rb', line 204

def search(request,message)
    if self.respond_to?(:before_search)
    request,message = send(:before_search,request,message)
    end
    query = '%%' + message['query'] + '%%'
    conditions_str = Array.new
    @model_class.content_columns.each do |col|
      name = col.name + ' like ?'
      conditions_str << name if col.type == :string
    end
    
    conditions = Array.new
    conditions << conditions_str.join(' OR ')
    conditions_str.length.times {|e| conditions<<query}
              
    results = @model_class.find(:all,:conditions=>conditions)
    response = {'success'=>true, 'rows'=>results, 'total'=>@model_class.count, 'count'=>results.length}
    
    if self.respond_to?(:after_search)
      response = send(:after_search,response)
    end
    response
end

#update(request, message) ⇒ Object



171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
# File 'lib/appcelerator/service.rb', line 171

def update(request,message)
  if self.respond_to?(:before_update)
  request,message = send(:before_update,request,message)
  end
  o = @model_class.find(message['id'])
  return {'success'=>false,'message'=>'record does not exist'} unless o
  message.each do |key,value|
  if o.attributes.has_key?(key)
    o[key]=true if value=='true'
    o[key]=false if value=='false'
    o[key]=value if o[key]!=true && o[key]!=false
  end
  end
  o.save!
  response = {'success'=>true,'id'=>o.id}
  if self.respond_to?(:after_update)
    response = send(:after_update,response)
  end
  response
end