module Fakecrm::Fetch
Public Instance Methods
build_continuation()
click to toggle source
# File lib/fakecrm/fetch.rb, line 11 def build_continuation Base64.urlsafe_encode64(params.merge({:offset => offset+limit, :limit => limit, :include_deleted => include_deleted?.to_s}).to_json) end
consider_deleted(resource)
click to toggle source
# File lib/fakecrm/fetch.rb, line 28 def consider_deleted(resource) if include_deleted? resource.with_deleted else resource end end
continuation()
click to toggle source
# File lib/fakecrm/fetch.rb, line 5 def continuation JSON.parse(Base64.decode64(params.fetch("continuation_handle", "e30="))) rescue => exception {} end
fetch_many(resource)
click to toggle source
# File lib/fakecrm/fetch.rb, line 111 def fetch_many(resource) query = consider_deleted(resource) remaining_count = query.count - offset result = {:results => ResourceView.decorate(query.all(:limit => limit, :offset => offset)) } if remaining_count > limit result["continuation_handle"] = build_continuation end status 200 body result.to_json end
fetch_one(resource, primary_key)
click to toggle source
# File lib/fakecrm/fetch.rb, line 124 def fetch_one(resource, primary_key) begin status 200 if resource.respond_to? :with_deleted body ResourceView.decorate(resource.with_deleted.get!(primary_key)).to_json else body ResourceView.decorate(resource.get!(primary_key)).to_json end rescue ::DataMapper::ObjectNotFoundError => e status 404 end end
include_deleted?()
click to toggle source
# File lib/fakecrm/fetch.rb, line 24 def include_deleted? "true" == continuation.fetch("include_deleted", params.fetch("include_deleted", "false")).to_s end
limit()
click to toggle source
# File lib/fakecrm/fetch.rb, line 15 def limit limit = params.fetch("limit", continuation.fetch("limit", "10")).to_i (1..1000).include?(limit) ? limit : 10 end
offset()
click to toggle source
# File lib/fakecrm/fetch.rb, line 20 def offset continuation.fetch("offset", 0).to_i end
search(resource, builtins=[], full_text_fields=[], sort_by = nil, sort_order = 'asc')
click to toggle source
# File lib/fakecrm/fetch.rb, line 37 def search(resource, builtins=[], full_text_fields=[], sort_by = nil, sort_order = 'asc') options = {} builtins.each do |built_in_field| options[built_in_field.to_sym] = params[built_in_field.to_s] if params.key?(built_in_field.to_s) end custom_fields = [] resource.properties.each do |property| custom_fields << property.name if property.name =~ /^custom/ end # custom fields params.each do |key, value| if key =~ /^custom_/ options[key] = value elsif key =~ /_id$/ # cast ids to integers options[key.to_sym] = value.to_i end end # full text if params.key?('q') search_words = params['q'].split(/\s/) sub_queries = [] values = [] search_words.each do |search_word| sub_query = (full_text_fields + custom_fields).map do |full_text_field| values << "%#{search_word}%" "(#{full_text_field} LIKE ?)" end.join(" OR ") sub_queries << " ( " + sub_query + " ) " end if !sub_queries.empty? options[:conditions] = [sub_queries.join(" AND ")] + values end end # Reject empty options to mimic WebCRM options = options.reject { |_, value| value.nil? || value == '' } query = resource.all(options) # FIXME: code duplication (fetch_many) total_count = query.count remaining_count = total_count - offset query_options = { :limit => limit, :offset => offset } if sort_by sort_order ||= 'asc' sort_order = 'asc' unless ['asc', 'desc'].include?(sort_order.downcase) if sort_by.to_sym == :updated_at order = { :order => [sort_by.to_sym.send(sort_order.to_sym), :id.send(sort_order.to_sym)] } else order = { :order => sort_by.to_sym.send(sort_order.to_sym) } end query_options.merge!(order) end result = {:results => ResourceView.decorate(query.all(query_options)), :total => total_count} if remaining_count > limit result["continuation_handle"] = build_continuation end status 200 body result.to_json end