Module: Sunrise::Models::Widget::ClassMethods

Defined in:
lib/sunrise/models/widget.rb

Class Method Summary collapse

Class Method Details

.extended(base) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/sunrise/models/widget.rb', line 11

def self.extended(base)
  base.class_eval do
    belongs_to :structure
    
    enumerated_attribute :widget_type
    enumerated_attribute :widget_position
    
    validates_presence_of :title, :content
    validates_numericality_of :sort_order, :only_integer => true, :allow_nil => true
   
   scope :sortable, order("#{quoted_table_name}.sort_order DESC")
   scope :recently, order("#{quoted_table_name}.created_at DESC")
   scope :visible, where(:is_visible => true)
   scope :with_type, lambda { |type| where(:widget_type_id => type.try(:id) || type) }
   scope :with_position, lambda { |position| where(:widget_position_id => position.try(:id) || position) }
   scope :with_title, lambda {|title| where(["#{quoted_table_name}.title LIKE ?", "%#{title}%"]) }
   scope :with_structure, lambda {|structure| where(["#{quoted_table_name}.structure_id = ?", structure]) }
  end
end