Programming

Ruby 使用 Enumerable 和 Comparable

Enumerable

要使用 Enumerable Module 時,只需要兩個步驟:首先 include Enumerable Module,然後在定義 each 方法。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
class Book
include Enumerable

attr_accessor :tags

def each
@tags.each do |tag|
yield tag
end
end
end

books = Books.new
books.count

這麼做的好處是,這樣就能使用所有 Enumerable Module 裡的 methods。

閱讀全文

Ruby 使用 attr accessor reader writer

attr

假設我們有一個 Book 的 class,裡面有他的參數名稱 name,然後想要對他存取參數的時候,會像這樣寫

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
class Book
@name = nil
def name
@name
end
def name=(name)
@name = name
end
# ....
end

book = Book.new
book.name # => nil
book.name = 'Cat'
book.name # => 'Cat'

閱讀全文

Ruby 建立不存在的目錄 與 TempFile 的路徑

Root Path

執行 ruby 程式的當前位置

1
2
Dir.pwd
# => "/Users/akii"

Exec File

執行 ruby 程式的檔案

1
2
__FILE__
# => "test.rb"

Create Dir If Not Exists

如果當前目錄下不存在 tmp 的資料夾,則創立一個。

1
2
tmpdir = "./tmp"
FileUtils.mkdir_p(tmpdir) unless File.exists?(tmpdir)

閱讀全文

Sass 整理一些常用的方法

Sass 整理

每次都要找實在是有點浪費時間,不如乾脆一次整理完。

If … Else …

1
2
3
4
5
6
7
8
9
10
$boolean: true !default;

div {
@if( $boolean ){
display: block;
}
@else {
display: none;
}
}

For

1
2
3
4
5
@for $i from 1 through 5 {
.wd-#{$i} {
width: #{$i * 20%};
}
}

閱讀全文

Ruby on Rails Assets Pipeline 的使用方式

Assets Pipeline

config/initializers/assets.rb 中把 assets 加入 precompile 的路徑,不建議使用 *.css *.js 加入所有的東西,因為會將一些有的沒的或是不需要的東西一起編譯進去。

assets pipeline 的主要好處就是把所有的 css 包成一個檔案,漸少 request 的數量,像是 application.css 會載入所有被 require 的檔案,最後只需要傳送一個 css 就行了,同理 javascript 也是如此。

Layout

看不懂在說什麼,直接看例子比較快。

假設現在我們的 layout 裡面有兩個不同的頁面 admin.html.erb 跟 user.html.erb,要分別讓他們使用不同的 css js。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
app
├--assets
│ ├--javascripts
│ │ ├--admin.js
│ │ ├--user.js
│ │ └--...
│ └--stylesheets
│ ├--admin.scss
│ ├--user.scss
│ └--...
└--view
└--layout
├--admin.html.erb
└--user.html.erb

但為了減少請求的數量,我們讓一個 layout 只需要一個 css 和一個 js,在 layout 中的話會像這樣:

1
2
3
4
5
6
7
# app/views/layouts/admin.html.erb
<%= stylesheet_link_tag 'admin', media: 'all' %>
<%= javascript_include_tag 'admin' %>

# app/views/layouts/user.html.erb
<%= stylesheet_link_tag 'user', media: 'all' %>
<%= javascript_include_tag 'user' %>

如果使用 admin 的 layout 就會載入 admin.css admin.js

如果使用 user 的 layout 就會載入 user.css user.js

閱讀全文

Ruby on Rails 在 production 環境下跑 rails 5 專案

Production Mode

資料庫 Migrate

migrate production 環境下的資料庫,至於設定檔就不在這邊多說了。

1
RAILS_ENV=production rake db:migrate

產生 Secret Key

config/secret.yml 這個檔案裡面需要 production 環境的 secret key。

1
2
3
# config/secret.yml
production:
secret_key_base: <%= ENV["SECRET_KEY_BASE"] %>

用終端機產生 secret key,然後放置環境變數中。

1
2
3
4
5
# 產生 secret key
rake secret

# 將剛剛產生出來的 secret key 放入環境變數中
export SECRET_KEY_BASE=$(rake secret)

閱讀全文

Ruby on Rails Devise 與 註冊時輸入使用者名稱

註冊時輸入使用者名稱

在產生 rails generate devise:views users 後,然後在註冊的地方增加 username 的欄位。

1
2
3
4
5
# app/views/users/registrations/new.html.erb
<div class="field">
<%= f.label :username %><br />
<%= f.text_field :username, autofocus: true %>
</div>

在 controller 增加允許使用者欄位的驗證。

1
2
3
4
5
6
7
8
9
10
11
12
# app/controllers/application_controller.rb
class ApplicationController < ActionController::Base
before_action :configure_permitted_parameters, if: :devise_controller?

protected

def configure_permitted_parameters
added_attrs = [:username, :email, :password, :password_confirmation, :remember_me]
devise_parameter_sanitizer.permit :sign_up, keys: added_attrs
devise_parameter_sanitizer.permit :account_update, keys: added_attrs
end
end

閱讀全文

Ruby on Rails 設定 Devise 與 註冊 登入 登出

Gemfile

測試環境為 rails 5

1
gem 'devise'

然後執行 bundle

Configuration

安裝 devise 的設定檔

1
rails generate devise:install

然後在 config/environments/development.rb 加上寄信認證的設定

1
2
3
# config/environments/development.rb
# config/environments/test.rb
config.action_mailer.default_url_options = { host: 'localhost', port: 3000 }

閱讀全文

Ruby on Rails 中使用 Rspec

Gemfile

1
2
3
group :development, :test do
gem 'rspec-rails'
end

然後執行 bundle

Initialize

建立新的 rspec 檔

1
rails generate rspec:install

閱讀全文

Ruby on Rails 用 Factory Girl 自動產生 rspec 測資

安裝

Gemfile 加上以下的部分,然後執行 bundle

1
2
3
4
# Gemfile
group :development, :test do
gem 'factory_girl_rails', '~> 4.0'
end

設定

spec/support/factory_girl.rb 新增一個檔案,且加入以下的東西。

1
2
3
4
5
6
7
# RSpec
## spec/support/factory_girl.rb
require 'factory_girl_rails'

RSpec.configure do |config|
config.include FactoryGirl::Syntax::Methods
end

然後在 spec/spec_helper.rb 的最前面加入以下行,使用 factory_girl 的設定檔。

1
2
## spec/spec_helper.rb
require 'support/factory_girl'

閱讀全文