Category Archives: Ruby on Rails

Learn Ruby on Rails

可愛極簡風的CMS — Refinery

http://refinerycms.com/guides/how-to-install-refinery-on-heroku

vim Gemfile.lock

bundle update rake

rake 0.8.7

heroku rake db:create && heroku rake db:migrate

heroku restart

好用的教育系統 Fedena

http://www.projectfedena.org/install

https://github.com/projectfedena


/ROR/fedena$ rvm list

rvm rubies

ruby-1.9.2-p180 [ x86_64 ]
=> ree-1.8.7-2011.03 [ x86_64 ]

@WOW:~/ROR/fedena$ rvm gemset list

gemsets for ree-1.8.7-2011.03
global
=> r235
r239
r309

@WOW:~/ROR/fedena$ rails -v
Rails 2.3.5

https://github.com/projectfedena/projectfedena_v2.0

gem install rails -v=2.3.5

gedit /config/database.yml

sudo gem install prawn -v=0.6.3

rake gems:install

rake db:create

rake db:migrate

chmod +x script/*

chmod 755 script/server

script/server

gem list

actionmailer (2.3.5)
actionpack (2.3.5)
activerecord (2.3.12, 2.3.5)
activeresource (2.3.5)
activesupport (2.3.12, 2.3.5)
declarative_authorization (0.5.3)
hoe (2.10.0)
mysql (2.8.1)
prawn (0.6.3)
prawn-core (0.6.3)
prawn-format (0.2.3)
prawn-layout (0.3.2)
prawn-security (0.1.1)
rack (1.0.1)
rails (2.3.5)
rake (0.8.7, 0.8.3)
rubygems-update (1.4.2)
searchlogic (2.5.6)
test-unit (1.2.3)

git init
git config --global user.name "Your Name"
git config --global user.email @gmail.com
git config --global github.user name
git config --global github.token

git commit -m 'first commit'
git remote add origin git@github.com:name/fedena.git

git remote rm origin

git push -u origin master

======================================================================
Deploy to Heroku:

git clone git://github.com/chondm/fedena.git
rm lib/tasks/rspec.rake

gem install -v=2.3.11

gem install bundler
bundle install
script/server

EDIT limit option in files follow:
db/migrate/20090622104053_create_students.rb
db/migrate/20090622104054_create_archived_students.rb
db/migrate/20090910062751_create_employees.rb
db/migrate/20100604103435_create_archived_employees.rb

ex: Change
t.binary :photo_data, :limit => 75.kilobytes
To
t.binary :photo_data

gem install heroku
heroku keys:add
heroku create
git push heroku master
heroku rake db:migrate

RoR的IDE就用gedit吧

參考:

Improving your gedit with gmate – a great code editor!


https://github.com/gmate/gmate

sudo add-apt-repository ppa:ubuntu-on-rails/ppa
sudo apt-get update
sudo apt-get install gedit-gmate

cd Downloads/
wget http://dl.dropbox.com/u/5986982/gmate.sh

chmod +x gmate.sh
sh gmate.sh
sudo apt-get install git-core

git clone git://github.com/gmate/gmate.git
cd gmate
sh install.sh

vim /etc/mime.types
text/x-ruby-source rhtml html.erb erb
text/x-eruby rjs
text/x-yaml yml yaml

sudo apt-get install gedit-plugins


gem install rails-footnotes

GIT clone
1) git clone git://github.com/MadsBuus/gedit-snapopen-plugin.git
2) create a symlink:
mkdir -p ~/.gnome2/gedit/
cd ~/.gnome2/gedit/
ln -s [path/to/clone] plugins

參考:RubyBuntu -4- Make gedit better than any IDE 😉
http://rbjl.net/22-rubybuntu-4-make-gedit-better-than-any-ide

參考: 把Gedit打造為Linux下的Textmate
http://kewang.pixnet.net/blog/post/14958399

gedit:

sudo apt-get install gedit-plugins

wget http://www.stambouliote.de/download/gedit_classbrowser-0.2.1.tar.gz
wget http://elias.hiex.at/gedit-plugins/auto_completion.gedit-plugin
wget http://elias.hiex.at/gedit-plugins/auto_completion.py

wget http://web2linux.com/wp-content/uploads/rails_syntax.tar.gz
tar
sudo mv rhtml.lang ruby_on_rails.lang yaml.lang /usr/share/gtksourceview-2.0/language-specs/
sudo mv rails.xml /usr/share/mime/packages/

autotest:

wget http://downloads.sourceforge.net/ruby-gnome2/ruby-gtk2-0.90.8.tar.gz

tar xzvf ruby-gtk2-0.90.8.tar.gz
cd ruby-gtk2
ruby extconf.rb
make
make install

wget http://downloads.sourceforge.net/ruby-gnome2/ruby-gnome2-all-0.90.8.tar.gz

tar xzvf ruby-gnome2-all-0.90.8.tar.gz
cd ruby-gnome2
ruby extconf.rb
make
make install

wget http://rubyforge.org/frs/download.php/27134/ruby-libnotify-0.3.3.tar.bz2
tar -xvjpf ruby-libnotify-0.3.3.tar.bz2
cd ruby-libnotify-0.3.3
ruby extconf.rb
make
sudo make install

gem install gtk2

sudo apt-get install libnotify-bin

notify-send hello world

sudo vim ~/.autotest

module Autotest::GnomeNotify

# Time notification will be displayed before disappearing automatically
EXPIRATION_IN_SECONDS = 2
ERROR_STOCK_ICON = “gtk-dialog-error"
SUCCESS_STOCK_ICON = “gtk-dialog-info"

# Convenience method to send an error notification message
#
# [stock_icon] Stock icon name of icon to display
# [title] Notification message title
# [message] Core message for the notification
def self.notify stock_icon, title, message
options = “-t #{EXPIRATION_IN_SECONDS * 1000} -i #{stock_icon}"
system “notify-send #{options} ‘#{title}’ ‘#{message}'"
end

Autotest.add_hook :red do |at|
notify ERROR_STOCK_ICON, “Tests failed", “#{at.files_to_test.size} tests failed"
end

Autotest.add_hook :green do |at|
notify SUCCESS_STOCK_ICON, “All tests passed, good job!", “"
end

end

apt-get install libinotify-ruby libgtk2-ruby libnotify-dev
sudo gem install ZenTest autotest autotest-rails redgreen launchy

wget http://thelucid.com/files/autotest_images.zip
unzip autotest_images.zip
mv autotest_images ~/.autotest_images

sudo vim ~/.autotest

require ‘rnotify’
require ‘launchy’
require ‘gtk2’

module Autotest::RNotify
class Notification
attr_accessor :verbose, :image_root, :tray_icon, :notification,
:image_pass, :image_pending, :image_fail,
:image_file_pass, :image_file_pending, :image_file_fail,
:status_image_pass, :status_image_pending, :status_image_fail

def initialize(timeout = 5000,
image_root = “#{ENV[‘HOME’]}/.autotest_images" ,
report_url = “doc/spec/report.html",
verbose = false)
self.verbose = verbose
self.image_root = image_root
self.image_file_pass = “#{image_root}/pass.png"
self.image_file_pending = “#{image_root}/pending.png"
self.image_file_fail = “#{image_root}/fail.png"

raise(“#{image_file_pass} not found") unless File.exists?(image_file_pass)
raise(“#{image_file_pending} not found") unless File.exists?(image_file_pending)
raise(“#{image_file_fail} not found") unless File.exists?(image_file_fail)

puts ‘Autotest Hook: loading Notify’ if verbose
Notify.init(‘Autotest’) || raise(‘Failed to initialize Notify’)

puts ‘Autotest Hook: initializing tray icon’ if verbose
self.tray_icon = Gtk::StatusIcon.new
tray_icon.pixbuf = Gdk::Pixbuf.new(image_file_pending,22,22)
tray_icon.tooltip = ‘RSpec Autotest’

puts ‘Autotest Hook: Creating Notifier’ if verbose
self.notification = Notify::Notification.new(‘X’, nil, nil, tray_icon)

notification.timeout = timeout

puts ‘Autotest Hook: Connecting mouse click event’ if verbose
tray_icon.signal_connect(“activate") do
Launchy::Browser.new.visit(report_url)
end

Thread.new { Gtk.main }
sleep 1
tray_icon.embedded? || raise(‘Failed to set up tray icon’)
end

def notify(icon, tray, title, message)
notification.update(title, message, nil)
notification.pixbuf_icon = icon
tray_icon.tooltip = “Last Result: #{message}"
tray_icon.pixbuf = tray
notification.show
end

def passed(title, message)
self.image_pass ||= Gdk::Pixbuf.new(image_file_pass, 48, 48)
self.status_image_pass ||= Gdk::Pixbuf.new(image_file_pass, 22, 22)
notify(image_pass, status_image_pass, title, message)
end

def pending(title, message)
self.image_pending ||= Gdk::Pixbuf.new(image_file_pending, 48, 48)
self.status_image_pending ||= Gdk::Pixbuf.new(image_file_pending, 22, 22)
notify(image_pending, status_image_pending, title, message)
end

def failed(title, message)
self.image_fail ||= Gdk::Pixbuf.new(image_file_fail, 48, 48)
self.status_image_fail ||= Gdk::Pixbuf.new(image_file_fail, 22, 22)
notify(image_fail, status_image_fail, title, message)
end

def quit
puts ‘Autotest Hook: Shutting Down…’ if verbose
#Notify.uninit
Gtk.main_quit
end
end

Autotest.add_hook :initialize do |at|
@notify = Notification.new
end

Autotest.add_hook :ran_command do |at|
results = at.results.last

unless results.nil?
output = results[/(\d+)\s+examples?,\s*(\d+)\s+failures?(,\s*(\d+)\s+pending)?/]
if output
failures = $~[2].to_i
pending = $~[4].to_i
end

if failures > 0
@notify.failed(“Tests Failed", output)
elsif pending > 0
@notify.pending(“Tests Pending", output)
else
unless at.tainted
@notify.passed(“All Tests Passed", output)
else
@notify.passed(“Tests Passed", output)
end
end
end
end

Autotest.add_hook :quit do |at|
@notify.quit
end
end

Ruby for Rails 學習心得01

Ruby for Rails : Rails開發者必備的Ruby學習手冊 –> 大推

Ruby code 怎麼去寫讀local檔和建立新文字檔,修改文字檔:
1. read file
a = File.read(“filename")

2. create file
a = File.new(“filename", “w")
a.close

3. write to file
a = File.new(“filename", “w")
a.puts b
a.close

*** ruby tool 實用工具(大推):
1. ruby -r debug filename (debug)
2. ruby -r profile filename (check utilization)

*** what’s erb 什麼是 .erb
1. 在html檔案裡面,置入Ruby code
2. 將RoR模子->操作->呈現,就是最後一個呈現網頁結果,通常會是或是
3. 執行erb和執行rb不一樣,執行erb要用 “erb filename"

*** 所謂的MVC結構
串起M-V-C,一定是Rails:

/usr/local/rvm/gems/ruby-1.9.2-p180/gems/rails-3.0.1

M的解釋:

Model (ActiveRecord): /usr/local/rvm/gems/ruby-1.9.2-p180/gems/activerecord-2.3.2/

C的解釋:

Control (ActionController): /usr/local/rvm/gems/ruby-1.9.2-p180/gems/actionpack-3.0.1/lib/action_controller

V的解釋:

View (ActionView): /usr/local/rvm/gems/ruby-1.9.2-p180/gems/actionpack-3.0.1/lib/action_view

Ruby on Rails 初學當中 — 拜讀 “Ruby Programming:向Ruby之父學程序設計"

Ruby Programming:向Ruby之父學程序設計
這本真完全給初學者念的,如果沒有任何基礎概念的,這本就對了^^!

Good online Ruby API index
http://www.gotapi.com/html

Day 12 — Ruby on Rails 終章

快被這段搞到費屬人,最後面除了看screencast之外,book一定要喵到,否則除蟲除不完>_<:

class Micropost 'microposts.created_at DESC'

# Return microposts from the users being followed by the given user.
scope :from_users_followed_by, lambda { |user| followed_by(user) }

private

# Return an SQL condition for users followed by the given user.
# We include the user's own id as well.
def self.followed_by(user)
following_ids = user.following_ids
where("user_id IN (#{following_ids}) OR user_id = :user_id",
{ :user_id => user })
end
end

總算全部搞定 ︿︿!成績單在此:http://falling-winter-693.heroku.com/
但,長路漫漫啊 >_<

Day 11 Ruby on Rails ….it was the :destory action for the authenticate before_filter

作者提出課程設計與實際運用,有出入,有些問題

http://getsatisfaction.com/railstutorial/topics/userscontroller_delete_destroy_as_a_non_signed_in_user_should_deny_access

原始code在此:

https://github.com/railstutorial/sample_app

Ruby on Rails — 丸來系乙

http://realblog.zkiz.com/greatsoup38/19103

http://www.ithome.com.tw/itadm/article.php?c=59621&s=4

http://blog.xdite.net/?p=2287

Day 10 Ruby on Rails — Damn

http://getsatisfaction.com/railstutorial/topics/upside_down_users_show_and_users_edit_in_spec_requests_friendly_forwardings_spec_rb

Day 9 Ruby on Rails — how to use .self


vim app/helpers/sessions_helper.rb

module SessionsHelper

def sign_in(user)
cookies.permanent.signed[:remember_token] = [user.id, user.salt]
self.current_user = user
#change to => current_user = user; can not add "self."
end
.
.
.
def sign_out
cookies.delete(:remember_token)
self.current_user = nil
#change to => current_user = nil; can not add "self."
end

private
.
.
.
end


git status
git add .
git commit -am "Complete..."
git checkout master
git merge sign-in-out
git push && git push heroku