Commit 33e2208a7138079c7f4da38de31774bb499a949b
1 parent
903e7dd4
Exists in
staging
and in
4 other branches
refactoring of articles api method
Showing
4 changed files
with
44 additions
and
11 deletions
Show diff stats
app/models/article.rb
| ... | ... | @@ -117,6 +117,16 @@ class Article < ActiveRecord::Base |
| 117 | 117 | {:include => 'categories_including_virtual', :conditions => { 'categories.id' => category.id }} |
| 118 | 118 | } |
| 119 | 119 | |
| 120 | + #FIXME make this test | |
| 121 | + scope :newer_than, lambda { |reference_id| | |
| 122 | + {:conditions => ["articles.id > #{reference_id}"]} | |
| 123 | + } | |
| 124 | + | |
| 125 | + #FIXME make this test | |
| 126 | + scope :older_than, lambda { |reference_id| | |
| 127 | + {:conditions => ["articles.id < #{reference_id}"]} | |
| 128 | + } | |
| 129 | + | |
| 120 | 130 | scope :by_range, lambda { |range| { |
| 121 | 131 | :conditions => [ |
| 122 | 132 | '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 | 27 | limit |
| 28 | 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 | 49 | #FIXME see if its needed |
| 31 | 50 | # def paginate(relation) |
| 32 | 51 | # per_page = params[:per_page].to_i | ... | ... |
lib/api/v1/articles.rb
| ... | ... | @@ -22,18 +22,19 @@ module API |
| 22 | 22 | from_date = DateTime.parse(params[:from]) if params[:from] |
| 23 | 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 | 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 | 38 | end |
| 38 | 39 | |
| 39 | 40 | desc "Return the article id" |
| ... | ... | @@ -42,7 +43,9 @@ module API |
| 42 | 43 | end |
| 43 | 44 | |
| 44 | 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 | 49 | end |
| 47 | 50 | |
| 48 | 51 | get ':id/children/:child_id' do | ... | ... |
lib/api/v1/comments.rb
| ... | ... | @@ -17,6 +17,7 @@ module API |
| 17 | 17 | # GET /articles/12/comments?oldest&limit=10&reference_id=23 |
| 18 | 18 | get ":id/comments" do |
| 19 | 19 | conditions = {} |
| 20 | +#FIXME See a way to use desc and cres | |
| 20 | 21 | conditions = ["id #{params.key?(:oldest) ? '<' : '>'} ?", params[:reference_id]] if params[:reference_id] |
| 21 | 22 | present environment.articles.find(params[:id]).comments.find(:all, :conditions => conditions, :limit => limit), :with => Entities::Comment |
| 22 | 23 | end | ... | ... |