Top Banner
Ruby on Rails BY thegiive ( License by CC 姓名標示 2.0 )
95

Ruby on Rails in UbiSunrise

May 10, 2015

Download

Technology

Wisely chen

My Talk in UbiSunrise 4/22
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Ruby on Rails in UbiSunrise

Ruby on Rails

BY thegiive

( License by CC 姓名標示 2.0 )

Page 2: Ruby on Rails in UbiSunrise

About me

• 網路 ID : thegiive

• SocialPicks.com 技術顧問

• 使用 Ruby on Rails 開發網頁程式

• Blog : http://lightyror.thegiive.net/

• Email : thegiive at gmail . com

Page 3: Ruby on Rails in UbiSunrise

Outline

• 關於我使用 Ruby on Rails 的故事

• Ruby on Rails 簡介

• Rails 高生產力的來源

• Active Record 火力展示

• 一些議題

• Active Record 進階火力展示

Page 4: Ruby on Rails in UbiSunrise

關於我和Ruby on Rails的故事

0

23.75

47.50

71.25

95.00

6 7 8 9 10 11 12 1 2 3 4

工作效率A

B C

Page 5: Ruby on Rails in UbiSunrise

A 點

0

23.75

47.50

71.25

95.00

6 7 8 9 10 11 12 1 2 3 4

工作效率

Page 6: Ruby on Rails in UbiSunrise

Before Rails day

• 使用 PHP

• 每天都被催進度• 上班時間被工作佔滿

10%

90%

工作 其他

Page 7: Ruby on Rails in UbiSunrise

After Rails day

• 使用 2 天的時間改寫了一個用PHP寫了兩週的 Project

• 換成我在催美工進度• 小型頁面平均一兩個小時可以完成

10%

20%

30%

40%

工作 Survey Bloging打混

Page 8: Ruby on Rails in UbiSunrise

Phone time project

• 完成了 “ Blog 換裝,國片啟航 “ 的後端程式

• 撰寫約 20 行 code

• 讀取 3 個資料表

Page 9: Ruby on Rails in UbiSunrise

好的工具可以帶人上天堂

Page 10: Ruby on Rails in UbiSunrise

B 點

0

23.75

47.50

71.25

95.00

6 7 8 9 10 11 12 1 2 3 4

工作效率

Page 11: Ruby on Rails in UbiSunrise

由於效率太高了

• 開始接了不少案子• 最忙時刻• 一個正職• 兩個兼職• 寫書

Page 12: Ruby on Rails in UbiSunrise

PHP

Rails

0 25 50 75 100

Coding RD溝通外部溝通 美工

Page 13: Ruby on Rails in UbiSunrise

工作效率變快,但是溝通成本依舊

Page 14: Ruby on Rails in UbiSunrise

C 點

0

23.75

47.50

71.25

95.00

6 7 8 9 10 11 12 1 2 3 4

工作效率

Page 15: Ruby on Rails in UbiSunrise

我買了兩個東西

PS2 太空戰士10

Page 16: Ruby on Rails in UbiSunrise

After PS2 Day

• 想做的事情很多• 想搞的 Project 很多

• 依舊沒有時間可以做更多事情(怪誰呢)

20%

30%

10%

10%

30%

工作 Blog SurveyPS2 看比賽

Page 17: Ruby on Rails in UbiSunrise

警告

當你在家工作時,特別是老闆主管都在國外,無法監督你的時候,以下商品危險性相當的高。使用請注意有沒有嚴重的副作用!!!

Page 18: Ruby on Rails in UbiSunrise

Ruby on Rails

• MVC 框架

• 以 Ruby 為主要語言

• DHH 為了 Basecamp 這個 Project 而開發出來

Page 19: Ruby on Rails in UbiSunrise

創造者

• David Heinemeier Hansson

• 網路簡稱 DHH

• 當時才 26 歲

圖片出自DHH Blog

Page 20: Ruby on Rails in UbiSunrise

How Hot is Rails

• Rails 2006研討會 400個座位一周內賣完,加開的 150個也在 24小時內賣完

• 2005年 Ruby on Rails 書籍銷售成長 1500% ,2006年又成長了 700%

• Ruby 佔有率一年內從 26名暴增到13名

Page 21: Ruby on Rails in UbiSunrise

Rails Clone

Page 22: Ruby on Rails in UbiSunrise

席捲全球的原因

•超快速網頁程式開發

Page 23: Ruby on Rails in UbiSunrise

