Skip to content

Commit

Permalink
Fix rails4 model_name compatibility issue
Browse files Browse the repository at this point in the history
  • Loading branch information
awakia committed Jun 7, 2013
1 parent eca661c commit e82d316
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 20 deletions.
8 changes: 4 additions & 4 deletions lib/parse_resource/base.rb
Expand Up @@ -90,7 +90,7 @@ def self.belongs_to(parent, options = {})
end

def to_pointer
klass_name = self.class.model_name
klass_name = self.class.model_name.to_s
klass_name = "_User" if klass_name == "User"
klass_name = "_Installation" if klass_name == "Installation"
{"__type" => "Pointer", "className" => klass_name.to_s, "objectId" => self.id}
Expand Down Expand Up @@ -170,12 +170,12 @@ def self.settings

# Gets the current class's model name for the URI
def self.model_name_uri
if self.model_name == "User"
if self.model_name.to_s == "User"
"users"
elsif self.model_name == "Installation"
elsif self.model_name.to_s == "Installation"
"installations"
else
"classes/#{self.model_name}"
"classes/#{self.model_name.to_s}"
end
end

Expand Down
18 changes: 9 additions & 9 deletions lib/parse_resource/parse_user.rb
Expand Up @@ -8,18 +8,18 @@ def self.authenticate(username, password)
app_id = settings['app_id']
master_key = settings['master_key']
resource = RestClient::Resource.new(base_uri, app_id, master_key)

begin
resp = resource.get(:params => {:username => username, :password => password})
user = model_name.constantize.new(JSON.parse(resp), false)
user
rescue
user = model_name.to_s.constantize.new(JSON.parse(resp), false)

user
rescue
false
end

end

def self.authenticate_with_facebook(user_id, access_token, expires)
base_uri = "https://api.parse.com/1/users"
app_id = settings['app_id']
Expand All @@ -38,13 +38,13 @@ def self.authenticate_with_facebook(user_id, access_token, expires)
}
}.to_json,
:content_type => 'application/json', :accept => :json)
user = model_name.constantize.new(JSON.parse(resp), false)
user = model_name.to_s.constantize.new(JSON.parse(resp), false)
user
rescue
false
end
end

def self.reset_password(email)
base_uri = "https://api.parse.com/1/requestPasswordReset"
app_id = settings['app_id']
Expand Down
14 changes: 7 additions & 7 deletions lib/parse_resource/query.rb
Expand Up @@ -20,12 +20,12 @@ def limit(limit)
criteria[:limit] = limit
self
end

def include_object(parent)
criteria[:include] = parent
self
end

def order(attr)
orders = attr.split(" ")
if orders.count > 1
Expand Down Expand Up @@ -102,31 +102,31 @@ def execute
return chunk_results(params) if criteria[:chunk]

resp = @klass.resource.get(:params => params)

if criteria[:count] == 1
results = JSON.parse(resp)['count']
return results.to_i
else
results = JSON.parse(resp)['results']
return results.map {|r| @klass.model_name.constantize.new(r, false)}
return results.map {|r| @klass.model_name.to_s.constantize.new(r, false)}
end
end

def chunk_results(params={})
criteria[:limit] ||= 100

start_row = criteria[:skip].to_i
end_row = [criteria[:limit].to_i - start_row - 1, 1].max
result = []

# Start at start_row, go to end_row, get results in chunks
(start_row..end_row).each_slice(criteria[:chunk].to_i) do |slice|
params[:skip] = slice.first
params[:limit] = slice.length # Either the chunk size or the end of the limited results

resp = @klass.resource.get(:params => params)
results = JSON.parse(resp)['results']
result = result + results.map {|r| @klass.model_name.constantize.new(r, false)}
result = result + results.map {|r| @klass.model_name.to_s.constantize.new(r, false)}
break if results.length < params[:limit] # Got back fewer than we asked for, so exit.
end
result
Expand Down

0 comments on commit e82d316

Please sign in to comment.