Commit 8ed077f7cb50c07c59073a9c47aacc4aaa638034
Committed by
Rodrigo Souto
1 parent
50d4c07f
Exists in
master
and in
29 other branches
refactoring of find conditions
Showing
3 changed files
with
20 additions
and
14 deletions
Show diff stats
lib/api/helpers.rb
| @@ -46,6 +46,19 @@ module API | @@ -46,6 +46,19 @@ module API | ||
| 46 | end | 46 | end |
| 47 | end | 47 | end |
| 48 | 48 | ||
| 49 | + | ||
| 50 | + def make_conditions_with_parameter(params = {}) | ||
| 51 | + conditions = {} | ||
| 52 | + from_date = DateTime.parse(params[:from]) if params[:from] | ||
| 53 | + until_date = DateTime.parse(params[:until]) if params[:until] | ||
| 54 | + | ||
| 55 | + conditions[:type] = parse_content_type(params[:content_type]) unless params[:content_type].nil? | ||
| 56 | + | ||
| 57 | + conditions[:created_at] = period(from_date, until_date) | ||
| 58 | + | ||
| 59 | + conditions | ||
| 60 | + end | ||
| 61 | + | ||
| 49 | #FIXME see if its needed | 62 | #FIXME see if its needed |
| 50 | # def paginate(relation) | 63 | # def paginate(relation) |
| 51 | # per_page = params[:per_page].to_i | 64 | # per_page = params[:per_page].to_i |
lib/api/v1/articles.rb
| @@ -19,16 +19,13 @@ module API | @@ -19,16 +19,13 @@ module API | ||
| 19 | # :params => API::Entities::Article.documentation | 19 | # :params => API::Entities::Article.documentation |
| 20 | # } | 20 | # } |
| 21 | get do | 21 | get do |
| 22 | - from_date = DateTime.parse(params[:from]) if params[:from] | ||
| 23 | - until_date = DateTime.parse(params[:until]) if params[:until] | ||
| 24 | 22 | ||
| 25 | - | ||
| 26 | - conditions = {} | ||
| 27 | #FIXME remove this line when hub be implemented | 23 | #FIXME remove this line when hub be implemented |
| 28 | - params[:content_type] = 'Folder' if params[:content_type].downcase == 'hub' | 24 | + params[:content_type] = 'Folder' if ((params[:content_type].nil? ? '' : params[:content_type].downcase) == 'hub') |
| 25 | + params[:content_type] = 'Folder' if ((params[:content_type].nil? ? '' : params[:content_type]) == 'CommunityHubPlugin::Hub') | ||
| 26 | + | ||
| 27 | + conditions = make_conditions_with_parameter(params) | ||
| 29 | 28 | ||
| 30 | - conditions[:type] = parse_content_type(params[:content_type]) | ||
| 31 | - conditions[:created_at] = period(from_date, until_date) | ||
| 32 | if params[:reference_id] | 29 | 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") | 30 | 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 | 31 | else |
| @@ -46,9 +43,7 @@ module API | @@ -46,9 +43,7 @@ module API | ||
| 46 | from_date = DateTime.parse(params[:from]) if params[:from] | 43 | from_date = DateTime.parse(params[:from]) if params[:from] |
| 47 | until_date = DateTime.parse(params[:until]) if params[:until] | 44 | until_date = DateTime.parse(params[:until]) if params[:until] |
| 48 | 45 | ||
| 49 | - conditions = {} | ||
| 50 | - conditions[:type] = parse_content_type(params[:content_type]) | ||
| 51 | - conditions[:created_at] = period(from_date, until_date) | 46 | + conditions = make_conditions_with_parameter(params) |
| 52 | if params[:reference_id] | 47 | if params[:reference_id] |
| 53 | articles = environment.articles.find(params[:id]).children.send("#{params.key?(:oldest) ? 'older_than' : 'newer_than'}", params[:reference_id]).find(:all, :conditions => conditions, :limit => limit, :order => "created_at DESC") | 48 | articles = environment.articles.find(params[:id]).children.send("#{params.key?(:oldest) ? 'older_than' : 'newer_than'}", params[:reference_id]).find(:all, :conditions => conditions, :limit => limit, :order => "created_at DESC") |
| 54 | else | 49 | else |
lib/api/v1/comments.rb
| @@ -16,11 +16,9 @@ module API | @@ -16,11 +16,9 @@ module API | ||
| 16 | # Example Request: | 16 | # Example Request: |
| 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 | - from_date = DateTime.parse(params[:from]) if params[:from] | ||
| 20 | - until_date = DateTime.parse(params[:until]) if params[:until] | ||
| 21 | 19 | ||
| 22 | - conditions = {} | ||
| 23 | - conditions[:created_at] = period(from_date, until_date) | 20 | + conditions = make_conditions_with_parameter(params) |
| 21 | + | ||
| 24 | if params[:reference_id] | 22 | if params[:reference_id] |
| 25 | comments = environment.articles.find(params[:id]).comments.send("#{params.key?(:oldest) ? 'older_than' : 'newer_than'}", params[:reference_id]).find(:all, :conditions => conditions, :limit => limit, :order => "created_at DESC") | 23 | comments = environment.articles.find(params[:id]).comments.send("#{params.key?(:oldest) ? 'older_than' : 'newer_than'}", params[:reference_id]).find(:all, :conditions => conditions, :limit => limit, :order => "created_at DESC") |
| 26 | else | 24 | else |