lib/zen/package/menus/lib/menus/model/menu.rb in zen-0.2.8 vs lib/zen/package/menus/lib/menus/model/menu.rb in zen-0.3b
- old
+ new
@@ -1,38 +1,45 @@
-#:nodoc:
module Menus
#:nodoc:
module Model
##
- # Model used for managing groups of menu items. This model uses the
- # following Sequel plugins:
+ # Model used for managing groups of menu items.
#
- # * sluggable
- #
- # @author Yorick Peterse
# @since 0.2a
#
class Menu < Sequel::Model
+ include Zen::Model::Helper
+
plugin :sluggable, :source => :name, :freeze => false
- # Define our relations
- one_to_many :menu_items, :class => "Menus::Model::MenuItem"
+ one_to_many :menu_items, :class => 'Menus::Model::MenuItem'
##
+ # Searches for a set of menus.
+ #
+ # @since 16-10-2011
+ # @param [String] query The search query.
+ # @return [Mixed]
+ #
+ def self.search(query)
+ return filter(
+ search_column(:name, query) |
+ search_column(:html_class, query) |
+ search_column(:html_id, query)
+ )
+ end
+
+ ##
# Specifies all validates rules used when creating or updating a menu.
- # A slug will be generated when a menu is first created but after that
- # they are required to ensure that they don't collide with existing slugs.
#
- # @author Yorick Peterse
# @since 0.2a
#
def validate
- validates_presence :name
- validates_presence :slug unless new?
- validates_unique :slug
-
- # Prevent people from entering random crap for class and ID names
- validates_format(/^[a-zA-Z\-_0-9]*/, [:html_class, :html_id])
+ validates_presence(:name)
+ validates_unique(:slug)
+ validates_max_length(255, [:name, :slug, :html_class, :html_id])
+ validates_format(/^[a-zA-Z\-_0-9\s]*$/, :html_class)
+ validates_format(/^[a-zA-Z\-_0-9]*$/ , :html_id)
end
end # Menu
end # Model
end # Menus