Module: SmartList::ClassMethods

Defined in:
lib/smart_list.rb

Instance Method Summary collapse

Instance Method Details

#group_listObject



56
57
58
# File 'lib/smart_list.rb', line 56

def group_list
  self.groups.map {|group| self.find(:all, :conditions => {self.group_col => group})}
end

#grouped_list(group_name) ⇒ Object



39
40
41
42
43
44
45
# File 'lib/smart_list.rb', line 39

def grouped_list(group_name)
  if self.group_col.nil? || self.group_col.blank?
    self.find(:all)
  else
    self.find(:all, :conditions => {self.group_col => group_name})
  end  
end

#groupsObject



47
48
49
50
51
52
53
54
# File 'lib/smart_list.rb', line 47

def groups
  if self.group_col.nil? || self.group_col.blank?
    raise "This is not a grouped list - use options[:group] set colummn to group lists"
  else
    puts self.group_col.inspect
    self.find(:all, :group => self.group_col).map {|x| x.group_name }
  end
end

#is_smart_list?Boolean

Returns:

  • (Boolean)


60
61
62
# File 'lib/smart_list.rb', line 60

def is_smart_list?
  self.included_modules.include?(InstanceMethods)
end

#reorder_group(group_name, options = {:order_by => :created_at}) ⇒ Object



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/smart_list.rb', line 64

def reorder_group(group_name, options = {:order_by => :created_at})
  if self.group_col.nil? || self.group_col.blank?
    raise "Only grouped lists could be reordered"
  else
    transaction do
      group = self.grouped_list(group_name)
      order_mode = options[:order_by].to_s
      ordered = group.sort_by {|item| item.send(order_mode)}
      ordered.each_with_index do |item, i| 
        item[self.base_class.order_bit_name] = AddOrderBit::ORDER_BIT_DEFAULT+i
        item.send(:update_without_callbacks)
      end  
      
      new_group = self.grouped_list(group_name).map {|item| item.id}
      if new_group == ordered.map {|item| item.id}
        return true
      else
        raise ActiveRecord::Rollback
        return false
      end  
    end  
  end    
end

#smart_list(options = {:order_bit => "order_bit", :group => nil, :scope_conditions => nil}) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/smart_list.rb', line 17

def smart_list(options = {:order_bit => "order_bit", :group => nil, :scope_conditions => nil})
  # Check if order_bit exists
  options[:order_bit] ||= "order_bit"
  options[:fixed] ||= false
  if self.column_names.index(options[:order_bit].to_s).nil?
    ::AddOrderBit.add_to_table(self.table_name)
  else
    unless is_smart_list?
      cattr_accessor :group_col, :order_bit_name, :base_class, :fixed_list
      send :include, InstanceMethods

      $active_smart_lists.merge!(Digest::SHA1.hexdigest(self.name) => self.name)
    end  
    self.send(:default_scope, :order => "#{options[:order_bit].to_s} ASC")
    self.send(:before_create, :set_order_bit)
    self.group_col = (options[:group].to_sym) rescue nil
    self.order_bit_name = options[:order_bit].to_sym
    self.base_class = options[:base_class].constantize rescue self.name.constantize
    self.fixed_list = options[:fixed]
  end
end