diff --git a/app/models/article.rb b/app/models/article.rb index 58eeba6..95bb872 100644 --- a/app/models/article.rb +++ b/app/models/article.rb @@ -127,6 +127,16 @@ class Article < ActiveRecord::Base {:include => 'categories_including_virtual', :conditions => { 'categories.id' => category.id }} } + #FIXME make this test + scope :newer_than, lambda { |reference_id| + {:conditions => ["articles.id > #{reference_id}"]} + } + + #FIXME make this test + scope :older_than, lambda { |reference_id| + {:conditions => ["articles.id < #{reference_id}"]} + } + scope :by_range, lambda { |range| { :conditions => [ 'published_at BETWEEN :start_date AND :end_date', { :start_date => range.first, :end_date => range.last } diff --git a/lib/api/helpers.rb b/lib/api/helpers.rb index 1a5f4c1..6b11c39 100644 --- a/lib/api/helpers.rb +++ b/lib/api/helpers.rb @@ -27,6 +27,25 @@ module API limit end + def period(from_date, until_date) + if from_date.nil? + begin_period = Time.at(0).to_datetime + end_period = until_date.nil? ? DateTime.now : until_date + else + begin_period = from_date + end_period = DateTime.now + end + + begin_period...end_period + end + + def parse_content_type(content_type) + return nil if content_type.blank? + content_type.split(',').map do |content_type| + content_type.camelcase + end + end + #FIXME see if its needed # def paginate(relation) # per_page = params[:per_page].to_i diff --git a/lib/api/v1/articles.rb b/lib/api/v1/articles.rb index 23c649b..bf49aad 100644 --- a/lib/api/v1/articles.rb +++ b/lib/api/v1/articles.rb @@ -22,18 +22,19 @@ module API from_date = DateTime.parse(params[:from]) if params[:from] until_date = DateTime.parse(params[:until]) if params[:until] - if from_date.nil? - begin_period = Time.at(0).to_datetime - end_period = until_date.nil? ? DateTime.now : until_date - else - begin_period = from_date - end_period = DateTime.now - end conditions = {} - conditions[:type] = params[:content_type] if params[:content_type] #FIXME validate type - conditions[:created_at] = begin_period...end_period - present environment.articles.find(:all, :conditions => conditions, :offset => (from_date.nil? ? 0 : 1), :limit => limit, :order => "created_at DESC"), :with => Entities::Article + #FIXME remove this line when hub be implemented + params[:content_type] = 'Folder' if params[:content_type].downcase == 'hub' + + conditions[:type] = parse_content_type(params[:content_type]) + conditions[:created_at] = period(from_date, until_date) + if params[:reference_id] + @articles = environment.articles.send("#{params.key?(:oldest) ? 'older_than' : 'newer_than'}", params[:reference_id]).find(:all, :conditions => conditions, :limit => limit, :order => "created_at DESC") + else + @articles = environment.articles.find(:all, :conditions => conditions, :limit => limit, :order => "created_at DESC") + end + present @articles, :with => Entities::Article end desc "Return the article id" @@ -42,7 +43,9 @@ module API end get ':id/children' do - present environment.articles.find(params[:id]).children.find(:all, :limit => limit), :with => Entities::Article + conditions[:type] = parse_content_type(params[:content_type]) + conditions[:created_at] = period(from_date, until_date) + present environment.articles.find(params[:id]).children.find(:all, conditions, :limit => limit), :with => Entities::Article end get ':id/children/:child_id' do diff --git a/lib/api/v1/comments.rb b/lib/api/v1/comments.rb index b47fe1e..6502add 100644 --- a/lib/api/v1/comments.rb +++ b/lib/api/v1/comments.rb @@ -17,6 +17,7 @@ module API # GET /articles/12/comments?oldest&limit=10&reference_id=23 get ":id/comments" do conditions = {} +#FIXME See a way to use desc and cres conditions = ["id #{params.key?(:oldest) ? '<' : '>'} ?", params[:reference_id]] if params[:reference_id] present environment.articles.find(params[:id]).comments.find(:all, :conditions => conditions, :limit => limit), :with => Entities::Comment end -- libgit2 0.21.2