Skip to content

Instantly share code, notes, and snippets.

@harfangk
harfangk / gist:785764
Created January 19, 2011 06:02
polymorphic
class Comment < ActiveRecord::Base
belongs_to :commentable, :polymorphic => true
end
class Article < ActiveRecord::Base
has_many :comments, :as => :commentable
end
class Photo < ActiveRecord::Base
has_many :comments, :as => :commentable
@harfangk
harfangk / gist:805250
Created February 1, 2011 01:40
authlogic deal
usersessionscontroller.rb
class UserSessionsController < ApplicationController
before_filter :require_no_user, :only => [:new, :create]
before_filter :require_user, :only => :destroy
def new
@user_session = UserSession.new
end
=========================================================================================================
-> bookshelf git:(master) ✗ rake test
Run options: --seed 28100
# Running:
F
Finished in 0.035472s, 28.1911 runs/s, 56.3822 assertions/s.
1) Failure:
>> dig irc.quake.net
; <<>> DiG 9.8.3-P1 <<>> irc.quake.net
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NXDOMAIN, id: 56425
;; flags: qr rd ra; QUERY: 1, ANSWER: 0, AUTHORITY: 1, ADDITIONAL: 0
;; QUESTION SECTION:
;irc.quake.net. IN A
{
"erb": {
"extends": "html",
"snippets": {
"erb": "<%= %>"
}
}
}
defmodule Rumbl.UserTest do
use Rumbl.ModelCase, async: true
alias Rumbl.User
@valid_attrs %{name: "A User", username: "eva", password: "secret"}
@invalid_attrs %{}
test "changeset with valid attributes" do
changeset = User.changeset(%User{}, @valid_attrs)
assert changeset.valid?
defmodule Rumbl.VideoChannel do
use Rumbl.Web, :channel
def join("videos:" <> video_id, _params, socket) do
{:ok, assign(socket, :video_id, String.to_integer(video_id))}
end
def handle_info(:ping, socket) do
count = socket.assigns[:count] || 1
push socket, "ping", %{count: count}
test/models/user_repo_test.ex
defmodule Storybook.UserRepoTest do
use Storybook.ModelCase
import Storybook.Factory
alias Storybook.User
@valid_attrs %{email: "[email protected]", username: "user", password: "password"}
@test_attrs %{email: "[email protected]", username: "different_user", password: "different_password"}
====================================================================================
new test:
test "converts unique_constraint on username to error" do
%User{}
|> User.registration_changeset(@valid_attrs)
|> Repo.insert!()
Repo.all(User)
|> IO.inspect()
attrs = Map.put(@test_attrs, :username, "user")
web/static/js/video.js
=========================================================================================================
/***
* Excerpted from "Programming Phoenix",
* published by The Pragmatic Bookshelf.
* Copyrights apply to this code. It may not be used to create training material,
* courses, books, articles, and the like. Contact us if you are in doubt.
* We make no guarantees that this code is fit for any purpose.
* Visit http://www.pragmaticprogrammer.com/titles/phoenix for more book information.
***/