Skip to content

Commit

Permalink
Merge branch 'master' of github.com:rhysd/daily_coding
Browse files Browse the repository at this point in the history
  • Loading branch information
rhysd committed May 12, 2012
2 parents 868a3c6 + 2720edf commit 9a97fb6
Show file tree
Hide file tree
Showing 11 changed files with 138 additions and 9 deletions.
5 changes: 5 additions & 0 deletions Capfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
load 'deploy'
# Uncomment if you are using Rails' asset pipeline
# load 'deploy/assets'
Dir['vendor/gems/*/recipes/*.rb','vendor/plugins/*/recipes/*.rb'].each { |plugin| load(plugin) }
load 'config/deploy' # remove this line to skip loading any of the default tasks
1 change: 1 addition & 0 deletions app/assets/javascripts/top.js.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,4 @@ $(document).ready ->
$('div#answers-unit .gist-part').remove()
$('div#answers-unit').append(data)


4 changes: 4 additions & 0 deletions app/assets/stylesheets/top.css.scss
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,7 @@
// You can use Sass (SCSS) here: http://sass-lang.com/

.signboard {line-height: 140% ;}

.lang-btn {
font-size: 18px;
}
8 changes: 8 additions & 0 deletions app/controllers/answers_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,14 @@ def index
else
Answer.order('updated_at DESC') # created_at指定して
end
authors = User.find(@answers.map {|a| a.id})
@answers = @answers.map.with_index do |a, i|
tmp = {}
tmp[:answer] = a
tmp[:author] = authors[i]
tmp
end
logger.debug(@answers)

html_str = render_to_string partial: 'index'
render text: html_str
Expand Down
4 changes: 2 additions & 2 deletions app/models/answer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@

class Answer < ActiveRecord::Base
extend DailyCoding
attr_accessible :lang, :url, :user_id
attr_accessible :lang, :url, :user

def self.create_or_update(uid, gist_url)
unless answer = Answer.find_by_url(gist_url)
answer = Answer.new do |a|
a.user = uid
a.user_id = uid
a.url = gist_url
a.lang = lang_type(gist_url)
a.body = self.hash_from_gist(gist_url + ".json")
Expand Down
9 changes: 6 additions & 3 deletions app/views/answers/_index.html.erb
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@

<div id='answers-body'>
<% @answers.each do |answer| %>
<div class="gist-part" data-src="<%= answer.url + ".json"%>">
<%== answer.body %>
<% @answers.each do |a| %>
<div class="author">
<p><a href="#">@<%= a[:author].login %></a></p>
</div>
<div class="gist-part">
<%== a[:answer].body %>
</div>
<% end %>
</div>
4 changes: 2 additions & 2 deletions app/views/top/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@
<li class="active">
<a class= "lang-btn" href="javascript:void(0);" data-href="/answers">ALL</a>
</li>
<% @langs.each_with_index do |lang, i| %>
<% @langs.each do |lang| %>
<li>
<a class="lang-btn" data-href=<%= "/answers/" + lang%> href="javascript:void(0);" ><%= lang %></a>
<a class="lang-btn" data-href=<%= "/answers/" + lang%> href="javascript:void(0);" ><%= lang %></a>
</li>
<% end %>
</ul>
Expand Down
66 changes: 66 additions & 0 deletions config/deploy.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@

# capistranoの出力がカラーになる
require 'capistrano_colors'

# cap deploy時に自動で bundle install が実行される
require "bundler/capistrano"

# RVMを利用している場合は必要
$:.unshift(File.expand_path('./lib', ENV['rvm_path']))
require 'rvm/capistrano'
set :rvm_ruby_string, '1.9.2'
set :rvm_type, :user

# リポジトリの設定
set :application, "daily_coding"
set :repository, "https://github.com/rhysd/#{application}.git"
set :scm, :git
set :branch, "master"
set :deploy_via, :remote_cache
set :deploy_to, "/var/www/#{application}"
set :rails_env, "production"

role :web, "www4257uf.sakura.ne.jp:65432" #デプロイ先SSHポートを指定(デフォルトは22)
role :app, "www4257uf.sakura.ne.jp:65432"
role :db, "www4257uf.sakura.ne.jp:65432", :primary => true

# SSHの設定
set :user, "y_uuki"
ssh_options[:port] = "65432"
ssh_options[:forward_agent] = true
default_run_options[:pty] = true

# cap deploy:setup 後、/var/www/ の権限変更
namespace :setup do
task :fix_permissions do
sudo "chown -R #{user}.#{user} #{deploy_to}"
end
end
after "deploy:setup", "setup:fix_permissions"

# Unicorn用に起動/停止タスクを変更
namespace :deploy do
task :start, :roles => :app do
run "cd #{current_path}; bundle exec unicorn_rails -c config/unicorn.rb -E #{rails_env} -D"
end
task :restart, :roles => :app do
if File.exist? "/tmp/unicorn_#{application}.pid"
run "kill -s USR2 `cat /tmp/unicorn_#{application}.pid`"
end
end
task :stop, :roles => :app do
run "kill -s QUIT `cat /tmp/unicorn.pid`"
end
end

# Rails3.1.1のProduction用
namespace :assets do
task :precompile, :roles => :web do
run "cd #{current_path} && RAILS_ENV=production bundle exec rake assets:precompile"
end
task :cleanup, :roles => :web do
run "cd #{current_path} && RAILS_ENV=production bundle exec rake assets:clean"
end
end
after :deploy, "assets:precompile"

42 changes: 42 additions & 0 deletions config/unicorn.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@

application = 'daily_coding'

listen "/tmp/unicorn_#{application}.sock"
pid "/tmp/unicorn_#{application}.pid"

worker_processes 6
preload_app true

# capistrano 用に RAILS_ROOT を指定
working_directory "/var/www/#{application}"

if ENV['RAILS_ENV'] == 'production'
shared_path = "/var/www/#{application}/shared"
stderr_path = "#{shared_path}/log/unicorn.stderr.log"
stdout_path = "#{shared_path}/log/unicorn.stdout.log"
end

# ログ
stderr_path File.expand_path('log/unicorn.log', ENV['RAILS_ROOT'])
stdout_path File.expand_path('log/unicorn.log', ENV['RAILS_ROOT'])

# ダウンタイムなくす
preload_app true

before_fork do |server, worker|
defined?(ActiveRecord::Base) and ActiveRecord::Base.connection.disconnect!

old_pid = "#{ server.config[:pid] }.oldbin"
unless old_pid == server.pid
begin
# SIGTTOU だと worker_processes が多いときおかしい気がする
Process.kill :QUIT, File.read(old_pid).to_i
rescue Errno::ENOENT, Errno::ESRCH
end
end
end

after_fork do |server, worker|
defined?(ActiveRecord::Base) and ActiveRecord::Base.establish_connection
end

2 changes: 1 addition & 1 deletion db/migrate/20120512082549_create_answers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ def change
t.string :url, :null => false
t.string :lang, :null => false
t.text :body, :null => false
t.integer :user
t.references :user

t.timestamps
end
Expand Down
2 changes: 1 addition & 1 deletion db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
t.string "url", :null => false
t.string "lang", :null => false
t.text "body", :null => false
t.integer "user"
t.integer "user_id"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
end
Expand Down

0 comments on commit 9a97fb6

Please sign in to comment.