開發速度有多快?

JAVA (Spring/Hibernate) Rails

時間 4 個月 (20小時/周 ) 4 晚 ( 5小時/晚)程式碼 3293 1164

設定檔1161 113

Class/method 62/549 55/126

Justin Gehtland 在他的Blog發表的數據

Page 24: Ruby on Rails in UbiSunrise

快速開發到底有多重要

• RD 內部溝通成本變低

• 凡事比較敢嘗試• 快速應付外界變化• 快速開發Web Site雛型,方便業務單位交涉

• 有Performance之後,要錢就比較容易!!

Page 25: Ruby on Rails in UbiSunrise

很可惜剛剛講的都是官話

Page 26: Ruby on Rails in UbiSunrise

• 你可以有時間打混,而且老闆還會稱讚你

• 可以早點下班• 可以整天玩 PS2,還不會被發現

快速開發重要性(機密版)

Page 27: Ruby on Rails in UbiSunrise

程式碼大量減少

• Justin Gehtland 提出 JAVA : Rails = 3.5 : 1

• 有人做出 Java : Rails = 25 : 1

• Proc.net 提出 PHP : Rails = 10 : 1

• JavaEye 站長估計 JAVA : Rails = 10 : 1

• 我實驗的結果 PHP : Rails = 8 : 1

網址 http://lightyror.blogspot.com/2006/10/rails.html

Page 28: Ruby on Rails in UbiSunrise

Rails 架構

Page 29: Ruby on Rails in UbiSunrise

高速開發的原因

• ORM

• Convention over Configuration

• Don’t Repeat Yourself

• Domain Specific Language

Page 30: Ruby on Rails in UbiSunrise

ORM

• Object Relational Mapping

• 將資料庫操作用物件導向的形式來呈現• Propel ,Hibernate ,Active Record ... 等

Page 31: Ruby on Rails in UbiSunrise

Convention over Configuration

• 不需要額外設定檔,一切以預設值加快研發速度

• 但是總是有辦法可以更改預設的方式

<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE hibernate-mappingPUBLIC "-//Hibernate/Hibernate Mapping DTD//EN""http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd"><hibernate-mapping><class name="test.hibernate.Order" table="orders"><id name="id" type="string" unsaved-value="null" ><column name="id" sql-type="integer" not-null="true"/><generator class="native"/></id><key column="order_id" /><one-to-many class="test.hibernate.OrderItem" /></set></class></hibernate-mapping>

Page 32: Ruby on Rails in UbiSunrise

DRY

• Don’t Repeat Yourself

• View 裡面有 Helper,Partial 方便 DRY

• Controller 裡面有 Module ,Lib 等方便DRY

Page 33: Ruby on Rails in UbiSunrise

Domain Specific Language

• Ruby 是一個很好的 DSL 製造語言

• Rails 是 DB-Driven Web App 的 DSL

• has_one , has_many , HABTM

• 大量使用 symbol ( :abc )

Page 34: Ruby on Rails in UbiSunrise

Migration

create_table :stores do |t| t.column :name , :string t.column :info , :text t.column :open_time , :string t.column :user_id , :integer t.column :created_at , :datetime t.column :updated_at , :datetime end

Page 35: Ruby on Rails in UbiSunrise

Active Record

• Rails 的心臟

• 目前數一數二的 ORM 技術

• Convention over Configuration

• 可以處理 1:1 ,1:m ,n:m 等資料庫關係

• 支援 MySQL ,SQL Server ,Oracle ,SQLite 等資料庫

Page 36: Ruby on Rails in UbiSunrise

ORM Example

Peopleid int

name varchar

phone varchar

address text

sex enum

a = People.new

a = People.find(1)

a.name = ‘David’

a.destroy

a.save

Page 37: Ruby on Rails in UbiSunrise

Relation Table Example

Peopleid int

name varchar

phone varchar

address text

sex enum

Article

id int

person_id int

title varchar

content text

created_at datatime

Page 38: Ruby on Rails in UbiSunrise

Relation ORMa = People.find(1)

a.articles 代表這個人寫的所有文章列表

a.articles.find(:first) 這個人寫的第一篇文章

a.articles << Article.new(...) Person a 新寫一篇文章

Page 39: Ruby on Rails in UbiSunrise

缺點?

• 速度• 國際化• Legacy系統的支援

• Template 支援度

Page 40: Ruby on Rails in UbiSunrise

大家都會這樣講

