Skip to content

Instantly share code, notes, and snippets.

@yanguango
Created August 23, 2010 12:37
Show Gist options
  • Save yanguango/545386 to your computer and use it in GitHub Desktop.
Save yanguango/545386 to your computer and use it in GitHub Desktop.
def drm_wrapping
status = params[:status].to_s.downcase
options = {
:joins => { :applications => :last_transaction },
:order => "state_transactions.created_at desc",
:include => :product
}
direction = params[:dir]=="down" ? "desc" : "asc"
if GameApplication.aasm_states.map{|s|s.name.to_s}.include?(status)
options[:conditions] = { "game_applications.status" => status }
end
sort = params[:sort]
search = params[:search]
@list_cols = %w(image title drm_status application_filename last_drm_update)
# Build sort
unless sort.blank?
sql_options = case sort.downcase.to_sym
when :drm_status
{ :order => "game_applications.status #{direction}" }
when :filename
{ :order => "LOWER(game_applications.filename) #{direction}" }
when :last_drm_update
{ :order => "state_transactions.created_at #{direction}" }
when :title
{ :order => "LOWER(products.title) #{direction}"}
else
{ :order => "games.#{sort} #{direction}"}
end
options.merge!(sql_options)
end
# Build conditions
unless search.blank?
conds_sql = SearchQueryParser.build_condition_query ['products.title', 'partners.company_name', 'partners.id'], search
conds_sql += "OR partners.id = #{search.to_i}"
options.merge!({:conditions => conds_sql}) unless conds_sql.blank?
end
if request.format.csv?
@collection ||= end_of_association_chain.find(:all, options)
else
@collection ||= end_of_association_chain.paginate({:page => page_by_param, :per_page => 50}.merge(options))
end
respond_to do |wants|
wants.html
wants.csv {
send_csv_data(@list_cols, @collection)
}
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment