Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

moving the sort value to a database column

  • Loading branch information...
commit edb05f14435d07bf91fee6daf09407c2b57e084a 1 parent eac89bd
erik authored
View
1  app/models/donation.rb
@@ -224,6 +224,7 @@ def update_facebook
def update_pitch_funding
pitch.current_funding = pitch.total_amount_donated.to_f # make sure the proper donation amounts are always used
+ pitch.sort_value = (1.0 - (pitch.current_funding / pitch.requested_amount))
pitch.save
end
View
9 app/models/news_item.rb
@@ -115,7 +115,7 @@ class NewsItem < ActiveRecord::Base
named_scope :featured, :conditions => {:feature => true}, :order => "news_items.created_at desc"
named_scope :unfunded, :conditions => "news_items.status NOT IN('accepted','funded','closed')"
named_scope :funded, :conditions => "news_items.status IN ('accepted','funded')"
- named_scope :almost_funded, :select => "news_items.*, if(news_items.status='active', 1.0 - (news_items.current_funding / news_items.requested_amount), news_items.created_at) as sort_value"
+ named_scope :almost_funded, :order => 'news_items.sort_value ASC', :conditions => "news_item.status='active'"
named_scope :published, :conditions => {:status => 'published'}
named_scope :suggested, :conditions => "news_items.type='Tip' AND news_items.status NOT IN ('unapproved','draft','closed')"
named_scope :browsable, :include => :user, :conditions => "news_items.status != 'unapproved' and news_items.status != 'closed'"
@@ -147,7 +147,6 @@ class NewsItem < ActiveRecord::Base
}
named_scope :order_results, lambda { |type|
- return { :select => "news_items.*, if(news_items.status='active', 1.0 - (news_items.current_funding / news_items.requested_amount), news_items.created_at) as sort_value" } if type=='almost-funded'
return { :order=>"news_items.created_at desc" }
}
@@ -210,11 +209,7 @@ def self.sanitize_sort(sort)
def self.get_stories(page, topic_id, grouping_id, topic, selected_filter, current_network, limit=nil)
having_cache ["news_items_stories_", page, topic_id, grouping_id, topic, selected_filter, current_network, limit, @@per_page], {:expires_in => CACHE_TIMEOUT } do
unless limit
- if selected_filter=='almost-funded'
- self.constrain_topic_id(topic_id).constrain_grouping_id(grouping_id).constrain_type(selected_filter).constrain_topic(topic).send(selected_filter.gsub('-','_')).order_results(selected_filter).browsable.by_network(current_network).paginate(:page => page, :select => "news_items.*, if(news_items.status='active', 1.0 - (news_items.current_funding / news_items.requested_amount), news_items.created_at) as sort_value", :order => "sort_value ASC" )
- else
- self.constrain_topic_id(topic_id).constrain_grouping_id(grouping_id).constrain_type(selected_filter).constrain_topic(topic).send(selected_filter.gsub('-','_')).order_results(selected_filter).browsable.by_network(current_network).paginate(:page => page)
- end
+ self.constrain_topic_id(topic_id).constrain_grouping_id(grouping_id).constrain_type(selected_filter).constrain_topic(topic).send(selected_filter.gsub('-','_')).order_results(selected_filter).browsable.by_network(current_network).paginate(:page => page)
else
self.constrain_topic_id(topic_id).constrain_grouping_id(grouping_id).constrain_type(selected_filter).constrain_topic(topic).send(selected_filter.gsub('-','_')).order_results(selected_filter).browsable.by_network(current_network).find(:all,:limit=>limit)
end
View
2  app/models/pitch.rb
@@ -46,7 +46,7 @@ class Pitch < NewsItem
default_url_options[:host] = APP_CONFIG[:default_host]
after_create :send_thank_you
-
+
aasm_state :unapproved
aasm_state :active
aasm_state :accepted
View
11 db/migrate/20120726175419_add_sort_value_to_news_items.rb
@@ -0,0 +1,11 @@
+class AddSortValueToNewsItems < ActiveRecord::Migration
+ def self.up
+ add_column :news_items, :sort_value, :float
+ NewsItem.reset_column_information
+ NewsItem.update_all("sort_value=(1.0 - (current_funding / requested_amount))")
+ end
+
+ def self.down
+ remove_column :news_items, :sort_value
+ end
+end
Please sign in to comment.
Something went wrong with that request. Please try again.