“ Ruby on Rails 是好東西,可惜他不是 xxx 寫的 ” 這句話我至少聽過 JAVA,

Python,Perl 三個版本

Page 41: Ruby on Rails in UbiSunrise

其他語言可以作出 Rails 嗎?

• 任何事情都有可能.....但....

• DHH 曾經解釋使用 Ruby 原因

• Ruby 的 DSL 能力

• 我曾經使用其他框架的體驗• 程式碼完成度,文件,參與人數多寡

Page 42: Ruby on Rails in UbiSunrise

“ 如果一個框架可以讓你僅變快 20%,或許你應該繼續使用比較保險的語言,像是 JAVA。但是如果你可以變快 300% ,甚至更高,那麼其他的

差異都變得不重要了。”

From Beyond Java

Page 43: Ruby on Rails in UbiSunrise

該怎麼讀書

Page 44: Ruby on Rails in UbiSunrise

Ruby 聖經?

• 你可以學到很紮實的 Ruby Programmming

• Ruby is not Rails

• Rails 只用到Ruby 60%

Page 45: Ruby on Rails in UbiSunrise

小習題

City

id int

name varchar

Neighbor

city_id int

neighbor_id int

程式會給兩個城市,目的是判斷 “ 隔壁 “ , ” 鄰近 “ 還是 ” 很遠 “ ?

Page 46: Ruby on Rails in UbiSunrise

難度

• city 跟自己是呈現 自我 n:m 關係(neighbor 是 join table)

• 要找到兩層以上的深度• 要寫的容易閱讀

Page 47: Ruby on Rails in UbiSunrise

function 判斷 ( A城市,B城市){ if( B城市是A城市的鄰居) { return ‘隔壁’ }else{ for ( A城市的每個鄰居城市,設為城市C){ if( B城市是C城市的鄰居){ return ‘鄰近’ } } return ‘很遠’ }}

Pseudo Code

Page 48: Ruby on Rails in UbiSunrise

