Biweekly Koding
2014.8.26
Lucius Choi
create_table "favlinks", force: true do |t|
t.string "title", null: false
t.text "description"
t.string "linkurl", null: false
t.integer "writer_id"
t.boolean "shared", default: true
t.integer "bundlelink_id"
t.datetime "created_at"
t.datetime "updated_at"
t.string "capture_loc"
end
$ bin/rails g scaffold Favlink title description:text linkurl
shared:boolean capture_loc writer:references bundlelink:references
in db/schema.rb
in terminal
bundlelink
favlink
favlink
favlink
favlink
favlink
bundlelink_id
FK:
create_table "bundlelinks", force: true do |t|
t.string "title", null: false
t.text "description"
t.integer "writer_id", null: false
t.boolean "shared", default: true
t.datetime "created_at"
t.datetime "updated_at"
end
$ bin/rails g scaffold Bundlelink title description:text
writer:references shared:boolean
class CreateBundlelinks < ActiveRecord::Migration
def change
create_table :bundlelinks do |t|
t.string :title, null: false
t.text :description
t.references :writer, index: true, null: false
t.boolean :shared, default: true
t.timestamps
end
end
end
<% if controller_name == "favlinks" %>
# in Gemfile
gem 'attr_searchable'
# in models/favlink.rb
class Favlink < ActiveRecord::Base
include AttrSearchable
attr_searchable :title, :description
attr_searchable :bundlelink => "bundlelink.title"
end
# in controllers/favlinks_controller.rb
class FavlinksController < ApplicationController
def index
if params[:search]
@favlinks = Favlink.search(params[:search])
else
@favlinks = @bundlelink ? @bundlelink.favlinks : \
(params[:whose] ? Favlink.whose(current_user) : Favlink.shared)
end
@favlinks = @favlinks.paginate(page: params[:page], per_page: 10)
if request.xhr?
sleep(3)
render :partial => @favlinks
end
end
...
end
class Favlink < ActiveRecord::Base
include AttrSeachable
pageless ajax call
# in views/layouts/shared/_sidebar.html.erb
<form action="/favlinks" class="navbar-form" style="margin-bottom:1.5em;">
<div class="input-group">
<input type="Search" name="search" placeholder="Search ..." class="form-control"
value="<%= params[:search] %>" />
<div class="input-group-btn">
<button class="btn btn-info">
<span class="glyphicon glyphicon-search"></span>
</button>
</div>
</div>
</form>
# in Gemfile
gem 'will_paginate'
gem 'pageless-rails', github: 'rorlab/pageless-rails'
pageless-rails v0.0.3 (by rorlab)
https://github.com/rorlab/pageless-rails
//= require jquery
//= require jquery.pageless
//= require jquery_ujs
//= require bootstrap
//= require_tree .
//= require turbolinks
in app/assets/javascripts/application.js
Module ApplicationHelper
def pageless(total_pages, url=nil, container=nil)
opts = {
:totalPages => total_pages,
:url => url,
:loaderMsg => 'Loading more pages...',
:loaderImage => asset_path('load1.gif')
}
container && opts[:container] ||= container
javascript_tag("$('#{container}').pageless(#{opts.to_json});")
end
end
in app/helpers/application_helper.rb
in config/initializers/assets.rb
Rails.application.config.assets.precompile += %w( load1.gif )
load.gif
load1.gif
<div id='favlinks'>
<%= render @favlinks %>
</div>
<div id="last_page"></div>
<%= will_paginate @favlinks %>
<%= pageless(@favlinks.total_pages, favlinks_path, '#favlinks') %>
in Gitbook,
gem 'acts-as-taggable-on'
in Gemfile,
in Terminal,
$ rake acts_as_taggable_on_engine:install:migrations
Copied migration 20140619084755
_acts_as_taggable_on_migration.acts_as_taggable_on_engine.rb
Copied migration 20140619084756
_add_missing_unique_indices.acts_as_taggable_on_engine.rb
Copied migration 20140619084757
_add_taggings_counter_cache_to_tags.acts_as_taggable_on_engine.rb
in app/models/favlink.rb,
class Favlink < ActiveRecord::Base
...
acts_as_taggable
...
end