https://deanpcmad.com/2014/setting-up-jenkins-ci-for-ruby-on-rails-testing/
Tag Archives: ruby on rails
[转]Ruby on Rails on Bash on Ubuntu on Windows
How to generate signature code for OAuth 1.0 by ruby on rails?
Signature generate class
lib/signature_generator.rb
module SignatureGenerator
class Generator
attr_reader :user_identifier, :consumer_key, :consumer_secret, :api_url, :method
def initialize(user_identifier, consumer_key, consumer_secret, api_url, method)
@user_identifier = user_identifier # Encrypted user_id
@consumer_key = consumer_key
@consumer_secret = consumer_secret
@api_url = api_url
@method = method # GET or POST
end
def generate_params
params = create_params
signature_base_string = signature_base_string(params)
signing_key = "#{consumer_secret}&" # or "#{consumer_secret}" different by third party api | consummer_secret + token_secret
params['oauth_signature'] = sign(signing_key, signature_base_string)
URI.encode_www_form(params)
end
private
def create_params
@create_params ||= {
oauth_version: '1.0',
oauth_nonce: generate_nonce,
oauth_timestamp: generate_timestamp,
oauth_consumer_key: consumer_key,
user_id: user_identifier, # necessary params
oauth_signature_method: 'HMAC-SHA1'
}
end
def generate_nonce(size = 32)
Base64.encode64(OpenSSL::Random.random_bytes(size)).gsub(/W/, '')
end
def generate_timestamp
Time.now.getutc.to_i.to_s
end
def signature_base_string(params)
encoded_params = params.sort.collect { |k, v| url_encode("#{k}=#{v}") }.join('%26')
"#{method}&#{url_encode(api_url)}&#{encoded_params}"
end
def url_encode(url)
CGI.escape(url)
end
def sign(key, base_string)
digest = OpenSSL::Digest.new('sha1')
hmac = OpenSSL::HMAC.digest(digest, key, base_string)
Base64.encode64(hmac).chomp.gsub(/n/, '')
end
end
end
Controller
require 'signature_generator'
class MyController < ApplicationController
include SignatureGenerator
def show
user_identifier = current_user.identifier # generate encrypted user_id
oauth = Generator.new(
user_identifier,
ENV['CONSUMER_KEY'],
ENV['CONSUMER_SECRET'],
ENV['API_URL'],
'GET'
)
sign = oauth.generate_params
@iframe_url = "#{ENV['IFRAME_BASE_URL']}?#{sign}"
end
end
Deploying Rails app using Nginx, Unicorn, Postgres and Capistrano to Digital Ocean
[Ruby on Rails]Active Job – Sidekiqを使ってのJobの実行 | Developers.IO
はじめに 先日、Ruby on Rails 4.2がリリースされました。このリリースで新たに追加された機能にActive Jobがあります。今回はActive Jobについての簡単な説明と、実際に動かしてみたソースについ […]
[Ruby on Rails]Active Job – Sidekiqを使ってのJobの実行 | Developers.IO
ActiveJob テスト方法
rspec でジョブのテストはこの gem があります:
[rspec-activejob]
https://github.com/gocardless/rspec-activejob
Rails のソースを見て、 ActiveJob は自分の Test Adapter があります:
https://github.com/rails/rails/blob/master/activejob/lib/active_job/queue_adapters/test_adapter.rb
「enqueued_jobs」というメソッドがあります。これはとてもいいものです!
————————————– マジック開始 ————————————–
describe 'whatever' do
include ActiveJob::TestHelper
after do
clear_enqueued_jobs
end
it 'should email' do
expect(enqueued_jobs.size).to eq(1)
end
end
この方法で、ジョブにあるタスクのサイズを確認できます。もっとできるもの、ここを見て、その中に何がありますか:
https://github.com/rails/rails/blob/master/activejob/lib/active_job/queue_adapters/test_adapter.rb#L42
Hashです!では、タスクの値も確認できますね!
p enqueued_jobs.to_s
Will get:
enqueued_jobs:[{:job=>PointJob, :args=>[1], :queue=>"default"}]
job -> job's class
args -> job's value
queue -> job's queue name
————————————– マジック完了 ————————————–
ここまで、ジョブの確認は十分でしょうか。
その他
-
「rspec-sidekiq」という「sidekiq」専用の rspec テストパッケージもあります:
https://github.com/philostler/rspec-sidekiq -
ActiveJob minitest
http://edgeapi.rubyonrails.org/classes/ActiveJob/TestHelper.html
参考
Ruby on Rails Test – Rspec & Capybara
English
http://www.sitepoint.com/basics-capybara-improving-tests
https://robots.thoughtbot.com/rspec-integration-tests-with-capybara
http://tutorials.jumpstartlab.com/topics/capybara/capybara_practice.html
Japanese
http://www.oiax.jp/rails/rspec_capybara_primer.html
http://easyramble.com/form-tests-with-capybara.html
How To Install Ruby on Rails with rbenv on Ubuntu 14.04 | DigitalOcean
How to add a gender column to user model under devise on rails?
In the application_controller.rb
before_action :configure_devise_permitted_parameters, if: :devise_controller?
protected
def configure_devise_permitted_parameters
registration_params = [:name, :sex, :description, :email, :password, :password_confirmation]
if params[:action] == 'update'
devise_parameter_sanitizer.for(:account_update) {
|u| u.permit(registration_params << :current_password)
}
elsif params[:action] == 'create'
devise_parameter_sanitizer.for(:sign_up) {
|u| u.permit(registration_params)
}
end
end
Generage a migration to add some custom fields to users table:
class AddNameSexDescriptionToUsers < ActiveRecord::Migration
def change
add_column :users, :name, :string
add_column :users, :sex, :integer, default: 0
add_column :users, :description, :text
end
end
Add enum to sex in the user model:
enum sex: [:unknown, :male, :female]
Read data in the devise’s registrations/edit.html.erb
view:
<%= f.select :sex, options_for_select(User.sexes.collect { |s| [s[0].humanize, s[0]] }, selected: current_user.sex), {}, class: "form-control" %>
Another way, add a helper method in the user model:
def self.sexes_for_select
sexes.keys.map{ |x| [x.humanize, x] }
end
Then the view(registrations/edit.html.erb
) will be:
<%= f.select :sex, User.sexes_for_select, {}, class: "form-control" %>