def check ( city_a , city_b ) if ( city_a.neighbors.include?(city_b) return ‘隔壁’ else city_a.neighbors.each do | neighbor_of_a | if (neighbor_of_a.neighbors.include?(city_b) ) return ‘鄰近’ end end return ‘很遠’ endend

Rails Code

Page 49: Ruby on Rails in UbiSunrise

接下來要幹麼

Page 50: Ruby on Rails in UbiSunrise

話說從頭

• 我答應 Ubi 的時候忘了問時間

• 也忘了問 Topic

• 星期五才發現事實的真相

Page 51: Ruby on Rails in UbiSunrise

快快樂樂聊 RoR * 1hr

RoR in pratice * 3hr

Page 52: Ruby on Rails in UbiSunrise
Page 53: Ruby on Rails in UbiSunrise

為了這個演講

• 禮拜六不能出去玩• 不能玩太空戰士10

• 三個小時準備投影片• 三個小時準備 Demo Code

Page 54: Ruby on Rails in UbiSunrise

愛吃網

• 仿自愛評網• 美食評鑑網站• Inspire by 一家可怕的義大利麵店

Page 55: Ruby on Rails in UbiSunrise

Code Stats

• Code 270 行

• 230行自動產生

• 手動撰寫 40行

• 80%時間花在版面

Page 56: Ruby on Rails in UbiSunrise

完成功能

• 美食評鑑• Tag

• Urmap 地圖

• Search

• 使用者帳號系統

Page 57: Ruby on Rails in UbiSunrise

功能 Outline

• 基本程式架構• 用戶登入• 縮圖製作• 搜尋撰寫• Urmap API

• Tag

Page 58: Ruby on Rails in UbiSunrise

等一下我要做一件很可怕的事情

Page 59: Ruby on Rails in UbiSunrise

Live Demo RoR 開發

• 非常的危險• 很多人都吃過這個虧• Contagions 也不例外

Page 60: Ruby on Rails in UbiSunrise

如果現在就放棄的話Demo 就結束了

Page 61: Ruby on Rails in UbiSunrise

因為我們是強者

Page 62: Ruby on Rails in UbiSunrise

最新嘗試

• Live Demo With Subversion

• 每個 reversion 代表一個階段

• May the force be with me

Page 63: Ruby on Rails in UbiSunrise

基本程式架構

Users

id int

Storesid int

user_id int

Commentsid int

user_id intstore_id int

Page 64: Ruby on Rails in UbiSunrise

直接看 code

• svn Committed revision 9.

Page 65: Ruby on Rails in UbiSunrise

遇到的第一個問題太難看了

Page 66: Ruby on Rails in UbiSunrise

版面配置

• 對於一個國小美術從來沒拿過甲的人來說,與其設計一個版面,不如拿強者的版面來用

Page 67: Ruby on Rails in UbiSunrise

強者已經幫你寫好了

• Yullio :Mollio plugin for Rails

• 自動產生 layout

• script/generate layout_yullio stores sidebar

• 重新啟動 Rails

Page 68: Ruby on Rails in UbiSunrise

直接看 code

• svn Committed revision 10.

• 修改 sidebar 跟文字 : 11

• 修改 Store Layout : 12

Page 69: Ruby on Rails in UbiSunrise

用戶登入

• 有一打以上 Plugin 可以使用

• session 操作

• before_filter 操作

Page 70: Ruby on Rails in UbiSunrise

還記得那天

• 樂多朋友• 突然改成封閉制

Page 71: Ruby on Rails in UbiSunrise

直接看 code

• User 登入登出 : 13

• 後台權限控管 : 14

Page 72: Ruby on Rails in UbiSunrise

縮圖製作

• Rmagick

• 功能使用超級頻繁• 有沒有辦法簡化

Page 73: Ruby on Rails in UbiSunrise

file_column

• Base on Rmagick

• 完美結合 Rails

• Upload,設定,顯示都只要一行

Page 74: Ruby on Rails in UbiSunrise

直接看 code

• 縮圖管理 : 15

Page 75: Ruby on Rails in UbiSunrise

搜尋

• Search Engine + Web API

• Solr + act_as_solr

• Ferret + ast_as_ferret

Page 76: Ruby on Rails in UbiSunrise

SQL Search

• Like ‘%....%’

• 可以看到如何寫 SQL code

• Rails 對 SQL 做的安全防護

Page 77: Ruby on Rails in UbiSunrise

直接看 code

• Committed revision 16.

• 加入了 partial template

Page 78: Ruby on Rails in UbiSunrise

Urmap API

• 國內最好的圖資業者• 國防役未來公司?• UrMap API 目前並不包含地址定位、地標搜尋或是導航等服務

Page 79: Ruby on Rails in UbiSunrise

兩個解決方式

• 教育使用者用經緯度來代表餐廳位置• “ Hello,明天約在東經25,北緯121度的地方見,不見不散 “

• 每個人手中拿一個 GPS 來找路

• 自行將地址轉為經緯度

Page 80: Ruby on Rails in UbiSunrise

該如何將地址轉為經緯度呢?

Page 81: Ruby on Rails in UbiSunrise

我朋友的好點子

請一個正妹打字員

每天將地址換算經緯度

近水樓台

嘿嘿嘿...

Page 82: Ruby on Rails in UbiSunrise

這點子不差不過太宅了

不適用於強者(自稱)

Page 83: Ruby on Rails in UbiSunrise

強者的思維

才不會忘記你

寫一個自動轉換程式

把請打字員的錢省下來

省下來的錢跟正妹約會

Page 84: Ruby on Rails in UbiSunrise

Cron Script

• 使用 ActiveRecord

• Ruby HTTP POST

Page 85: Ruby on Rails in UbiSunrise

直接看 code

• Commit reversion 17

Page 86: Ruby on Rails in UbiSunrise

Table Relationship

• Active Record 的拿手好戲

• 先進入到 console 模式建立 relationship

Page 87: Ruby on Rails in UbiSunrise

直接看 code

• Commit reversion 18

• ruby script/console

Page 88: Ruby on Rails in UbiSunrise

Comment

• 投票• 給意見

Page 89: Ruby on Rails in UbiSunrise

直接看 code

• Commit reversion 19

Page 90: Ruby on Rails in UbiSunrise

Tag

• ast_as_taggable

• model.tag_with('red library book')

• model.tags

Page 91: Ruby on Rails in UbiSunrise

直接看 code

• Committed revision 20.

• 列表加入縮圖 Committed revision 21

Page 92: Ruby on Rails in UbiSunrise

愛吃網完成

• 三個小時左右• 大部份時間花在美工• 不需花錢請正妹打字員

Page 93: Ruby on Rails in UbiSunrise

有人要投資愛吃網嗎?

Page 94: Ruby on Rails in UbiSunrise

有正妹要當打字員的嗎?

Page 95: Ruby on Rails in UbiSunrise

Q & A