Skip to content

Instantly share code, notes, and snippets.

@ivanacostarubio
Created September 6, 2012 22:16
Show Gist options
  • Select an option

  • Save ivanacostarubio/3660809 to your computer and use it in GitHub Desktop.

Select an option

Save ivanacostarubio/3660809 to your computer and use it in GitHub Desktop.
class Lead
@@filter_options = ["followup","skype", "documents", "estimate"]
@@all_options = ["followup","skype", "documents", "won", "lost", "estimate"]
validates :status, :inclusion => { :in => @@all_options}
validates_presence_of :status, :name_project, :name_client, :url_project
def self.filter_options
@@filter_options
end
def self.filter_all_options
@@all_options
end
def self.filter_by_status(status)
where(:status => status)
end
def self.sorted_by_status
Lead.filter_options.each do |status|
instance_variable_set("@" + status, Lead.where(:status => status))
end
all = Array.new
Lead.filter_options.each do |status|
local = instance_variable_get("@" + status)
all += local.to_a
end
all
end
def self.all_sorted_by_status
Lead.filter_all_options.each do |status|
instance_variable_set("@" + status, Lead.where(:status => status))
end
all = Array.new
Lead.filter_all_options.each do |status|
local = instance_variable_get("@" + status)
all += local.to_a
end
all
end
end
VS
class Lead
@@all_options = ["followup","skype", "documents", "won", "lost", "estimate"]
def self.sorted_by_status
self.abstract_sorted(index_filter)
end
def self.all_sorted_by_status
self.abstract_sorted(filter_all_opctions)
end
def self.abstract_sorted(collection)
all = Array.new
collection.each do |status|
instance_variable_set("@" + status, Lead.where(:status => status))
end
collection.each do |status|
local = instance_variable_get("@" + status)
all += local.to_a
end
all
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment