Module: SmartList::InstanceMethods

Defined in:
lib/smart_list.rb

Instance Method Summary collapse

Instance Method Details

#class_name_shaObject



185
186
187
# File 'lib/smart_list.rb', line 185

def class_name_sha
  Digest::SHA1.hexdigest(self.class.name)
end

#followers(options = {}) ⇒ Object



169
170
171
172
173
174
175
# File 'lib/smart_list.rb', line 169

def followers(options = {})
  if self.base_class.group_col.nil? 
    self.base_class.find_by_sql("SELECT * from #{self.base_class.table_name} where #{self.base_class.order_bit_name} #{options[:include_self] == true ? '>=' : '>'} #{self.order_bit}")    
  else
    self.base_class.find_by_sql("SELECT * from #{self.base_class.table_name} where #{self.base_class.order_bit_name} #{options[:include_self] == true ? '>=' : '>'} #{self.order_bit} and #{self.base_class.group_col.to_s} = '#{self.group_name}'")    
  end    
end

#group_list_items(conditions = {}) ⇒ Object



125
126
127
128
129
130
131
# File 'lib/smart_list.rb', line 125

def group_list_items(conditions = {})
  if self.base_class.group_col.nil? || self.base_class.group_col.blank?
    self.base_class.find(:all)
  else
    self.base_class.find_by_sql("SELECT * from #{self.base_class.table_name} where #{self.base_class.group_col} = '#{self[self.base_class.group_col].to_s}' order by #{self.base_class.order_bit_name} ASC")
  end
end

#group_nameObject



155
156
157
158
159
160
161
# File 'lib/smart_list.rb', line 155

def group_name
  if self.base_class.group_col.nil?     
    ""
  else
    self.send(self.base_class.group_col.to_s)
  end
end

#move_down!Object



105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/smart_list.rb', line 105

def move_down!
  if self.base_class.fixed_list == false
    if self.base_class.group_col.nil?     
      all = self.base_class.find(:all)
    else
      all = self.base_class.find(:all, :conditions => {self.base_class.group_col => self.send(self.base_class.group_col)})
    end
    unless self.order_position == all.size-1
       old_pos = self.order_bit_pos
       post_item = self.post
       if old_pos != post_item.order_bit_pos
         self.update_attributes(self.base_class.order_bit_name => post_item.order_bit_pos)
         post_item.update_attributes(self.base_class.order_bit_name => old_pos)
       else 
         self.update_attributes(self.base_class.order_bit_name => old_pos+1)
        end 
    end
  end  
end

#move_up!Object



90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/smart_list.rb', line 90

def move_up!
  if self.base_class.fixed_list == false
    unless self.order_position == 0
      old_pos = self.order_bit_pos
      pre_item = self.pre
      if old_pos != pre_item.order_bit_pos
        self.update_attributes(self.base_class.order_bit_name => pre_item.order_bit_pos)
        pre_item.update_attributes(self.base_class.order_bit_name => old_pos)
      else
        self.update_attributes(self.base_class.order_bit_name => old_pos-1)
      end    
    end
  end  
end

#order_bit_posObject



151
152
153
# File 'lib/smart_list.rb', line 151

def order_bit_pos
  self.attributes[self.base_class.order_bit_name.to_s]
end

#order_positionObject



146
147
148
149
# File 'lib/smart_list.rb', line 146

def order_position
  all = self.group_list_items.map {|x| x.id}  
  all.index(self.id)
end

#postObject

Next Item



140
141
142
143
144
# File 'lib/smart_list.rb', line 140

def post
  all = self.group_list_items
  pos = (self.order_position == all.size-1 ? self.order_position : self.order_position + 1)
  all[pos]
end

#preObject

Previos Item



134
135
136
137
# File 'lib/smart_list.rb', line 134

def pre
  pos = (self.order_position == 0 ? 0 : (self.order_position - 1 rescue 0))
  all = self.group_list_items[pos]
end

#previous_items(options = {}) ⇒ Object



177
178
179
180
181
182
183
# File 'lib/smart_list.rb', line 177

def previous_items(options = {})
  if self.base_class.group_col.nil? 
    self.base_class.find_by_sql("SELECT * from #{self.base_class.table_name} where #{self.base_class.order_bit_name} #{options[:include_self] == true ? '<=' : '<'} #{self.order_bit}")    
  else
    self.base_class.find_by_sql("SELECT * from #{self.base_class.table_name} where #{self.base_class.order_bit_name} #{options[:include_self] == true ? '<=' : '<'} #{self.order_bit} and #{self.base_class.group_col.to_s} = '#{self.group_name}'")    
  end    
end

#set_order_bitObject



163
164
165
166
167
# File 'lib/smart_list.rb', line 163

def set_order_bit
  if self.base_class.fixed_list == false
    self[self.base_class.order_bit_name] = self.base_class.last[self.base_class.order_bit_name] + 1 rescue 100
  end  
end