Created
May 7, 2009 14:31
-
-
Save jacqui/108114 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 Sponsor < ActiveRecord::Base | |
has_slug :source_column => :name, :prepend_id => false | |
validates_presence_of :name, :url, :level | |
has_attached_file :logo, | |
:styles => { :medium => "190x190" } | |
# named scopes could work here too. | |
# named_scope :featured, :conditions => {:level => 'Featured'}, :order => 'name' | |
def self.featured | |
find_all_by_level("Featured", :order => "name") | |
end | |
# named_scope :secondary, :conditions => "NOT (level = 'Featured'), :order => 'name' | |
def self.secondary | |
find(:all, :conditions => ["NOT (level = ?)", "Featured"], :order => "name") | |
end | |
end |
This file contains hidden or 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 File.expand_path(File.dirname(__FILE__) + '/../spec_helper') | |
describe Sponsor do | |
before do | |
@sponsor = Sponsor.new | |
end | |
["name", "url", "level"].each do |field| | |
it "requires #{field}" do | |
Sponsor.new.should have_at_least(1).error_on(field.to_sym) | |
end | |
end | |
it "is able to create" do | |
Factory(:sponsor, | |
:logo => upload_file(File.dirname(__FILE__) + '/../fixtures/hashrocket_logo.png', 'image/png') | |
).should be_valid | |
end | |
describe "'level' finders" do | |
before do | |
@featured_sponsors = [Factory(:featured_sponsor), | |
Factory(:featured_sponsor, :name => "Engine Yard", :url => "http://www.engineyard.com")] | |
@secondary_sponsors = [Factory(:sponsor), | |
Factory(:sponsor, :name => "Fail Blog", :url => "http://failblog.com")] | |
end | |
context ".featured" do | |
it "returns all featured sponsors" do | |
Sponsor.featured.should == @featured_sponsors.sort_by(&:name) | |
end | |
it "orders alphabetically on sponsor name" do | |
Sponsor.featured.first.name.should == "Engine Yard" | |
end | |
it "returns an empty array if no sponsors exist" do | |
@featured_sponsors.each(&:destroy) | |
Sponsor.featured.should be_empty | |
end | |
end | |
context ".secondary" do | |
it "returns all secondary sponsors" do | |
Sponsor.secondary.should == @secondary_sponsors.sort_by(&:name) | |
end | |
it "does not return featured sponsors" do | |
Sponsor.secondary.should_not include(@featured_sponsors) | |
end | |
it "returns an empty array if no sponsors exist" do | |
@secondary_sponsors.each(&:destroy) | |
Sponsor.secondary.should be_empty | |
end | |
end | |
end | |
end |
This file contains hidden or 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 Talk < ActiveRecord::Base | |
has_slug :prepend_id => false | |
belongs_to :speaker | |
has_and_belongs_to_many :time_slots, :order => 'time_slots.start ASC' | |
TRACKS = ['Main', 'A', 'B', 'C', 'All'] | |
CATEGORIES = ['Session', 'Meal', 'Keynote', 'Panel'] | |
validates_presence_of :title, :speaker, :track | |
validates_inclusion_of :category, :in => CATEGORIES | |
validates_inclusion_of :track, :in => TRACKS | |
def speaker_name | |
speaker.name | |
end | |
def track | |
self[:track] || 'All' | |
end | |
def session? | |
category == 'Session' | |
end | |
end | |
This file contains hidden or 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 File.expand_path(File.dirname(__FILE__) + '/../spec_helper') | |
describe Talk do | |
describe "validations" do | |
["title", "speaker", "category"].each do |field| | |
it "requires #{field}" do | |
Talk.new.should have_at_least(1).error_on(field) | |
end | |
end | |
it "requires a valid category" do | |
Talk.new(:category => 'Fake Category').should have_at_least(1).error_on(:category) | |
end | |
it "requires a valid track" do | |
Talk.new(:track => 'Fake Track').should have_at_least(1).error_on(:track) | |
end | |
end | |
before do | |
@talk = Factory(:talk) | |
end | |
it "has a speaker" do | |
@talk.speaker.should be_a_kind_of(Speaker) | |
end | |
it "returns the speaker's name" do | |
@talk.speaker_name.should == @talk.speaker.name | |
end | |
it "defaults track to 'All'" do | |
@talk.track.should == 'All' | |
end | |
it "#session?" do | |
Talk.new(:category => 'Session').should be_session | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment