Skip to content

Instantly share code, notes, and snippets.

@JasonSpatial
Created April 6, 2015 17:17
Show Gist options
  • Save JasonSpatial/df6a11aaab327cd7795e to your computer and use it in GitHub Desktop.
Save JasonSpatial/df6a11aaab327cd7795e to your computer and use it in GitHub Desktop.
The old List model (Rails 1.2.6 version)
class List < ActiveRecord::Base
acts_as_paranoid
has_many :categories,
:dependent=>:destroy,
:order => "categories.position"
has_many :items,
:order => "items.position"
belongs_to :user
validates_presence_of :name, :user_id
def total_items
items.size
end
def self.copy(lists, new_list_name, current_user)
new_list = current_user.lists.create(:name => new_list_name)
lists.each do |list|
list.categories.find(:all, :conditions=>["categories.deleted_at is null or categories.deleted_at > '#{Time.now.strftime("%Y-%m-%d %H:%M:%S")}'"]).each do |category|
new_category = new_list.categories.create(:name => category.name,
:user_id => category.user_id,
:position => category.position)
category.items.find(:all, :conditions=>["items.deleted_at is null or items.deleted_at > '#{Time.now.strftime("%Y-%m-%d %H:%M:%S")}'"]).each do |item|
new_category.items.create(:name => item.name,
:description => item.description,
:quantity => item.quantity,
:size => item.size,
:price => item.price,
:position => item.position,
:list_id => new_list.id,
:user_id => item.user_id)
end
end
end
new_list
end
def create_from_items(_items)
items = Item.find(_items)
#for each item duplicate it
items.each do |item|
if categories.exists?(:name=>item.category.name)
#category exists
category = categories.find(:first, :conditions=>{:name=>item.category.name})
else
#category does not exist, add it.
category = categories.create(:name => item.category.name,
:user_id => user.id,
:position => item.category.position)
end
category.items.create(:name => item.name,
:description => item.description,
:quantity => item.quantity,
:size => item.size,
:price => item.price,
:position => item.position,
:list_id => id,
:user_id => item.user_id)
end
end
def self.added(from=Date.today, to=Date.today)
List.find(:all, :conditions=>["created_at between ? and ?",from,to + 1])
end
def self.email_bulk_lists(options={})
options[:lists].nil? ? (return nil) : lists = List.find(options[:lists])
options[:send_to].nil? ? (return nil) : send_to = options[:send_to]
options[:user].nil? ? (return nil) : user = options[:user]
message = options[:message] || ""
pattern = EMAIL_PATTERN
if send_to =~ pattern
NotificationMailer.deliver_email_list(lists, user, send_to, message)
#record the send
ListEmail.create( :user=>user,
:email=>send_to,
:comments=>message,
:list_ids=>lists.map(&:id).join(',')
)
notice = "Your list#{lists.length > 1 ? 's have' : ' has'} been sent to <b>#{send_to}</b>."
return ['notice',notice]
else
return ['error',"Please supply a valid email address."]
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment