Permalink
Browse files

add specs and indentation to prepare a refactoring on search functions

  • Loading branch information...
1 parent 04d4b94 commit b5b6976c5b08c806ec203f2a196eceb83ec7b127 Yannick Francois committed Nov 18, 2012
Showing with 155 additions and 91 deletions.
  1. +15 −20 app/models/article.rb
  2. +67 −71 app/models/content.rb
  3. +41 −0 spec/models/article_spec.rb
  4. +32 −0 spec/models/content_spec.rb
View
@@ -98,32 +98,27 @@ def has_child?
include Article::States
- class << self
- def last_draft(article_id)
- article = Article.find(article_id)
- while article.has_child?
- article = Article.child_of(article.id).first
- end
- article
+ def self.last_draft(article_id)
+ article = Article.find(article_id)
+ while article.has_child?
+ article = Article.child_of(article.id).first
end
+ article
+ end
- def search_with_pagination(search_hash, paginate_hash)
-
- state = (search_hash[:state] and ["no_draft", "drafts", "published", "withdrawn", "pending"].include? search_hash[:state]) ? search_hash[:state] : 'no_draft'
-
-
- list_function = ["Article.#{state}"] + function_search_no_draft(search_hash)
-
- if search_hash[:category] and search_hash[:category].to_i > 0
- list_function << 'category(search_hash[:category])'
- end
+ def self.search_with_pagination(search_hash, paginate_hash)
+ state = (search_hash[:state] and ["no_draft", "drafts", "published", "withdrawn", "pending"].include? search_hash[:state]) ? search_hash[:state] : 'no_draft'
- list_function << "page(paginate_hash[:page])"
- list_function << "per(paginate_hash[:per_page])"
+ list_function = ["Article.#{state}"] + function_search_no_draft(search_hash)
- eval(list_function.join('.'))
+ if search_hash[:category] && search_hash[:category].to_i > 0
+ list_function << 'category(search_hash[:category])'
end
+ list_function << "page(paginate_hash[:page])"
+ list_function << "per(paginate_hash[:per_page])"
+
+ eval(list_function.join('.'))
end
def year_url
View
@@ -21,13 +21,13 @@ class Content < ActiveRecord::Base
:published_at => (
if date_at =~ /\d{4}-\d{2}-\d{2}/
DateTime.strptime(date_at, '%Y-%m-%d').beginning_of_day..DateTime.strptime(date_at, '%Y-%m-%d').end_of_day
- elsif date_at =~ /\d{4}-\d{2}/
- DateTime.strptime(date_at, '%Y-%m').beginning_of_month..DateTime.strptime(date_at, '%Y-%m').end_of_month
- elsif date_at =~ /\d{4}/
- DateTime.strptime(date_at, '%Y').beginning_of_year..DateTime.strptime(date_at, '%Y').end_of_year
- else
- date_at
- end
+ elsif date_at =~ /\d{4}-\d{2}/
+ DateTime.strptime(date_at, '%Y-%m').beginning_of_month..DateTime.strptime(date_at, '%Y-%m').end_of_month
+ elsif date_at =~ /\d{4}/
+ DateTime.strptime(date_at, '%Y').beginning_of_year..DateTime.strptime(date_at, '%Y').end_of_year
+ else
+ date_at
+ end
)}
}
}
@@ -39,11 +39,11 @@ class Content < ActiveRecord::Base
scope :searchstring, lambda {|search_string|
tokens = search_string.split(' ').collect {|c| "%#{c.downcase}%"}
{:conditions => ['state = ? AND ' + (['(LOWER(body) LIKE ? OR LOWER(extended) LIKE ? OR LOWER(title) LIKE ?)']*tokens.size).join(' AND '),
- "published", *tokens.collect{ |token| [token] * 3 }.flatten]}
+ "published", *tokens.collect{ |token| [token] * 3 }.flatten]}
}
scope :already_published, lambda { {:conditions => ['published = ? AND published_at < ?', true, Time.now],
- :order => default_order,
- }}
+ :order => default_order,
+ }}
serialize :whiteboard
@@ -65,78 +65,74 @@ def shorten_url
self.redirects << r
end
- class << self
- def find_already_published(what = :all, at = nil, options = { })
- if what.respond_to?(:has_key?)
- what, options = :all, what
- elsif at.respond_to?(:has_key?)
- options, at = at, nil
- end
- at ||= options.delete(:at) || Time.now
- with_scope(:find => { :conditions => ['published_at < ?', at]}) do
- find_published(what, options)
- end
+ def self.find_already_published(what = :all, at = nil, options = { })
+ if what.respond_to?(:has_key?)
+ what, options = :all, what
+ elsif at.respond_to?(:has_key?)
+ options, at = at, nil
+ end
+ at ||= options.delete(:at) || Time.now
+ with_scope(:find => { :conditions => ['published_at < ?', at]}) do
+ find_published(what, options)
end
+ end
- def find_by_published_at(column_name = :published_at)
- from_where = "FROM #{self.table_name} WHERE #{column_name} is not NULL AND type='#{self.name}'"
-
- # 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(#{column_name}, '%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(#{column_name}, '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 #{column_name} 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
+ def self.find_by_published_at(column_name = :published_at)
+ from_where = "FROM #{self.table_name} WHERE #{column_name} is not NULL AND type='#{self.name}'"
+
+ # 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(#{column_name}, '%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(#{column_name}, '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 #{column_name} 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
- dates.reject!{|d| d.blank? || d.publication.blank?}
- dates.sort!{|a,b| b.publication <=> a.publication}
-
- dates
end
- end
+ dates.reject!{|d| d.blank? || d.publication.blank?}
+ dates.sort!{|a,b| b.publication <=> a.publication}
- def function_search_no_draft(search_hash)
- list_function = []
- if search_hash.nil?
- search_hash = {}
- end
+ dates
+ end
+ end
- if search_hash[:searchstring]
- list_function << 'searchstring(search_hash[:searchstring])' unless search_hash[:searchstring].to_s.empty?
- end
+ def self.function_search_no_draft(search_hash)
+ list_function = []
+ search_hash ||= {}
- if search_hash[:published_at] and %r{(\d\d\d\d)-(\d\d)} =~ search_hash[:published_at]
- list_function << 'published_at_like(search_hash[:published_at])'
- end
+ if search_hash[:searchstring]
+ list_function << 'searchstring(search_hash[:searchstring])' unless search_hash[:searchstring].to_s.empty?
+ end
- if search_hash[:user_id] && search_hash[:user_id].to_i > 0
- list_function << 'user_id(search_hash[:user_id])'
- end
+ if search_hash[:published_at] and %r{(\d\d\d\d)-(\d\d)} =~ search_hash[:published_at]
+ list_function << 'published_at_like(search_hash[:published_at])'
+ end
- if search_hash[:published]
- list_function << 'published' if search_hash[:published].to_s == '1'
- list_function << 'not_published' if search_hash[:published].to_s == '0'
- end
+ if search_hash[:user_id] && search_hash[:user_id].to_i > 0
+ list_function << 'user_id(search_hash[:user_id])'
+ end
- list_function
+ if search_hash[:published]
+ list_function << 'published' if search_hash[:published].to_s == '1'
+ list_function << 'not_published' if search_hash[:published].to_s == '0'
end
+
+ list_function
end
def whiteboard
@@ -727,4 +727,45 @@ def assert_sets_trigger(art)
end
end
+
+ describe ".search_with_pagination" do
+ #TODO move those kind of test to a "integration specs" that can be run only for integration
+ context "given some datas" do
+ it "returns an empty array when no article and no params" do
+ Article.search_with_pagination({}, {page: nil, per_page: 12}).should be_empty
+ end
+
+ it "returns article" do
+ article = FactoryGirl.create(:article)
+ Article.search_with_pagination({}, {page: nil, per_page: 12}).should eq([article])
+ end
+
+ it "returns only published article where search params ask about published state" do
+ published_article = FactoryGirl.create(:article, state: 'published')
+ article = FactoryGirl.create(:article, state: 'draft')
+ Article.search_with_pagination({state: 'published'}, {page: nil, per_page: 12}).should eq([published_article])
+ end
+
+ it "returns only quantity of article ask in per_page" do
+ article = FactoryGirl.create(:article, state: 'published')
+ out_of_per_page_article = FactoryGirl.create(:article, state: 'draft')
+ Article.search_with_pagination({}, {page: nil, per_page: 1}).should eq([article])
+ end
+
+ it "returns no draft article by default" do
+ article = FactoryGirl.create(:article, state: 'published')
+ draft_article = FactoryGirl.create(:article, state: 'draft')
+ Article.search_with_pagination({}, {page: nil, per_page: 12}).should eq([article])
+ end
+
+ it "returns article of search categorie" do
+ show_category = FactoryGirl.create(:category, name: 'show')
+ hide_category = FactoryGirl.create(:category, name: 'not_show')
+ article = FactoryGirl.create(:article, categories: [show_category])
+ hide_article = FactoryGirl.create(:article, categories: [hide_category])
+ Article.search_with_pagination({category: show_category.id}, {page: nil, per_page: 12}).should eq([article])
+ end
+
+ end
+ end
end
@@ -58,5 +58,37 @@
@content.really_send_notifications
end
end
+
+ describe "#function_search_no_draft" do
+ it "returns empty array when nil given" do
+ Content.function_search_no_draft(nil).should be_empty
+ end
+
+ it "returns article that match with searchstring" do
+ expected_function = ['searchstring(search_hash[:searchstring])']
+ Content.function_search_no_draft({searchstring: 'something'}).should eq expected_function
+ end
+
+ it "returns article that match with published_at" do
+ expected_function = ['published_at_like(search_hash[:published_at])']
+ Content.function_search_no_draft({published_at: '2012-02'}).should eq expected_function
+ end
+
+ it "returns article that match with user_id" do
+ expected_function = ['user_id(search_hash[:user_id])']
+ Content.function_search_no_draft({user_id: '1'}).should eq expected_function
+ end
+
+ it "returns article that match with not published" do
+ expected_function = ['not_published']
+ Content.function_search_no_draft({published: '0'}).should eq expected_function
+ end
+
+ it "returns article that match with published" do
+ expected_function = ['published']
+ Content.function_search_no_draft({published: '1'}).should eq expected_function
+ end
+
+ end
end

0 comments on commit b5b6976

Please sign in to comment.