Sha256: 8b01340ac60dbcbab6872ad77bebaa6ea60d17319bc46bc14fbda63a6279a8ce

Contents?: true

Size: 1.3 KB

Versions: 4

Compression:

Stored size: 1.3 KB

Contents

class <%=parent_class_name%> < ActiveRecord::Base

  acts_as_nested_set

  has_many :<%=child_plural_name%>, :dependent => :destroy

  validates_presence_of :title
  validates_presence_of :filename
  validates_uniqueness_of :filename

  def self.page(search, page)
    with_permissions_to(:manage).search(search).order("title").paginate(:per_page => 10, :page => page)
  end

  def self.view(page)
    roots.paginate(:per_page => 10, :page => page)
  end

  def self.search(search)
    if search
      where("title LIKE ?", "%#{search}%")
    else
      scoped
    end
  end

  has_one :menu_item, :as => :linkable, :dependent => :destroy

  after_create :create_menu_item
  after_update :update_menu_item

  def update_menu_item
    @menu_item = self.menu_item
    if @menu_item == nil
      self.create_menu_item
    else
      @menu_item.update_attributes( 
                                   :title => self.title,
                                   :url => "/<%=child_plural_name%>-for/#{self.filename}",
                                   :active => self.active
                                  )
    end
  end

  def create_menu_item
    @menu_item = self.build_menu_item(
      :title => self.title,
      :url => "/<%=child_plural_name%>-for/#{self.filename}",
      :active => self.active
    )
    @menu_item.save
  end

end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
dust-generators-0.3.3 lib/generators/dust/albums/templates/app/models/album.rb
dust-generators-0.3.2 lib/generators/dust/albums/templates/app/models/album.rb
dust-generators-0.3.1 lib/generators/dust/albums/templates/app/models/album.rb
dust-generators-0.2.8 lib/generators/dust/albums/templates/app/models/album.rb