Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Model.last method added like in ActiveRecord in Rails framework #100

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,14 @@ GEM
addressable (2.3.4)
ansi (1.4.3)
builder (3.0.4)
columnize (0.3.6)
crack (0.3.2)
debugger (1.6.1)
columnize (>= 0.3.1)
debugger-linecache (~> 1.2.0)
debugger-ruby_core_source (~> 1.2.3)
debugger-linecache (1.2.0)
debugger-ruby_core_source (1.2.3)
git (1.2.5)
i18n (0.6.1)
jeweler (1.6.4)
Expand Down Expand Up @@ -49,6 +56,7 @@ PLATFORMS
DEPENDENCIES
activemodel
activesupport
debugger
jeweler (~> 1.6.4)
json
reek (~> 1.2.8)
Expand Down
8 changes: 8 additions & 0 deletions lib/parse_resource/query.rb
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ def execute
return results.to_i
else
results = JSON.parse(resp)['results']
puts results
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this shouldnt be left here

return results.map {|r| @klass.model_name.to_s.constantize.new(r, false)}
end
end
Expand Down Expand Up @@ -137,6 +138,13 @@ def first
execute.first
end

#my contribution
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remove this comment.

def last
order('created_at desc')
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What if you don't want to order by created_at?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree. If this method were to be added it would need to take some kind of option param to allow users to return the last object sorted by a field they specify

limit(1)
execute.last
end

def all
execute
end
Expand Down
7 changes: 7 additions & 0 deletions lib/parse_resource/query_methods.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,13 @@ def first
Query.new(self).limit(1).first
end

# Find the last object. Fairly random, not based on any specific condition.
#my contribution
def last
Query.new(self).limit(1).last
end


# Limits the number of objects returned
#
def limit(n)
Expand Down
11 changes: 11 additions & 0 deletions test/test_parse_resource.rb
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,17 @@ def test_first
end
end

def test_last
VCR.use_cassette('test_last', :record => :new_episodes) do
Fork.destroy_all
f = Fork.create(:points => "lasttt")
p = Fork.last
assert p.is_a?(Fork)
assert f.id, p.id
assert f.points, p.points
end
end

def test_find_by
VCR.use_cassette('test_find_by', :record => :new_episodes) do
p1 = Post.create(:title => "Welcome111")
Expand Down