diff --git a/app/assets/javascripts/angularjs/archives.js.coffee b/app/assets/javascripts/angularjs/archives.js.coffee index 8df73c9..a7271ee 100644 --- a/app/assets/javascripts/angularjs/archives.js.coffee +++ b/app/assets/javascripts/angularjs/archives.js.coffee @@ -1,7 +1,14 @@ -@app.controller 'ArchivesController', ($scope, $http, $location, $timeout)-> - url = "/archives.json" - $http.get(url).success (res)-> - $scope.start_with = res.start_with +@app.controller 'ArchivesController', ($scope, $http, $location, $timeout, $cookies)-> + url = $location.absUrl() + ".json" + start_with = $cookies.start_with if window.location.pathname == $cookies.start_with_type + $http + url: url + method: 'GET' + params: + start_with: start_with + all: true + .success (res)-> + $scope.update_start_with(res.start_with) $scope.posts = res.posts $scope.no_more_flag = false @@ -14,9 +21,10 @@ method: 'GET' params: start_with: $scope.start_with + type: $scope.type ).success (res)-> $scope.no_more_flag = true if res.posts.length == 0 - $scope.start_with = res.start_with + $scope.update_start_with(res.start_with) $scope.posts = $scope.posts.concat(res.posts) $timeout -> $scope.loading_flag = false @@ -27,3 +35,8 @@ $scope.visit = (id)-> window.location.href = ("/blogs/" + id) + + $scope.update_start_with = (start_with)-> + $scope.start_with = start_with + $cookies.start_with_type = window.location.pathname + $cookies.start_with = start_with diff --git a/app/controllers/archives_controller.rb b/app/controllers/archives_controller.rb index eeb7764..11b0e7f 100644 --- a/app/controllers/archives_controller.rb +++ b/app/controllers/archives_controller.rb @@ -1,14 +1,23 @@ class ArchivesController < ApplicationController def index - type = map[archive_params[:type]] + type = map[params[:type]] limit = 10 - start_with = archive_params[:start_with] - @posts = Post.limit(limit).desc(:created_at) + start_with = params[:start_with] + @posts = Post.desc(:created_at) + if type @posts = @posts.where(type: type) + @type = type end - if start_with + # all 与 start_with 参数同在, 说明是要获取所有start_with之前的数据 + if params[:all] and params[:start_with] + @posts = @posts.where(:created_at.gte => Time.at(start_with.to_i)) + else + @posts = @posts.limit(limit) + end + + if !params[:all] and start_with @posts = @posts.where(:created_at.lt => Time.at(start_with.to_i)) end @@ -28,10 +37,6 @@ class ArchivesController < ApplicationController end end - def archive_params - params.permit(:type, :start_with) - end - private def map { @@ -41,10 +46,6 @@ class ArchivesController < ApplicationController } end - def archive_params - params.permit(:type, :start_with, :format) - end - def build_summary(post) { title: post.title, diff --git a/app/views/archives/index.html.slim b/app/views/archives/index.html.slim index 8c96092..2a498e5 100644 --- a/app/views/archives/index.html.slim +++ b/app/views/archives/index.html.slim @@ -1,6 +1,6 @@ .row ng-controller="ArchivesController" .small-12.large-8.columns - ul.archives-field + ul.archives-field ng-model="type" ng-init=" type= '#{@type}' " li ng-repeat=" post in posts " a.blog-title ng-click="visit(post.id)" |{{ post.title }} diff --git a/spec/controllers/blogs_controller_spec.rb b/spec/controllers/blogs_controller_spec.rb index 2d2830a..757dca2 100644 --- a/spec/controllers/blogs_controller_spec.rb +++ b/spec/controllers/blogs_controller_spec.rb @@ -65,19 +65,20 @@ describe BlogsController do describe "get SHOW" do it "#prev, #next" do posts = create_list(:post_list, 3) + posts = Post.order_by(created_at: 'asc') selected = posts[1] - s_prev = posts[2] - s_next = posts[1] + s_prev = posts[0] + s_next = posts[2] get :show, id: selected.id expect(assigns(:prev)).to eq(s_prev) expect(assigns(:next)).to eq(s_next) - selected = posts[2] + selected = posts[0] get :show, id: selected.id expect(assigns(:prev)).to be_nil expect(assigns(:next)).to eq(posts[1]) - selected = posts[0] + selected = posts[2] get :show, id: selected.id expect(assigns(:prev)).to eq(posts[1]) expect(assigns(:next)).to be_nil