Created
December 20, 2010 22:28
-
-
Save atj/749121 to your computer and use it in GitHub Desktop.
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 Project < ActiveRecord::Base | |
acts_as_indexed :fields => [ | |
:title, :description, :website | |
] | |
validates_presence_of :title, :description | |
validates_uniqueness_of :title | |
acts_as_nested_set | |
has_friendly_id :title, :use_slug => true | |
belongs_to :page | |
belongs_to :image | |
after_create :add_linked_page | |
after_update :update_linked_page | |
before_destroy :remove_linked_page | |
protected | |
def add_linked_page | |
return if self.page.present? | |
self.page = Page.create( | |
:title => self.title, | |
:link_url => "/projects/#{self.to_param}", | |
:deletable => false, | |
:parent => self.parent ? self.parent.page : Page.find_by_link_url('/projects') | |
) | |
self.save! | |
end | |
def update_linked_page | |
return unless self.page.present? | |
self.page.attributes = { | |
:title => self.title, | |
:link_url => "/projects/#{self.to_param}", | |
:parent => self.parent ? self.parent.page : Page.find_by_link_url('/projects') | |
} | |
self.page.save! | |
end | |
def remove_linked_page | |
self.page.destroy! if self.page.present? | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment