Skip to content

Instantly share code, notes, and snippets.

@tb0yd
Created April 22, 2009 21:02
Show Gist options
  • Save tb0yd/100069 to your computer and use it in GitHub Desktop.
Save tb0yd/100069 to your computer and use it in GitHub Desktop.
{:company_size=>"200-500",
:creator=>
#<User id: 7525, email: "[email protected]", encrypted_password: "c017333d5105b87a8429648fd79233c2f0cc72ed", salt: "4c651fe5fab9102852419445f5b54282d583dcd2", token: "df6eddf5af9082b8eb9a93ecf998de24fcd186e3", token_expires_at: nil, email_confirmed: false>,
:name=>"",
:description=>"This is a great description.",
:company_type=>"Private",
:vision=>"I see the light!"}
class Company < ActiveRecord::Base
attr_writer :pass_validation
COMPANY_SIZE_OPTIONS = %w(1-10 10-50 50-200 200-500 500-1000 1000-5000 5000-10000 10000_or_more)
COMPANY_TYPE_OPTIONS = %w(public private non_profit government)
belongs_to :creator, :class_name => "User", :foreign_key => "creator_user_id"
has_many :job_posts, :dependent => :destroy
has_and_belongs_to_many :uploaded_documents
accepts_nested_attributes_for :uploaded_documents, :reject_if => proc { |a| a['uploaded_data'].blank? }
has_and_belongs_to_many :uploaded_images
validates_presence_of :creator, :name, :company_type, :company_size, :on => :create
def logo
self.uploaded_images.first
end
def logo=(p)
self.uploaded_images.clear
self.uploaded_images << p if p
end
def pass_validation?
if self.pass_validation == true
self.pass_validation = false
true
else
false
end
end
protected
attr_reader :pass_validation
def before_validation
#cant be has_many :through; due to needing to allow other users to use this file
self.uploaded_documents.each do |doc|
doc.user = User.find(self.creator_user_id) if doc.user.nil?
end
end
def presence_of_uploaded_image
errors.add("uploaded_images", "only one is allowed") if self.uploaded_images.size > 1
end
end
def update
@company = Company.find(params[:id])
@company.update_attributes(params[:company])
if @company.save
flash[:notice] = t(:company_successfully_updated)
redirect_to company_path(@company)
else
render :action => :edit, :id => @company.id
end
end
it "should error out when you leave name blank" do
company = Company.create!(@valid_attributes)
@invalid_attributes = @valid_attributes
@invalid_attributes[:name] = ""
@invalid_attributes[:company_size] = "200-500"
@invalid_attributes[:company_type] = "Private"
@invalid_attributes[:description] = "This is a great description."
@invalid_attributes[:vision] = "I see the light!"
company.attributes = @invalid_attributes
company.valid?
puts company.errors.collect &:to_s
1.should == 2
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment