diff options
author | Rhenium <rhenium@rhe.jp> | 2013-08-08 08:02:06 +0900 |
---|---|---|
committer | Rhenium <rhenium@rhe.jp> | 2013-08-08 08:02:06 +0900 |
commit | 45495a6df59e765c2ea9ad190604eee8d574670b (patch) | |
tree | 1e758954d1808ec0ee9232863a93d9e5e2699930 | |
parent | 651ed9204ef5e190fa8fe3c88434348b13260976 (diff) | |
download | aclog-45495a6df59e765c2ea9ad190604eee8d574670b.tar.gz |
update autoloading tweets
-rw-r--r-- | app/assets/javascripts/html-autoload.js | 55 | ||||
-rw-r--r-- | app/assets/javascripts/tweets.js | 17 | ||||
-rw-r--r-- | app/controllers/tweets_controller.rb | 14 | ||||
-rw-r--r-- | app/views/tweets/_tweets.html.haml | 2 | ||||
-rw-r--r-- | app/views/tweets/show.html.haml | 2 | ||||
-rw-r--r-- | app/views/tweets/show.json.jbuilder | 2 | ||||
-rw-r--r-- | db/schema.rb | 20 | ||||
-rw-r--r-- | vendor/assets/javascripts/jquery.autopager-1.0.0-mod.js | 173 |
8 files changed, 85 insertions, 200 deletions
diff --git a/app/assets/javascripts/html-autoload.js b/app/assets/javascripts/html-autoload.js new file mode 100644 index 0000000..1e1f313 --- /dev/null +++ b/app/assets/javascripts/html-autoload.js @@ -0,0 +1,55 @@ +(function($) { + var window = this, + options = {}, + content, + loading = false; + + $.autopager = function(_options) { + var autopager = this.autopager; + + var defaults = { + content: "#content", + onStart: function() {}, + onComplete: function() {}, + page: 1, + currentUrl: window.location.href, + nextUrl: null + }; + + options = $.extend({}, defaults, _options); + content = $(options.content); + + $(window).scroll(function() { + if (content.offset().top + content.height() < $(document).scrollTop() + $(window).height()) { + $.autopager.loadNext(); + } + }); + + return this; + }; + + $.extend($.autopager, { + loadNext: function() { + if (loading || !options.nextUrl) { + return; + } + + loading = true; + options.onStart(); + $.get(options.nextUrl, insertContent, "text"); + return this; + } + }); + + function insertContent(res) { + var json = JSON.parse(res); + var nextPage = $(json.html); + + options.page = options.page + 1; + options.currentUrl = options.nextUrl; + options.nextUrl = json.next; + content.append(nextPage); + options.onComplete(); + loading = false; + } +})(jQuery); diff --git a/app/assets/javascripts/tweets.js b/app/assets/javascripts/tweets.js index 5968264..20bab9f 100644 --- a/app/assets/javascripts/tweets.js +++ b/app/assets/javascripts/tweets.js @@ -1,18 +1,15 @@ -//= require jquery.autopager-1.0.0-mod +//= require html-autoload $(function() { - $(".pagination").hide(); +// $(".pagination").hide(); $.autopager({ - autoLoad: true, - content: ".tweets", - start: function(current, next) { + content: $(".tweets"), + nextUrl: $("a[rel=next]").attr("href"), + onStart: function() { $(".loading").show(); }, - load: function(current, next) { + onComplete: function() { $(".loading").hide(); } }); - $("a[rel=next]").click(function() { - $.autopager("load"); - return false; - }); }); + diff --git a/app/controllers/tweets_controller.rb b/app/controllers/tweets_controller.rb index 2930473..4a471e4 100644 --- a/app/controllers/tweets_controller.rb +++ b/app/controllers/tweets_controller.rb @@ -1,7 +1,9 @@ # -*- encoding: utf-8 -*- class TweetsController < ApplicationController def show - tweet_required + @tweets = Tweet.where(id: params[:id]) + raise Aclog::Exceptions::TweetNotFound unless @tweets.first + @user = @tweets.first.user @caption = "#{@user.screen_name}'s Tweet" end @@ -93,12 +95,6 @@ class TweetsController < ApplicationController @user_b = _require_user(params[:user_id_b], params[:screen_name_b]) end - def tweet_required - @tweet = Tweet.find_by(id: params[:id]) - raise Aclog::Exceptions::TweetNotFound unless @tweet - @user = _require_user(@tweet.user_id, nil) - end - def check_public! authorize_to_show_best!(@user) end @@ -106,6 +102,10 @@ class TweetsController < ApplicationController def render(*args) if lookup_context.exists?(params[:action], params[:controller]) super(*args) + elsif request.xhr? + html = render_to_string(partial: "tweets/tweet", collection: @tweets.includes(:user), as: :tweet) + n = url_for(params[:page] ? params.merge(page: params[:page] + 1) : params.merge(max_id: @tweets.last.id - 1)) + super json: {html: html, next: n} else super("_tweets") end diff --git a/app/views/tweets/_tweets.html.haml b/app/views/tweets/_tweets.html.haml index 0fb4a99..63c802c 100644 --- a/app/views/tweets/_tweets.html.haml +++ b/app/views/tweets/_tweets.html.haml @@ -4,5 +4,5 @@ .pagination - if params[:page] = link_to raw("Next ›"), params.merge(page: params[:page].to_i + 1), rel: :next - - elsif @tweets.last + - else = link_to raw("Next ›"), params.merge(max_id: @tweets.last.id - 1), rel: :next diff --git a/app/views/tweets/show.html.haml b/app/views/tweets/show.html.haml deleted file mode 100644 index 65fcf3c..0000000 --- a/app/views/tweets/show.html.haml +++ /dev/null @@ -1,2 +0,0 @@ -.tweets - = render partial: "tweet", locals: {tweet: @tweet} diff --git a/app/views/tweets/show.json.jbuilder b/app/views/tweets/show.json.jbuilder index c92aaa0..e58cabf 100644 --- a/app/views/tweets/show.json.jbuilder +++ b/app/views/tweets/show.json.jbuilder @@ -1 +1 @@ -json.partial! "tweet", tweet: @tweet +json.partial! "tweet", tweet: @tweets.first diff --git a/db/schema.rb b/db/schema.rb index c07932d..b6c4db9 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -56,13 +56,21 @@ ActiveRecord::Schema.define(version: 20130805001722) do add_index "retweets", ["tweet_id"], name: "index_retweets_on_tweet_id", using: :btree add_index "retweets", ["user_id"], name: "index_retweets_on_user_id", using: :btree + create_table "stolen_tweets", force: true do |t| + t.integer "tweet_id", limit: 8 + t.integer "original_id", limit: 8 + end + + add_index "stolen_tweets", ["original_id"], name: "index_stolen_tweets_on_original_id", using: :btree + add_index "stolen_tweets", ["tweet_id"], name: "index_stolen_tweets_on_tweet_id", unique: true, using: :btree + create_table "tweets", force: true do |t| - t.text "text", null: false - t.text "source" - t.integer "user_id", limit: 8, null: false + t.text "text", limit: 16777215, null: false + t.text "source", limit: 16777215 + t.integer "user_id", limit: 8, null: false t.datetime "tweeted_at" - t.integer "favorites_count", default: 0 - t.integer "retweets_count", default: 0 + t.integer "favorites_count", default: 0 + t.integer "retweets_count", default: 0 end add_index "tweets", ["user_id"], name: "index_tweets_on_user_id", using: :btree @@ -70,7 +78,7 @@ ActiveRecord::Schema.define(version: 20130805001722) do create_table "users", force: true do |t| t.string "screen_name" t.string "name" - t.text "profile_image_url" + t.text "profile_image_url", limit: 16777215 t.datetime "created_at" t.datetime "updated_at" t.boolean "protected" diff --git a/vendor/assets/javascripts/jquery.autopager-1.0.0-mod.js b/vendor/assets/javascripts/jquery.autopager-1.0.0-mod.js deleted file mode 100644 index 7c5bc87..0000000 --- a/vendor/assets/javascripts/jquery.autopager-1.0.0-mod.js +++ /dev/null @@ -1,173 +0,0 @@ -/* - * jQuery.autopager v1.0.0 - * - * Copyright (c) 2009 lagos - * Dual licensed under the MIT and GPL licenses. - * - * http://lagoscript.org - */ -(function($) { - var window = this, options = {}, - content, currentUrl, nextUrl, - active = false, - defaults = { - autoLoad: true, - page: 1, - content: '.content', - link: 'a[rel=next]', - insertBefore: null, - appendTo: null, - start: function() {}, - load: function() {}, - disabled: false - }; - - $.autopager = function(_options) { - var autopager = this.autopager; - - if (typeof _options === 'string' && $.isFunction(autopager[_options])) { - var args = Array.prototype.slice.call(arguments, 1), - value = autopager[_options].apply(autopager, args); - - return value === autopager || value === undefined ? this : value; - } - - _options = $.extend({}, defaults, _options); - autopager.option(_options); - - content = $(_options.content).filter(':last'); - if (content.length) { - if (!_options.insertBefore && !_options.appendTo) { - var insertBefore = content.next(); - if (insertBefore.length) { - set('insertBefore', insertBefore); - } else { - set('appendTo', content.parent()); - } - } - } - - setUrl(); - - return this; - }; - - $.extend($.autopager, { - option: function(key, value) { - var _options = key; - - if (typeof key === "string") { - if (value === undefined) { - return options[key]; - } - _options = {}; - _options[key] = value; - } - - $.each(_options, function(key, value) { - set(key, value); - }); - return this; - }, - - enable: function() { - set('disabled', false); - return this; - }, - - disable: function() { - set('disabled', true); - return this; - }, - - destroy: function() { - this.autoLoad(false); - options = {}; - content = currentUrl = nextUrl = undefined; - return this; - }, - - autoLoad: function(value) { - return this.option('autoLoad', value); - }, - - load: function() { - if (active || !nextUrl || options.disabled) { - return; - } - - active = true; - options.start(currentHash(), nextHash()); - $.get(nextUrl, insertContent, "text"); - return this; - } - - }); - - function set(key, value) { - switch (key) { - case 'autoLoad': - if (value && !options.autoLoad) { - $(window).scroll(loadOnScroll); - } else if (!value && options.autoLoad) { - $(window).unbind('scroll', loadOnScroll); - } - break; - case 'insertBefore': - if (value) { - options.appendTo = null; - } - break - case 'appendTo': - if (value) { - options.insertBefore = null; - } - break - } - options[key] = value; - } - - function setUrl(context) { - currentUrl = nextUrl || window.location.href; - nextUrl = $(options.link, context).attr('href'); - } - - function loadOnScroll() { - if (content.offset().top + content.height() < $(document).scrollTop() + $(window).height()) { - $.autopager.load(); - } - } - - function insertContent(res) { - var _options = options, - nextPage = $('<div/>').append(res.replace(/<script(.|\s)*?\/script>/g, "").replace(/<\?xml\s.+?\?>/, "")), - nextContent = nextPage.find(_options.content); - - set('page', _options.page + 1); - setUrl(nextPage); - if (nextContent.length) { - if (_options.insertBefore) { - nextContent.insertBefore(_options.insertBefore); - } else { - nextContent.appendTo(_options.appendTo); - } - _options.load.call(nextContent.get(), currentHash(), nextHash()); - content = nextContent.filter(':last'); - } - active = false; - } - - function currentHash() { - return { - page: options.page, - url: currentUrl - }; - } - - function nextHash() { - return { - page: options.page + 1, - url: nextUrl - }; - } -})(jQuery); |