Commit a7e596881d1bf09c22b13c28981d353b4ea03cc4

Authored by Leandro Santos
Committed by Rodrigo Souto
1 parent f34e9ad3

refactoring of articles api method

app/models/article.rb
@@ -127,6 +127,16 @@ class Article < ActiveRecord::Base @@ -127,6 +127,16 @@ class Article < ActiveRecord::Base
127 {:include => 'categories_including_virtual', :conditions => { 'categories.id' => category.id }} 127 {:include => 'categories_including_virtual', :conditions => { 'categories.id' => category.id }}
128 } 128 }
129 129
  130 + #FIXME make this test
  131 + scope :newer_than, lambda { |reference_id|
  132 + {:conditions => ["articles.id > #{reference_id}"]}
  133 + }
  134 +
  135 + #FIXME make this test
  136 + scope :older_than, lambda { |reference_id|
  137 + {:conditions => ["articles.id < #{reference_id}"]}
  138 + }
  139 +
130 scope :by_range, lambda { |range| { 140 scope :by_range, lambda { |range| {
131 :conditions => [ 141 :conditions => [
132 'published_at BETWEEN :start_date AND :end_date', { :start_date => range.first, :end_date => range.last } 142 'published_at BETWEEN :start_date AND :end_date', { :start_date => range.first, :end_date => range.last }
lib/api/helpers.rb
@@ -27,6 +27,25 @@ module API @@ -27,6 +27,25 @@ module API
27 limit 27 limit
28 end 28 end
29 29
  30 + def period(from_date, until_date)
  31 + if from_date.nil?
  32 + begin_period = Time.at(0).to_datetime
  33 + end_period = until_date.nil? ? DateTime.now : until_date
  34 + else
  35 + begin_period = from_date
  36 + end_period = DateTime.now
  37 + end
  38 +
  39 + begin_period...end_period
  40 + end
  41 +
  42 + def parse_content_type(content_type)
  43 + return nil if content_type.blank?
  44 + content_type.split(',').map do |content_type|
  45 + content_type.camelcase
  46 + end
  47 + end
  48 +
30 #FIXME see if its needed 49 #FIXME see if its needed
31 # def paginate(relation) 50 # def paginate(relation)
32 # per_page = params[:per_page].to_i 51 # per_page = params[:per_page].to_i
lib/api/v1/articles.rb
@@ -22,18 +22,19 @@ module API @@ -22,18 +22,19 @@ module API
22 from_date = DateTime.parse(params[:from]) if params[:from] 22 from_date = DateTime.parse(params[:from]) if params[:from]
23 until_date = DateTime.parse(params[:until]) if params[:until] 23 until_date = DateTime.parse(params[:until]) if params[:until]
24 24
25 - if from_date.nil?  
26 - begin_period = Time.at(0).to_datetime  
27 - end_period = until_date.nil? ? DateTime.now : until_date  
28 - else  
29 - begin_period = from_date  
30 - end_period = DateTime.now  
31 - end  
32 25
33 conditions = {} 26 conditions = {}
34 - conditions[:type] = params[:content_type] if params[:content_type] #FIXME validate type  
35 - conditions[:created_at] = begin_period...end_period  
36 - present environment.articles.find(:all, :conditions => conditions, :offset => (from_date.nil? ? 0 : 1), :limit => limit, :order => "created_at DESC"), :with => Entities::Article 27 + #FIXME remove this line when hub be implemented
  28 + params[:content_type] = 'Folder' if params[:content_type].downcase == 'hub'
  29 +
  30 + conditions[:type] = parse_content_type(params[:content_type])
  31 + conditions[:created_at] = period(from_date, until_date)
  32 + if params[:reference_id]
  33 + @articles = environment.articles.send("#{params.key?(:oldest) ? 'older_than' : 'newer_than'}", params[:reference_id]).find(:all, :conditions => conditions, :limit => limit, :order => "created_at DESC")
  34 + else
  35 + @articles = environment.articles.find(:all, :conditions => conditions, :limit => limit, :order => "created_at DESC")
  36 + end
  37 + present @articles, :with => Entities::Article
37 end 38 end
38 39
39 desc "Return the article id" 40 desc "Return the article id"
@@ -42,7 +43,9 @@ module API @@ -42,7 +43,9 @@ module API
42 end 43 end
43 44
44 get ':id/children' do 45 get ':id/children' do
45 - present environment.articles.find(params[:id]).children.find(:all, :limit => limit), :with => Entities::Article 46 + conditions[:type] = parse_content_type(params[:content_type])
  47 + conditions[:created_at] = period(from_date, until_date)
  48 + present environment.articles.find(params[:id]).children.find(:all, conditions, :limit => limit), :with => Entities::Article
46 end 49 end
47 50
48 get ':id/children/:child_id' do 51 get ':id/children/:child_id' do
lib/api/v1/comments.rb
@@ -17,6 +17,7 @@ module API @@ -17,6 +17,7 @@ module API
17 # GET /articles/12/comments?oldest&limit=10&reference_id=23 17 # GET /articles/12/comments?oldest&limit=10&reference_id=23
18 get ":id/comments" do 18 get ":id/comments" do
19 conditions = {} 19 conditions = {}
  20 +#FIXME See a way to use desc and cres
20 conditions = ["id #{params.key?(:oldest) ? '<' : '>'} ?", params[:reference_id]] if params[:reference_id] 21 conditions = ["id #{params.key?(:oldest) ? '<' : '>'} ?", params[:reference_id]] if params[:reference_id]
21 present environment.articles.find(params[:id]).comments.find(:all, :conditions => conditions, :limit => limit), :with => Entities::Comment 22 present environment.articles.find(params[:id]).comments.find(:all, :conditions => conditions, :limit => limit), :with => Entities::Comment
22 end 23 end