wtorek, 28 lutego 2012
niedziela, 26 lutego 2012
How to proper test scopes (named scopes)
I just read few posts about "how to test named scopes". I'm asking - why should I use FactoryGirl and create tons of unnecessary objects just to test `order(:position).first`?!
How not to do this
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'spec_helper' | |
describe Post do | |
context '#recent' do | |
it 'should return most recent post' do | |
_, post = [Factory(:post), Factory(:post)] | |
Post.recent.should == post | |
end | |
end | |
context '#for_user' do | |
it 'should return only posts created by given user' do | |
post1, post2, _ = [Factory(:post, user_id: 1), Factory(:post, user_id: 1), Factory(:post, user_id: 2)] | |
Post.for_user(1).should == [post1, post2] | |
end | |
end | |
end |
How to do this
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'spec_helper' | |
describe Post do | |
context '#recent' do | |
it 'should return most recent post' do | |
post = mock_model Post | |
Post.should_receive(:order).with('created_at DESC').and_return post | |
post.should_receive(:first).and_return post | |
Post.recent | |
end | |
end | |
context '#for_user' do | |
it 'should return only posts created by given user' do | |
post = mock_model Post | |
Post.should_receive(:where).with(user_id: 1) | |
Post.for_user 1 | |
end | |
end | |
end |
czwartek, 16 lutego 2012
named_accessors: attr_accessors na sterydach
attr_accessor jest świetne (szczególnie jeżeli ktoś przychodzi do Rubiego np. z Javy), ale ma jedną zasadniczą wadę - nazwa zmiennej instancji == nazwa metody (settera i gettera). W 99% to bardzo dobrze, ale dla tego jednego procenta powstało named_accessors.
Mam nadzieję że komuś się kiedyś przyda :)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Foobar | |
def initialize(foo, bar) | |
@foo, @bar = foo, bar | |
end | |
named_reader :foo, as: :foobar | |
named_writer :bar, as: :barbaz | |
# named_accessor :variable_name, as: :method_name | |
end |
poniedziałek, 23 stycznia 2012
param_protected i Devise
Kilka dni temu pisałem już o gemie param_protected. Jeżeli używamy go razem z Devise, będziemy musieli zrobić zmodyfikować kontrolery i dodać definicję dozwolonych parametrów.
Po pierwsze musimy zmapować URL-e na nasze nowe kontrolery.
Po pierwsze musimy zmapować URL-e na nasze nowe kontrolery.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
MyApp::Application.routes.draw do | |
devise_for :users, controllers: { | |
registrations: 'auth/devise/registrations', | |
sessions: 'auth/devise/sessions', | |
# ... | |
} | |
end |
niedziela, 22 stycznia 2012
Hakierzy
Mam dość komentarzy na temat tych ciągłych ataków na rządowe strony. Jak można wypowiadać się na dany temat bez żadnej wiedzy.
Anonymous - cyber Al-Kaida?
Anonymous (ang., rzeczownik niepoliczalny), Anonimowi, Grupa Anonimowych – niezidentyfikowana grupa aktywistów z całego świata podejmująca protesty oraz inne działania w internecie. Działania przypisywane grupie "Anonymous" są podejmowane przez niezidentyfikowane jednostki, które przybierają nazwę "Anonymous" atakując serwery rządowe lub korporacyjne.W ostatnim czasie, na fali protestów przeciwko SOPA, PIPA czy ACTA w sposób zatrważający moim zdaniem roście poparcie dla grupy Anonymous. Poparcie graniczące wręcz z uwielbieniem. Chociaż może się wydawać, że Anonimowi są naszą ostatnią linią obrony, ostatnim bastionem wolności należy się zastanowić nad słusznością ich działań.
sobota, 21 stycznia 2012
Tapeta dla Rubistów
Choć grafik ze mnie żaden zrobiłem taką oto tapetę dla Rubistów: (oryginał tutaj. tak wiem, nałożyłem tylko logo Rubiego :P)
![]() |
Link do pobrania |
Pulpit prezentuje się teraz znokomicie :)
piątek, 20 stycznia 2012
czwartek, 19 stycznia 2012
Haskell-Platform (+ GHC) na Mac OS X 10.7 (Lion)
Czym jest haskell każdy wie (jak nie, to zapraszam tu), więc nie będę go reklamował tylko od razu przejdę do sedna. Zakładam też że każdy użytkownik OSX używa Homebrew do zarządzania pakietami (jeśli nie to gorąco zachęcam!).
Ponieważ instalacja ghc (kompilatora Haskella) chwilę trwa dlatego dobrze jest uruchomić instalację z opcją -v żebyśmy widzieli cały czas co się dzieje i czy przypadkiem nie występują błędy kompilacji etc.
Opcja 64 też jest ważna! Bez niej nie skompilujemy prawidłowo haskell-platform
Teraz możemy uruchomić REPL Haskella (komendą ghci)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
brew install -v ghc --64bit |
Opcja 64 też jest ważna! Bez niej nie skompilujemy prawidłowo haskell-platform
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
brew install -v haskell-platform |
Teraz możemy uruchomić REPL Haskella (komendą ghci)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let greet who = "Hello " ++ who ++ "!" | |
greet "World" {- Hello World! -} |
sobota, 14 stycznia 2012
param_protected wycina potrzebne parametry
param_protected to świetny gem, który przenosi funkcjonalność attr_accessible oraz attr_protected do kontrolerów. Składnia jest bardzo prosta:
Jedyny problem jest taki, że nie ma defaultowej listy dozwolonych parametrów. Tzn. że wycinane są takie atrybuty jak action, controller, id, utf8 czy (w przypadku używania devise) authenticity_token. Formularze nie będą przez to działać poprawnie, będziemy cały czas wylogowywani itd.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class PostsControllers < ApplicationController | |
param_accessible [post: [:title, :content, category: [:name]], only: [:create] | |
param_protected :accepted, only: [:create, :update] | |
end |
czwartek, 5 stycznia 2012
Capybara i Devise - mock logowania
Piszemy request spec-i dla naszej aplikacji.
No i problem! Jak zalogować użytkownika?
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'spec_helper' | |
describe 'PostsController' do | |
before { visit new_post_path } | |
it "should display form for post creation" do | |
pending "we need to sign in first!!!" | |
end | |
end |
Subskrybuj:
Posty (Atom)