Screencast: Decent Exposure

decent_exposure ist ein Helfer für deklarative Interfaces in Rails-Controller. Ryan zeigt in dieser Woche wie das Plugin eingesetzt werden kann.

 

Download:

Download(21.9 MB, 9:55)
Alternativer Download für iPod & Apple TV(21.2 MB, 9:55)

 

Resourcen:

 

Quellcode:

[ruby]
# articles_controller.rb
expose(:articles) { Article.order(:name) }
expose(:article)

# or without decent_exposure

def articles
@articles ||= Article.order(:name)
end
helper_method :articles

def article
@article ||= params[:id] ? Article.find(params[:id]) : Article.new(params[:article])
end
helper_method :article

# comments_controller.rb
expose(:article)
expose(:comments) { article.comments }
expose(:comment)
[/ruby]

Screencast: jQuery Tokeninput

Autokompletierung ist eine hilfreiche Funktion für viele Anwender. Mit jQuery Tokeninput ist die relativ einfach umzusetzen. Ryan zeigt zudem noch wie es im Zusammenhang mit Rails eingesetzt werden kann.

 

Download:

Download(21.7 MB, 11:09)
Alternativer Download für iPod & Apple TV(20.4 MB, 11:09)

 

Resourcen:

 

Quellcode:

[bash]
bundle
rails g jquery:install
[/bash]

[ruby]
# Gemfile
gem "jquery-rails"

# models/book.rb
class Book < ActiveRecord::Base
attr_accessible :name, :author_tokens
has_many :authorships
has_many :authors, :through => :authorships
attr_reader :author_tokens

def author_tokens=(ids)
self.author_ids = ids.split(",")
end
end

# authors_controller.rb
def index
@authors = Author.where("name like ?", "%#{params[:q]}%")
respond_to do |format|
format.html
format.json { render :json => @authors.map(&:attributes) }
end
end
[/ruby]

[html]
<!– layouts/application.html.erb –>
<%= stylesheet_link_tag "application", "token-input-facebook" %>
<%= javascript_include_tag :defaults, "jquery.tokeninput" %>

<!– books/_form.html.erb –>
<p>
<%= f.label :author_tokens, "Authors" %><br />
<%= f.text_field :author_tokens, "data-pre" => @book.authors.map(&:attributes).to_json %>
</p>
[/html]

[javascript]
// application.js
$(function() {
$("#book_author_tokens").tokenInput("/authors.json", {
crossDomain: false,
prePopulate: $("#book_author_tokens").data("pre"),
theme: "facebook"
});
});
[/javascript]

Request Specs und Capybara

Request specs in RSpec sind einge großartige Möglichkeit den kompletten Applikation-Stack zu testen. Ryan zeigt diese Woche wie dies und capybara in Verbindung mit Selenium eingesetzt werden kann.

 

Download:

Download(20.3 MB, 13:16)
Alternativer Download für iPod & Apple TV(20.6 MB, 13:16)

 

Resourcen:

 

Quellcode:

[bash]
bundle
rails g rspec:install
rails g integration_test task
rake spec:requests
[/bash]

[ruby]
# Gemfile
group :development, :test do
gem ‚rspec-rails‘
gem ‚capybara‘, :git => ‚git://github.com/jnicklas/capybara.git‘
gem ‚launchy‘
gem ‚database_cleaner‘
end

# spec/requests/tasks_spec
describe "Tasks" do
describe "GET /tasks" do
it "displays tasks" do
Task.create!(:name => "paint fence")
visit tasks_path
page.should have_content("paint fence")
end

it "supports js", :js => true do
visit tasks_path
click_link "test js"
page.should have_content("js works")
end
end

describe "POST /tasks" do
it "creates task" do
visit tasks_path
fill_in "New Task", :with => "mow lawn"
click_button "Add"
page.should have_content("Successfully added task.")
page.should have_content("mow lawn")
end
end
end

# spec_helper.rb
require ‚capybara/rspec‘

RSpec.configure do |config|
# …
config.use_transactional_fixtures = false

config.before(:suite) do
DatabaseCleaner.strategy = :truncation
end

config.before(:each) do
DatabaseCleaner.start
end

config.after(:each) do
DatabaseCleaner.clean
end
end
[/ruby]

[html]
<!– tasks/index.html.erb –>
<%= link_to_function "test js", ‚$(this).html("js works")‘ %>
[/html]

Screencast: Internationalisierung im Backend

Internationalisierung ist für viele Applikationen unerlässlich und Rails bringt bereits einiges mit um es zu umzusetzen. Jedoch können YAML Dateien unübersichtlich werden, wenn der Umfang sehr groß ist. Ryan zeigt diese Woche wie Redis für die Verwaltung von Übersetzungen, über ein Webinterface, eingesetzt werden kann.

 

Download:

Download(18 MB, 11:11)
Alternativer Download für iPod & Apple TV(18.3 MB, 11:11)

 

Resourcen:

 

Quellcode:

[bash]
rails g controller translations index
brew install redis
[/bash]

[ruby]
# Gemfile
gem ‚redis‘

# config/initializers/i18n_backend.rb
TRANSLATION_STORE = Redis.new
I18n.backend = I18n::Backend::Chain.new(I18n::Backend::KeyValue.new(TRANSLATION_STORE), I18n.backend)

# translations_controller.rb
def index
@translations = TRANSLATION_STORE
end

def create
I18n.backend.store_translations(params[:locale], {params[:key] => params[:value]}, :escape => false)
redirect_to translations_url, :notice => "Added translation"
end
[/ruby]

[html]
<!– home/index.html.erb –>
<h1><%= t "welcome" %></h1>

<!– translations/index.html.erb –>
<h1>Translations</h1>

<ul>
<% @translations.keys.each do |key| %>
<li><%= key %>: <%= @translations[key] %></li>
<% end %>
</ul>

<h2>Add Translation</h2>

<%= form_tag translations_path do %>
<p>
<%= label_tag :locale %><br />
<%= text_field_tag :locale %>
</p>
<p>
<%= label_tag :key %><br />
<%= text_field_tag :key %>
</p>
<p>
<%= label_tag :value %><br />
<%= text_field_tag :value %>
</p>
<p><%= submit_tag "Submit" %></p>
<% end %>
[/html]

[text]
# config/locales/en.yml
en:
welcome: "Welcome"
[/text]

Eloquent Ruby

It’s easy to write correct Ruby code, but to gain the fluency needed to write great Ruby code, you must go beyond syntax and absorb the “Ruby way” of thinking and problem solving. In Eloquent Ruby, Russ Olsen helps you write Ruby like true Rubyists do–so you can leverage its immense, surprising power.

Olsen draws on years of experience internalizing the Ruby culture and teaching Ruby to other programmers. He guides you to the “Ah Ha!” moments when it suddenly becomes clear why Ruby works the way it does, and how you can take advantage of this language’s elegance and expressiveness.

Eloquent Ruby starts small, answering tactical questions focused on a single statement, method, test, or bug. You’ll learn how to write code that actually looks like Ruby (not Java or C#); why Ruby has so many control structures; how to use strings, expressions, and symbols; and what dynamic typing is really good for.

Next, the book addresses bigger questions related to building methods and classes. You’ll discover why Ruby classes contain so many tiny methods, when to use operator overloading, and when to avoid it. Olsen explains how to write Ruby code that writes its own code–and why you’ll want to. He concludes with powerful project-level features and techniques ranging from gems to Domain Specific Languages.

A part of the renowned Addison-Wesley Professional Ruby Series, Eloquent Ruby will help you “put on your Ruby-colored glasses” and get results that make you a true believer.