summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--app/models/license.rb5
-rw-r--r--spec/models/license_spec.rb21
2 files changed, 22 insertions, 4 deletions
diff --git a/app/models/license.rb b/app/models/license.rb
index c8d2fdb..abcd728 100644
--- a/app/models/license.rb
+++ b/app/models/license.rb
@@ -1,5 +1,6 @@
class License < ActiveRecord::Base
- def self.most_recent(page, per_page)
- License.order(:created_at => :desc)
+ def self.most_recent(page: 1, per_page: 10)
+ offset = (page - 1) * per_page
+ License.order(created_at: :desc).offset(offset).limit(per_page)
end
end
diff --git a/spec/models/license_spec.rb b/spec/models/license_spec.rb
index 9998ad9..a939372 100644
--- a/spec/models/license_spec.rb
+++ b/spec/models/license_spec.rb
@@ -5,11 +5,28 @@ describe License do
let!(:oldest_license) { License.create }
let!(:newest_license) { License.create }
- let(:results) { License.most_recent(1, 2) }
-
it "returns the most recently created well licenses" do
+ results = License.most_recent(page: 1, per_page: 2)
results.first.should == newest_license
results.last.should == oldest_license
end
+
+ it "returns the first page of results" do
+ results = License.most_recent(page: 1, per_page: 1)
+ results.count.should == 1
+ results.first.should == newest_license
+ end
+
+ it "returns the next page of results" do
+ results = License.most_recent(page: 2, per_page: 1)
+ results.count.should == 1
+ results.first.should == oldest_license
+ end
+
+ it "returns the first page of results when the page is below 1" do
+ results = License.most_recent(page: -1, per_page: 1)
+ results.count.should == 1
+ results.first.should == newest_license
+ end
end
end