Permalink
Browse files

change find_by_published_at.

perhaps need to add a column about year and month only to improve perfs.. if needed

refactoring some stuff around
  • Loading branch information...
1 parent 04d3d57 commit 6f3ab89320f2dc522854a217060d718f90b1d2ce Yannick Francois committed Dec 10, 2012
@@ -82,23 +82,6 @@ def task_overview
content_tag :li, link_to(_('Back to list'), :action => 'index')
end
- def collection_select_with_current(object, method, collection, value_method, text_method, current_value, prompt=false)
- result = "<select name='#{object}[#{method}]'>\n"
-
- if prompt == true
- result << "<option value=''>" << _("Please select") << "</option>"
- end
- for element in collection
- if current_value and current_value == element.send(value_method)
- result << "<option value='#{element.send(value_method)}' selected='selected'>#{element.send(text_method)}</option>\n"
- else
- result << "<option value='#{element.send(value_method)}'>#{element.send(text_method)}</option>\n"
- end
- end
- result << "</select>\n"
- return result
- end
-
def render_void_table(size, cols)
if size == 0
"<tr>\n<td colspan=#{cols}>" + _("There are no %s yet. Why don't you start and create one?", _(controller.controller_name)) + "</td>\n</tr>\n"
View
@@ -15,7 +15,7 @@ class Article < Content
belongs_to :user
- has_many :pings, :dependent => :destroy, :order => "created_at ASC"
+ has_many :pings, :dependent => :destroy, :order => "created_at ASC"
has_many :trackbacks, :dependent => :destroy, :order => "created_at ASC"
has_many :feedback, :order => "created_at DESC"
has_many :resources, :order => "created_at DESC", :dependent => :nullify
@@ -119,19 +119,6 @@ def self.search_with_pagination(search_hash, paginate_hash)
eval(list_function.join('.'))
end
- def permalink_url_options
- format_url = blog.permalink_format.dup
- format_url.gsub!('%year%', published_at.year.to_s)
- format_url.gsub!('%month%', sprintf("%.2d", published_at.month))
- format_url.gsub!('%day%', sprintf("%.2d", published_at.day))
- format_url.gsub!('%title%', URI.encode(permalink.to_s))
- if format_url[0,1] == '/'
- format_url[1..-1]
- else
- format_url
- end
- end
-
def permalink_url(anchor=nil, only_path=false)
@cached_permalink_url ||= {}
@cached_permalink_url["#{anchor}#{only_path}"] ||= blog.url_for(permalink_url_options, anchor: anchor, only_path: only_path)
@@ -197,41 +184,12 @@ def previous
end
def self.find_by_published_at
- from_where = "FROM contents WHERE published_at is not NULL AND type='Article'"
-
- # Implement adapter-specific groupings below, or allow us to fall through to the generic ruby-side grouping
-
- if defined?(ActiveRecord::ConnectionAdapters::Mysql2Adapter) && self.connection.is_a?(ActiveRecord::ConnectionAdapters::Mysql2Adapter)
- # MySQL uses date_format
- find_by_sql("SELECT date_format(published_at, '%Y-%m') AS publication #{from_where} GROUP BY publication ORDER BY publication DESC")
- elsif defined?(ActiveRecord::ConnectionAdapters::PostgreSQLAdapter) && self.connection.is_a?(ActiveRecord::ConnectionAdapters::PostgreSQLAdapter)
- # PostgreSQL uses to_char
- find_by_sql("SELECT to_char(published_at, 'YYYY-MM') AS publication #{from_where} GROUP BY publication ORDER BY publication DESC")
-
- else
- # If we don't have an adapter-safe conversion from date -> YYYY-MM,
- # we'll do the GROUP BY server-side. There won't be very many objects
- # in this array anyway.
- date_map = {}
- dates = find_by_sql("SELECT published_at AS publication #{from_where}")
-
- dates.map! do |d|
- d.publication = Time.parse(d.publication).strftime('%Y-%m')
- d.freeze
- if !date_map.has_key?(d.publication)
- date_map[d.publication] = true
- d
- end
- end
- dates.reject!{|d| d.blank? || d.publication.blank?}
- dates.sort!{|a,b| b.publication <=> a.publication}
-
- dates
- end
+ result = select('published_at').where('published_at is not NULL').where(type: 'Article')
+ result.map{ |d| [d.published_at.strftime('%Y-%m')]}.uniq
end
- def self.get_or_build_article id = nil
- return Article.find(id) if id
+ def self.get_or_build_article(id)
+ return Article.find(id) if id.present?
article = Article.new.tap do |art|
art.allow_comments = art.blog.default_allow_comments
art.allow_pings = art.blog.default_allow_pings
@@ -420,6 +378,20 @@ def self.time_delta(year = nil, month = nil, day = nil)
private
+ def permalink_url_options
+ format_url = blog.permalink_format.dup
+ format_url.gsub!('%year%', published_at.year.to_s)
+ format_url.gsub!('%month%', sprintf("%.2d", published_at.month))
+ format_url.gsub!('%day%', sprintf("%.2d", published_at.day))
+ format_url.gsub!('%title%', URI.encode(permalink.to_s))
+ if format_url[0,1] == '/'
+ format_url[1..-1]
+ else
+ format_url
+ end
+ end
+
+
def html_urls_to_ping
urls_to_ping = []
html_urls.delete_if{|url| already_ping?(url)}.uniq.each do |url_to_ping|
@@ -28,12 +28,9 @@
<option value='withdrawn'><%= _("Withdrawn") %></option>
</select>
</td>
- <td><%= collection_select_with_current('search', 'category', Category.all, "id", "name", @search[:category].to_i, true)
- %></td>
- <td><%= collection_select_with_current(:search, :user_id, User.find(:all), "id", "name", @search[:user_id].to_i, true) %></td>
- <td>
- <%= collection_select_with_current(:search, :published_at, Article.find_by_published_at, "publication", "publication", @search[:published_at], true) %>
- </td>
+ <td><%= select_tag('search_category', options_from_collection_for_select(Category.all, 'id', 'name'), {prompt: _("Please select")}) %></td>
+ <td><%= select_tag('search_user_id', options_from_collection_for_select(User.all, 'id', 'name'), {prompt: _("Please select")}) %></td>
+ <td><%= select_tag('search_published_at', options_for_select(Article.find_by_published_at), {prompt: _("Please select")}) %></td>
<td><input type="submit" value='<%= _("Filter") %>' class='btn' />
</tr>
<tr>
View
@@ -61,7 +61,7 @@ def some_user
extended 'extended content for fun'
guid
permalink 'a-big-article'
- published_at '2005-01-01 02:00:00'
+ published_at DateTime.new(2005,1,1,2,0,0)
user
categories []
tags []
@@ -134,14 +134,12 @@ def some_user
factory :second_article, :parent => :article do |a|
a.title 'Another big article'
- a.published_at Time.now - 2.seconds
end
factory :article_with_accent_in_html, :parent => :article do |a|
a.title 'article with accent'
a.body '&eacute;coute The future is cool!'
a.permalink 'article-with-accent'
- a.published_at Time.now - 2.seconds
end
factory :blog do
@@ -592,23 +592,25 @@ def assert_sets_trigger(art)
end
describe "#get_or_build" do
- context "when no params given" do
+ context "when nil params given" do
before(:each) do
- @article = Article.get_or_build_article
+ @article = Article.get_or_build_article(nil)
end
- it "should return article" do
+ it "is an Article" do
@article.should be_a(Article)
end
- context "should have blog default value for" do
+ context "have blog default value for" do
it "allow_comments" do
@article.allow_comments.should be == @blog.default_allow_comments
end
+
it "allow_pings" do
@article.allow_pings.should be == @blog.default_allow_pings
end
- it "should have default text filter" do
+
+ it "text filter" do
@article.text_filter_id.should be_nil
@article.text_filter.should be == @blog.text_filter_object
end
@@ -812,8 +814,26 @@ def assert_sets_trigger(art)
end
describe "#find_by_published_at" do
- it "respond to find_by_published_at" do
- Article.should respond_to(:find_by_published_at)
+ it "returns an empty array when no articles" do
+ Article.find_by_published_at.should be_empty
end
+
+ context "returns objects that respond to publication with YYYY-MM published_at date format" do
+ it "with article published_at date" do
+ FactoryGirl.create(:article, published_at: Date.new(2010, 11, 23))
+ result = Article.find_by_published_at
+ result.count.should eq 1
+ result.first.should eq ["2010-11"]
+ end
+
+ it "with 2 articles" do
+ FactoryGirl.create(:article, published_at: Date.new(2010, 11, 23))
+ FactoryGirl.create(:article, published_at: Date.new(2002, 4, 9))
+ result = Article.find_by_published_at
+ result.count.should eq 2
+ result.sort.should eq [["2010-11"], ["2002-04"]].sort
+ end
+ end
+
end
end

0 comments on commit 6f3ab89

Please sign in to comment.