Skip to content

Instantly share code, notes, and snippets.

@svenyurgensson
Created September 19, 2012 13:30
Show Gist options
  • Save svenyurgensson/3749692 to your computer and use it in GitHub Desktop.
Save svenyurgensson/3749692 to your computer and use it in GitHub Desktop.
def index
@find_text = params[:text]
@find_type = params[:type]
page = params[:page] || 1
from = @find_from = (params[:from].blank?) ? (Time.now - 6.days).strftime("%d.%m.%Y") : params[:from]
till = @find_till = (params[:till].blank?) ? (Time.now + 1.day).strftime("%d.%m.%Y"): params[:till]
text = params[:text]
type = params[:type] || 1
type = type.to_i
conditions = []
if till
begin
from = Date.strptime(from, "%d.%m.%Y")
till = Date.strptime(till, "%d.%m.%Y")
rescue
flash[:error] = 'дата должна быть в формате dd.mm.yyyy.'
redirect_to messages_path
end
# conditions.push(
# "(private_messages.created_at BETWEEN '#{from.strftime("%Y-%m-%d")}' AND '#{till.strftime("%Y-%m-%d")}')"
# )
end
if text
text = "%#{text}%"
conditions.push "(message LIKE #{PrivateMessage.connection.quote(text)})"
end
if type
case type
when 1
conditions.push("(is_answered=false AND type_id = #{PrivateMessage::TP_USER_MSG})")
when 2
conditions.push("(is_answered=true AND type_id = #{PrivateMessage::TP_USER_MSG})")
when 3
conditions.push("(type_id = #{PrivateMessage::TP_ADMIN_MSG})")
else
conditions.push("(type_id IN (#{PrivateMessage::TP_ADMIN_MSG},#{PrivateMessage::TP_USER_MSG}))")
end
end
conditions.empty? ? conditions = nil : conditions = conditions.join(" AND ")
@messages =
PrivateMessage.paginate(
:all,
:include => [:author],
:page => page,
:conditions => conditions,
:order => 'private_messages.created_at DESC'
)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment