Skip to content

Instantly share code, notes, and snippets.

@mrchrisadams
Created June 19, 2009 11:31
Show Gist options
  • Save mrchrisadams/132563 to your computer and use it in GitHub Desktop.
Save mrchrisadams/132563 to your computer and use it in GitHub Desktop.
should be valid
You have a nil object when you didn't expect it!
The error occurred while evaluating nil.support?
/Users/chrisadams/RailsApps/nag2/app/models/support.rb:10 :in `unique_parents?'
/Library/Ruby/Gems/1.8/gems/activesupport-2.3.2/lib/active_support/callbacks.rb:178 :in `send'
/Library/Ruby/Gems/1.8/gems/activesupport-2.3.2/lib/active_support/callbacks.rb:178 :in `evaluate_method'
/Library/Ruby/Gems/1.8/gems/activesupport-2.3.2/lib/active_support/callbacks.rb:166 :in `call'
/Library/Ruby/Gems/1.8/gems/activesupport-2.3.2/lib/active_support/callbacks.rb:90 :in `run'
/Library/Ruby/Gems/1.8/gems/activesupport-2.3.2/lib/active_support/callbacks.rb:90 :in `each'
/Library/Ruby/Gems/1.8/gems/activesupport-2.3.2/lib/active_support/callbacks.rb:90 :in `send'
/Library/Ruby/Gems/1.8/gems/activesupport-2.3.2/lib/active_support/callbacks.rb:90 :in `run'
/Library/Ruby/Gems/1.8/gems/activesupport-2.3.2/lib/active_support/callbacks.rb:276 :in `run_callbacks'
/Library/Ruby/Gems/1.8/gems/activerecord-2.3.2/lib/active_record/validations.rb:1033 :in `valid_without_callbacks?'
/Library/Ruby/Gems/1.8/gems/activerecord-2.3.2/lib/active_record/callbacks.rb:315 :in `valid?'
/Users/chrisadams/RailsApps/nag2/spec/models/support_spec.rb:7
/tmp/textmate-command-7000.rb:3
8 def unique_parents?
9 # if the support item with the same supportable and same user already exists, stop the creation
10 if self.user.support?(self)
11 errors.add(:user, "is already supporting this thought")
12 end
# User factories
Factory.define :user do |f|
f.user_name 'nag-o-meter'
f.first_name 'John'
f.last_name 'Doe'
f.dob Date.new(1982, 5, 11)
f.sequence(:email) {|n| "person#{n}@example.com" }
f.password 'this_one_goes_to_eleven'
f.password_confirmation { |u| u.password }
f.accept_tos '1'
f.active '1'
end
Factory.define :invalid_user, :parent => :user do |f|
f.user_name ''
end
# Thought factories
Factory.define :thought do |f|
f.user_id 1
f.association :user
f.thought_type "idea"
f.title "The world is flat"
f.description "see friedman"
end
Factory.define :invalid_thought, :parent => :thought do |f|
f.title ''
end
# Support factories
Factory.define :support do |f|
f.user_id 1
f.association :user
f.supportable_id 2
f.association :supportable
end
Factory.define :invalid_support, :parent => :support do |f|
f.user_id ""
end
require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
describe Support do
it "should be valid" do
@support = Support.new Factory.attributes_for(:support)
@support.should be_valid
end
end
describe Support, "Saying you'll do it on a thought" do
it "shouldn't create a new support if one with the same parent user and parent thought already exists" do
@user = Factory.create(:user)
@thought = Factory.create(:thought)
@support = Support.new Factory.attributes_for(:support)
@support.should be_valid
@second_support = Support.new Factory.attributes_for(:support)
@second_support.should_not be_valid
end
end
class Support < ActiveRecord::Base
belongs_to :supportable, :polymorphic => true
belongs_to :user
validate_on_create :unique_parents?
def unique_parents?
# if the support item with the same supportable and same user already exists, stop the creation
if self.user.support?(self)
errors.add(:user, "is already supporting this thought")
end
end
end
class User < ActiveRecord::Base
authenticates_many :user_sessions
acts_as_authentic do |c|
c.validates_length_of_password_field_options = {:on => :update, :minimum => 4, :if => :has_no_credentials?}
c.validates_length_of_password_confirmation_field_options = {:on => :update, :minimum => 4, :if => :has_no_credentials?}
c.transition_from_crypto_providers = OldNagTransitionProvider
end
has_many :thoughts
has_many :feedbacks
has_many :actions
has_many :suggestions
has_many :comments
has_many :intents, :conditions => { :completed => false }
has_many :completed_intents, :conditions => { :completed => true }, :class_name => "Intent"
has_many :intented_actions, :through => :intents, :source => :action
has_many :completed_actions, :through => :completed_intents, :source => :action
has_many :stories
has_many :supports
has_many :supported_actions, :through => :supports, :source => :supportable, :source_type => "Action"
has_many :supported_feedbacks, :through => :supports, :source => :supportable, :source_type => "Feedback"
has_many :supported_thoughts, :through => :supports, :source => :supportable, :source_type => "Thought"
# the nag_tag sizes are used in the comments section
# the comment size is used for when a user comments on their own actions/thoughts/feedback
# the profile size is used when a user creates an action/thought/feedback item
def self.curved_corner(px)
"\\( +clone -threshold -1 -draw 'fill black polygon 0,0 0,#{px} #{px},0 fill white circle #{px},#{px} #{px},0' \\( +clone -flip \\) -compose Multiply -composite \\( +clone -flop \\) -compose Multiply -composite \\) +matte -compose CopyOpacity -composite"
end
has_attached_file :avatar,
:whiny_thumbnails => true,
:styles => {
:nag_tag => { :format => :png, :geometry => "50x50#", :convert_options => curved_corner(5) },
:comment => { :format => :png, :geometry => "70x70#", :convert_options => curved_corner(10)},
:profile => { :format => :png, :geometry => "100x100#", :convert_options => curved_corner(15)},
},
:url => "/system/:attachment/:id/:style/:basename.:extension",
:path => ":rails_root/public/system/:attachment/:id/:style/:basename.:extension",
:default_url => "/images/icons/missing_:style.png"
attr_accessor :town, :postcode, :country
attr_accessor :accept_tos
# attr accessors are needed for the virtual attributes too
attr_accessor :email_name, :email_domain
before_validation :populate_email
# activation support methods
def active?
active
end
def activate!
update_attribute :active, true
end
def deliver_activation_instructions!
reset_perishable_token!
Notifications.deliver_activation_instructions(self)
end
def deliver_activation_confirmation!
reset_perishable_token!
Notifications.deliver_activation_confirmation(self)
end
def has_no_credentials?
self.crypted_password.blank? && self.openid_identifier.blank?
end
def deliver_password_reset_instructions!
reset_perishable_token!
Notifications.deliver_forgot_password(self)
end
# needed for comatose admin to work
attr_protected :admin, :active
validates_presence_of :user_name, :message => "Don't forget your nag name!"
validates_presence_of :first_name, :message => "You need to add your first name."
validates_presence_of :last_name, :message => "You need to add your last name."
validates_presence_of :dob, :message => "Your date of birth is missing."
validates_presence_of :email, :message => "Your email address is missing."
validates_presence_of :password, :password_confirmation, :on => :create
validates_acceptance_of :accept_tos, :message => "You'll need to accept the terms of service before you can start nagging"
# Setter
def populate_email
unless @email_name.blank? and @email_domain.blank?
write_attribute :email, [@email_name, '@', @email_domain].join('')
end
end
def activity(filter="", limit=70)
ActivityCollator.for(self, filter, limit)
end
def does_or_did?(action)
intented_actions.include?(action) or completed_actions.include?(action)
end
def support?(thing)
supported_actions.include?(thing) or supported_feedbacks.include?(thing) or supported_thoughts.include?(thing)
end
def intent_for_action(action)
intents.find_by_action_id action.id
end
# helpers for user migration from old nag
def first_time_from_old_nag?
from_old_nag? and not introduced_to_new_nag?
end
def introduce_to_new_nag
update_attribute :introduced_to_new_nag, true
end
# hotness calcuations
named_scope :hottest, :order => "hotness desc", :limit => 10
def self.update_hotness
find_each(&:update_hotness)
end
def update_hotness
brand_new_hotness = [actions, thoughts, feedbacks].map { |c| c.sum(:hotness) }.sum
update_attribute :hotness, brand_new_hotness
end
end
class Thought < ActiveRecord::Base
belongs_to :user
has_many :comments, :as => :commentable
has_many :pictures, :as => :imagable
has_many :supports, :as => :supportable
has_many :supporters, :through => :supports, :source => :user
has_many :actions
validates_presence_of :user_id, :title, :description, :thought_type
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment